Skip to content
Published on

Installing Kubernetes on Ubuntu 22.04 (kubeadm)

Authors
  • Name
    Twitter

Overview

Let's learn how to install Kubernetes on Ubuntu 22.04. The server setup consists of 1 master node and 2 worker nodes. The Kubernetes installation process can be broadly divided into the following tasks:

  1. Container Runtime installation
  2. cri-dockerd installation
  3. Kubernetes installation with kubeadm

You can choose one of the following 4 Container Runtimes:

Learn more about Container Runtimes

Container Runtime (Docker) Install

Decided on Docker Engine among the Container Runtimes supported on the Kubernetes website.

Before installation, check if the port is open.

nc 127.0.0.1 6443

Uninstall Old Version

Use the following command to remove any previously installed Docker-related files.

sudo apt-get remove docker docker-engine docker.io containerd runc

Set up the repository

sudo apt-get update
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

install docker

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Verifying Docker Installation

sudo docker run hello-world

If the following message appears, the installation was successful.

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Installing cri-dockerd

To use Docker as the Kubernetes engine, the cri-dockerd adapter must be installed. Refer to the cri-dockerd documentation.

Enter su - to run the following commands with root privileges.

git clone https://github.com/Mirantis/cri-dockerd.git
# Run these commands as root
###Install GO###
wget https://storage.googleapis.com/golang/getgo/installer_linux
chmod +x ./installer_linux
./installer_linux
source ~/.bash_profile

cd cri-dockerd
mkdir bin
go build -o bin/cri-dockerd
mkdir -p /usr/local/bin
install -o root -g root -m 0755 bin/cri-dockerd /usr/local/bin/cri-dockerd
cp -a packaging/systemd/* /etc/systemd/system
sed -i -e 's,/usr/bin/cri-dockerd,/usr/local/bin/cri-dockerd,' /etc/systemd/system/cri-docker.service
systemctl daemon-reload
systemctl enable cri-docker.service
systemctl enable --now cri-docker.socket

If the following message appears, the installation was successful.

Created symlink /etc/systemd/system/multi-user.target.wants/cri-docker.service/etc/systemd/system/cri-docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/cri-docker.socket/etc/systemd/system/cri-docker.socket.

Install kubeadm with the following commands.

sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

After gaining root privileges with su -, run the following command on the master node.

kubeadm init --cri-socket unix:///var/run/cri-dockerd.sock --pod-network-cidr=10.244.0.0/16

If the following message appears during the init process, refer to this blog to restart Docker, then clean up with kubeadm reset --cri-socket unix:///var/run/cri-dockerd.sock and run kubeadm init --cri-socket unix:///var/run/cri-dockerd.sock again.

he HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error

kubelet cannot start if swap is enabled. Therefore, disable swap on both the master and worker nodes using sudo swapoff -a. To persist this setting across reboots, comment out the swap entry in /etc/fstab using sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab.

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

If the following message appears when running kubeadm init, the installation was successful.

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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

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.219.110:6443 --token 0yrk20.i10d95793j5d9z9a \
	--discovery-token-ca-cert-hash sha256:b1ff8a6681b2fa13029892xxxxxxxxxxxxxxxxx

Install the network add-on using the following commands. Among the various add-ons, flannel seems to be the most widely used, so I chose this one.

wget https://raw.githubusercontent.com/flannel-io/flannel/v0.20.2/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml

Proceed with joining on the worker nodes.

kubeadm join 192.168.219.110:6443 --token 0yrk20.i10d95793j5d9z9a \
    --discovery-token-ca-cert-hash sha256:b1ff8a6681b2fa13029892xxxxxxxxxxxxxxxxx \
    --cri-socket unix:///var/run/cri-dockerd.sock

When you enter kubectl get nodes on the master node, you can confirm that the worker node status is Ready as shown below.

$kubectl get nodes
NAME                              STATUS   ROLES           AGE   VERSION
master          Ready    control-plane   15m   v1.26.0
node1           Ready    <none>          32s   v1.26.0
node2           Ready    <none>          32s   v1.26.0

There was an issue where pods were not created and hung with the message kubernetes open /run/flannel/subnet.env: no such file or directory. I resolved it by referring to the solution. Create the /run/flannel/subnet.env file on all worker nodes with the following content.

/run/flannel/subnet.env
FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.0.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true

This concludes the post on installing Kubernetes on Ubuntu using kubeadm. Thank you.

If the kube-flannel pod is in CrashLoopBackOff state, refer to the solution and run kubectl patch node nodename -p '{"spec":{"podCIDR":"10.244.0.0/16"}}'.

kube-flannel   kube-flannel-ds-fg8lc             0/1     CrashLoopBackOff

Subsequent issues were resolved by referring to https://potato-yong.tistory.com/150.

Resolved by adding --kubelet-insecure-tls to the metrics-server deployment and adding hostNetwork: true to spec.template.spec.

root@cubi01:~# kubectl top node cubi01
NAME     CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
cubi01   234m         5%     8696Mi          55%
root@cubi01:~# kubectl top node cubi02
NAME     CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
cubi02   46m          1%     6730Mi          42%

reference