How to Install Kubernetes on Ubuntu 20.04

Introduction

Kubernetes is an open source platform for managing container technologies such as Docker.

Docker lets you create application containers from a pre-configured image. Kubernetes provides the next step, allowing you to balance loads between the containers and run multiple containers across multiple systems.

This guide will walk you through how to install Kubernetes on Ubuntu 20.04.

How to install and configure Kubernetes on Ubuntu 20.04.

Prerequisites

  • 2 or more Linux servers running Ubuntu 20.04
  • Access to a user account on each system with sudo or root privileges
  • Command-line/terminal window (CtrlAltT)

Note: Instructions in this tutorial can also be applied to newer Ubuntu versions, such as Ubuntu 22.04.

Set up Docker

Kubernetes requires an existing Docker installation. Install and enable Docker on each server node by following the steps below :

1. Update the package list:

sudo apt update

2. Next, install Docker with the command:

sudo apt install docker.io -y
Installing Docker.

3. Set Docker to launch on boot by entering the following:

sudo systemctl enable docker

4. Verify Docker is running:

sudo systemctl status docker
Viewing Docker service status.

5. Start Docker if it is not running:

sudo systemctl start docker

Install Kubernetes

Step 1: Add Kubernetes Signing Key

Since you are downloading Kubernetes from a non-standard repository, it is essential to ensure that the software is authentic. This is done by adding a signing key.

On each node, use the curl command to download the key, then store it in a safe place (default is /usr/share/keyrings):

curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo tee /usr/share/keyrings/kubernetes.gpg

Step 2: Add Software Repositories

Kubernetes is not included in the default repositories. To add the Kubernetes repository to your list, enter the following on each node:

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/kubernetes.gpg] http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list

Step 3: Kubernetes Installation Tools

Kubeadm (Kubernetes Admin) is a tool that helps initialize a cluster. It fast-tracks setup by using community-sourced best practices. Kubelet is the work package, which runs on every node and starts containers. The tool gives you command-line access to clusters.

Execute the following commands on each server node.

1. Install Kubernetes tools with the command:

sudo apt install kubeadm kubelet kubectl
Installing Kubernetes tools.
sudo apt-mark hold kubeadm kubelet kubectl
Setting Kubernetes tools on hold.

Allow the process to complete.

2. Verify the installation with:

kubeadm version
Checking the kubeadm version.

Note: Make sure you install the same version of each package on each machine. Different versions can create instability. Also, this process prevents apt from automatically updating Kubernetes. For update instructions, please see the developers’ instructions.

Note: BMC offers balanced and affordable server instances well suited for containerized services deployment. To simplify and streamline the process, deploy Kubernetes clusters on BMC using our Rancher solution.

Deploy Kubernetes

Step 1: Prepare for Kubernetes Deployment

This section shows you how to prepare the servers for a Kubernetes deployment. Execute the following steps on each server node.

1. Disable the swap memory. To perform this action, execute swapoff:

sudo swapoff -a

Then type the sed command below:

sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

2. Load the required containerd modules. Start by opening the configuration file for containerd in a text editor:

sudo nano /etc/modules-load.d/containerd.conf

3. Add the following two lines:

overlay
br_netfilter
Editing containerd configuration.

4. Next, use the modprobe command to add the modules:

sudo modprobe overlay
sudo modprobe br_netfilter

5. Configure Kubernetes networking. Open the kubernetes.conf file:

sudo nano /etc/sysctl.d/kubernetes.conf

6. Add the following lines:

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

7. Save the file and exit, then reload the configuration by typing:

sudo sysctl --system
Reloading system configuration.

Step 2: Assign Unique Hostname for Each Server Node

1. Decide which server to set as the master node. Then enter the command:

sudo hostnamectl set-hostname master-node

2. Next, set a worker node hostname by entering the following on the worker server:

sudo hostnamectl set-hostname worker01

If you have additional worker nodes, use this process to set a unique hostname on each.

3. Edit the hosts file on each node by adding the IP address and hostname of the servers you want to add to the cluster.

Editing the hosts file.

Step 3: Initialize Kubernetes on Master Node

Switch to the master node, and follow the steps to initialize Kubernetes on it:

