Orchestrating containerized applications has become a cornerstone of modern software delivery. While Docker simplifies the packaging of applications, Kubernetes (often abbreviated as K8s) provides the robust infrastructure needed to manage, scale, and orchestrate those containers across a cluster of machines. For system administrators and developers who prefer a stable, Debian-based environment, Ubuntu remains the premier choice for hosting Kubernetes clusters. This guide details the technical process of deploying a high-availability Kubernetes control plane and worker nodes on Ubuntu 20.04 LTS or 22.04 LTS.
Prerequisites and System Preparation
Before diving into the installation, ensure you have at least two Ubuntu servers (one master/control-plane node and one or more worker nodes) with SSH access. It is highly recommended to disable the firewall tools to avoid interference during the setup, or configure specific ports for Kubernetes traffic. The following steps must be performed on all nodes in your cluster.
First, disable Swap space, as Kubernetes requires contiguous memory allocation and cannot function with swap enabled:
sudo swapoff -a
# To make this permanent, comment out the swap line in /etc/fstab
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
Next, load the necessary kernel modules. Kubernetes relies on overlay for storage and br_netfilter for networking.
cat <
To ensure networking works correctly, set the sysctl parameters so that bridges see traffic and IP forwarding is enabled:
cat <
Installing the Container Runtime and Kubelet
Kubernetes does not manage the container runtime itself but relies on external runtimes like containerd, CRI-O, or Docker. We will use containerd as it is the current industry standard recommended by the Kubernetes SIG-Node team.
Begin by installing the dependencies and adding the Kubernetes APT repository. This ensures you are pulling packages directly from the official K8s channel rather than the default Ubuntu repositories, which may host older versions.
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg
# Add the Kubernetes GPG key
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
# Add the repository
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
# Install the packages
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
Initializing the Cluster
Now that all components are installed, log into your primary master node. Use kubeadm init to bootstrap the cluster. If your cluster is behind a proxy or uses a specific pod CIDR, you may need to adjust the arguments.
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
Once the initialization completes successfully, you will see an output containing a kubeadm join command. Copy this token; you will need it to add worker nodes to the cluster. To start using the cluster as a regular user, run the following commands:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Deploying a Pod Network
Kubernetes requires a container network interface (CNI) plugin to allow pods to communicate with each other across nodes. We will deploy Flannel, a simple and popular overlay network.
kubectl apply -f https://github.com/coreos/flannel/raw/master/Documentation/kube-flannel.yml
Verify that all nodes are in the Ready state using:
kubectl get nodes
Conclusion
Deploying Kubernetes on Ubuntu is a straightforward process when following the official guidelines and maintaining a systematic approach to system configuration. By disabling swap, loading the correct kernel modules, and utilizing the official Kubernetes APT repository, you establish a solid foundation for production-grade workloads. Remember to secure your cluster by managing tokens, configuring RBAC, and regularly updating your node software. With your cluster up and running, you are now ready to deploy your first microservices architecture.