Deploy a Kubernetes Cluster on Ubuntu 26.04 with containerd

Set up a two-node Kubernetes cluster on Ubuntu 26.04 with containerd and kubeadm, with notes for Debian and CentOS Stream.

When building cloud infrastructure, it's crucial to keep scalability, automation, and availability top of mind.

In the past, it was really difficult to uphold high standards of scalability, automation, and availability in the cloud. Only the top IT companies could do it. But today, courtesy of Kubernetes, anyone can build a self-sufficient, auto-healing, and auto-scaling cloud infrastructure.

All it takes is a few YAML files.

In this article, we'll go through all the steps you need to set up a Kubernetes cluster on two Ubuntu 26.04 LTS machines. The same steps work on Ubuntu 24.04 LTS, because the commands read the release codename from the server itself.

This guide uses Kubernetes 1.37. For a newer release, change the version number in the repository commands in Step 2.

What is Kubernetes?

Kubernetes is open-source software that allows you to run application pods inside a cluster of master and worker nodes. A cluster must at least have 1 master and 1 worker node. Production clusters usually run several of each. A pod, the smallest unit Kubernetes deploys, is a group of one or more containers that run on the same node and share network and storage resources.

Kubernetes cluster diagram: one master node linked to three worker nodes, each running kubelet and containerd with pods that hold containers

The master node is responsible for managing the cluster and ensuring that the desired state (defined in the YAML configuration files) is always maintained.

If a pod fails, the control plane on the master node restarts or replaces it, so the cluster keeps the state you declared. Scaling the number of pods up and down with traffic needs an autoscaler, such as the Horizontal Pod Autoscaler, which this guide does not set up.

The master node is accompanied by worker node(s). These run the application containers. Each Kubernetes node has the following components:

  • Kube-proxy: A network proxy that allows pods to communicate.
  • Kubelet: It's responsible for starting the pods, and maintaining their state and lifetime.
  • Container runtime: A package that creates containers, and allows them to interact with the operating system. Docker used to be the primary container runtime until the Kubernetes team deprecated it in v1.20.

As of v1.24, support for Docker has been removed from the Kubernetes source code. The recommended alternatives are containerd and CRI-O. Container images built with Docker still run on both, because they follow the same OCI image standard.

However, you can still set Docker up using cri-dockerd, open-source software that lets you integrate Docker with the Container Runtime Interface (CRI).

In this article, we will be using containerd as our runtime. Let's begin!


Step 1. Install containerd

To install containerd, follow these steps on both VMs:

  1. Load the overlay and br_netfilter modules required for networking.

    sudo modprobe overlay
    sudo modprobe br_netfilter
    cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
    overlay
    br_netfilter
    EOF
  2. To allow iptables to see bridged traffic, as required by Kubernetes, we need to set the values of certain fields to 1.

    sudo tee /etc/sysctl.d/kubernetes.conf<<EOF
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    net.ipv4.ip_forward = 1
    EOF
  3. Apply the new settings without restarting.

    sudo sysctl --system
  4. Install curl and the other tools the repository steps use. Most Ubuntu images already have them, in which case apt only updates them.

    sudo apt update
    sudo apt install -y ca-certificates curl gpg
  5. Add Docker's signing key and the repository from which we will install containerd.

    sudo install -m 0755 -d /etc/apt/keyrings
    sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
    sudo chmod a+r /etc/apt/keyrings/docker.asc
    sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
    Types: deb
    URIs: https://download.docker.com/linux/ubuntu
    Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
    Components: stable
    Architectures: $(dpkg --print-architecture)
    Signed-By: /etc/apt/keyrings/docker.asc
    EOF

    The command prints the new file, with your release codename and architecture filled in:

    Types: deb
    URIs: https://download.docker.com/linux/ubuntu
    Suites: resolute
    Components: stable
    Architectures: amd64
    Signed-By: /etc/apt/keyrings/docker.asc

    On Debian, run the same commands with debian in place of ubuntu in the two Docker URLs. The other steps in this guide are the same on Debian. Debian does not install ufw by default, so run sudo apt install -y ufw before you set up the firewall in Step 2.

    CentOS 7 reached end of life in June 2024, so use CentOS Stream or another supported release. On CentOS Stream, use dnf where this guide uses apt, and add Docker's CentOS repository in place of the Ubuntu one:

    sudo dnf -y install dnf-plugins-core
    sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

    Step 2 also changes in two places on CentOS Stream. In place of items 1 and 2, add the Kubernetes repository and install kubelet, kubeadm and kubectl the way the Red Hat-based distributions tab of the Kubernetes install guide shows, including its step that sets SELinux to permissive mode. In place of the ufw steps in items 4 to 6, open the same ports in firewalld, which CentOS Stream uses by default and which already allows SSH. Run sudo firewall-cmd --permanent --add-port=6443/tcp for each port (write a range with a hyphen, such as 2379-2380/tcp). Allow 8472/udp from the other node's IP only, with sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.11" port port="8472" protocol="udp" accept' on the master node and the master's IP on the worker. Then run sudo firewall-cmd --reload.

  6. Update and then install the containerd package.

    sudo apt update -y
    sudo apt install -y containerd.io

    The update should read the Docker repository for your release:

    Get:1 https://download.docker.com/linux/ubuntu resolute InRelease [32.5 kB]
    Get:2 https://download.docker.com/linux/ubuntu resolute/stable amd64 Packages [34.6 kB]
    ...
    Fetched 67.1 kB in 1s (92.6 kB/s)

    Docker's repository ships containerd 2.x. To check the version you installed:

    containerd --version
    containerd containerd v2.4.2 b7a25261e029d68978bbf290df0a62fc819b3c51
  7. Set up the default configuration file. Do not skip this step: the configuration file in the containerd.io package turns off the CRI plugin that Kubernetes needs.

    sudo mkdir -p /etc/containerd
    sudo containerd config default | sudo tee /etc/containerd/config.toml

    The command prints the whole file, which starts with version = 4.

  8. Next up, we need to modify the containerd configuration file and ensure that SystemdCgroup is set to true. In containerd 2.x, the setting sits in the [plugins.'io.containerd.cri.v1.runtime'.containerd.runtimes.runc.options] section, and it appears only once in the file, so one command changes it:

    sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml

    To check the change, print that section:

    sed -n '/runtimes.runc.options\]/,/SystemdCgroup/p' /etc/containerd/config.toml

    Make sure the contents of your section match the following:

              [plugins.'io.containerd.cri.v1.runtime'.containerd.runtimes.runc.options]
                BinaryName = ''
                CriuImagePath = ''
                CriuWorkPath = ''
                IoGid = 0
                IoUid = 0
                NoNewKeyring = false
                Root = ''
                ShimCgroup = ''
                SystemdCgroup = true

    If you edit the file by hand instead, look for this section name. Guides written for containerd 1.x name an io.containerd.grpc.v1.cri section, which no longer holds this setting.

  9. Finally, to apply these changes, we need to restart containerd.

    sudo systemctl restart containerd

    To check that containerd is indeed running, use this command:

    systemctl is-active containerd

    Expect this output:

    active

