How To Setup Kubernetes Cluster

How to setup Kubernetes Cluster

How To Setup Kubernetes Cluster

Introduction

Kubernetes has become the de facto standard for container orchestration, enabling organizations to efficiently manage and scale containerized applications. In this article, we will guide you through the step-by-step process of setting up a complete Kubernetes cluster on Ubuntu. Whether you’re a beginner or an experienced system administrator, this comprehensive guide will provide you with the knowledge and tools necessary to deploy and manage your own Kubernetes cluster.

Prior to getting started, we highly recommend going through our comprehensive previous guide, which provides detailed information about Kubernetes and its various components. This will equip you with a solid understanding of Kubernetes before you begin. Click here

Prerequisites For Lab Setup

Here is the model setup we will be using for this tutorial:

  • Master Node: master-node with the IP address 10.64.0.21
  • Worker Node 1: worker-node1 with the IP address 10.64.0.22
  • Worker Node 2: worder-node2 with the IP address 10.64.0.23

Make sure that each node meets the following minimum requirements:

  • 2 CPUs
  • 8 GB of hard disk space
  • 4 GB of RAM

Now that we have covered the basic requirements and the setup, let’s proceed to configure a Kubernetes cluster on Ubuntu 20.04.

Before diving into the Kubernetes installation, we need to prepare our Ubuntu environment for the setup.

Step 1: Configure Hostname and Update Host Files:

To begin, follow these steps to set up the hostname and update the host files for each Kubernetes node:

For the master node:

  1. Log in to the master-node using SSH.
  2. Set the hostname for the master node by executing the following command:
sudo hostnamectl set-hostname master-node

 

For the worker node 1:

  1. Log in to the worker node 1 using SSH.
  2. Set the hostname for the worker node 1 by executing the following command:
$ sudo hostnamectl set-hostname worder-node1

For the worker node 2:

  1. Log in to the worker node 2 using SSH.
  2. Set the hostname for the worker node 2 by executing the following command:
$ sudo hostnamectl set-hostname worker-node2
 

Next, update the /etc/hosts file on each of the three nodes with the respective IP addresses and hostnames. Open the file using a text editor and add the following entries:

10.64.0.21 master-node

10.64.0.22 worker-node1
10.64.0.23 worker-node2
 

Save the changes to the /etc/hosts file.

Now you are ready to proceed with the setup. Let’s move on to the next step.

Step2. Disable Swap Memory On Each Nodes:

Disabling swap memory is a recommended step for Kubernetes installations, as it can interfere with the performance and stability of the cluster.
 
sudo swapoff -a
 

Step 3. Install Docker On Master and Worker Nodes

Docker is a prerequisite for running Kubernetes as it provides the container runtime environment. Follow the steps below to install Docker on Ubuntu.

Installing Docker on All Nodes
  1. Update the package manager:
$ sudo apt update
  1. Install Docker’s dependencies:
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common
  1. Add the Docker GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg –dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  1. Add the Docker repository:
$ echo “deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  1. Update the package manager again:
$ sudo apt update
  1. Install Docker:
$ sudo apt install docker-ce docker-ce-cli containerd.io

 

Configuring Docker

After installing Docker, there are a few configurations we need to apply.

  1. Create a new docker group and add your user to it:
$ sudo groupadd docker

$ sudo usermod -aG docker $USER
  1. Enable Docker to start on boot:
$ sudo systemctl enable docker
  1. Restart Docker:
$ sudo systemctl restart docker

 

Step 4. Installing Kubernete 

With Docker installed and configured, we can proceed to install Kubernetes components on our Nodes.

Adding Kubernetes repository

  1. Import the Kubernetes GPG key:
$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add –
  1. Add the Kubernetes repository:
$ sudo apt-add-repository “deb http://apt.kubernetes.io/ kubernetes-xenial main”
  1. Update the package manager:
$ sudo apt update
 
 4. Install Kubernetes control plane components:

 

$ sudo apt install kubelet kubeadm kubectl
 
5. Prevent automatic updates of the Kubernetes packages:

 

$ sudo apt-mark hold kubelet kubeadm kubectl
 

Step 5. Setting up the Master Node

Now that we have Kubernetes installed, we can proceed to set up the master node of our cluster.

Initializing the cluster
  1. Initialize the cluster on the master node:
$ sudo kubeadm init –pod-network-cidr=10.244.0.0/16
 

2. Follow the instructions provided by the kubeadm init command to set up the kubeconfig and join worker nodes 

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

By default, Kubernetes doesn’t provide networking capabilities. We need to install a network plugin to enable communication between pods across different nodes.

  1. Install the Calico network plugin:
$ kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
  1. Verify that the Calico pods are running:
$ kubectl get pods –all-namespaces

 

Joining worker nodes to the cluster

To add worker nodes to the cluster, you need to run a kubeadm join command followed by a token and discovery hash provided during the kubeadm init step.

Step 6: Verify Cluster On the master node, you can check the status of the cluster by running:

$ kubectl get nodes
 

Conclusion

Conclusion: Congratulations! You have successfully set up a complete Kubernetes cluster on Ubuntu. You can now leverage the power of Kubernetes to deploy and manage containerized applications at scale. This guide provided a comprehensive overview of the step-by-step process, from installing Docker and Kubernetes tools to configuring the master node and joining worker nodes. You’re now ready to embark on your containerization journey with Kubernetes!

Nitin Kumar

Nitin Kumar is a skilled DevOps Engineer with a strong passion for the finance domain. Specializing in automating processes and improving software delivery. With expertise in cloud technologies and CI/CD pipelines, he is adept at driving efficiency and scalability in complex IT environments.