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.
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 (Ctrl–Alt–T)
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
3. Set Docker to launch on boot by entering the following:
sudo systemctl enable docker
4. Verify Docker is running:
sudo systemctl status docker
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
sudo apt-mark hold kubeadm kubelet kubectl
Allow the process to complete.
2. Verify the installation with:
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
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
7. Save the file and exit, then reload the configuration by typing:
sudo sysctl --system
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.
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
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"
}
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"
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.
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
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
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.