Kubernetes Cluster On Ubuntu 24.04: A Step-by-Step Guide
Hey guys! Today, we're diving into the exciting world of Kubernetes and setting up a cluster on Ubuntu 24.04. Whether you're a seasoned DevOps pro or just getting your feet wet with container orchestration, this guide will walk you through each step of the process. Let's get started!
Prerequisites
Before we jump into the nitty-gritty, let's make sure you have everything you need:
- Ubuntu 24.04 Servers: You'll need at least two Ubuntu 24.04 servers. One will act as the master node, and the others will be worker nodes. Make sure these servers have network connectivity and can communicate with each other.
- SSH Access: Ensure you can SSH into all the servers. This is crucial for running commands and configuring the cluster.
- Sudo Privileges: The user you're using to SSH into the servers needs to have sudo privileges. This allows you to install packages and make system-level changes.
- Basic Linux Knowledge: A basic understanding of Linux commands and concepts will be super helpful.
- Internet Connection: All servers should have access to the internet to download packages and dependencies.
With these prerequisites in place, you're well-prepared to set up your Kubernetes cluster. Letâs move on to the next steps!
Step 1: Install Container Runtime (Docker)
First things first, let's install Docker, which will serve as our container runtime. Kubernetes needs a container runtime to run your applications, and Docker is a popular choice. Hereâs how you do it on Ubuntu 24.04:
-
Update Package Index:
sudo apt updateThis command updates the package list, ensuring you have the latest versions of the software.
-
Install Required Packages:
sudo apt install -y apt-transport-https ca-certificates curl software-properties-commonThese packages are necessary for adding and using the Docker repository.
-
Add Docker GPG Key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpgThis command adds the official Docker GPG key to your system, allowing you to verify the downloaded packages.
-
Add 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/nullThis adds the Docker repository to your systemâs package sources.
-
Update Package Index Again:
sudo apt updateUpdate the package list to include the Docker repository.
-
Install Docker Engine:
sudo apt install -y docker-ce docker-ce-cli containerd.ioThis command installs Docker Engine, Docker CLI, and Containerd, which are the core components of Docker.
-
Verify Docker Installation:
sudo docker run hello-worldRunning the
hello-worldimage confirms that Docker is installed correctly. If you see a welcome message, you're good to go! -
Enable Docker Service:
sudo systemctl enable docker sudo systemctl start dockerThese commands ensure that Docker starts automatically on boot.
Installing Docker is a crucial step because it provides the foundation for running containerized applications within your Kubernetes cluster. With Docker up and running, youâre ready to move on to the next phase of setting up Kubernetes. Make sure you follow each step carefully, and you'll be well on your way to having a fully functional cluster. Next, we'll tackle installing and configuring Kubernetes components themselves.
Step 2: Install Kubernetes Components (kubeadm, kubelet, kubectl)
Now that Docker is installed, let's get into the heart of Kubernetes by installing the necessary components: kubeadm, kubelet, and kubectl. These tools are essential for creating and managing your Kubernetes cluster. Hereâs how you install them:
-
Install Kubernetes Components:
sudo apt update sudo apt install -y kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectlkubelet: The agent that runs on each node in the cluster.kubeadm: A tool for bootstrapping Kubernetes clusters.kubectl: The command-line tool for interacting with the Kubernetes API.
The
apt-mark holdcommand prevents these packages from being accidentally updated, which could cause compatibility issues. -
Configure cgroup driver:
Kubernetes uses cgroup driver systemd, so ensure Docker also uses it.
sudo tee /etc/docker/daemon.json <<EOF { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2" } EOF sudo systemctl restart dockerThis configuration ensures that Docker and Kubernetes use the same cgroup driver, which is crucial for stability.
-
Verify Installation:
kubeadm version kubectl version --client kubelet --versionThese commands verify that
kubeadm,kubectl, andkubeletare installed correctly and display their versions.
Installing these Kubernetes components is a pivotal step in setting up your cluster. kubeadm simplifies the cluster creation process, kubelet ensures that your nodes can participate in the cluster, and kubectl allows you to manage and interact with your cluster. By completing this step, you are laying a solid foundation for the rest of the setup process. Make sure you pay close attention to the versions and configurations to avoid potential issues down the line. Next, we'll initialize the Kubernetes cluster using kubeadm.
Step 3: Initialize the Kubernetes Cluster (Master Node)
Alright, now for the main event! Let's initialize the Kubernetes cluster on your master node. This process sets up the control plane, which manages the entire cluster. Hereâs how to do it:
-
Initialize Kubernetes:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16The
--pod-network-cidrflag specifies the IP address range for the pod network. This range should not overlap with any existing network in your environment.10.244.0.0/16is a common choice, especially when using Flannel as the network plugin.Important: This process will take a few minutes. Once it's done, it will provide you with a
kubeadm joincommand. Save this command â you'll need it to join your worker nodes to the cluster. -
Configure kubectl:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/configThese commands configure
kubectlto connect to your cluster. They copy the admin configuration file to your home directory and set the correct ownership. -
Install a Pod Network Add-on (Flannel):
Kubernetes requires a pod network add-on to enable communication between pods. Flannel is a popular and simple option.
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/kube-flannel.ymlThis command applies the Flannel configuration to your cluster, setting up the pod network.
-
Verify Cluster Status:
kubectl get nodesThis command shows the status of your nodes. Initially, the master node might show as
NotReady. This is normal until the pod network is fully configured. After a few minutes, it should show asReady.
Initializing the Kubernetes cluster is a significant milestone. By running kubeadm init, youâve created the foundation upon which your entire cluster will operate. Configuring kubectl ensures you can interact with the cluster, and installing a pod network add-on like Flannel enables communication between your pods. Keep an eye on the node status to ensure everything comes up correctly. Next up, we'll join the worker nodes to the cluster.
Step 4: Join Worker Nodes to the Cluster
With the master node up and running, itâs time to add your worker nodes to the cluster. This involves running the kubeadm join command that you saved from the initialization step. Hereâs how to do it on each worker node:
-
Run the
kubeadm joinCommand:sudo kubeadm join <your_master_ip>:<your_master_port> --token <your_token> --discovery-token-ca-cert-hash sha256:<your_hash>Replace
<your_master_ip>:<your_master_port>,<your_token>, and<your_hash>with the values provided by thekubeadm initcommand on the master node. If you don't have thekubeadm joincommand, you can get it by running the following on the master node:kubeadm token create --print-join-commandThis command generates a new
kubeadm joincommand. -
Verify Node Status (on the Master Node):
kubectl get nodesThis command, run on the master node, shows the status of all nodes in the cluster. After a few minutes, you should see your worker nodes listed with a status of
Ready.
Joining worker nodes to the cluster expands your cluster's capacity, allowing you to run more applications and distribute the workload. The kubeadm join command securely connects your worker nodes to the master node, enabling them to receive instructions and run pods. Verifying the node status ensures that everything is connected correctly. Now that your worker nodes are part of the cluster, you're ready to start deploying applications.
Step 5: Deploy a Sample Application
Now that you have a fully functional Kubernetes cluster, let's deploy a sample application to see everything in action. Weâll use a simple Nginx deployment for this example. Hereâs how to do it:
-
Create a Deployment:
kubectl create deployment nginx --image=nginxThis command creates a deployment named
nginxusing thenginximage from Docker Hub. -
Expose the Deployment:
kubectl expose deployment nginx --port=80 --type=NodePortThis command exposes the
nginxdeployment as a service of typeNodePort.NodePortmakes the application accessible on a specific port on each node in the cluster. -
Get the Service Details:
kubectl get service nginxThis command shows the details of the
nginxservice, including the NodePort. -
Access the Application:
Open a web browser and navigate to
http://<any_node_ip>:<node_port>, replacing<any_node_ip>with the IP address of any node in your cluster and<node_port>with the NodePort value obtained in the previous step. You should see the default Nginx welcome page.
Deploying a sample application is the final step in verifying that your Kubernetes cluster is working correctly. By creating a deployment and exposing it as a service, youâre able to access your application from outside the cluster. This confirms that the networking, pod scheduling, and service discovery are all functioning as expected. Congratulations, you've successfully deployed an application on your Kubernetes cluster!
Conclusion
And there you have it! Youâve successfully created a Kubernetes cluster on Ubuntu 24.04. From installing the container runtime to deploying a sample application, you've walked through each essential step. Kubernetes can seem daunting at first, but with a bit of patience and the right guidance, you can harness its power to manage and scale your applications effectively. Happy orchestrating!