How to Install Kubernetes on Ubuntu 20.04

install kubernetes ubuntu 20.04

Containers are a good choice to bundle and run our own applications. When the number of containers increases rapidly and need to manage them in a convenient way. That’s the reason why Kubernetes comes. Kubernetes (K8s) is an open-source system for automating deployment, scaling, managing containerized applications and services.

Kubernetes cluster contains master and worker nodes. The master node control and manages a group of worker nodes. You can have multiple master nodes for high availability clusters.

This tutorial shows how to install a Kubernetes cluster with kubeadm on Ubuntu 20.04.

Preparing Environment

  • Use 2 Linux host running Ubuntu 20.04

Setting static IP addresses for Master node and Worker node

- Master node: 192.168.1.11
- Worker node: 192.168.1.12

  • Configure the hostname for each machine

We will use node-1 as the master and node-2 as the worker node.

$ sudo hostnamectl set-hostname node-1
$ sudo hostnamectl set-hostname node-2
  • Disable swap memory on each ubuntu node
$ sudo swapoff -a

Add Kubernetes repository

Kubernetes is not available in Ubuntu’s default repository, so have to add it manually.

On both Master and Worker Node perform the following:

Adding Kubernetes signing key as follows:

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

Then, add the Kubernetes repo, run:

$ echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" >> ~/kubernetes.list
$ sudo mv ~/kubernetes.list /etc/apt/sources.list.d
$ sudo apt update

Installing Kubernetes tools

In this section, we will install Kubeadm, cri-o, Kubelet, and Kubernetes-cni. All these tools need to be installed on both master and worker nodes.

Make sure to install the recommended and supported version. Here we are installing version 1.18 of Kubernetes and crio.

Install Kubeadm

Kubeadm is a tool that is a part of the Kubernetes project which helps to initialize the Kubernetes cluster.

In this tutorial, we're going to install kubeadm version 1.18.0-00, type:

$ sudo apt install -y kubeadm=1.18.0-00 --allow-unauthenticated

Note: you can find the specific version by the following command:

$ curl -s https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages | grep Version | awk '{print $2}'

Install Kubectl

Kubectl is the Kubernetes command-line tool. It allows you to run commands in the Kubernetes clusters. You can use kubectl to deploy applications, manage cluster resources, and view logs.

Install kubectl v1.18.0-00 by the following command:

$ sudo apt install -y kubectl=1.18.0-00 --allow-unauthenticated

Install CRI-O

CRI-O is an OCI compliant container runtime interface (CRI). The most commonly used runtime is Docker. From the release of Kubernetes 1.20, the container runtime interface (CRI) shim for Docker is being deprecated. Docker produces images that are not an OCI (Open Container Initiative) image.

We should use OCI compliant container runtime to pull and run OCI images, especially if you use Kubernetes services like GKE, EKS, or AKS.

You can still use docker, then install using sudo apt-get install docker.io

Here I will use cri-o which is a compliant runtime. Keep cri-o version matching with Kubernetes version.

Make sure you install the supported version.

First, use modprobe command to load the overlay and br_netfilter modules on both of Master and Worker node:

$ sudo modprobe overlay
$ sudo modprobe br_netfilter

Then, create a sysctl config file to enable IP forwarding and netfilter settings during the reboots by inserting the following lines to /etc/sysctl.d/99-kubernetes-cri.conf file on Master and Worker node:

net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1

Apply the config file by running:

$ sudo sysctl --system

Now, we specify the Ubuntu OS and cri-o version as follows:

$ sudo -i
# export OS=xUbuntu_20.04
# export VERSION=1.18

Then run the following commands as root user:

# echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/ /" > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
# echo "deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/$VERSION/$OS/ /" > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable:cri-o:$VERSION.list

# curl -L https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:$VERSION/$OS/Release.key | apt-key add -
# curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/Release.key | apt-key add -

# apt update
# apt install cri-o cri-o-runc

Once, the installation is finished, the conmon (Container Monitoring) utility was installed. Find the path of conmon:

$ which conmon

/usr/bin/conmon

Edit the /etc/crio/crio.conf file as follows:

