Kubernetes Cluster On Ubuntu 24: A Step-by-Step Guide

by Team 54 views
Kubernetes Cluster on Ubuntu 24: A Step-by-Step Guide

Let's dive into setting up a Kubernetes cluster on Ubuntu 24. This guide will walk you through each step, making it easy to deploy and manage your containerized applications. Kubernetes, often abbreviated as K8s, has become the go-to orchestration platform, and Ubuntu 24 provides a solid foundation for running it. We'll cover everything from preparing your Ubuntu servers to deploying your first application.

Prerequisites

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

  • Multiple Ubuntu 24 Servers: You'll need at least two servers – one for the master node and one or more for worker nodes. For a production environment, consider having multiple master nodes for redundancy.
  • Sudo Privileges: Ensure the user account you're using has sudo privileges on all servers.
  • Network Connectivity: All servers should be able to communicate with each other over the network. Ensure there are no firewalls blocking necessary ports.
  • Basic Linux Knowledge: Familiarity with Linux commands will be helpful.

Step 1: Preparing the Ubuntu Servers

First, we need to prepare each of our Ubuntu servers. This involves updating the package lists, installing necessary dependencies, and configuring the network settings. This initial setup is crucial for a smooth Kubernetes installation. Let's walk through the commands and configurations needed.

Updating the Package List and Installing Dependencies

Begin by updating the package list on each server to ensure you have the latest versions of available software. Open your terminal and run:

sudo apt update
sudo apt upgrade -y

Next, install the necessary dependencies. These packages provide the tools and libraries required for Kubernetes to function correctly. Run the following command to install apt-transport-https, curl, and containerd:

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

Installing and Configuring Containerd

Containerd is a container runtime that Kubernetes uses to manage containers. It is a core component, so setting it up correctly is vital. First, download the containerd configuration file:

mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml

Now, edit the /etc/containerd/config.toml file using your favorite text editor (like nano or vim) to change the SystemdCgroup option to true:

sudo nano /etc/containerd/config.toml

Find the line that says SystemdCgroup = false and change it to SystemdCgroup = true. This setting ensures that containerd uses systemd for managing cgroups, which is recommended for Kubernetes.

Save the file and exit the text editor. Then, restart containerd to apply the changes:

sudo systemctl restart containerd
sudo systemctl enable containerd

Disabling Swap

Kubernetes requires swap to be disabled. Swap can cause performance issues and conflicts with Kubernetes' resource management. To disable swap, first, turn it off:

sudo swapoff -a

Next, comment out the swap entry in the /etc/fstab file to prevent it from being enabled on reboot. Open the file with a text editor:

sudo nano /etc/fstab

Find the line that starts with /swapfile and add a # at the beginning to comment it out. Save the file and exit the editor.

Setting up the Kubernetes Repository

Add the Kubernetes repository to your system's package manager. First, download the Google Cloud public key:

curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/kubernetes-archive-keyring.gpg

Then, add the Kubernetes repository to your APT sources:

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

Note: Even though the repository URL contains kubernetes-xenial, it is compatible with Ubuntu 24. Finally, update the package list again to include the new repository:

sudo apt update

Step 2: Installing Kubernetes Components

Now that our servers are prepared, we can install the core Kubernetes components: kubelet, kubeadm, and kubectl. These tools are essential for managing and running your Kubernetes cluster. Let's install each component and configure them appropriately.

Installing Kubelet, Kubeadm, and Kubectl

Install the Kubernetes components using the following command:

sudo apt install -y kubelet kubeadm kubectl

After the installation, hold the package versions to prevent unintended upgrades. This is important for maintaining the stability of your Kubernetes cluster:

sudo apt-mark hold kubelet kubeadm kubectl

Initializing the Kubernetes Master Node

On the server you've designated as the master node, initialize the Kubernetes cluster using kubeadm. Choose a pod network CIDR that does not conflict with your existing network. A common choice is 10.244.0.0/16.

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

This process will take a few minutes. Once it completes, it will output 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 – you'll need it later.

Configuring Kubectl

To use kubectl with your cluster, you need to configure it with the appropriate credentials. Run the following commands to set up kubectl for your user:

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

Now you can use kubectl to interact with your Kubernetes cluster. For example, you can check the status of the nodes:

kubectl get nodes

At this point, the master node will likely show as NotReady because we haven't installed a pod network yet.

Deploying a Pod Network

A pod network allows pods to communicate with each other. We'll use Calico, a popular and flexible networking solution. Apply the Calico manifest:

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

It will take a few minutes for the Calico pods to start. You can monitor their status using:

kubectl get pods -n kube-system

Once the Calico pods are running, the master node should transition to the Ready state.

Step 3: Joining Worker Nodes to the Cluster

Now, let's add the worker nodes to the cluster. On each worker node, run the kubeadm join command that was outputted when you initialized the master node. It should look something like this:

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

If you've lost the kubeadm join command, you can regenerate it on the master node:

sudo kubeadm token create --print-join-command

Run this command on the master node and then use the output on each worker node. After running the kubeadm join command on each worker node, they should join the cluster.

Verifying Worker Node Status

Back on the master node, use kubectl to verify that the worker nodes have joined the cluster:

kubectl get nodes

You should see all your worker nodes listed, and their status should eventually change to Ready once the networking is configured.

Step 4: Deploying a Sample Application

With your Kubernetes cluster up and running, let's deploy a simple application to test it out. We'll deploy a basic Nginx web server.

Creating a Deployment

Create a deployment using the kubectl create deployment command:

kubectl create deployment nginx --image=nginx

This command tells Kubernetes to create a deployment named nginx using the nginx image from Docker Hub.

Exposing the Deployment

To access the Nginx server from outside the cluster, you need to expose the deployment as a service. We'll use a NodePort service, which makes the service available on a specific port on each node.

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

Accessing the Application

To find the port that Kubernetes has assigned to the service, use the following command:

kubectl get service nginx

Look for the NodePort value in the output. It will be a port number between 30000 and 32767. You can then access the Nginx server by navigating to http://<node-ip>:<node-port> in your web browser, replacing <node-ip> with the IP address of one of your nodes and <node-port> with the assigned NodePort.

Step 5: Monitoring and Maintaining Your Cluster

Once your Kubernetes cluster is running, it's essential to monitor its health and performance. Kubernetes provides several tools for monitoring, logging, and updating your cluster.

Monitoring Tools

  • Kubectl: Use kubectl to check the status of your nodes, pods, and services. The kubectl get command is your primary tool for querying the cluster state.
  • Kubernetes Dashboard: The Kubernetes Dashboard is a web-based UI that provides an overview of your cluster. You can deploy it using the official documentation.
  • Prometheus and Grafana: For more advanced monitoring, consider deploying Prometheus and Grafana. These tools provide detailed metrics and visualizations of your cluster's performance.

Logging

Centralized logging is crucial for troubleshooting issues in your cluster. Consider using tools like Elasticsearch, Fluentd, and Kibana (EFK stack) or Loki for collecting and analyzing logs.

Updating Your Cluster

Regularly update your Kubernetes components to the latest stable versions. Use kubeadm upgrade to upgrade the master node and then drain and upgrade the worker nodes. Always follow the official Kubernetes documentation for upgrade procedures.

Conclusion

Setting up a Kubernetes cluster on Ubuntu 24 can seem daunting at first, but by following this step-by-step guide, you can have your cluster up and running in no time. From preparing your servers to deploying your first application, we've covered the essential steps for building a solid Kubernetes foundation. Remember to monitor and maintain your cluster regularly to ensure its stability and performance. Happy containerizing, folks!