DEV Community

Cover image for How to Build a Kubernetes Cluster Using kubeadm
vansh bhardwaj
vansh bhardwaj

Posted on

How to Build a Kubernetes Cluster Using kubeadm

Introduction to Kubernetes and kubeadm

Kubernetes (K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It provides a framework to run distributed systems resiliently, offering features like scaling, failover, and service discovery. Setting up a Kubernetes cluster is a fundamental step for developers and DevOps engineers aiming to manage containerized applications at scale. One of the most efficient ways to create and manage a Kubernetes cluster is by using kubeadm, a tool designed to bootstrap Kubernetes clusters.

Image description

In this guide, we will set up a Kubernetes cluster with one master node and one worker node using kubeadm on AWS EC2 instances.

Requirements for Kubernetes Cluster

Master Node: We will use a t2.large instance.
Worker Node: We will use a t2.medium instance.
Operating System: Ubuntu 20.04 or newer (64-bit).
Network: Open port 6443 on the master node (used by Kubernetes API Server).
Memory Requirements:
Master Node: Minimum 2 CPUs and 2 GB RAM (recommended 4 GB RAM).
Worker Node: Minimum 2 CPUs and 2 GB RAM.
Tools Required: Docker, kubeadm, kubelet, kubectl.

Steps to Set Up the Kubernetes Cluster

Step 1: Set Up AWS EC2 Instances

Launch two EC2 instances:
Master Node: Use a t2.large instance.

Worker Node: Use a t2.medium instance.

  1. Use Ubuntu as the OS.

  2. Open the necessary ports ( port 6443 on the master node)

Step 2: Update and Upgrade Both Nodes

Run the following commands on both the master and worker nodes to update the system:

sudo apt update
sudo apt upgrade -y

Step 3: Install Docker on Both Nodes

Docker is required to manage containers in Kubernetes. Install it using the following commands:

sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker $USER
sudo reboot

Step 4: Install kubeadm, kubelet, and kubectl on Both Nodes

These tools are essential for setting up and managing the Kubernetes cluster:

Add the Kubernetes signing key:

sudo curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.31/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

Add the Kubernetes repository:

echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.31/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

Update the package list:

sudo apt update

Install kubeadm, kubelet, and kubectl:

sudo apt install -y kubelet kubeadm kubectl

Step 5: Initialize the Master Node

On the master node, run the following commands:

Switch to the root user:

sudo su

Initialize the cluster:

kubeadm init

Set up the kubeconfig for kubectl:

export KUBECONFIG=/etc/kubernetes/admin.conf

Set up a pod network using Weave Net:

kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml

Step 6: Add the Worker Node to the Cluster

On the master node, generate the join command:

kubeadm token create --print-join-command

The output will be similar to:

kubeadm join 172.31.84.246:6443 --token jcgr7p.8x2yqehcg0jbsk80 --discovery-token-ca-cert-hash sha256:cfd5cce76dfe4330f0cd8fed5feed709bdb0efcf1fa0656f188475e3bda7563f

Copy this command and use it on the worker node to join the cluster.

Now, on the worker node, run the following commands:

Switch to the root user:

sudo su

Reset pre-flight checks

kubeadm reset pre-flight checks

Use the join command generated earlier to connect the worker node to the cluster:

kubeadm join 172.31.84.246:6443 --token jcgr7p.8x2yqehcg0jbsk80 --discovery-token-ca-cert-hash sha256:cfd5cce76dfe4330f0cd8fed5feed709bdb0efcf1fa0656f188475e3bda7563f --v=5

Note -> Append “--v=5” at the end, this ensures we use version 5 to join kubeadm

We should get the following output “This node has joined the cluster”

Step 7: Verify the Cluster

On the master node, verify that the worker node has successfully joined the cluster:

kubectl get nodes

You should see an output like this:

NAME STATUS ROLES AGE VERSION
ip-172-31-47-111 Ready <none> 2m38s v1.31.1
ip-172-31-84-246 Ready control-plane 33m v1.31.1

Step 8: Deploy an Application (Nginx)

To test the cluster, deploy an Nginx pod:

kubectl run nginx --image=nginx --restart=Never

Check the pod status:

kubectl get pods

You should see an output like this:

NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 3m44s

On the worker node, you can verify the pod using Docker commands:

docker ps
ctr -n k8s.io containers list
crictl ps

Note -> Kubernetes 1.20+ deprecated Docker as the default container runtime (June 23) in favor of containerd or CRI-O. If your worker node is using containerd or cri-o, docker ps won’t show running containers. Instead, you can check the running containers using:

For containerd: ctr -n k8s.io containers list
For cri-o: crictl ps

Conclusion

Setting up a Kubernetes cluster using kubeadm is a straightforward process that involves:

  1. Preparing the nodes by installing necessary tools like Docker and kubeadm.
  2. Initializing the master node and setting up the control plane.
  3. Joining worker nodes to the cluster using a token.
  4. Verifying the cluster and deploying applications to test the setup.

With Kubernetes, you now have the ability to deploy, scale, and manage containerized applications efficiently. This setup serves as a foundation for experimenting with advanced Kubernetes features and workloads.

About Me

Hi, I’m Vansh. I’m Building stuff on the web. Exploring Cloud and DevOps. Passionate about creating and scaling solutions. Let’s connect on Twitter: [heyyvanshh].

Top comments (0)