...
# Path to the conmon binary, used for monitoring the OCI runtime.
conmon = "/usr/bin/conmon"         #<-- Edit this line. Around line 108

...

registries = [                     #<-- Edit and add registries. Around line 351
        "docker.io",
        "quay.io",
]
....

Enable the cri-o and make sure that it is running:

$ sudo systemctl daemon-reload
$ sudo systemctl enable crio
$ sudo systemctl start crio
$ sudo systemctl status crio

Output:

● crio.service - Container Runtime Interface for OCI (CRI-O)
   Loaded: loaded (/usr/lib/systemd/system/crio.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-12-10 15:46:37 UTC; 3 days ago
     Docs: https://github.com/cri-o/cri-o
...

Install Kubelet

Kubelet is an agent running on each node and is responsible for talking to the API server on master node and driving the container runtime to start workloads.

Configure kubelet to understand how to interact with cri-o by inserting the following line to /etc/default/kubelet file:

KUBELET_EXTRA_ARGS=--feature-gates="AllAlpha=false,RunAsGroup=true" --container-runtime=remote --cgroup-driver=systemd --container-runtime-endpoint='unix:///var/run/crio/crio.sock' --runtime-request-timeout=5m

On both of Master and Worker node, run the following command to install kubelet:

$ sudo apt install -y kubelet=1.18.0-00 --allow-unauthenticated

Install Kubernetes-cni

To enable container networking in the cluster, we have to install kubernetes-cni.

Run the following command:

$ sudo apt-get install -y kubernetes-cni --allow-unauthenticated

Deploying Kubernetes cluster

Deploying the Kubernetes cluster involves 2 steps. 1st  step would be to initialize the master node and 2nd step is to join the worker node to the cluster.

Initialize master node

To Initialize Kubernetes on Master node, type:

$ sudo kubeadm init --apiserver-advertise-address=192.168.1.11 --pod-network-cidr=10.244.0.0/16
kubeadm init
kubeadm init

It will take a few minutes to finish. Once the initialization was done, the terminal will display the output as follow:

initialized successfully
initialized successfully

Take note of the line to join the cluster marked above, will use it in the next step to join worker node to cluster.

Now, run the following command to create Kubernetes config directory on Master node:

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

Next, deploy a pod network to cluster:

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

Verifying that all Control-plane components have successfully installed:

$ kubectl get pod --all-namespaces
get pod –all-namespaces
get pod –all-namespaces

Join worker node to cluster

Now login worker node and join the worker node to the cluster.

On the worker node machine, run the following command:

$ sudo kubeadm join 192.168.1.11:6443 --token 9ii02d.nsmrmu1asascv2yg \
    --discovery-token-ca-cert-hash sha256:1104bf70b03a2d030ffc0a462f9dbcbcdd9975393e9a9ac2a1f18500f1b6b74e

After the joining process was finished, go back to the Master node, and run:

$ kubectl get node
NAME     STATUS   ROLES    AGE     VERSION
node-1   Ready    master   2m37s   v1.18.0
node-2   Ready    none     22s     v1.18.0

To get more information about the node, type:

$ kubectl get node -owide
NAME     STATUS   ROLES    AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION   CONTAINER-RUNTIME
node-1   Ready    master   9h    v1.18.0   10.2.0.4              Ubuntu 20.04.1 LTS   5.4.0-1029-gcp   cri-o://1.18.4
node-2   Ready    none     9h    v1.18.0   10.2.0.5              Ubuntu 20.04.1 LTS   5.4.0-1029-gcp   cri-o://1.18.4

The outputs show details master node and nodes which has joined the cluster.

Read Also: How to Install Kubernetes Locally using Vagrant/Minikube

Conclusion

As containerization is getting popular, the need for managing containerized workloads and services make Kubernetes so popular. All most major cloud providers started supporting managed Kubernetes which makes life easier.

Kubernetes is completely free and can download any time from its repository. Hope you enjoyed installing Kubernetes on Ubuntu. You can try to deploy applications like MySQL or WordPress on the K8 cluster.

Thank you for reading and please leave your suggestion in the below comment section.

2 Comments... add one

Leave a Comment