Kubernetes Cluster Creation Using Kubeadm

Kubernetes

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It allows you to manage clusters of nodes efficiently.

In this guide, I'll walk you through setting up a multi-node Kubernetes cluster using Kubeadm. I used EC2 instances (t2.medium) for both the master and worker nodes.


Prerequisites

Before we start, make sure you have:

  • Two or more Ubuntu 18.04 or higher EC2 instances (t2.medium recommended)

  • At least 2GB of RAM and 2 CPU cores per instance

  • Network connectivity between nodes

  • Root access to each server


Step 1: Update and Install Dependencies (Run on All Nodes)

Update the package list and install required dependencies to ensure the latest versions are available:

sudo apt-get update
sudo apt install apt-transport-https curl -y

Step 2: Install Containerd (Run on All Nodes)

Kubernetes requires a container runtime. We will install Containerd, which is a lightweight container runtime:

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install containerd.io -y

Configure Containerd (Run on All Nodes)

Generate and update the default configuration file for Containerd:

sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml

Enable SystemdCgroup to ensure better compatibility with Kubernetes:

sudo sed -i -e 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml

Alternatively, you can manually open the configuration file and change the setting:

sudo nano /etc/containerd/config.toml

Find the line:

SystemdCgroup = false

Change it to:

SystemdCgroup = true

Save and exit (CTRL+X, then Y, then Enter).

Restart Containerd to apply changes:

sudo systemctl restart containerd

Step 3: Install Kubernetes (Run on All Nodes)

Install Kubernetes components (kubelet, kubeadm, and kubectl) to set up and manage the cluster:

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
sudo systemctl enable --now kubelet

Step 4: Disable Swap (Run on All Nodes)

Kubernetes requires swap to be disabled for performance reasons:

sudo swapoff -a

To make this change permanent, remove swap entries in /etc/fstab:

sudo nano /etc/fstab

Step 5: Enable Kernel Modules and Sysctl Settings (Run on All Nodes)

Enable kernel modules and IP forwarding for Kubernetes networking:

sudo modprobe br_netfilter
sudo sysctl -w net.ipv4.ip_forward=1

Step 6: Initialize the Kubernetes Cluster (Run on Master Node Only)

Initialize the cluster using kubeadm, specifying a pod network CIDR:

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

After initialization, 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: Install Flannel (Run on Master Node Only)

Flannel is a networking solution that allows pods to communicate across nodes:

kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

Verify that all system pods are running:

kubectl get pods --all-namespaces

Step 8: Join Worker Nodes to the Cluster (Run on Worker Nodes Only)

After initializing the cluster, the kubeadm init command will output a join command similar to this:

kubeadm join <MASTER_NODE_IP>:6443 --token <TOKEN> --discovery-token-ca-cert-hash sha256:<HASH>

Run this command on each worker node to add it to the cluster.

To verify if worker nodes have successfully joined, run this command on the master node:

kubectl get nodes

Conclusion

Congratulations! ๐ŸŽ‰ You have successfully set up a multi-node Kubernetes cluster using Kubeadm. You can now deploy applications, set up services, and explore more Kubernetes features!

Let me know if you have any questions!

0
Subscribe to my newsletter

Read articles from pranay shegaonkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

pranay shegaonkar
pranay shegaonkar