Guide to Installing Kubernetes on Ubuntu

Utkarsh Kakkar - May 28 - - Dev Community

Welcome to my in-depth guide on installing Kubernetes on Ubuntu. Kubernetes, often abbreviated as K8s, is a powerful tool for automating the deployment, scaling, and management of containerised applications. Whether you're a seasoned developer or just starting out, this guide will walk you through the process step-by-step.

Prerequisites

Before we dive into the installation, let's me make sure you have everything you need:

A Ubuntu machine: This can be a physical server, a virtual machine, or even a cloud instance.

Root access: You need sudo privileges to install software and make system changes.

Basic knowledge of the command line: While not mandatory, familiarity with basic Linux commands will be helpful.

*Step 1: Update Your System
*

First, ensure your system is up-to-date. Open your terminal and run the following commands:

sudo apt update
sudo apt upgrade -y

This will update your package list and install the latest versions of your installed packages.

*Step 2: Install Docker
*

Kubernetes uses Docker to manage its containers. If Docker isn't already installed on your system, you can install it with these commands:

sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker

Verify the installation by checking the Docker version:

docker --version

You should see the Docker version information, confirming that Docker is installed and running.

*Step 3: Install Kubernetes Components
*

Kubernetes consists of several components, but for now, we'll focus on kubeadm, kubelet, and kubectl.

kubeadm: A tool to bootstrap the cluster.
kubelet: An agent that runs on each node in the cluster.
kubectl: A command-line tool to interact with the cluster.

Add the Kubernetes repository and install the components:

sudo apt install -y apt-transport-https ca-certificates curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo bash -c 'cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF'
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

*Step 4: Disable Swap
*

Kubernetes requires swap to be disabled. Edit the /etc/fstab file and comment out any swap entry:

sudo nano /etc/fstab

Find the line that contains swap and add a # at the beginning of the line. Save and exit the file. Then, disable swap with:

sudo swapoff -a

*Step 5: Initialise the Kubernetes Cluster
*

With Docker and the Kubernetes components installed, it's time to initialise the cluster. On the master node, run:

sudo kubeadm init --pod-network-cidr=10.244.0.0/16

Note the --pod-network-cidr option. This specifies the network range for the pods and is necessary for certain network plugins.

After initialisation, you'll see a kubeadm join command in the output. Save this command; you'll need it to add worker nodes to the cluster.

*Step 6: Set Up Your Kubernetes Configuration
*

To start using the cluster, you need to set up your local kubeconfig file. Run the following commands:

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

*Step 7: Install a Pod Network
*

Kubernetes uses a network plugin to handle the communication between pods. We'll use Flannel, a simple and effective choice. Install it with:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

*Step 8: Join Worker Nodes to the Cluster
*

If you have additional nodes, run the kubeadm join command you saved earlier on each worker node:

sudo kubeadm join <master-node-ip>:<master-node-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>

Replace the placeholders with the actual values from the kubeadm init output.

*Step 9: Verify the Installation
*

Finally, check that all nodes have joined the cluster and are in the Ready state:

kubectl get nodes

Enter fullscreen mode Exit fullscreen mode

You should see a list of your master and worker nodes, each with a STATUS of Ready.

.