Set Up Kubernetes cluster using Kubeadm

Set Up Kubernetes cluster using Kubeadm

·

4 min read

Introduction

Let start set up a Kubernetes cluster with one master node and two worker nodes using Kubeadm. Kubeadm is a tool that simplifies the process of setting up a Kubernetes cluster.

Before we start, please make sure that you have the following prerequisites:

  • Three Ubuntu 20.04 servers with at least 2GB RAM and 2 CPUs.

  • The servers should have a unique hostname and static IP address.

  • All servers should have Docker installed.

Once you have the prerequisites ready, let's get started.

Step 1: Update Server

Before installing any software, it's always a good practice to update the system packages to the latest version. This step ensures that the server is up-to-date with the latest security patches and bug fixes.

sudo apt update
sudo apt upgrade -y

Step 2: Install Kubernetes and Kubeadm

In this step, we install the Kubernetes and Kubeadm packages on all three servers. Kubeadm is a tool that simplifies the process of setting up a Kubernetes cluster. It automates many of the complex tasks involved in creating a cluster, such as configuring the networking, generating certificates, and creating the necessary Kubernetes configuration files.

sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-release
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt update
sudo apt install -y kubectl kubeadm kubelet

Step 3: Disable Swap

Kubernetes requires disabling swap on all nodes. Swap is a space on a disk that is used as an extension of physical memory. When the physical memory (RAM) becomes full, the operating system moves inactive pages from the RAM to the swap space.

sudo swapoff -a
sudo sed -i '/ swap / s/^/#/' /etc/fstab

Disabling swap ensures that the Kubernetes components have access to all the physical memory available on the server, which is critical for performance and stability.

Step 4: Configure Firewall

Kubernetes uses various ports for communication between the nodes. In this step, we configure the firewall on all nodes to allow communication between the nodes. We open the required ports for Kubernetes components, such as the API server, etcd, and kubelet.

sudo ufw allow OpenSSH
sudo ufw allow 6443/tcp
sudo ufw allow 2379/tcp
sudo ufw allow 2380/tcp
sudo ufw allow 10250/tcp
sudo ufw allow 10251/tcp
sudo ufw allow 10252/tcp
sudo ufw allow 10255/tcp
sudo ufw enable

Step 5: Initialize Master Node

In this step, we initialize the master node using Kubeadm. This process involves downloading and installing the necessary Kubernetes components and creating the necessary configuration files. The --pod-network-cidr flag specifies the range of IP addresses that the pod network will use.

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

Once the initialization process is complete, Kubeadm generates a join command that we will use to join the worker nodes to the cluster.

Note down the join command somewhere as we will need it later.

Step 6: Configure kubectl

kubectl is the command-line tool used to interact with the Kubernetes API server. In this step, we configure kubectl to access the Kubernetes API server on the master node. We copy the admin.conf file from the master node to the local kubeconfig file, which kubectl uses to authenticate with the API server.

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: Deploy Pod Network

A pod network is a network of nodes that allows pods to communicate with each other. In this step, we deploy a pod network add-on to the cluster. There are several pod network add-ons available for Kubernetes, and we are using Flannel in this tutorial. Flannel is a simple and easy-to-use network add-on that provides an overlay network for pods to communicate with each other.

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

Step 8: Join Worker Nodes

In this step, we join the worker nodes to the cluster using the join command generated by Kubeadm in Step 5. This process involves downloading and installing the necessary Kubernetes components and creating the necessary configuration files.

sudo kubeadm join <master

Step 9: Verify Cluster

In the final step, we verify that the cluster is up and running by running the kubectl get nodes command on the master node. This command should display all the nodes in the cluster, including the master node and the two worker nodes.

kubectl get nodes

Conclusion

Congratulations! You have successfully set up a Kubernetes cluster with one master node and two worker nodes using Kubeadm. You can now deploy applications to the cluster and manage them using Kubernetes commands.

Did you find this article valuable?

Support Lohith K by becoming a sponsor. Any amount is appreciated!