Step 2. Install Kubernetes

With our container runtime installed and configured, we are ready to install Kubernetes.

  1. Add the repository key and the repository.

    curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.37/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
    echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.37/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

    The v1.37 in both lines is the Kubernetes version. For a newer release, change it in both. The second command prints the repository line:

    deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.37/deb/ /
  2. Update your system, install the 3 Kubernetes packages, and hold them at this version.

    sudo apt update -y
    sudo apt install -y kubelet kubeadm kubectl
    sudo apt-mark hold kubelet kubeadm kubectl

    kubeadm is the command that sets up the cluster, kubelet runs on every machine in the cluster and starts the pods, and kubectl is the command-line tool you use to manage the cluster. The hold stops apt upgrade from moving them to a new version, so you choose when to upgrade the cluster.

    The end of the output should look like this:

    Setting up kubeadm (1.37.2-1.1) ...
    Setting up kubectl (1.37.2-1.1) ...
    Setting up kubernetes-cni (1.9.1-1.1) ...
    Setting up kubelet (1.37.2-1.1) ...
    kubelet set on hold.
    kubeadm set on hold.
    kubectl set on hold.
  3. Set the appropriate hostname for each machine.

    sudo hostnamectl set-hostname "master-node"
    exec bash

    And on the worker node:

    sudo hostnamectl set-hostname "node1"
    exec bash
  4. Set up the firewall on the master node. Allow SSH first, then the ports the control plane uses:

    sudo ufw allow OpenSSH
    sudo ufw allow 6443/tcp
    sudo ufw allow 2379:2380/tcp
    sudo ufw allow 10250/tcp
    sudo ufw allow 10257/tcp
    sudo ufw allow 10259/tcp
    sudo ufw allow from 10.0.0.11 to any port 8472 proto udp

    The last rule lets flannel, the pod network we set up in Step 3, carry pod traffic between the nodes on 8472/udp. It allows the worker node's IP only, because this traffic is not authenticated.

  5. And these rules on the worker node:

    sudo ufw allow OpenSSH
    sudo ufw allow 10250/tcp
    sudo ufw allow 10256/tcp
    sudo ufw allow 30000:32767/tcp
    sudo ufw allow 30000:32767/udp
    sudo ufw allow from 10.0.0.10 to any port 8472 proto udp

    Remember to replace the IPs with those of your systems. In this guide, 10.0.0.10 is the master node and 10.0.0.11 is the worker node. With more worker nodes, every node needs an 8472/udp rule for each of the other nodes' IPs.

    This guide runs the cluster over the servers' public IPs. To keep traffic between the nodes off the public internet, you can put them on a private network: HostAfrica offers a VLAN add-on for Cloud Servers. Running the cluster over a private network also means telling kubeadm and flannel which IP and interface to use; the kubeadm documentation covers this.

  6. Turn the firewall on, on both nodes, and answer y at the prompt. Check that the SSH rule from items 4 and 5 is in place first: with the firewall on and no SSH rule, you lose your connection to the server.

    sudo ufw enable

    Expect this output:

    Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
    Firewall is active and enabled on system startup

    To list the rules, run sudo ufw status. On the master node, the output starts like this:

    Status: active
    
    To                         Action      From
    --                         ------      ----
    OpenSSH                    ALLOW       Anywhere
    6443/tcp                   ALLOW       Anywhere
    2379:2380/tcp              ALLOW       Anywhere
    10250/tcp                  ALLOW       Anywhere
    10257/tcp                  ALLOW       Anywhere
    10259/tcp                  ALLOW       Anywhere
    8472/udp                   ALLOW       10.0.0.11
    ...
  7. To allow kubelet to work properly, we need to disable swap on both machines. The first command turns swap off now, and the second comments out the swap line in /etc/fstab, so swap stays off after a reboot.

    sudo swapoff -a
    sudo sed -i '/[[:space:]]swap[[:space:]]/ s/^/#/' /etc/fstab

    To check, run grep swap /etc/fstab. The swap line should now start with #:

    #/swap.img	none	swap	sw	0	0
  8. Finally, enable the kubelet service on both systems so we can start it.

    sudo systemctl enable kubelet

