Kubernetes Cluster Setup Using Kubeadm

Recently I cleared CKA (Certified Kubernetes Administrator) exam, I was thinking why not start a series on my exam preparation journey. In this post I will walk you through the Kubernetes Cluster Setup Using Kubeadm.  I am going to setup total 3 virtual machine on the VMware workstation with Ubuntu operating system. First VM will be Master/Control Plane and other two virtual machines will act as worker nodes. You can use Minikube also, but I will walk you through it later.

If you would like to know how to setup VMware workstation and Ubuntu system, please refer this link. We can setup kubeadm cluster on virtual machine or physical host, here I will be using virtual machines. Please check this official link for cluster requirement setup.

My personal computer has 32 GB RAM and Core i7 processor, but you can install it on 16 GB RAM and Core i5 system also. It work perfectly fine, I have personally tested.

Master/Control Plane setup

Step 1-

Install the container runtime,

a. We will install Docker in this setup, this is required to run the pods on each node. Technically you can run pods on Controlplane but it is not recommended/best practice.

Docker install on Ubuntu

Kubernetes support below container runtime

Docker, CRI-O and containerd

b. Once Docker is installed, you need to add your user to the docker group (otherwise you’d have to run all docker commands with sudo, which could lead to security issues). To add your user to the docker group, issue the command:

Add local user to docker group

Log out and log back in, so the changes will take effect.

Start and enable the docker daemon with the commands:

Step 2-

Installation of Kubernetes

A. First add the Kubernetes GPG key with the below command

curl s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add

B. Now add the necessary repository with the below command

sudo aptaddrepository “deb http://apt.kubernetes.io/ kubernetes-xenial main”

C. Now install the packages   kubeadm, kubelet and kubectl

  • kubeadm: the command to bootstrap the cluster.
  • kubelet: the component that runs on all of the machines in your cluster and does things like starting pods and containers.
  • kubectl: the command line util to talk to your cluster.

sudo aptget install kubeadm kubelet kubectl y    

This command will install latest version of these packages with all software dependencies.  If you want to want to install specific version of these packages, use below command (for e.g)

sudo apt-get install -y kubeadm=1.20.2-00

sudo apt-get install -y kubelet=1.20.2.-00

sudo apt-get install -y kubectl=1.20.2-00

Repeat above steps on other worker nodes also.

Step 3-

Disable the swap In order to run Kubernetes, we need to disable to swap on all the machines.

sudo swapoff -a

Ubuntu swap disable

You may be wondering why disable the swap, the reason is —

The idea of kubernetes is to tightly pack instances to as close to 100% utilized as possible. All deployments should be pinned with CPU/memory limits. So if the scheduler sends a pod to a machine it should never use swap at all. You don’t want to swap since it’ll slow things down.

Its mainly for performance.

Step 4-

Initialize the master

Now we need to initialize the master node,  run below command. Make sure you replace the IP with your master host IP

Kubeadm initialize

Once initialization is complete you will get list of command or instruction to be performed on master node and worker node.  Kubeadm init

On the master only, create a directory for the cluster with the command:

mkdir -p $HOME/.kube

Copy the config file into this directory with the command:

sudo cp i /etc/kubernetes/admin.conf $HOME/.kube/config

Give the config file the proper permissions with the command

sudo chown $(id u):$(id g) $HOME/.kube/config

The last command shown in the above screenshot will be used to join the worker node with master. Please make a note of it.

Step 5-

Deploying a POD Network– Before you join a worker to the master node or start deploying PODs, you must deploy POD network. One of the solution supported by Kubernetes is Flannel. For other list of supported add-on please refer this link.

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

  Flannel installation
TIP – If you know how to setup a DNS on Ubuntu then I would highly recommend you to configure it. Else, you can manually add all host IPs in /etc/hosts file in master node.

Worker Node Setup

Please setup additional Ubuntu VMs for worker node configuration.

Step 1-

A. We will install Docker in this setup, this is required to run the pods on each node.

Docker install Ubuntu

Kubernetes support below container runtime

Docker, CRI-O and containerd

b. Once Docker is installed, you need to add your user to the docker group (otherwise you’d have to run all docker commands with sudo, which could lead to security issues). To add your user to the docker group, issue the command:

Log out and log back in, so the changes will take effect.

Start and enable the docker daemon with the commands:

Step 2-

Installation of Kubernetes

A. First add the Kubernetes GPG key with the below command

curl s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add

B. Now add the necessary repository with the below command

sudo aptaddrepository “deb http://apt.kubernetes.io/ kubernetes-xenial main”

C. Now install the packages   kubeadm, kubelet and kubectl

sudo aptget install kubeadm kubelet kubectl y    

This command will install latest version of these packages with all software dependencies.  If you want to want to install specific version of these packages, use below command (for e.g)

sudo apt-get install -y kubeadm=1.20.2-00

sudo apt-get install -y kubelet=1.20.2.-00

sudo apt-get install -y kubectl=1.20.2-00

Step 3-

Disable the swap 

In order to run Kubernetes, we need to disable to swap on all the machines.

sudo swapoff -a

open /etc/fstable file and comment last line.

Ubuntu swap disable

Step 4-

Joining the worker node the master node – Please note down the last command shown in the screenshot under Step 4. If you haven’t, no problem just run below command on the master node

kubeadm token createprintjoincommand

copy paste entire output and run it on worker node

kubeadm worker node addition

Now you can run  “kubectl get node” command on the master node to see available node in the kubeadm cluster.

kubectl get nodes

Please follow above steps to add additional worker nodes in to the cluster.

Hope you have enjoyed the article on Kubernetes cluster Setup Using Kubeadm. Feel free share you suggestion about this post. Happy learning !! Stay safe !!

Leave a Reply