Kubernetes On Ubuntu 24: A Step-by-Step Setup Guide
Hey guys! Today, we're diving into the exciting world of Kubernetes and setting up a cluster on Ubuntu 24. Kubernetes, often abbreviated as K8s, is an open-source container orchestration system that automates the deployment, scaling, and management of containerized applications. It was originally designed by Google and is now maintained by the Cloud Native Computing Foundation (CNCF). Ubuntu 24, being the latest LTS (Long Term Support) release, provides a stable and robust platform for running Kubernetes. Whether you're a seasoned DevOps engineer or just starting with container orchestration, this guide will walk you through each step to get your Kubernetes cluster up and running smoothly.
Prerequisites
Before we get started, let's make sure you have everything you need:
- Ubuntu 24 Server: You'll need at least two Ubuntu 24 servers – one for the master node and the other for the worker node(s). For a production environment, it's highly recommended to have at least three master nodes for high availability.
- User with sudo privileges: Ensure you have a user account with
sudoprivileges on all servers. - Internet Connection: A stable internet connection to download the necessary packages.
- Basic Linux knowledge: Familiarity with basic Linux commands will be helpful.
Step 1: Update and Upgrade System Packages
First things first, let's update and upgrade the system packages on all your Ubuntu servers. Open your terminal and run the following commands:
sudo apt update
sudo apt upgrade -y
The apt update command refreshes the package lists, while apt upgrade installs the newest versions of all packages currently installed on the system. The -y flag automatically answers "yes" to any prompts, ensuring a smooth and unattended upgrade process. This step is crucial because it ensures that you are starting with the latest and most secure versions of all software packages. Updating the system also resolves any potential dependency issues that might arise during the installation of Kubernetes components. After running these commands, it's always a good practice to reboot the server to ensure all updates are applied correctly. This simple step can save you from encountering unexpected issues later on during the Kubernetes setup.
Step 2: Install Container Runtime (Containerd)
Kubernetes needs a container runtime to manage containers. Containerd is a popular choice. Let's install it. First, load the necessary kernel modules:
sudo modprobe overlay
sudo modprobe br_netfilter
Next, configure necessary sysctl settings:
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
sudo tee /etc/sysctl.d/99-kubernetes-cri.conf <<EOF
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sudo sysctl --system
Now, install containerd:
sudo apt update
sudo apt install -y containerd
Configure containerd by creating a configuration file:
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
Finally, restart containerd:
sudo systemctl restart containerd
sudo systemctl enable containerd
Installing containerd involves several steps to ensure it integrates well with the system and Kubernetes. First, we load the overlay and br_netfilter kernel modules, which are essential for container networking and storage. Then, we configure sysctl settings to enable IP forwarding and allow bridged traffic to be processed by iptables. These settings are crucial for Kubernetes networking to function correctly. After that, we install the containerd package using apt. We then generate a default configuration file for containerd and restart the service to apply the changes. Enabling the containerd service ensures that it starts automatically on boot. By following these steps carefully, you can ensure that containerd is properly installed and configured to support your Kubernetes cluster.
Step 3: Install kubeadm, kubelet, and kubectl
These are the core components of Kubernetes. kubeadm is a tool for bootstrapping a Kubernetes cluster. kubelet is the agent that runs on each node. 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 -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
Now, install the required packages:
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
Installing kubeadm, kubelet, and kubectl is a critical step in setting up a Kubernetes cluster. Kubeadm simplifies the process of initializing and managing the cluster. Kubelet acts as the node agent, ensuring that containers are running as expected. Kubectl provides the command-line interface for interacting with the Kubernetes API server. First, we add the Kubernetes apt repository to our system, which allows us to install the necessary packages. We also install apt-transport-https, ca-certificates, and curl to ensure secure communication with the repository. After adding the repository, we update the package lists and install kubelet, kubeadm, and kubectl. Finally, we use apt-mark hold to prevent these packages from being automatically updated, which is important for maintaining the stability of the Kubernetes cluster. This ensures that the versions of these core components remain consistent and prevents unexpected issues caused by automatic upgrades.
Step 4: Initialize the Kubernetes Cluster (Master Node)
On your master node, initialize the Kubernetes cluster. Choose a pod network CIDR. This example uses 10.244.0.0/16:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
After the initialization completes, you'll see instructions to configure kubectl. Follow these instructions:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Also, apply a network plugin. We'll use Calico:
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml
Initializing the Kubernetes cluster on the master node is a pivotal step. The kubeadm init command sets up the control plane components, such as the API server, scheduler, and controller manager. The --pod-network-cidr flag specifies the IP address range that will be used for pods within the cluster. Choosing an appropriate CIDR is crucial for avoiding IP address conflicts with your existing network infrastructure. After the initialization completes, kubeadm provides instructions for configuring kubectl, the command-line tool that allows you to interact with the cluster. This involves creating a .kube directory in your home directory, copying the admin.conf file to this directory, and setting the correct ownership. Finally, we apply a network plugin, in this case, Calico, which provides networking and network policy capabilities for the cluster. Calico is a popular choice because it's easy to set up and provides robust networking features. By completing these steps, you lay the foundation for a functional and manageable Kubernetes cluster.
Step 5: Join Worker Nodes to the Cluster
On each worker node, use the kubeadm join command that was outputted by kubeadm init on the master node. It should look something like this:
sudo kubeadm join <master-ip>:<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
Joining worker nodes to the Kubernetes cluster is the final step in building your multi-node cluster. The kubeadm join command, which you obtain from the master node after initialization, securely connects the worker nodes to the control plane. This command includes the master node's IP address and port, a token for authentication, and a hash of the CA certificate for verification. By running this command on each worker node, you register them with the cluster and allow them to run containerized workloads. If you lose the original kubeadm join command, you can easily regenerate it on the master node using kubeadm token create --print-join-command. This command generates a new token and prints the complete kubeadm join command. After joining the worker nodes, you can verify their status on the master node using kubectl get nodes. This command displays a list of all nodes in the cluster, including their status, roles, and ages. Ensuring that all worker nodes are in a Ready state indicates that they have successfully joined the cluster and are ready to run pods.
Step 6: Verify the Cluster
Back on the master node, check the status of the nodes:
kubectl get nodes
You should see your master node and worker node(s) listed as Ready.
Also, check the status of the pods:
kubectl get pods --all-namespaces
This will show you all the pods running in your cluster, including the system pods.
Verifying the cluster's health is crucial to ensure that everything is functioning as expected. Using kubectl get nodes, you can check the status of all nodes in the cluster. A Ready status indicates that the node is healthy and able to run workloads. If a node is in a NotReady state, it indicates that there may be issues with the node's configuration or connectivity. Additionally, kubectl get pods --all-namespaces provides a comprehensive view of all pods running in the cluster. This command lists all pods, including those in the kube-system namespace, which are essential for the cluster's operation. By examining the status of these pods, you can identify any potential issues with the control plane components or other critical services. A healthy cluster should have all nodes in a Ready state and all essential pods running without errors.
Conclusion
And there you have it! You've successfully set up a Kubernetes cluster on Ubuntu 24. This setup provides a solid foundation for deploying and managing your containerized applications. Remember to explore further configurations and optimizations based on your specific needs. Happy containerizing!
Setting up a Kubernetes cluster on Ubuntu 24 can seem daunting at first, but by following these steps, you can quickly get a fully functional cluster up and running. Kubernetes offers powerful features for automating the deployment, scaling, and management of containerized applications. With your new cluster, you can start deploying your applications and exploring the many benefits of container orchestration. Whether you're running a small development environment or a large-scale production system, Kubernetes on Ubuntu 24 provides a flexible and scalable platform for your workloads. So, go ahead, experiment with different configurations, and unleash the power of Kubernetes!