Kubernetes - Setup on Ubuntu



Setting Up a Kubernetes Cluster

Before setting up Kubernetes, you need to have a Virtual Datacenter (vDC) where machines can communicate with each other over a network. This can be a physical setup, a cloud-based infrastructure, or a virtualized environment.

In this guide, we will focus on setting up a Kubernetes cluster on Ubuntu using modern best practices.

Prerequisites

  • Linux machine
  • At least 2GB RAM, 2 CPUs, and 20GB disk space
  • Internet connectivity

Note: This guide assumes you're working on Ubuntu machines, but similar steps apply to other Linux distributions.

Step 1: Configure Hostname and Update Hosts File

First, let's set up the hostname on our node −

$ sudo hostnamectl set-hostname master-node

Next, update the /etc/hosts file to define host-to-IP mappings:

$ sudo nano /etc/hosts

Add the following entries, then save and exit the file:

192.168.43.95  master-node

Step 2: Disable Swap and Load Kernel Modules

Kubernetes needs swap to be disabled for optimal performance. We'll run these commands:

$ sudo swapoff -a
$ sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

Now, we'll load the necessary kernel modules:

$ sudo modprobe overlay
$ sudo modprobe br_netfilter

To ensure these modules load at startup, we'll create a configuration file:

$ echo -e "overlay\nbr_netfilter" | sudo tee /etc/modules-load.d/k8s.conf

Next, we'll configure sysctl settings to enable IP forwarding −

$ echo -e "net.bridge.bridge-nf-call-ip6tables = 1\nnet.bridge.bridge-nf-call-iptables = 1\nnet.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/kubernetes.conf

Apply the changes −

$ sudo sysctl --system

Step 3: Install and Configure Containerd

Kubernetes requires a container runtime. We'll use containerd.

First, we'll install the dependencies:

$ sudo apt update
$ sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates

Add Dockers Official GPG Key & Repository −

$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | 
sudo tee /etc/apt/keyrings/docker.asc > /dev/null
$ echo "deb [signed-by=/etc/apt/keyrings/docker.asc] 
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" 
| sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Now, we'll install containerd:

$ sudo apt update && sudo apt install -y containerd.io

We'll configure containerd to use SystemdCgroup:

$ sudo mkdir -p /etc/containerd
$ containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
$ sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
$ sudo systemctl restart containerd

Step 4: Add Kubernetes Package Repository

Kubernetes packages are not available in Ubuntu's default repositories, so we'll add the official Kubernetes repository.

Add Kubernetess Official GPG Key & Repository:

$ sudo curl -fsSLo /etc/apt/keyrings/kubernetes-apt-keyring.asc
https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key
$ echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.asc] 
https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /" |
sudo tee /etc/apt/sources.list.d/kubernetes.list

Step 5: Install Kubernetes Components

Now, we'll install Kubernetes components −

$ sudo apt update
$ sudo apt install -y kubelet kubeadm kubectl

To prevent updates from breaking the installation, we'll hold these packages at their current version:

$ sudo apt-mark hold kubelet kubeadm kubectl

Step 6: Initialize the Kubernetes Cluster

We'll initialize the cluster:

$ sudo kubeadm init --control-plane-endpoint=master-node

Output

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user −

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

Alternatively, if you are the root user, you can run −

export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.

Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at.

https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root −

kubeadm join 192.168.43.95:6443 --token rkvznu.vtx45jcb1rdp893o \
--discovery-token-ca-cert-hash sha256:bc53a89377ba5c7dfae23bb09c6b059ff9c699971f5f5d8ac20a4f76a2ce8f5a

Tutorialspoint@master-node:~$

This message indicates that our control-plane has been successfully initialized. Now, we need to configure kubectl for cluster management:

$ 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: Verify Kubernetes is Running

To check if our cluster is up and running, we'll create a test pod:

$ kubectl run test-pod --image=busybox --restart=Never -- sleep 3600

Output

pod/test-pod created

Now ,we'll if verify the pod is running:

$ kubectl get pods

Output

NAME       READY   STATUS    RESTARTS   AGE
test-pod   1/1     Running   0          10s

As you can see from the output, the Kubernetes cluster is set up correctly

Step 8: Add Worker Nodes (Optional)

To add worker nodes, run the following command on each worker node as root:

$ kubeadm join 192.168.43.95:6443 --token rkvznu.vtx45jcb1rdp893o \
--discovery-token-ca-cert-hash sha256:bc53a89377ba5c7dfae23bb09c6b059ff9c699971f5f5d8ac20a4f76a2ce8f5a

Conclusion

By following this guide, you will have:

  • Set up a master node
  • Installed essential dependencies
  • Configured container runtime and networking
  • Initialized the Kubernetes control plane
  • Deployed a test pod to verify the setup

At this stage, our Kubernetes cluster is fully operational, and you can start deploying and managing containerized applications efficiently.

Advertisements