Hey guys! Setting up a Kubernetes cluster on Ubuntu 24.04 might sound intimidating, but trust me, it’s totally doable. In this guide, we'll break it down into easy-to-follow steps, ensuring you get your cluster up and running smoothly. Let's dive in!

    Preparing Your Ubuntu 24.04 Servers

    Before we even think about Kubernetes, we need to prep our Ubuntu 24.04 servers. This involves updating the system, installing necessary packages, and configuring some basic settings. Think of it as laying the foundation for a rock-solid Kubernetes experience.

    First things first, update your package lists and upgrade any outdated packages. Open your terminal and run these commands:

    sudo apt update
    sudo apt upgrade -y
    

    This ensures you have the latest versions of all installed software. Next, we need to install a few crucial packages. These packages are essential for containerization and networking, which are the backbone of Kubernetes. Run the following command:

    sudo apt install -y apt-transport-https ca-certificates curl software-properties-common conntrack
    

    apt-transport-https allows you to access repositories over HTTPS, ensuring secure connections. ca-certificates provides the necessary SSL certificates for verifying the authenticity of websites. curl is a command-line tool for transferring data with URLs, and software-properties-common adds scripts to manage software repositories. Finally, conntrack is a tool used for tracking network connections, which is vital for Kubernetes networking.

    Next, let's disable swap. Kubernetes doesn't play nice with swap enabled. Disable it by running:

    sudo swapoff -a
    

    To make this change permanent, you’ll need to comment out the swap entry in your /etc/fstab file. Open the file with your favorite text editor (like nano):

    sudo nano /etc/fstab
    

    Find the line that starts with /swapfile and comment it out by adding a # at the beginning of the line. Save the file and exit. This prevents swap from being re-enabled on reboot.

    Finally, configure the iptables firewall to allow bridged traffic. This is crucial for Kubernetes networking to function correctly. Run these commands:

    sudo sysctl net.bridge.bridge-nf-call-iptables=1
    sudo sysctl net.ipv4.ip_forward=1
    

    To make these changes permanent, you need to add them to a sysctl configuration file. Create a new file:

    sudo nano /etc/sysctl.d/99-kubernetes-cri.conf
    

    Add the following lines to the file:

    net.bridge.bridge-nf-call-iptables = 1
    net.ipv4.ip_forward = 1
    

    Save the file and exit. Then, apply the changes by running:

    sudo sysctl --system
    

    By completing these steps, you've successfully prepared your Ubuntu 24.04 servers for the next stage: installing the container runtime.

    Installing the Container Runtime (Docker or Containerd)

    Kubernetes needs a container runtime to manage containers. Two popular choices are Docker and Containerd. Both are excellent, but Containerd is often favored for its lightweight design and tighter integration with Kubernetes.

    Installing Docker

    If you prefer Docker, here’s how to install it. First, remove any old versions of Docker:

    sudo apt remove docker docker-engine docker.io containerd runc
    

    Then, install the necessary packages to allow apt to use a repository over HTTPS:

    sudo apt update
    sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release -y
    

    Add Docker’s official GPG key:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    

    Set up the stable Docker repository:

    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
    

    Install Docker Engine:

    sudo apt update
    sudo apt install docker-ce docker-ce-cli containerd.io -y
    

    Finally, start and enable Docker:

    sudo systemctl start docker
    sudo systemctl enable docker
    

    Verify that Docker is running correctly:

    sudo docker run hello-world
    

    Installing Containerd

    If you opt for Containerd, here’s how to get it installed. First, install the necessary packages:

    sudo apt update
    sudo apt install -y containerd
    

    Configure Containerd by creating a default configuration file:

    sudo mkdir -p /etc/containerd
    sudo containerd config default | sudo tee /etc/containerd/config.toml
    

    Next, edit the /etc/containerd/config.toml file:

    sudo nano /etc/containerd/config.toml
    

    Find the line that says SystemdCgroup = false and change it to SystemdCgroup = true. Save the file and exit.

    Restart Containerd to apply the changes:

    sudo systemctl restart containerd
    

    Enable Containerd to start on boot:

    sudo systemctl enable containerd
    

    With either Docker or Containerd installed, your servers are now ready to run containers. The next step is installing Kubernetes itself.

    Installing Kubernetes Components (kubeadm, kubelet, kubectl)

    Now comes the exciting part: installing the Kubernetes components. We'll install kubeadm, kubelet, and kubectl on all our nodes. kubeadm is the command-line tool for bootstrapping the cluster. kubelet is the agent that runs on each node and manages the containers. kubectl is the command-line tool for interacting with the cluster.

    First, add the Kubernetes apt repository:

    sudo apt-get update
    sudo apt-get install -y apt-transport-https ca-certificates curl
    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 -a /etc/apt/sources.list.d/kubernetes.list
    

    Update the package lists and install the Kubernetes components:

    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 these packages from being accidentally updated, which could cause compatibility issues. With these components installed, you’re one step closer to having a fully functional Kubernetes cluster.

    Initializing the Kubernetes Cluster

    With kubeadm, kubelet, and kubectl installed on all nodes, it’s time to initialize the Kubernetes cluster. This process should only be done on the control-plane node.

    First, initialize the cluster:

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

    The --pod-network-cidr flag specifies the IP address range for the pod network. This range should not overlap with any existing network in your environment. Make sure you save the kubeadm join command that is outputted at the end of this process. You'll need it to join worker nodes to the cluster.

    Next, configure kubectl to connect to the cluster. Run these commands as a regular user:

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

    Now, you need to install a pod network add-on. This allows pods to communicate with each other. We'll use Calico in this example, but you can choose any pod network add-on that you prefer.

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

    Wait a few minutes for the pods to become ready. You can check the status of the pods by running:

    kubectl get pods --all-namespaces
    

    With the control-plane node initialized and the pod network add-on installed, it's time to join the worker nodes to the cluster.

    Joining Worker Nodes to the Cluster

    To join worker nodes to the cluster, you'll need the kubeadm join command that was outputted during the kubeadm init process. It should look something like this:

    kubeadm join <control-plane-ip>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
    

    Run this command on each worker node. After running the command, the worker node will join the cluster.

    You can check the status of the nodes by running the following command on the control-plane node:

    kubectl get nodes
    

    It may take a few minutes for the nodes to become ready. Once they are ready, you'll see them listed in the output of the command. Congrats! You now have a fully functional Kubernetes cluster.

    Verifying the Kubernetes Cluster

    Now that your cluster is set up, let’s make sure everything is working as expected. A simple way to verify the cluster is to deploy a sample application.

    Create a deployment:

    kubectl create deployment nginx --image=nginx
    

    Expose the deployment as a service:

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

    Get the service information:

    kubectl get service nginx
    

    The output will show the NodePort that has been assigned to the service. You can then access the application by navigating to the IP address of any of your nodes, followed by the NodePort in your web browser. If you see the default Nginx welcome page, congratulations! Your Kubernetes cluster is working perfectly.

    Conclusion

    Setting up a Kubernetes cluster on Ubuntu 24.04 might seem daunting at first, but by following these steps, you can get your cluster up and running in no time. Remember to prepare your servers, install the container runtime, install the Kubernetes components, initialize the cluster, and join the worker nodes. With a little bit of effort, you'll be able to deploy and manage your applications with ease using Kubernetes. Happy clustering!