Create Kubernetes Cluster On Ubuntu 24.04: A Step-by-Step Guide

by Team 64 views
Create Kubernetes Cluster on Ubuntu 24.04: A Step-by-Step Guide

Hey guys! Ready to dive into the exciting world of Kubernetes on the latest Ubuntu 24.04? In this comprehensive guide, we’ll walk you through setting up your very own Kubernetes cluster, step by step. Whether you’re a seasoned DevOps engineer or just starting out, this article will provide you with the knowledge and confidence to get your cluster up and running smoothly. So, let's get started!

Prerequisites

Before we begin, let's make sure you have everything you need. Here’s a checklist:

  • Ubuntu 24.04 Servers: You’ll need at least two Ubuntu 24.04 servers. One will serve as the master node, and the other(s) will be worker nodes. I recommend having at least 2 worker nodes for redundancy.
  • Internet Connection: Ensure all your servers have a stable internet connection to download the necessary packages.
  • User with Sudo Privileges: You should have a user account with sudo privileges on all servers. This allows you to run commands with administrative rights.
  • Basic Linux Knowledge: Familiarity with basic Linux commands will be helpful.
  • Containerization Concepts: A basic understanding of containerization (e.g., Docker) will make the Kubernetes concepts easier to grasp.

Step 1: Update and Upgrade Your Servers

First things first, let’s update and upgrade our Ubuntu servers. This ensures that we have the latest packages and security updates. Log in to each of your servers and run the following commands:

sudo apt update
sudo apt upgrade -y

The apt update command refreshes the package lists, and apt upgrade -y upgrades all installed packages to their latest versions. The -y flag automatically answers "yes" to any prompts, making the process non-interactive.

Step 2: Install Container Runtime (Docker)

Kubernetes needs a container runtime to run your applications. Docker is a popular choice, and we’ll use it in this guide. To install Docker, follow these steps:

sudo apt install docker.io -y

This command installs the Docker runtime. Next, let’s start and enable Docker to ensure it runs on boot:

sudo systemctl start docker
sudo systemctl enable docker

To verify that Docker is running correctly, you can run:

sudo docker info

This command provides detailed information about your Docker installation. If everything is working, you should see output without any errors. Also, add your user to the docker group so you don't have to use sudo with docker commands:

sudo usermod -aG docker $USER
newgrp docker

Step 3: Install Kubernetes Components (kubeadm, kubelet, kubectl)

Now, let’s install the Kubernetes components: kubeadm, kubelet, and kubectl. These are essential for setting up and managing your Kubernetes cluster. First, we need to add the Kubernetes apt repository. Run these commands on all servers:

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

Keywords: Kubernetes components, kubeadm, kubelet, and kubectl installation are critical for setting up and managing the cluster. The first command downloads the Kubernetes package signing key, and the second adds the Kubernetes apt repository to your system. After adding the repository, update the package lists again:

sudo apt update

Now, install the Kubernetes components:

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

The apt install command installs kubelet, kubeadm, and kubectl. The apt-mark hold command prevents these packages from being accidentally updated, which could cause compatibility issues.

Step 4: Initialize the Kubernetes Master Node

It’s time to initialize the Kubernetes master node. Run this command only on the master node:

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

Keywords: Kubernetes master node initialization using kubeadm init is the starting point of creating the cluster. The --pod-network-cidr flag specifies the IP address range for pods. This example uses 10.244.0.0/16, which is compatible with the Calico network plugin (which we’ll install later). After the command completes, it will output a kubeadm join command. Save this command; you’ll need it to join the worker nodes to the cluster. Also, the output will provide instructions to configure kubectl for your user. Follow these instructions:

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

These commands configure kubectl to communicate with your Kubernetes cluster. Now, you can run kubectl get nodes to check the status of your nodes (you’ll only see the master node at this point).

Step 5: Install a Pod Network (Calico)

A pod network allows pods to communicate with each other. We’ll use Calico, a popular and powerful network plugin. To install Calico, run this command on the master node:

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

Keywords: Pod network installation using Calico enables communication between pods within the cluster. Calico is a popular choice due to its flexibility and advanced networking features. This command applies the Calico manifest, which sets up the Calico network plugin in your cluster. It may take a few minutes for Calico to be fully initialized. You can monitor the progress by running:

kubectl get pods -n kube-system

Wait until all Calico pods are in the Running state.

Step 6: Join Worker Nodes to the Cluster

Now, let’s join the worker nodes to the cluster. Remember the kubeadm join command that was outputted when you initialized the master node? Run that command on each of your worker nodes:

sudo kubeadm join <master-ip>:<master-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>

Keywords: Worker nodes joining the Kubernetes cluster is done using the kubeadm join command. Replace <master-ip>, <master-port>, <token>, and <hash> with the values from the kubeadm join command outputted during the master node initialization. This command configures the worker nodes to connect to the master node and join the cluster. After running this command on each worker node, go back to the master node and run:

kubectl get nodes

You should now see all your worker nodes listed, with a status of Ready. If a node shows NotReady, wait a few minutes and try again. It might take some time for the nodes to fully join the cluster.

Step 7: Deploy a Sample Application

Congratulations! Your Kubernetes cluster is now up and running. Let’s deploy a sample application to test it out. We’ll deploy a simple Nginx web server. Create a file named nginx-deployment.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: LoadBalancer

Keywords: Sample application deployment tests the functionality of the Kubernetes cluster. This YAML file defines a Deployment and a Service for an Nginx web server. Save this file and apply it to your cluster:

kubectl apply -f nginx-deployment.yaml

This command creates the Nginx deployment and service. To check the status of the deployment, run:

kubectl get deployments
kubectl get services

It may take a few minutes for the deployment to be fully up and running. Once the service has an external IP address (or a hostname if you’re using a cloud provider), you can access the Nginx web server in your browser.

Troubleshooting Tips

  • Nodes Not Joining: If worker nodes are not joining the cluster, double-check the kubeadm join command and ensure that the master node is accessible from the worker nodes.

  • Calico Not Initializing: If Calico pods are not in the Running state, check the Calico logs for errors:

    kubectl logs -n kube-system <calico-pod-name>
    
  • Network Issues: If you’re experiencing network issues, ensure that your firewall is not blocking traffic between the nodes.

  • DNS Resolution: Sometimes DNS resolution issues can prevent pods from communicating correctly. Check your CoreDNS pods in the kube-system namespace.

Conclusion

And there you have it! You’ve successfully created a Kubernetes cluster on Ubuntu 24.04. This guide has covered everything from setting up the prerequisites to deploying a sample application. Now that you have a working cluster, you can start exploring the vast capabilities of Kubernetes and deploy your own applications. Remember to consult the official Kubernetes documentation for more in-depth information and advanced configurations. Happy clustering!