Kubernetes Cluster: Install On Ubuntu 24.04 (Step-by-Step)
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 engineer or just starting your journey, this guide will walk you through each step. Let's get started!
Prerequisites
Before we jump into the installation, let's make sure we have everything we need. This section ensures that your Ubuntu 24.04 environment is properly prepared, covering essential aspects such as system updates, container runtime installation (Docker), and crucial networking configurations necessary for a smooth Kubernetes deployment. Failing to prepare adequately can lead to roadblocks later, so let’s nail this first.
- Ubuntu 24.04: Make sure you have Ubuntu 24.04 installed and running on your machines. A clean installation is always recommended to avoid conflicts with existing software.
- Internet Connection: You'll need a stable internet connection to download the necessary packages and dependencies.
- User with Sudo Privileges: Ensure you have a user account with sudo privileges. This will allow you to run commands with administrative rights.
- Basic Linux Knowledge: Familiarity with basic Linux commands will be helpful.
Update Your System
First things first, let's update our system to make sure we have the latest packages. Open your terminal and run the following commands:
sudo apt update
sudo apt upgrade -y
This ensures that all existing packages are up to date. Updating your system is crucial for security and stability. Outdated packages can introduce vulnerabilities and compatibility issues. Additionally, newer package versions often include performance improvements and bug fixes that can significantly enhance your system's overall efficiency. So, don't skip this step!
Install Container Runtime (Docker)
Kubernetes needs a container runtime to run containers. We'll use Docker, which is a popular choice. Let's install it. First, install the necessary packages to allow apt to use a repository over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
Next, 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
Add the Docker repository to APT sources:
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
Update the package index again:
sudo apt update
Finally, install Docker Engine:
sudo apt install docker-ce docker-ce-cli containerd.io -y
Verify that Docker is installed correctly by checking its version:
docker --version
Start and enable Docker to run on boot:
sudo systemctl start docker
sudo systemctl enable docker
Verify Docker service status:
sudo systemctl status docker
Why Docker? Docker is a leading containerization platform that simplifies the process of packaging, distributing, and running applications. By using Docker, you ensure that your applications run consistently across different environments. Docker containers encapsulate all the necessary dependencies, libraries, and configurations, eliminating compatibility issues. This makes Docker an essential tool for modern application deployment and a perfect fit for Kubernetes.
Disable Swap
Kubernetes requires swap to be disabled. Let's disable it temporarily and then permanently.
Disable swap temporarily:
sudo swapoff -a
To disable it permanently, comment out the swap line in /etc/fstab:
sudo nano /etc/fstab
Comment out the line that looks like this:
# /swapfile none swap sw 0 0
Save and exit the file. This step is crucial because Kubernetes performs best when swap is disabled. Swap can cause performance degradation and unpredictable behavior in a Kubernetes cluster. Disabling swap ensures that Kubernetes relies on physical memory, leading to more consistent and reliable performance.
Install Kubernetes Components
Now, let's install the Kubernetes components: kubeadm, kubelet, and kubectl. These are the building blocks of your Kubernetes cluster. kubeadm is the tool for bootstrapping the cluster, kubelet is the agent that runs on each node, and kubectl is the command-line tool to interact with the cluster.
Add Kubernetes Repository
First, add the Kubernetes repository:
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
Add the Kubernetes repository to APT sources:
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
Install Kubeadm, Kubelet, and Kubectl
Update the package index and install the Kubernetes components:
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
Hold the package versions to prevent accidental upgrades:
sudo apt-mark hold kubelet kubeadm kubectl
Understanding the Components:
kubeadm: A tool that simplifies the process of bootstrapping a Kubernetes cluster. It performs the necessary actions to initialize the control plane nodes and join worker nodes to the cluster.kubelet: An agent that runs on each node in the cluster. It listens for instructions from the control plane and ensures that containers are running as expected.kubectl: The command-line tool that allows you to interact with the Kubernetes cluster. You can use it to deploy applications, manage resources, and monitor the cluster's health.
Initialize the Kubernetes Cluster
With the components installed, it’s time to initialize the Kubernetes cluster. This involves setting up the control plane and configuring the networking.
Initialize the Control Plane
Run the following command to initialize the Kubernetes control plane:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
Important: Take note of the kubeadm join command that is printed at the end of the output. You'll need this to join worker nodes to the cluster.
Configure Kubectl
To use kubectl as a non-root user, run the following commands:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Install a Pod Network Add-on
Kubernetes requires a pod network add-on to enable communication between pods. We'll use Calico, a popular and flexible networking solution. Apply the Calico manifest:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Why Calico? Calico provides a highly scalable and feature-rich networking solution for Kubernetes. It supports various networking policies, allowing you to control traffic flow between pods. Calico also integrates seamlessly with Kubernetes, making it easy to deploy and manage. Other options include Flannel, Weave Net, and Cilium.
Verify the Cluster
Check the status of the nodes:
kubectl get nodes
All nodes should be in the Ready state. It might take a few minutes for the nodes to become ready.
Check the status of the pods:
kubectl get pods --all-namespaces
All the core DNS pods and Calico pods should be running.
Joining Worker Nodes
To add worker nodes to the cluster, use the kubeadm join command that was printed during the kubeadm init step. The command should look something like this:
sudo kubeadm join <control-plane-ip>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Run this command on each worker node you want to add to the cluster.
Approve Joining Nodes
After running the join command on the worker nodes, you may need to approve the joining nodes on the control plane. Check for any pending Certificate Signing Requests (CSRs) with the following command:
kubectl get csr
If there are any pending CSRs, approve them using:
kubectl approve <csr-name>
Replace <csr-name> with the actual name of the CSR.
Deploying a Sample Application
Now that our cluster is up and running, let’s deploy a sample application to test it out. We'll deploy a simple Nginx deployment.
Create a Deployment
Create a deployment file named nginx-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Apply the deployment:
kubectl apply -f nginx-deployment.yaml
Create a Service
Create a service to expose the Nginx deployment. Create a file named nginx-service.yaml:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Apply the service:
kubectl apply -f nginx-service.yaml
Access the Application
Get the external IP address of the service:
kubectl get service nginx-service
Access the application in your browser using the external IP address.
Conclusion
And there you have it! You've successfully installed a Kubernetes cluster on Ubuntu 24.04. This is just the beginning. Kubernetes is a powerful platform, and there's so much more to explore. Dive deeper into deployments, services, networking, and more to unlock the full potential of Kubernetes. Happy clustering!
Remember, setting up a Kubernetes cluster is no small feat. It requires careful attention to detail and a solid understanding of the underlying concepts. By following this guide, you've taken a significant step towards mastering Kubernetes and modern application deployment.