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

by Team 63 views
Kubernetes Cluster Setup on Ubuntu 24.04: A Step-by-Step Guide

Hey guys! Today, we're diving into setting up a Kubernetes cluster on Ubuntu 24.04. This guide will walk you through each step, ensuring you have a fully functional and robust cluster. Let's get started!

Prerequisites

Before we jump into the setup, let's make sure you have everything you need:

  • 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. For a production environment, consider having multiple master nodes for high availability.
  • Internet Access: All servers should have internet access to download the necessary packages.
  • User with Sudo Privileges: Ensure you have a user account with sudo privileges on all servers.
  • Basic Linux Knowledge: Familiarity with basic Linux commands is helpful.

Step 1: Update and Upgrade Your Servers

First, let's update and upgrade the package lists on all your servers. This ensures you have the latest versions of all installed software. Open your terminal and run the following commands:

sudo apt update
sudo apt upgrade -y

This command updates the package lists and upgrades the installed packages to their newest versions. The -y flag automatically answers "yes" to any prompts, making the process smoother. Keeping your system up-to-date is crucial for security and stability.

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 for this guide. To install Docker, follow these steps:

sudo apt install docker.io -y

Once Docker is installed, start and enable the Docker service:

sudo systemctl start docker
sudo systemctl enable docker

Verify that Docker is running correctly by checking its status:

sudo systemctl status docker

You should see output indicating that the Docker service is active and running. Docker is the engine that will run your containers, so ensuring it's set up correctly is vital. Next, you might want to configure Docker to use a specific storage driver or set up Docker Compose, but for basic Kubernetes functionality, this is sufficient.

Step 3: Install kubeadm, kubelet, and kubectl

Now, let's install the Kubernetes tools: kubeadm, kubelet, and kubectl. These tools are essential for managing your Kubernetes cluster. Run the following commands on all servers:

First, add the Kubernetes package repository:

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

Next, update the package lists again:

sudo apt update

Finally, install kubeadm, kubelet, and kubectl:

sudo apt install -y kubeadm kubelet kubectl

After installing these tools, hold their versions to prevent accidental upgrades:

sudo apt-mark hold kubeadm kubelet kubectl

kubeadm is the command-line tool for bootstrapping Kubernetes clusters. kubelet is the agent that runs on each node and communicates with the master node. kubectl is the command-line tool for interacting with the Kubernetes API. These tools are the foundation of your Kubernetes cluster, so make sure they are installed correctly and their versions are held.

Step 4: Initialize the Kubernetes Cluster (Master Node)

On your designated master node, initialize the Kubernetes cluster using kubeadm. Choose a pod network CIDR and specify it during initialization. A common choice is 10.244.0.0/16. Run the following command:

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

This command will generate a kubeadm join command that you'll need to run on the worker nodes to join them to the cluster. Make sure to copy this command as you'll need it later. The output will also provide instructions on how to configure kubectl to interact with the cluster. Follow those instructions:

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

Now, you should be able to use kubectl to interact with your cluster. For example, you can check the nodes:

kubectl get nodes

You'll see that the master node is in a NotReady state. This is because we haven't installed a pod network yet.

Step 5: Install a Pod Network (CNI)

A pod network, or Container Network Interface (CNI), allows pods to communicate with each other. We'll use Calico as our CNI. Install Calico by applying its manifest:

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

This command downloads the Calico manifest and applies it to your cluster. Calico will automatically configure the network for your pods. A CNI is essential for pod-to-pod communication, so choosing and installing one is a crucial step. After applying the manifest, wait a few minutes for the Calico pods to start. You can check their status using:

kubectl get pods -n kube-system

Once the Calico pods are running, check the status of your nodes again:

kubectl get nodes

Now, the master node should be in a Ready state.

Step 6: Join Worker Nodes to the Cluster

On each worker node, run the kubeadm join command that was generated during the kubeadm init step on the master node. The command should look something like this:

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

Replace <master-ip>, <master-port>, <token>, and <hash> with the values from the kubeadm init output. This command joins the worker node to the cluster. Joining worker nodes expands your cluster's capacity, allowing you to run more applications. After running the command on each worker node, go back to the master node and check the status of the nodes:

kubectl get nodes

You should see all the worker nodes listed in the output, and their status should be Ready after a few minutes.

Step 7: Deploy a Sample Application

Now that your cluster is up and running, let's deploy a sample application to test it. We'll deploy a simple Nginx deployment. Create a file named nginx-deployment.yaml with the following content:

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

Apply this deployment to your cluster:

kubectl apply -f nginx-deployment.yaml

This command creates a deployment with three replicas of the Nginx container. Deploying a sample application verifies that your cluster is functioning correctly, and you can access the application to ensure everything is working as expected. Check the status of the deployment:

kubectl get deployments

And check the status of the pods:

kubectl get pods

After a few minutes, all three Nginx pods should be running.

Step 8: Expose the Application (Optional)

To access the Nginx application from outside the cluster, you can expose it using a service. Create a service of type NodePort:

kubectl expose deployment nginx-deployment --type=NodePort --port=80

This command creates a service that exposes the Nginx deployment on a random port on each node in the cluster. Exposing your application makes it accessible to users outside the cluster, allowing them to interact with it. Find the port that was assigned to the service:

kubectl get services nginx-deployment

The output will show the port number under the PORT(S) column. You can then access the Nginx application by navigating to http://<node-ip>:<port> in your web browser. Replace <node-ip> with the IP address of one of your nodes and <port> with the port number that was assigned to the service.

Conclusion

And there you have it! You've successfully set up a Kubernetes cluster on Ubuntu 24.04. You now have a fully functional cluster that you can use to deploy and manage your applications. Remember to secure your cluster and configure it according to your specific needs. Keep exploring Kubernetes and its vast ecosystem to unlock its full potential! Happy clustering, folks!