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.

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:
-
Load the
overlayandbr_netfiltermodules required for networking.sudo modprobe overlay sudo modprobe br_netfilter cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf overlay br_netfilter EOF -
To allow
iptablesto see bridged traffic, as required by Kubernetes, we need to set the values of certain fields to1.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 -
Apply the new settings without restarting.
sudo sysctl --system -
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 -
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 EOFThe 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.ascOn Debian, run the same commands with
debianin place ofubuntuin the two Docker URLs. The other steps in this guide are the same on Debian. Debian does not installufwby default, so runsudo apt install -y ufwbefore 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
dnfwhere this guide usesapt, 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.repoStep 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/tcpfor each port (write a range with a hyphen, such as2379-2380/tcp). Allow 8472/udp from the other node's IP only, withsudo 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 runsudo firewall-cmd --reload. -
Update and then install the containerd package.
sudo apt update -y sudo apt install -y containerd.ioThe 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 --versioncontainerd containerd v2.4.2 b7a25261e029d68978bbf290df0a62fc819b3c51 -
Set up the default configuration file. Do not skip this step: the configuration file in the
containerd.iopackage turns off the CRI plugin that Kubernetes needs.sudo mkdir -p /etc/containerd sudo containerd config default | sudo tee /etc/containerd/config.tomlThe command prints the whole file, which starts with
version = 4. -
Next up, we need to modify the containerd configuration file and ensure that
SystemdCgroupis set totrue. 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.tomlTo check the change, print that section:
sed -n '/runtimes.runc.options\]/,/SystemdCgroup/p' /etc/containerd/config.tomlMake 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 = trueIf you edit the file by hand instead, look for this section name. Guides written for containerd 1.x name an
io.containerd.grpc.v1.crisection, which no longer holds this setting. -
Finally, to apply these changes, we need to restart containerd.
sudo systemctl restart containerdTo check that containerd is indeed running, use this command:
systemctl is-active containerdExpect this output:
active
Step 2. Install Kubernetes
With our container runtime installed and configured, we are ready to install Kubernetes.
-
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.listThe
v1.37in 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/ / -
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 kubectlkubeadmis the command that sets up the cluster,kubeletruns on every machine in the cluster and starts the pods, andkubectlis the command-line tool you use to manage the cluster. The hold stopsapt upgradefrom 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. -
Set the appropriate hostname for each machine.
sudo hostnamectl set-hostname "master-node" exec bashAnd on the worker node:
sudo hostnamectl set-hostname "node1" exec bash -
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 udpThe 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.
-
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 udpRemember to replace the IPs with those of your systems. In this guide,
10.0.0.10is the master node and10.0.0.11is 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.
-
Turn the firewall on, on both nodes, and answer
yat 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 enableExpect this output:
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y Firewall is active and enabled on system startupTo 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 ... -
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/fstabTo check, run
grep swap /etc/fstab. The swap line should now start with#:#/swap.img none swap sw 0 0 -
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.
-
Run the following command on the master node to allow Kubernetes to fetch the required images before cluster initialization:
sudo kubeadm config images pullExpect 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 -
Initialize the cluster.
sudo kubeadm init --pod-network-cidr=10.244.0.0/16The 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 -
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/configIf you work as root, you can run
export KUBECONFIG=/etc/kubernetes/admin.confinstead. This works for root only, because only root can read that file.To check that
kubectlcan reach the cluster:kubectl get nodesNAME STATUS ROLES AGE VERSION master-node NotReady control-plane 18s v1.37.2 -
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
NotReadyand CoreDNS does not start.kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.ymlExpect 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 createdFlannel uses the
10.244.0.0/16pod network by default, which matches the--pod-network-cidrin item 2, so the manifest needs no edit. -
Use the
get nodescommand to verify that our master node is ready. It can take a minute after the previous step.kubectl get nodesExpect the following output:
NAME STATUS ROLES AGE VERSION master-node Ready control-plane 69s v1.37.2 -
Also check whether all the default pods are running:
kubectl get pods --all-namespacesYou 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 -
We are now ready to move to the worker node. Execute the kubeadm join from item 2 with
sudoon 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. -
If you go back to the master node, you should now be able to see the worker node in the output of the
get nodescommand.kubectl get nodesStraight after the join, the worker node shows as
NotReadywhile flannel starts on it:NAME STATUS ROLES AGE VERSION master-node Ready control-plane 96s v1.37.2 node1 NotReady <none> 7s v1.37.2After 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 -
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=workerTo verify that the role was set:
kubectl get nodesThe 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.
-
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 -
Check where the pods run.
kubectl get pods -o wideNAME 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. -
Find the NodePort Kubernetes picked for the service.
kubectl get service nginxNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx NodePort 10.97.179.61 <none> 80:32561/TCP 15sThe NodePort is the number after
80:, here32561. -
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.
-
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.
