Install Kubernetes Cluster On Ubuntu 24.04 With Kubeadm
Hey guys! Ready to dive into the world of container orchestration? Let's get our hands dirty and learn how to install a Kubernetes cluster on Ubuntu 24.04 using kubeadm. Kubernetes, often shortened to K8s, is the go-to platform for automating deployment, scaling, and management of containerized applications. It's like the ultimate control center for your containers, making sure everything runs smoothly and efficiently. Using kubeadm simplifies the process of setting up a Kubernetes cluster, making it accessible to both beginners and experienced users. This guide will walk you through each step, ensuring you have a working cluster up and running in no time. We will cover everything from setting up your Ubuntu 24.04 machines to deploying a sample application, so buckle up and let's get started!
Prerequisites: What You'll Need
Before we jump into the installation, let's make sure you have everything you need. You'll need a few things to get started:
- Ubuntu 24.04 Machines: You'll need at least one Ubuntu 24.04 machine, but for a real cluster, aim for three or more. These can be virtual machines (VMs) or physical servers. If you're using VMs, make sure your hypervisor can handle the resource allocation you set. Ideally, each machine should have at least 2 GB of RAM and 2 CPUs. More resources are always better, especially for testing or production environments. A good solid-state drive (SSD) will also boost performance.
- Network Connectivity: Make sure all your machines can communicate with each other over the network. This is crucial for the cluster to function correctly. Ensure there are no firewall rules blocking the required ports (more on that later).
- SSH Access: You should be able to SSH into your Ubuntu machines. This makes it easier to manage the servers without needing a physical connection to each one. This access is essential for executing the commands needed for the setup.
- Internet Access: Your machines need internet access to download the necessary packages. Kubernetes and its dependencies will be downloaded from the internet during the installation process.
- Basic Linux Knowledge: Familiarity with the Linux command line is helpful. You should know how to navigate directories, run commands, and edit files. Don't worry if you're not an expert; this guide will walk you through the specifics.
- Root or Sudo Access: You'll need root or sudo privileges on each machine to install software and make system-level changes. Ensure you can execute commands with sudo if you're not logged in as root.
- Container Runtime: Kubernetes needs a container runtime to manage containers. We'll be using containerd in this guide, which is a popular and efficient choice. It's lightweight and easy to integrate with Kubernetes.
Make sure your machines meet these prerequisites. With these sorted, you are ready to get the installation rolling!
Step 1: Updating and Configuring Your Ubuntu Machines
Alright, first things first! Let's get our Ubuntu machines ready for Kubernetes. This involves updating the system, configuring the network, and installing some essential packages. Think of this as preparing the foundation for your Kubernetes castle.
Update System Packages
Start by updating the package lists and upgrading the installed packages on each of your Ubuntu machines. Open a terminal and run the following commands:
sudo apt update
sudo apt upgrade -y
The -y flag automatically answers yes to all prompts, so you don't have to manually confirm each step. This ensures that your system has the latest security updates and package versions.
Disable Swap
Kubernetes has some specific requirements, and one of them is that swap should be disabled. Having swap enabled can negatively affect the performance of Kubernetes. To disable swap, run these commands:
sudo swapoff -a
sudo sed -i '/ swap / s/^/#/' /etc/fstab
The first command disables the swap immediately, and the second command comments out the swap entries in the /etc/fstab file. This prevents swap from being re-enabled on reboot.
Configure Networking
Next, we need to ensure that the networking is set up correctly. The container network interface (CNI) needs to be properly configured. Ensure that your machines have unique hostnames and that the /etc/hosts file is correctly configured.
Set Hostname
First, set a unique hostname for each machine. Replace your-hostname with the desired hostname. For example, if you have three machines, you could name them k8s-master, k8s-node1, and k8s-node2.
sudo hostnamectl set-hostname your-hostname
After setting the hostname, you need to update the /etc/hosts file. Add the IP addresses and hostnames of all your Kubernetes nodes to the /etc/hosts file on each machine. This ensures that the nodes can resolve each other's hostnames correctly.
sudo nano /etc/hosts
Add lines like this (replace the IP addresses with your actual IP addresses):
127.0.0.1 localhost
192.168.1.10 k8s-master
192.168.1.11 k8s-node1
192.168.1.12 k8s-node2
Save and close the file. You'll need to do this on each machine.
Install Required Packages
We need to install some packages that are essential for Kubernetes and container management. This includes apt-transport-https, curl, and other utilities.
sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-release
These packages provide the necessary tools for managing software repositories and downloading packages securely.
With these steps completed, your Ubuntu machines are now properly configured and ready for Kubernetes installation.
Step 2: Installing Containerd
Containerd is a lightweight, industry-standard container runtime that is often preferred for Kubernetes. It is simpler than Docker and integrates well with Kubernetes. Let's install it!
Add Containerd Repository
First, we add the official Containerd repository to our system. This allows us to install Containerd using apt.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
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 Containerd
Now, update the package list and install Containerd:
sudo apt update
sudo apt install -y containerd
Configure Containerd
Next, configure Containerd to work with Kubernetes. Create a configuration file if one doesn't already exist:
sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml
Then, edit the configuration file to include the necessary settings. Open the config.toml file with a text editor:
sudo nano /etc/containerd/config.toml
Find the [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] section and ensure SystemdCgroup = true. If it is not set, add it. Save and close the file. Example:
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
Restart Containerd
Finally, restart Containerd to apply the changes:
sudo systemctl restart containerd
Verify Containerd Installation
Make sure that Containerd is running and working as expected. Run this command to check its status:
sudo systemctl status containerd
You should see that Containerd is active and running without any errors.
With Containerd set up, you are one step closer to your Kubernetes cluster.
Step 3: Installing Kubernetes Components with kubeadm
Now, let’s get to the heart of the matter and install the Kubernetes components using kubeadm. kubeadm is a command-line tool that simplifies the process of bootstrapping and managing Kubernetes clusters. It takes care of all the complex tasks, so you can focus on building and deploying your applications.
Add Kubernetes Repository
First, add the Kubernetes repository to your system to install the latest versions. The following commands add the necessary GPG key and repository information. Be careful about the versions; these steps are for Ubuntu 24.04.
sudo curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
Install Kubernetes Packages
Update your package list and install the Kubernetes packages: kubelet, kubeadm, and kubectl. These tools are essential for managing your Kubernetes cluster.
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
During the installation, Kubernetes components will be downloaded and installed.
Hold Kubernetes Packages
To prevent the Kubernetes packages from being automatically upgraded, you should hold them. This ensures that your cluster's version remains consistent until you're ready for an upgrade. Run the following command:
sudo apt-mark hold kubelet kubeadm kubectl
With the packages installed and held, you're ready to initialize your Kubernetes cluster.
Step 4: Initializing the Kubernetes Cluster
This is where the magic happens! We'll initialize the Kubernetes cluster on the master node. The master node is responsible for managing the cluster, scheduling pods, and controlling the overall state of the cluster.
Initialize the Master Node
On the master node (e.g., k8s-master), run the kubeadm init command. Replace the 192.168.1.10 with the IP address of your master node. This command initializes the Kubernetes control plane. It sets up the necessary components, such as the API server, scheduler, and controller manager.
sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.1.10
Replace 192.168.1.10 with the correct IP address of your master node. The --pod-network-cidr option specifies the network range for the pods. The --apiserver-advertise-address option tells the API server the IP address it should advertise for the cluster. If you don't provide the advertise address, Kubernetes will try to automatically detect it, which can sometimes fail in cloud environments.
Keep a note of the kubeadm join command that is outputted by the kubeadm init command. This command is very important, because you'll need it to join worker nodes to the cluster.
Configure kubectl
After the initialization is complete, you need to configure kubectl, the command-line tool for interacting with your cluster. You can configure kubectl by copying the admin configuration file to your user's home directory. This allows you to manage the cluster without root access.
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 create the .kube directory in your home folder, copy the admin configuration file, and set the correct ownership and permissions.
Install a Pod Network (CNI)
Next, you must install a Pod network. Kubernetes uses a CNI-compliant network plugin for pod networking. There are several options available, such as Calico, Flannel, and Weave Net. For this guide, we'll install Calico. You can install Calico using the following command:
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml
This command downloads and applies the Calico manifest, which creates the necessary resources for the Calico network. The network might take a minute or two to become fully operational, so be patient. If you'd like a more lightweight solution, consider Flannel, which you can install with the following command:
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
Verify the Cluster Status
Verify that the cluster is running correctly. Run the following command to check the status of your nodes:
kubectl get nodes
You should see the master node in the Ready state. The output will look similar to this:
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane 3m v1.29.0
If the status shows NotReady, it means something went wrong during the installation or configuration. Double-check all the steps and ensure that all prerequisites are met. Your Kubernetes cluster is now set up and ready to go!
Step 5: Joining Worker Nodes
Now, let's add worker nodes to your cluster. Worker nodes are where your pods will run. They are managed by the master node and execute the workload of your applications. This process involves the kubeadm join command.
Join the Worker Nodes
On each worker node (e.g., k8s-node1, k8s-node2), run the kubeadm join command. This command was outputted after running kubeadm init on the master node. The command connects the worker nodes to the cluster. The command usually looks like this, but be sure to use the one that kubeadm init outputted. Here's a placeholder:
sudo kubeadm join <master-ip>:<master-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Run the command on each worker node. If you did not save the join command, you can retrieve it by running the following on the master node:
# Replace <your-node-name> with the hostname of the node to get the join command.
# Or, to retrieve the join command:
$ kubeadm token create --print-join-command
Verify Node Status
Go back to your master node and run kubectl get nodes again. You should see all of your nodes in the Ready state. This confirms that they have successfully joined the cluster and are ready to run pods.
kubectl get nodes
The output will list all the nodes in your cluster, along with their status. You should see a Ready status for each node. If a node is not ready, it means that the node isn't communicating with the master node, or the node might have some misconfigurations. Double-check your network configurations and ensure that all the prerequisites are met on each node.
Now that you have your worker nodes joined, your cluster is almost complete and ready to deploy your first application!
Step 6: Deploying a Sample Application
Let's deploy a simple application to verify that everything is working. We will deploy a basic Nginx web server. Deploying an application helps to confirm that the cluster is functioning correctly. This also enables you to ensure that the pods are being created, scheduled, and run on your worker nodes.
Create a Deployment
Create a deployment that will manage the application's pods. We use kubectl to create the deployment with the following command:
kubectl create deployment nginx --image=nginx:latest
This command creates a deployment named nginx and pulls the latest version of the nginx image from Docker Hub. Kubernetes will then create pods based on the deployment configuration. You can monitor the deployment status by watching the events or checking the deployment itself.
Expose the Deployment
Expose the deployment using a service of type NodePort. This makes the application accessible from outside the cluster. Run the following command:
kubectl expose deployment nginx --port=80 --type=NodePort
This command creates a service that exposes the nginx deployment on port 80. The NodePort type allocates a port on each node, making the application accessible via the node's IP address and the allocated port.
Get the Service Information
Get the service information to find the node port that has been assigned. This information is needed to access the application.
kubectl get service nginx
The output will show the node port assigned to the service. For example:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.100.12.185 <none> 80:31406/TCP 1m
In this example, the node port is 31406.
Access the Application
Access the application by pointing your web browser to the IP address of one of your nodes and the node port. For example, if your node's IP address is 192.168.1.11, and the node port is 31406, you would use http://192.168.1.11:31406.
If you see the Nginx welcome page, congratulations! You have successfully deployed an application on your Kubernetes cluster. If it doesn't load, verify that the pods are running, the services are configured, and there are no firewall rules that are blocking access to the node ports.
Troubleshooting Common Issues
Here are some common issues that you might encounter during the installation and how to resolve them. Remember, troubleshooting is a key part of learning!
- Network Issues: Ensure your machines can communicate with each other over the network, particularly on the ports used by Kubernetes. Check firewall rules on all machines. Verify that DNS resolution is working correctly. A common mistake is using the wrong IP addresses for master and worker nodes, or a misconfigured CNI. Ensure that your CNI is properly configured.
- Containerd Issues: Verify that containerd is running on all the nodes. Check the containerd logs for any errors. Double-check your
/etc/containerd/config.tomlfile to ensure the systemd cgroup driver is set correctly. Check the status of the containerd service usingsudo systemctl status containerd. Make sure that containerd is up-to-date and not corrupted. - kubeadm and kubectl Issues: Ensure that the versions of
kubeadm,kubectl, andkubeletare compatible with your Kubernetes cluster. Make sure that you have correctly configured thekubectlto access the cluster. Check the logs forkubeletandkubeadmfor error messages. Double-check the configuration files. Confirm you can access the Kubernetes API server usingkubectl get nodes. Make sure that the configuration files have the correct permissions and that they point to the correct API server address and port. - Pod Network Issues: Ensure that you have installed a CNI plugin, such as Calico or Flannel. Verify that the CNI plugin is running correctly by checking the logs for the relevant pods. Check for any errors or issues related to the CNI plugin. Make sure the pod network CIDR that you configured is not overlapping with your node's network. Check the status of the pods and their network connectivity. Verify that the service is correctly exposed, and the node ports are open.
- Time Synchronization: Make sure that all the nodes in the cluster are synchronized with the same time server. Time drift can cause issues with the Kubernetes certificates and communication. Use
ntporchronyto synchronize the time on all the nodes. You can quickly check the time difference withtimedatectl status. The time must be consistent across all nodes. Otherwise, the certificates will expire prematurely.
Conclusion: You Did It!
Awesome work, guys! You've successfully installed a Kubernetes cluster on Ubuntu 24.04 using kubeadm. You've gone through the process of setting up the prerequisites, installing the necessary components, initializing the master node, joining worker nodes, and deploying a sample application. Kubernetes is a powerful tool, and with a bit of practice, you'll be able to manage your containerized applications with ease. Keep experimenting, and exploring all the features of Kubernetes. Kubernetes offers huge flexibility and scalability. Keep learning and have fun!
Remember, the key to success is to follow the steps carefully, verify your configuration at each stage, and troubleshoot any issues that arise. You can do it!
If you have questions, drop them below. Happy containerizing!