How to Install Kubernetes on Ubuntu 22.04

By
Marko Aleksic
Published:
December 13, 2023

Kubernetesย is anย open-sourceย platform for OCI-compliant container workload orchestration. As a container orchestrator, Kubernetes automates container deployment across multiple systems and helps scale and manage containerized applications.

This guide shows how to install Kubernetes on Ubuntu 22.04 in five steps.

How to install and configure Kubernetes on Ubuntu 22.04.

Prerequisites

  • Two or more servers running Ubuntu 22.04.
  • Command-line access.
  • Access to a user account with sudo privileges on each system.

Note: Instructions in this tutorial can also be applied to older Ubuntu versions, such as Ubuntu 20.04 LTS.

Set up Docker

Kubernetes requires a CRI-compliant container engine runtime such as Docker, containerd, or CRI-O. This article shows how to deploy Kubernetes using Docker.

Install Docker on each server node by executing the steps below:

1. Update the package list:

sudo apt update

2. Install Docker with the following command:

sudo apt install docker.io -y
Installing Docker on Ubuntu 22.04.

3. Set Docker to launch on boot by entering:

sudo systemctl enable docker

4. Verify Docker is running:

sudo systemctl status docker
Viewing Docker service status.

5. If Docker is not running, start it with the following command:

sudo systemctl start docker

Install Kubernetes

Setting up Kubernetes on an Ubuntu system involves adding the Kubernetes repository to the APT sources list and installing the relevant tools. Follow the steps below to install Kubernetes on all the nodes in the cluster.

Step 1: Add Kubernetes Signing Key

Since Kubernetes is not available from a standard repository, download the signing key to verify the software's authenticity.

On each node, use the curl command to download the key and store it in a safe place (default is /etc/apt/keyrings/:

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

Step 2: Add Software Repositories

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

echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

Ensure all packages are up to date:

sudo apt update

Step 3: Install Kubernetes Tools

Each Kubernetes deployment consists of three separate tools:

  • Kubeadm. A tool that initializes a Kubernetes cluster by fast-tracking the setup using community-sourced best practices.
  • Kubelet. The work package that runs on every node and starts containers. It provides a command-line access to clusters.
  • Kubectl. The command-line interface for interacting with clusters.

Execute the following commands on each server node to install the Kubernetes tools:

1. Run the install command:

sudo apt install kubeadm kubelet kubectl
Installing Kubernetes tools.

2. Mark the packages as held back to prevent automatic installation, upgrade, or removal:

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

Note: The process presented in this tutorial prevents APT from automatically updating Kubernetes. For instructions on how to update, please see the official developers' instructions.

3. Verify the installation with:

kubeadm version
Checking the kubeadm version.

The output of the version command shows basic deployment information.

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

With the necessary tools installed, proceed to deploy the cluster. Follow the steps below to make the necessary system adjustments, initialize the cluster, and join worker nodes.

Step 1: Prepare for Kubernetes Deployment

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

1. Disable all swap spaces with the swapoff command:

sudo swapoff -a

Then use the sed command below to make the necessary adjustments to the /etc/fstab file:

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

2. Load the required containerd modules. Start by opening the containerd configuration file in a text editor, such as nano:

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

3. Add the following two lines:

overlay
br_netfilter
Editing containerd configuration.

Save the file and exit.

4. Next, add the modules with theย modprobe command:

sudo modprobe overlay
sudo modprobe br_netfilter

5. Open theย kubernetes.confย file to configure Kubernetes networking:

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.

Save the file and exit.

7. Reload the configuration by typing:

sudo sysctl --system
Reloading system configuration.

Step 2: Assign Unique Hostname for Each Server Node

1. Choose a master node. Then, enter the command on that node to name it accordingly:

sudo hostnamectl set-hostname master-node

2. Next, set the hostname on the first worker node by entering the following command:

sudo hostnamectl set-hostname worker01

Use this process on additional worker nodes to set a unique hostname on each.

3. Edit the hosts file on each node by adding the IP addresses and hostnames of the servers that will be part of the cluster.

Editing the hosts file.

4. Restart the terminal application to apply the hostname change.

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.

3. Reload the configuration and restart the kubelet:

sudo systemctl daemon-reload && sudo 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 and restart Docker:

sudo systemctl daemon-reload && sudo systemctl restart docker

7. Open the kubeadm configuration file:

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

8. Add the following line to the file:

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

Save the file and exit.

9. Reload the configuration and restart the kubelet:

sudo systemctl daemon-reload && sudo systemctl restart kubelet

10. Finally, initialize the cluster by typing:

sudo kubeadm init --control-plane-endpoint=master-node --upload-certs

Once the operation finishes, the output displays aย kubeadm joinย command at the bottom. Make a note of this command, as it will be used to join the worker nodes to the cluster.

Master node joins the cluster.

11. Create a directory for the Kubernetes cluster:

mkdir -p $HOME/.kube

12. Copy the configuration file to the directory:

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

13. Change the ownership of the directory to the current user and group using the chown command:

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

Step 4: Deploy Pod Network to Cluster

A pod network enables communication between nodes in a cluster. This tutorial uses Flannel, a node network manager, to create a pod network.

Apply the Flannel manager to the master node by executing the steps below:

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

Repeat the following steps on each worker node to create a cluster:

1. Stop and disable AppArmor:

sudo systemctl stop apparmor && sudo systemctl disable apparmor

2. Restart containerd:

sudo systemctl restart containerd.service

3. Apply the kubeadm join command from Step 3 on worker nodes to connect them to the master node. Prefix the command with sudo:

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

Replace [master-node-ip], [token], and [hash] with the values from the kubeadm join command output.

4. After a few minutes, switch to the master server and enter the following command to check the status of the nodes:

kubectl get nodes
Viewing deployed nodes.

The system displays the master node and the worker nodes in the cluster.

Conclusion

After following the steps presented in this article, you should haveย Kubernetes installed on Ubuntu. The article included instructions for installing the required packages and deploying Kubernetes across all your nodes.

If you are a beginner with no Kubernetes deployment experience,ย Minikubeย is a great place to start.

Was this article helpful?
YesNo