Install and Deploy Kubernetes Cluster on Ubuntu 20.04

0
469

Welcome to our tutorial on how to install and deploy Kubernetes Cluster on Ubuntu 20.04. Kubernetes, according to kubernetes.io is an open-source production-grade container orchestration platform. It facilitates automated deployment, scaling and management of containerized applications.

Install and Deploy Kubernetes on Ubuntu 20.04

In this tutorial, we are going to deploy a two node Kubernetes cluster. Below are our node details.

NodeHostnameIP AddressvCPUsRAM (GB)OS
Mastermaster.kifarunix-demo.com192.168.56.10122Ubuntu 20.04 LTS
Worker 1wk01.kifarunix-demo.com192.168.57.622Ubuntu 20.04 LTS
Worker 2wk02.kifarunix-demo.com192.168.58.622Ubuntu 20.04 LTS

Run System Update

To begin with, ensure that your system packages are up-to-date;

apt update
apt upgrade

Disable Swap

Running Kubernetes requires that you disable swap.

Check if swap is enabled.

swapon --show
NAME      TYPE SIZE USED PRIO
/swap.img file   2G   0B   -2

If there is no output, then swap is not enabled. If it is enabled as shown in the output above, run the command below to disable it.

swapoff -v /swap.img

Or simply

swapoff -a

To permanently disable swap, comment out or remove the swap line on /etc/fstab file.

vim /etc/fstab
...
#                
# / was on /dev/sda2 during curtin installation
/dev/disk/by-uuid/f3d575c6-e940-4472-b406-30da6b66965a / ext4 defaults 0 0
#/swap.img      none    swap    sw      0       0

or Simply remove it;

sed "-i.bak" '/swap.img/d' /etc/fstab

Install Docker Container Runtime on Ubuntu 20.04

Kubernetes uses container runtime to run containers in Pods. It supports multiple container runtimes including DockercontainerdCRI-O. IN this demo, we will be using Docker CE. Therefore, on all nodes, master and workers, you need to install Docker.

Follow the link below to learn how to install Docker CE on Ubuntu 20.04.

Install Docker CE on Ubuntu 20.04

You can verify Docker installation;

docker --version
Docker version 19.03.11, build 42e35e61f3

Install Kubernetes on Ubuntu 20,04

There are a number of node components, required to provide Kubernetes runtime environment that needs to be installed on each node. These include:

  • kubelet: runs as an agent on each node and ensures that containers are running in a Pod.
  • kubeadm: Bootstraps Kubernetes cluster
  • kubectl: Used to run commands against Kubernetes clusters. 

These components are available on the default Ubuntu 20.04 repos. Thus, you need to install Kubernetes repos to install them.

Install Kubernetes Repository GPG Signing Key

Run the command below to install Kubernetes repo GPG key.

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

Install Kubernetes Repository on Ubuntu 20.04

Next install the Kubernetes repository;

echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" > /etc/apt/sources.list.d/kurbenetes.list

Install Kubernetes components

apt update
apt install kubelet kubeadm kubectl

Initialize Kubernetes Control Plane

After the installation of the container runtime as well the Kubernetes components, it is time to initialize the Kubernetes master node. The Kubernetes master is responsible for maintaining the desired state for your cluster.

Run the command below on the master node to bootstrap the Kubernetes control-plane node.

kubeadm init --apiserver-advertise-address=192.168.56.101 --pod-network-cidr=172.16.0.0/16

Note;

  • --apiserver-advertise-address: Defines the IP address the API Server will advertise it’s listening on. If not set the default network interface will be used.
  • --pod-network-cidr: Specify range of IP addresses for the pod network. If set, the control plane will automatically allocate CIDRs for every node. You use this to define your preferred network range if there is gonna be a collision between your network plugin’s preferred Pod network and some of your host networks.

Once the initialization is done, you should be able to see an output similar to the one below;

...
Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

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

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.56.101:6443 --token v573ma.gf8iyl3a8mtd35rb \
    --discovery-token-ca-cert-hash sha256:a110e087527d1eeb48389f1e9ac8540ee52e237f940a6813ca9c033fe0fafa71

As suggested on the output above, you run the commands below on the master node to start using your cluster.

Be sure to run the commands as regular user, with sudo rights.

su - koromicha

