How to Install Kubernetes on Ubuntu?

How to Install Kubernetes on Ubuntu: Step-by-Step Guide by OpsNexa

Kubernetes is a powerful container orchestration platform that automates the deployment, scaling, and management of containerized applications. Installing Kubernetes on Ubuntu is a common task for developers and system administrators who want to set up their own Kubernetes clusters, either for development, testing, or production environments.

In this guide, we will walk you through the steps on how to install Kubernetes on Ubuntu, ensuring you get your cluster up and running with minimal hassle. Additionally, we’ll discuss how OpsNexa can assist you with Kubernetes installation, troubleshooting, and management, ensuring a smooth experience.

Prerequisites for Installing Kubernetes on Ubuntu

Before diving into the installation process, it’s essential to ensure that your system meets the following prerequisites:

  1. Ubuntu 20.04+: Ensure you’re using a compatible version of Ubuntu. This guide assumes you’re working with Ubuntu 20.04 LTS or higher. If you’re using an earlier version, consider upgrading to ensure compatibility.

  2. At least 2 GB of RAM: Kubernetes requires a minimum of 2 GB of RAM for smooth operation.

  3. Swap Disabled: Kubernetes requires that swap memory be disabled on all nodes in the cluster.

  4. Sudo Access: You need root (or sudo) privileges on the machine where you plan to install Kubernetes.

  5. Internet Access: Your system should have access to the internet to download the necessary installation packages.

Once you’ve confirmed the prerequisites, let’s move forward with the installation steps.

Step-by-Step Guide to Install Kubernetes on Ubuntu

Step 1: Update Your System

Before starting the installation, it’s good practice to ensure your system is up to date. Run the following commands to update the package list and install the latest security patches:

bash
sudo apt-get update && sudo apt-get upgrade -y

Step 2: Install Docker (Kubernetes Dependency)

Kubernetes relies on Docker as its container runtime. Install Docker on your Ubuntu machine using the following commands:

  1. Install dependencies:

    bash
    sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y
  2. Add Docker’s official GPG key:

    bash
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  3. Add the Docker repository to APT sources:

    bash
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  4. Install Docker:

    bash
    sudo apt-get update
    sudo apt-get install docker-ce -y
  5. Verify Docker installation:

    bash
    sudo systemctl status docker

Once Docker is installed, make sure it’s running properly.

Step 3: Disable Swap Memory

Kubernetes requires swap memory to be disabled on all nodes. To disable swap, run the following command:

bash
sudo swapoff -a

To make this change permanent, edit the /etc/fstab file and comment out any swap entries:

bash
sudo nano /etc/fstab

Find the line with swap and add a # at the beginning of the line to disable it permanently.

Step 4: Add Kubernetes Repository

Now, add the official Kubernetes repository to your system:

  1. Add Kubernetes’ GPG key:

    bash
    curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
  2. Add the Kubernetes APT repository:

    bash
    sudo apt-add-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"

Step 5: Install Kubernetes Components (kubeadm, kubelet, kubectl)

Once the Kubernetes repository is added, you can install the essential Kubernetes components:

  1. Install kubeadm, kubelet, and kubectl:

    bash
    sudo apt-get update
    sudo apt-get install -y kubeadm kubelet kubectl
  2. Mark the installed components to prevent them from being automatically updated:

    bash
    sudo apt-mark hold kubeadm kubelet kubectl

These three components are critical for managing your Kubernetes cluster:

  • kubeadm: Tool to initialize and manage Kubernetes clusters.

  • kubelet: Ensures that containers are running in pods on the nodes.

  • kubectl: Command-line tool for interacting with your Kubernetes cluster.

Step 6: Initialize the Kubernetes Cluster (Control Plane)

Now that Kubernetes components are installed, it’s time to initialize the cluster using kubeadm. Run the following command to initialize the Kubernetes control plane (master node):

bash
sudo kubeadm init --pod-network-cidr=192.168.0.0/16

The --pod-network-cidr flag is required to specify the IP range for your pod network. This CIDR range is specific to certain networking solutions (e.g., Calico, Flannel), and 192.168.0.0/16 is a commonly used range.

After running the kubeadm init command, you should see a message with further instructions. These include the command to set up the kubeconfig for your user, which allows you to interact with your cluster using kubectl.

Step 7: Set Up kubeconfig for kubectl

To interact with your Kubernetes cluster as a non-root user, you need to set up the kubeconfig file for kubectl. Run the following command to copy the kubeconfig file to your home directory:

bash
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

This allows your user to interact with the Kubernetes cluster using kubectl.

Step 8: Install a Pod Network

For Kubernetes pods to communicate with each other across nodes, you need a network solution like Calico, Flannel, or Weave. We’ll use Calico for this guide. To install Calico, run the following command:

bash
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

This will install the Calico networking solution, which provides networking and network policy for your Kubernetes cluster.

Step 9: Join Worker Nodes to the Cluster

If you have additional nodes that you want to join to the cluster, kubeadm init will provide you with a join token to add the worker nodes. The output of kubeadm init will look like this:

php-template
kubeadm join <your-cluster-ip>:6443 --token <your-token> --discovery-token-ca-cert-hash sha256:<hash>

On each worker node, run the kubeadm join command to join them to the cluster.

Step 10: Verify the Cluster

After the cluster is initialized and the worker nodes are joined, you can verify the status of your cluster by running:

bash
kubectl get nodes

You should see all the nodes in the cluster (including master and worker nodes) listed as Ready.

Step 11: Install a Dashboard (Optional)

If you prefer a graphical interface to manage your Kubernetes cluster, you can install the Kubernetes Dashboard. To install it, run:

bash
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml

To access the dashboard, create a service account and a cluster role binding:

bash
kubectl create serviceaccount dashboard -n kubernetes-dashboard
kubectl create clusterrolebinding dashboard-admin-binding --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard

To access the dashboard, run:

bash
kubectl proxy

And open the following URL in your browser:

bash
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

How OpsNexa Can Assist with Kubernetes Installation

While the steps outlined above will help you install Kubernetes on Ubuntu, setting up and maintaining a production-grade Kubernetes cluster requires ongoing management, optimization, and troubleshooting. That’s where OpsNexa can help.

1. Kubernetes Installation:

OpsNexa can assist with smooth, error-free Kubernetes installation, including multi-node setups and configuring networking solutions like Calico or Flannel.

2. Cluster Management:

Our team offers managed Kubernetes services, including node scaling, resource management, and security configurations, ensuring your cluster runs efficiently.

3. Troubleshooting and Support:

Encountering issues after installation? OpsNexa provides expert troubleshooting and support services to help you resolve issues quickly and get back on track.

4. Ongoing Monitoring and Optimization:

We provide continuous monitoring and performance optimization for your Kubernetes clusters to ensure they remain secure, high-performing, and cost-efficient.

Contact OpsNexa for Devops architect and devops hiring solutions.