How-To Guides

Mastering the Basics: How to Deploy Kubernetes on Ubuntu 22.04

Container orchestration is no longer optional for modern software development; it is the backbone of scalable, resilient, and portable infrastructure. While managed services like EKS, GKE, and AKS handle much of the heavy lifting, understanding how to build a Kubernetes cluster from scratch on bare-metal or virtual machines is an essential skill for any intermediate to advanced developer. This guide walks you through deploying a single-node Kubernetes cluster using kubeadm on Ubuntu 22.04 LTS. This approach provides a clean, production-like environment that you can later scale into a multi-node cluster.

Prerequisites and System Preparation

Before diving into the installation, ensure your environment meets the following criteria. You will need at least one server running Ubuntu 22.04, with a minimum of 2 CPU cores and 4GB of RAM. SSH access as a non-root user with sudo privileges is also required.

First, we must update the system repositories and install prerequisite packages. This ensures that kernel modules and networking tools are up to date.

sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl gpg

Kubernetes relies heavily on CNI (Container Network Interface) plugins for pod communication. While kubeadm can install networking later, we should prepare the system for IP forwarding and disable swap space, as Kubernetes does not support it. Disabling swap is a critical step that is often overlooked by beginners, leading to cluster initialization failures.

sudo swapon --show
sudo swapoff -a
sudo sed -i '/ swap / s/^/#' /etc/fstab

Installing Docker and Kubernetes Binaries

Kubernetes is a meta-system; it orchestrates containers but does not run them directly. Therefore, a container runtime like Docker (or containerd) must be installed first. We will use Docker CE, the community edition, for this demonstration.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io

Next, we add the Kubernetes repository. Note that you should check the current stable version of Kubernetes on the official website and update the version numbers in the following commands accordingly. As of this writing, we will assume version 1.29.x is the stable release.

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/kubernetes-apt-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/kubernetes-apt-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

The apt-mark hold command prevents automatic updates from breaking your Kubernetes installation, which is a common pitfall during OS upgrades.

Initializing the Control Plane

Now that the binaries are installed, we can initialize the cluster. This command sets up the API server, scheduler, controller manager, and etcd. It also configures a pod network using the Flannel plugin by default, though we will configure it explicitly for clarity.

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

If this command succeeds, you will see a series of commands to configure your kubectl client. This step is crucial because it sets up the authentication tokens required to communicate with the cluster.

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

However, the cluster is not yet fully operational. You will notice a warning stating that no CNI plugin has been installed. We need to apply a network overlay. Flannel is a simple, efficient choice for development and small production environments.

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

Verifying the Installation

To confirm that everything is working correctly, run the node status command. You should see one node in the Ready state.

kubectl get nodes

Expected output:

NAME   STATUS   ROLES           AGE   VERSION
node1  Ready    control-plane   2m    v1.29.0

You can further verify the installation by deploying a simple application. Let's create a basic Nginx deployment to test pod scheduling and networking.

kubectl create deployment nginx --image=nginx:latest
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get svc nginx

Once the service is created, note the NodePort value (usually between 30000 and 32767). You can now access the Nginx welcome page by navigating to http://<your-server-ip>:<node-port> in your browser. If you see the default Nginx page, your Kubernetes cluster is successfully handling traffic.

Cleaning Up and Next Steps

It is good practice to test your cleanup procedures. To remove the cluster, run the following command. This resets the Kubernetes state on the node, allowing you to re-initialize if necessary.

sudo kubeadm reset

While a single-node cluster is excellent for learning and development, production environments require high availability. The next logical step is to join worker nodes to your cluster using the kubeadm join command generated during initialization. Additionally, consider implementing a persistent storage solution like Local Path Provisioner or NFS to ensure data survives pod restarts. By mastering these fundamentals on Ubuntu, you lay a solid foundation for managing complex, distributed systems in the cloud.

Share: