Kubernetes On Ubuntu 24.04: A Quick Cluster Setup Guide

by Team 56 views
Kubernetes Cluster on Ubuntu 24.04: A Quick Setup Guide

Let's dive into setting up a Kubernetes cluster on Ubuntu 24.04. This guide provides a straightforward approach to getting your cluster up and running. Whether you're a seasoned DevOps engineer or just starting with Kubernetes, this article aims to make the process as smooth as possible.

Prerequisites

Before we get started, make sure you have the following:

  • Ubuntu 24.04 Servers: You'll need at least two Ubuntu 24.04 servers – one for the master node and one or more for worker nodes. Make sure these servers can communicate with each other over the network.
  • User with Sudo Privileges: Ensure you have a user account with sudo privileges on all the servers.
  • Internet Connection: An active internet connection on all servers to download the necessary packages.
  • Basic Linux Knowledge: Familiarity with basic Linux commands will be helpful.

Setting up the Basics

Before installing Kubernetes, we need to take care of a few preliminary steps on all the nodes (both master and worker nodes). These steps involve updating the package list, installing container runtime, and disabling swap. Let's get started!

  • Update Package List: First, update the package list to ensure you have the latest versions of software packages.

    sudo apt update
    sudo apt upgrade -y
    

    This command updates the package list and upgrades the installed packages to their latest versions. The -y flag automatically answers "yes" to any prompts, making the process non-interactive. Always a good idea to keep your system updated, right?

  • Install Container Runtime (Docker): Kubernetes needs a container runtime to run containers. Docker is a popular choice, so let's install it.

    sudo apt install docker.io -y
    

    After installation, let's start and enable Docker to ensure it runs on boot. Here's how:

    sudo systemctl start docker
    sudo systemctl enable docker
    sudo systemctl status docker
    

    The systemctl status docker command verifies that Docker is up and running. Docker is the workhorse that will run our containers, so we need to make sure it's purring like a kitten.

  • Disable Swap: Kubernetes requires swap to be disabled. Here's how to disable it temporarily and permanently.

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

    sudo swapoff -a turns off swap immediately. The sed command comments out the swap line in /etc/fstab, preventing it from being enabled on reboot. Swap can mess with Kubernetes' memory management, so disabling it is crucial.

Installing Kubernetes Components

With the basics out of the way, let's install the Kubernetes components. This involves adding the Kubernetes repository, installing kubeadm, kubelet, and kubectl, and pinning their versions. Here we go!

  • Add Kubernetes Repository: Add the Kubernetes repository to apt to install Kubernetes packages.

    sudo apt-get update
    sudo apt-get install -y apt-transport-https ca-certificates curl
    sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
    echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
    

    These commands add the Kubernetes repository to your system's package sources. This allows apt to find and install Kubernetes packages. Adding the repo is like giving your system a treasure map to find all the Kubernetes goodies!

  • Install Kubeadm, Kubelet, and Kubectl: Now, install kubeadm, kubelet, and kubectl.

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

    kubeadm is a tool for bootstrapping Kubernetes clusters. kubelet is the agent that runs on each node and ensures that containers are running in a Pod. kubectl is the command-line tool for interacting with your cluster. Holding the versions prevents them from being accidentally updated.

Initializing the Kubernetes Master Node

It's time to initialize the Kubernetes master node. This involves using kubeadm to set up the control plane. Buckle up!

  • Initialize Kubernetes: Initialize the Kubernetes master node with kubeadm.

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

    The --pod-network-cidr specifies the IP address range for Pods. This range should not overlap with any existing network in your environment. Important: Save the kubeadm join command that is printed at the end of the initialization. You'll need it to join worker nodes to the cluster. This is like getting the secret handshake to join the club!

  • Configure Kubectl: Configure kubectl to interact 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
    

    These commands copy the Kubernetes configuration file to your user's .kube directory and set the correct permissions. This allows you to use kubectl to manage your cluster without needing sudo.

  • Install Pod Network Addon (Calico): Install a Pod network addon, such as Calico, to enable networking between Pods.

    kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml
    

    Calico provides network policies and networking for Kubernetes. This command applies the Calico manifest, setting up the necessary components for Pod networking. Without this, your Pods won't be able to talk to each other!

Joining Worker Nodes to the Cluster

Now, let's join the worker nodes to the cluster. This involves running the kubeadm join command that you saved earlier. Let's get those nodes connected!

  • Join Worker Nodes: On each worker node, run the kubeadm join command that was outputted during the kubeadm init step on the master node.

    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 registers the worker node with the master node, adding it to the cluster. Think of it as enrolling the worker nodes into Kubernetes school.

Verifying the Cluster

With the worker nodes joined, let's verify that the cluster is up and running. This involves checking the node status and deploying a sample application. Fingers crossed!

  • Check Node Status: Check the status of the nodes using kubectl.

    kubectl get nodes
    

    This command shows the status of all nodes in the cluster. Make sure all nodes are in the Ready state. If a node is not ready, check the kubelet logs for any errors.

  • Deploy a Sample Application: Deploy a sample application to test the cluster.

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

    These commands create a deployment named nginx using the nginx image and expose it as a NodePort service. You can then access the application using the node's IP address and the exposed port. This is like giving your cluster a test drive to make sure everything is working smoothly!

Conclusion

Congratulations! You've successfully set up a Kubernetes cluster on Ubuntu 24.04. This guide covered the essential steps, from preparing the environment to verifying the cluster. Now you're ready to deploy and manage your containerized applications with Kubernetes. Whether you're orchestrating microservices or running batch jobs, Kubernetes provides a powerful platform for managing your workloads. Happy clustering, folks! You've now got a rocking Kubernetes cluster on Ubuntu 24.04. Go forth and deploy! Remember to keep exploring and experimenting with Kubernetes to unlock its full potential. Keep learning, keep building, and keep your clusters humming!