Step 3. Setting up the cluster

With our container runtime and Kubernetes packages installed, we are ready to initialize our Kubernetes cluster.

  1. Run the following command on the master node to allow Kubernetes to fetch the required images before cluster initialization:

    sudo kubeadm config images pull

    Expect an output similar to the following:

    [config/images] Pulled registry.k8s.io/kube-apiserver:v1.37.2
    [config/images] Pulled registry.k8s.io/kube-controller-manager:v1.37.2
    [config/images] Pulled registry.k8s.io/kube-scheduler:v1.37.2
    [config/images] Pulled registry.k8s.io/kube-proxy:v1.37.2
    [config/images] Pulled registry.k8s.io/coredns/coredns:v1.14.6
    [config/images] Pulled registry.k8s.io/pause:3.10.2
    [config/images] Pulled registry.k8s.io/etcd:3.7.0-0
  2. Initialize the cluster.

    sudo kubeadm init --pod-network-cidr=10.244.0.0/16

    The initialization may take a few moments to finish. Expect an output similar to the following:

    [init] Using Kubernetes version: v1.37.2
    [preflight] Running pre-flight checks
    ...
    Your Kubernetes control-plane has initialized successfully!
    ...
    Then you can join any number of worker nodes by running the following on each as root:
    
    kubeadm join 10.0.0.10:6443 --token <token> \
            --discovery-token-ca-cert-hash sha256:<hash>

    You will see a kubeadm join at the end of the output, with your master node's IP and your own token and hash. Copy and save it in some file. We will have to run this command on the worker node to allow it to join the cluster. But fear not, if you forget to save it, or misplace it, you can also regenerate it using this command:

    sudo kubeadm token create --print-join-command
  3. Now create a folder to house the Kubernetes configuration in the home directory, and set up the required permissions for it.

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

    If you work as root, you can run export KUBECONFIG=/etc/kubernetes/admin.conf instead. This works for root only, because only root can read that file.

    To check that kubectl can reach the cluster:

    kubectl get nodes
    NAME          STATUS     ROLES           AGE   VERSION
    master-node   NotReady   control-plane   18s   v1.37.2
  4. Deploy a pod network to our cluster. This is required to interconnect the different Kubernetes components. Until a pod network is in place, the master node shows as NotReady and CoreDNS does not start.

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

    Expect an output like this:

    namespace/kube-flannel created
    serviceaccount/flannel created
    clusterrole.rbac.authorization.k8s.io/flannel created
    clusterrolebinding.rbac.authorization.k8s.io/flannel created
    configmap/kube-flannel-cfg created
    daemonset.apps/kube-flannel-ds created

    Flannel uses the 10.244.0.0/16 pod network by default, which matches the --pod-network-cidr in item 2, so the manifest needs no edit.

  5. Use the get nodes command to verify that our master node is ready. It can take a minute after the previous step.

    kubectl get nodes

    Expect the following output:

    NAME          STATUS   ROLES           AGE   VERSION
    master-node   Ready    control-plane   69s   v1.37.2
  6. Also check whether all the default pods are running:

    kubectl get pods --all-namespaces

    You should get an output like this:

    NAMESPACE      NAME                                  READY   STATUS    RESTARTS   AGE
    kube-flannel   kube-flannel-ds-ff67t                 1/1     Running   0          43s
    kube-system    coredns-559f6c778d-bkqgl              1/1     Running   0          60s
    kube-system    coredns-559f6c778d-fzknx              1/1     Running   0          60s
    kube-system    etcd-master-node                      1/1     Running   0          65s
    kube-system    kube-apiserver-master-node            1/1     Running   0          65s
    kube-system    kube-controller-manager-master-node   1/1     Running   0          66s
    kube-system    kube-proxy-bf875                      1/1     Running   0          60s
    kube-system    kube-scheduler-master-node            1/1     Running   0          65s
  7. We are now ready to move to the worker node. Execute the kubeadm join from item 2 with sudo on the worker node:

    sudo kubeadm join 10.0.0.10:6443 --token <token> \
            --discovery-token-ca-cert-hash sha256:<hash>

    You should see an output similar to the following:

    [preflight] Running pre-flight checks
    ...
    This node has joined the cluster:
    * Certificate signing request was sent to apiserver and a response was received.
    * The Kubelet was informed of the new secure connection details.
    
    Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
  8. If you go back to the master node, you should now be able to see the worker node in the output of the get nodes command.

    kubectl get nodes

    Straight after the join, the worker node shows as NotReady while flannel starts on it:

    NAME          STATUS     ROLES           AGE   VERSION
    master-node   Ready      control-plane   96s   v1.37.2
    node1         NotReady   <none>          7s    v1.37.2

    After about 20 seconds, the output should look as follows:

    NAME          STATUS   ROLES           AGE    VERSION
    master-node   Ready    control-plane   115s   v1.37.2
    node1         Ready    <none>          26s    v1.37.2
  9. Finally, to set the proper role for the worker node, run this command on the master node:

    kubectl label node node1 node-role.kubernetes.io/worker=worker

    To verify that the role was set:

    kubectl get nodes

    The output should show the worker node's role as follows:

    NAME          STATUS   ROLES           AGE    VERSION
    master-node   Ready    control-plane   117s   v1.37.2
    node1         Ready    worker          28s    v1.37.2

To add more worker nodes, run Steps 1 and 2 on each new machine, then repeat items 7 to 9 of Step 3.

To set up many nodes, or to build the same cluster again, you can automate these steps with Ansible, which runs them on every machine from one control host over SSH. Kubespray, a Kubernetes subproject, is a ready-made set of Ansible playbooks that deploys a cluster on Ubuntu, Debian or CentOS Stream, with containerd as its default runtime, so you do not have to write and maintain your own.


Step 4. Test the cluster (optional)

To see the cluster run a workload, deploy a small web server and reach it from your own computer. Run these commands on the master node.

  1. Create an nginx deployment with two pods, and expose it on a NodePort, a port in the 30000 to 32767 range that every node listens on.

    kubectl create deployment nginx --image=nginx --replicas=2
    kubectl expose deployment nginx --type=NodePort --port=80
  2. Check where the pods run.

    kubectl get pods -o wide
    NAME                     READY   STATUS    RESTARTS   AGE   IP           NODE          NOMINATED NODE   READINESS GATES
    nginx-69b9cdbbdd-25l2p   1/1     Running   0          15s   10.244.1.3   node1         <none>           <none>
    nginx-69b9cdbbdd-dhtjz   1/1     Running   0          15s   10.244.1.2   node1         <none>           <none>

    Both pods run on node1. kubeadm puts a taint on the master node (node-role.kubernetes.io/control-plane:NoSchedule) that keeps ordinary pods off it, so your workloads run on the worker nodes.

  3. Find the NodePort Kubernetes picked for the service.

    kubectl get service nginx
    NAME    TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
    nginx   NodePort   10.97.179.61   <none>        80:32561/TCP   15s

    The NodePort is the number after 80:, here 32561.

  4. From your own computer, request the page from the worker node's IP and the NodePort, for example curl http://10.0.0.11:32561:

    curl http://<worker-ip>:<nodeport>

    The reply is the nginx welcome page, which includes this line:

    <title>Welcome to nginx!</title>

    The firewall does not filter NodePort traffic, because kube-proxy forwards it before ufw's input rules see it. A NodePort service is reachable on every node's public IP, the master node's included.

  5. Delete the test service and deployment when you are done.

    kubectl delete service nginx
    kubectl delete deployment nginx

That's it! We have successfully set up a two-node Kubernetes cluster!

As a next step, you can add Windows Server worker nodes to the cluster.

Avatar for Lindy dB.

The Author

Lindy dB.

Lindy brings over 7 years of experience in SEO, web development, UI/UX design, and marketing to the table, with a special focus on the hosting industry. When not geeking out over the latest digital trends, you can find Lindy buried in a good book or exploring worlds in video games.