Next, create a Kubernetes cluster directory.

mkdir -p $HOME/.kube

Copy Kubernetes admin configuration file to the cluster directory created above.

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

Set the proper ownership for the cluster configuration file.

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

Verify the status of the Kubernetes cluster;

kubectl get nodes
NAME                        STATUS     ROLES    AGE   VERSION
master.kifarunix-demo.com   NotReady   master   17m   v1.18.3

As you can see, the cluster is not ready yet.

Deploy Kubernetes Cluster Pod Network

A Pod is a group of one or more related containers in a Kubernetes cluster. They share the same lifecycle, storage/network. For Pods to communicate with one another, you must deploy a Container Network Interface (CNI) based Pod network add-on.

To deploy a CNI Pod network, run the command below on the master node;

kubectl apply -f [podnetwork].yaml

Where [podnetwork].yaml is the path to your preferred CNI. In this demo, we are using Flannel. Flannel is a virtual network that attaches IP addresses to containers.

Deploy Flannel network by running the command below. Similarly, execute the command as the user with which you created the Kubernetes cluster on its home directory.

[email protected]:~$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

Different types of CNI are explained on Kubernetes addons page.

Once the command completes, you can list the Pods in the namespaces by running the command below;

kubectl get pods --all-namespaces
NAMESPACE     NAME                                                READY   STATUS    RESTARTS   AGE
kube-system   coredns-66bff467f8-9j4q5                            1/1     Running   0          17m
kube-system   coredns-66bff467f8-blggv                            1/1     Running   0          17m
kube-system   etcd-master.kifarunix-demo.com                      1/1     Running   0          17m
kube-system   kube-apiserver-master.kifarunix-demo.com            1/1     Running   0          17m
kube-system   kube-controller-manager-master.kifarunix-demo.com   1/1     Running   0          17m
kube-system   kube-flannel-ds-amd64-nw2rw                         1/1     Running   0          7m6s
kube-system   kube-proxy-srmgx                                    1/1     Running   0          17m
kube-system   kube-scheduler-master.kifarunix-demo.com            1/1     Running   0          17m

Check the status of the node;

kubectl get nodes
NAME                        STATUS   ROLES    AGE   VERSION
master.kifarunix-demo.com   Ready    master   23m   v1.18.3

Add Other Nodes to Kubernetes Cluster

You can now add other nodes to the Kubernetes cluster using the kubeadm join command as follows.

Login to worker nodes and execute the command below below on each one of them to add them to the cluster.

Note that this command is displayed after initializing the control plane above and it should be executed as root user.

sudo kubeadm join 192.168.56.101:6443 --token an2ocw.6zx0pc0dgw3wj3jl \
--discovery-token-ca-cert-hash sha256:5fb8fc688dd88f3c7d450a5461705e6dc0014631050313f7b9ce199b538658a6

If you didn’t save the Kubernetes Cluster joining command, you can at any given time print using the command below;

kubeadm token create --print-join-command

Once the command runs, you will get an output similar to below;

...
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

On the Kubernetes control plane (master), run the command below to verify that the nodes have joined the cluster.

kubectl get nodes
NAME                        STATUS   ROLES    AGE     VERSION
master.kifarunix-demo.com   Ready    master   40m     v1.18.3
wk01.kifarunix-demo.com     Ready       4m11s   v1.18.3
wk02.kifarunix-demo.com     Ready       3m51s   v1.18.3

As you can see, we now have a cluster. Run the command below to get cluster information.

kubectl cluster-info
Kubernetes master is running at https://192.168.56.101:6443
KubeDNS is running at https://192.168.56.101:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

You are now ready to deploy an application on Kubernetes cluster.

That marks the end of our guide on how to install and deploy Kubernetes Cluster on Ubuntu 20.04. In our next tutorial, we will learn how to deploy an application on Kubernetes cluster.

Further Reading

Getting Started with Kubernetes

Other Tutorials

You can as well check our other tutorials

Configure Highly Available HAProxy with Keepalived on Ubuntu 20.04

Install and Setup HAProxy on Ubuntu 20.04

Install and Configure Filebeat on CentOS 8

Install and Configure NXLog CE on Ubuntu 20.04

LEAVE A REPLY

Please enter your comment!
Please enter your name here