1. Open the kubelet file in a text editor.

sudo nano /etc/default/kubelet

2. Add the following line to the file:

KUBELET_EXTRA_ARGS="--cgroup-driver=cgroupfs"

Save and exit the file.

3. Execute the following commands to reload the configuration:

systemctl daemon-reload
Reloading daemons with systemctl.
systemctl restart kubelet

4. Open the Docker daemon configuration file:

sudo nano /etc/docker/daemon.json

5. Append the following configuration block:

    {
      "exec-opts": ["native.cgroupdriver=systemd"],
      "log-driver": "json-file",
      "log-opts": {
      "max-size": "100m"
   },

       "storage-driver": "overlay2"
       }
Editing the daemon.json file.

Save the file and exit.

6. Reload the configuration:

systemctl daemon-reload
systemctl restart docker

7. Open the kubeadm configuration file:

sudo nano /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

8. Add the line below to the file:

Environment="KUBELET_EXTRA_ARGS=--fail-swap-on=false"
Kubeadm configuration.

Save the file and exit.

9. Reload the kubelet:

systemctl daemon-reload
systemctl restart kubelet

10. Initialize the cluster by typing:

sudo kubeadm init --control-plane-endpoint=[master-hostname] --upload-certs

Once this command finishes, it will display a kubeadm join message at the end. Make a note of the whole entry. This will be used to join the worker nodes to the cluster.

Master node joins the cluster.

11. Enter the following to create a directory for the Kubernetes cluster:

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

Step 4: Deploy Pod Network to Cluster

A Pod Network is a way to allow communication between different nodes in the cluster. This tutorial uses the Flannel node network manager to create a pod network.

1. Use kubectl to install Flannel:

kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

2. Untaint the node:

kubectl taint nodes --all node-role.kubernetes.io/control-plane-

Step 5: Join Worker Node to Cluster

As indicated in Step 3, you can enter the kubeadm join command on worker nodes to connect them to the master node. Repeat the steps on each worker node you want to add to the cluster.

1. Execute the following commands to disable apparmor:

systemctl stop apparmor
sudo systemctl disable apparmor
systemctl restart containerd.service

2. Enter the command you noted in Step 3:

kubeadm join [master-node-ip]:6443 --token abcdef.1234567890abcdef --discovery-token-ca-cert-hash sha256:1234..cdef
Worker node joins the cluster.

Replace the alphanumeric codes with those from your master server. Repeat for each worker node on the cluster. Wait a few minutes; then you can check the status of the nodes.

3. Switch to the master server, and enter:

kubectl get nodes
Viewing deployed nodes.

The system should display the worker nodes that you joined to the cluster.

Conclusion

After following the steps mentioned in this article carefully, you should now have Kubernetes installed on Ubuntu.

For beginners who still have no experience in deploying multiple containers, Minikube is a great way to start. Minikube is a system for running a single node cluster locally and is excellent for learning the basics, before moving on to Kubernetes.

This network uses multiple servers to communicate back and forth. Kubernetes allows you to launch and manage Docker containers across multiple servers in the pod.

Was this article helpful?
YesNo
Dejan Tucakov
Dejan is the Head of Content at phoenixNAP with over 8 years of experience in Web publishing and technical writing. Prior to joining PNAP, he was Chief Editor of several websites striving to advocate for emerging technologies. He is dedicated to simplifying complex notions and providing meaningful insight into data center and cloud technology.
Next you should read
What is Kubernetes? Complete Guide
April 20, 2023

If you are using Docker, you need to learn about Kubernetes. It is an open-source container orchestration ...
Read more
How to Install a Kubernetes Cluster on CentOS 7
November 8, 2019

Use Kubernetes to launch and orchestrate your applications efficiently. The steps outlined in this tutorial ...
Read more
How To Install Node.js & NPM on Ubuntu 18.04
March 18, 2019

Node.js is an open-source cross-platform JavaScript (JS) runtime environment. It is used for building fast ...
Read more
How To Remove Docker Images, Containers, Networks & Volumes
February 7, 2019

Docker allows users to create a container in which an application or process can run. In this guide, you will ...
Read more