Kubernetes Cluster Setup On Ubuntu 24.04: A Detailed Guide
Hey everyone! Today, we're diving deep into setting up a Kubernetes cluster on Ubuntu 24.04. Whether you're a seasoned DevOps engineer or just starting your journey with container orchestration, this guide will walk you through each step to get your cluster up and running smoothly. Let’s get started!
Prerequisites
Before we jump into the installation process, let’s make sure we have everything we need. Having the right prerequisites in place will save us headaches down the road. Here’s what you should have:
- Ubuntu 24.04 Servers: You’ll need at least two Ubuntu 24.04 servers. One will act as the master node, and the others will be worker nodes. Ensure these servers have static IP addresses and can communicate with each other.
 - User with sudo privileges: Make sure you have a user account with 
sudoprivileges on all the servers. This will allow you to run administrative commands. - Internet Connection: All servers should have a stable internet connection to download the necessary packages.
 - Basic Linux Knowledge: A basic understanding of Linux commands will be helpful.
 
Having these prerequisites sorted out ensures that the installation process goes as smoothly as possible. Now, let's move on to the first real step: installing containerd.
Step 1: Installing Containerd
Containerd is a container runtime that manages the complete container lifecycle of its host system. It’s a core component for running Kubernetes, so we need to get it installed on all our nodes. Here’s how you do it:
- 
Update Package Index:
First, update the package index on all your servers to make sure you have the latest package information.
sudo apt update - 
Install Dependencies:
Next, install the necessary dependencies for containerd.
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common - 
Add Docker’s GPG Key:
Add Docker’s GPG key to ensure the packages we download are authentic.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg - 
Add Docker Repository:
Add the Docker repository to your APT sources.
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 - 
Update Package Index Again:
Update the package index again to include the new Docker repository.
sudo apt update - 
Install Containerd:
Finally, install containerd.
sudo apt install -y containerd.io - 
Configure Containerd:
Generate the default containerd configuration file.
sudo mkdir -p /etc/containerd containerd config default | sudo tee /etc/containerd/config.toml - 
Edit Containerd Configuration:
Edit the
/etc/containerd/config.tomlfile to usesystemdas the cgroup driver. Open the file with your favorite text editor (likenanoorvim).sudo nano /etc/containerd/config.tomlFind the line that says
SystemdCgroup = falseand change it toSystemdCgroup = true.[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc] [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] SystemdCgroup = true - 
Restart Containerd:
Restart containerd to apply the changes.
sudo systemctl restart containerd - 
Enable Containerd:
Enable containerd to start on boot.
sudo systemctl enable containerd 
With containerd successfully installed and configured, we’re one step closer to having our Kubernetes cluster up and running. Next, we'll install kubeadm, kubelet, and kubectl.
Step 2: Installing Kubeadm, Kubelet, and Kubectl
These three tools are essential for setting up and managing a Kubernetes cluster. Kubeadm is used to bootstrap the cluster, kubelet is the agent that runs on each node, and kubectl is the command-line tool to interact with the cluster. Let's get them installed:
- 
Add Kubernetes Repository:
Add the Kubernetes repository to your APT sources.
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list - 
Update Package Index:
Update the package index to include the new Kubernetes repository.
sudo apt update - 
Install Kubeadm, Kubelet, and Kubectl:
Install kubeadm, kubelet, and kubectl, making sure to hold their versions to prevent automatic updates that could cause compatibility issues.
sudo apt install -y kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl - 
Verify Installation:
Verify that the installation was successful by checking the versions of the installed tools.
kubeadm version kubelet --version kubectl version --client 
Now that we have kubeadm, kubelet, and kubectl installed, we can move on to initializing the Kubernetes cluster.
Step 3: Initializing the Kubernetes Cluster
Initializing the Kubernetes cluster is a crucial step. This is where we use kubeadm to set up the master node. Here’s how to do it:
- 
Initialize the Kubernetes Master Node:
Run the following command to initialize the Kubernetes master node. Replace
--apiserver-advertise-addresswith the IP address of your master node, and--pod-network-cidrwith the network range you plan to use for your pods (e.g.,10.244.0.0/16for Flannel).sudo kubeadm init --apiserver-advertise-address=<MASTER_NODE_IP> --pod-network-cidr=10.244.0.0/16Note: Save the
kubeadm joincommand that is outputted at the end of this process. You will need it to join the worker nodes to the cluster. - 
Configure Kubectl:
After the initialization is complete, you need to configure
kubectlto connect to the cluster. Follow the instructions provided in the output ofkubeadm init.mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config - 
Apply a Network Plugin:
Kubernetes requires a network plugin to enable communication between pods. We'll use Flannel in this example. Apply the Flannel manifest.
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml - 
Verify Node Status:
Check the status of your nodes. It might take a few minutes for the nodes to become ready.
kubectl get nodes 
With the Kubernetes master node initialized and kubectl configured, we’re ready to join the worker nodes to the cluster.
Step 4: Joining Worker Nodes to the Cluster
Now that the master node is set up, we need to add the worker nodes to the cluster. This is where the kubeadm join command we saved earlier comes into play. Follow these steps on each of your worker nodes:
- 
Run the Kubeadm Join Command:
Execute the
kubeadm joincommand that you saved from the master node initialization.sudo kubeadm join <MASTER_NODE_IP>:<PORT> --token <TOKEN> --discovery-token-ca-cert-hash sha256:<HASH>Replace
<MASTER_NODE_IP>,<PORT>,<TOKEN>, and<HASH>with the values from thekubeadm joincommand output. - 
Verify Node Status on Master Node:
Back on the master node, verify that the worker nodes have joined the cluster.
kubectl get nodesYou should see all your worker nodes listed with a status of
Ready. 
Step 5: Deploying a Sample Application
Now that we have our Kubernetes cluster up and running, let’s deploy a simple application to test it out. We’ll deploy a basic Nginx deployment.
- 
Create a Deployment:
Create an Nginx deployment using
kubectl. Copy and paste the following YAML configuration into a file namednginx-deployment.yaml.apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 - 
Apply the Deployment:
Apply the deployment using
kubectl.kubectl apply -f nginx-deployment.yaml - 
Check Deployment Status:
Check the status of the deployment.
kubectl get deployments - 
Create a Service:
Create a service to expose the Nginx deployment. Copy and paste the following YAML configuration into a file named
nginx-service.yaml.apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer - 
Apply the Service:
Apply the service using
kubectl.kubectl apply -f nginx-service.yaml - 
Check Service Status:
Check the status of the service. It might take a few minutes for the external IP to be assigned.
kubectl get services - 
Access the Application:
Once the external IP is assigned, access the Nginx application in your web browser using the external IP.
http://<EXTERNAL_IP> 
Congratulations! You’ve successfully deployed a sample application on your Kubernetes cluster.
Conclusion
And there you have it! You’ve successfully set up a Kubernetes cluster on Ubuntu 24.04. This guide walked you through installing containerd, kubeadm, kubelet, and kubectl, initializing the cluster, joining worker nodes, and deploying a sample application. With this foundation, you can now explore more advanced Kubernetes features and start deploying your own applications. Happy clustering, guys!