Kubernetes On Ubuntu 24.04: A Step-by-Step Installation Guide

by Team 62 views
Kubernetes on Ubuntu 24.04: A Step-by-Step Installation Guide

Hey guys! Ready to dive into the world of container orchestration? Today, we're going to walk through setting up a Kubernetes cluster on Ubuntu 24.04 LTS. This guide is designed to be super easy to follow, even if you're relatively new to Kubernetes. Let's get started!

Prerequisites

Before we jump into the installation, let’s make sure we have everything we need. Think of it as gathering your tools before starting a big project.

  • Ubuntu 24.04 LTS: You'll need at least three machines running Ubuntu 24.04 LTS. One will serve as the master node, and the others will be worker nodes.
  • Internet Connection: Make sure all machines can access the internet to download packages.
  • SSH Access: You'll need SSH access to all the machines to configure them remotely.
  • Root Privileges: You’ll need sudo or root access to perform the installations and configurations.
  • Basic Linux Knowledge: Familiarity with basic Linux commands will definitely help.

Step 1: Update and Upgrade Your Systems

First things first, let's make sure our systems are up to date. This is crucial because we want to start with the latest packages and security updates. Log into each of your Ubuntu machines and run the following commands:

sudo apt update
sudo apt upgrade -y

The apt update command refreshes the package lists, while apt upgrade -y upgrades all installed packages to their newest versions. The -y flag automatically answers "yes" to any prompts, making the process smoother.

Step 2: Install Container Runtime (Docker)

Kubernetes needs a container runtime to run containers. While it supports multiple runtimes, Docker is the most commonly used and easiest to set up. Let’s install Docker on all your machines.

sudo apt install docker.io -y

Once Docker is installed, let’s start it and enable it to start on boot:

sudo systemctl start docker
sudo systemctl enable docker

To verify that Docker is running correctly, you can run:

sudo systemctl status docker

You should see that the Docker service is active and running. Also, it’s a good idea to add your user to the docker group so you don’t have to use sudo every time you run Docker commands:

sudo usermod -aG docker $USER
newgrp docker

Log out and back in for the group change to take effect.

Why is Docker Important?

Docker is a containerization platform that allows you to package applications and their dependencies into standardized units called containers. These containers can then be run consistently across different environments, making it perfect for Kubernetes.

Step 3: Install Kubernetes Components

Now, let’s install the Kubernetes components: kubeadm, kubelet, and kubectl. These are the building blocks of our Kubernetes cluster.

  • kubeadm: A tool for bootstrapping a Kubernetes cluster.
  • kubelet: An agent that runs on each node in the cluster and makes sure that containers are running in a Pod.
  • kubectl: A command-line tool for interacting with the Kubernetes cluster.

First, we need to add the Kubernetes apt repository. Run these commands on all machines:

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

Next, update the package list and install the Kubernetes components:

sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

The apt-mark hold command prevents these packages from being accidentally updated, which can cause compatibility issues in your cluster.

Step 4: Initialize the Kubernetes Master Node

Okay, it's time to set up the master node! Choose one of your machines to be the master. On this machine, run the following command:

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

The --pod-network-cidr flag specifies the IP address range for pods in the cluster. This range should not overlap with any existing network in your environment.

After running this command, you’ll see a bunch of output, including a kubeadm join command. Copy this command and save it somewhere safe. You’ll need it to join the worker nodes to the cluster.

Also, follow the instructions in the output to set up kubectl for your user. It usually involves running these commands:

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

These commands configure kubectl to connect to your new Kubernetes cluster.

Step 5: Deploy a Pod Network

Kubernetes needs a pod network to allow pods to communicate with each other. We'll use Calico, a popular and easy-to-set-up networking solution. Run the following command on the master node:

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

This command applies the Calico manifest, which sets up the pod network. It might take a few minutes for the pods to start running.

You can check the status of the Calico pods with:

kubectl get pods -n kube-system

Wait until all Calico pods are in the Running state before proceeding.

Why Calico?

Calico provides networking and network security solutions for containers, virtual machines, and native cloud workloads. It’s known for its simplicity and scalability, making it a great choice for Kubernetes clusters.

Step 6: Join Worker Nodes to the Cluster

Now, let’s add the worker nodes to the cluster. Remember that kubeadm join command you saved earlier? Run that command on each of your worker nodes. It should look something like this:

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

Replace <master-ip>, <port>, <token>, and <hash> with the values from the kubeadm init output. If you’ve lost the command, you can regenerate it on the master node using:

sudo kubeadm token create --print-join-command

Once the worker nodes have joined, you can check their status on the master node:

kubectl get nodes

You should see all your nodes listed, with their status as Ready.

Step 7: Testing Your Cluster

Alright, let's make sure everything’s working as expected. We’ll deploy a simple Nginx pod to test the cluster. Create a file named nginx-pod.yaml with the following content:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80

Now, apply this file to your cluster:

kubectl apply -f nginx-pod.yaml

Check the status of the pod:

kubectl get pods

Once the pod is running, you can access it by exposing it as a service. Create a service definition 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: NodePort

Apply the service:

kubectl apply -f nginx-service.yaml

Find the NodePort assigned to the service:

kubectl get service nginx-service

Access the Nginx server by navigating to http://<node-ip>:<nodeport> in your web browser. If you see the Nginx welcome page, congratulations! Your Kubernetes cluster is up and running.

Troubleshooting

Setting up a Kubernetes cluster can sometimes be tricky. Here are some common issues and how to resolve them:

  • Nodes Not Joining:
    • Make sure the master node's IP address is correct in the kubeadm join command.
    • Check that the token and CA cert hash are correct.
    • Ensure that the worker nodes can communicate with the master node on the necessary ports.
  • Pods Not Running:
    • Check the pod logs using kubectl logs <pod-name>.
    • Make sure the pod network is correctly configured.
    • Verify that the container image exists and can be pulled.
  • DNS Resolution Issues:
    • Ensure that the CoreDNS pods are running correctly in the kube-system namespace.
    • Check the CoreDNS configuration.

Conclusion

So, there you have it! You’ve successfully installed a Kubernetes cluster on Ubuntu 24.04 LTS. This is a huge step towards mastering container orchestration. Feel free to experiment with different configurations and deployments to deepen your understanding. Happy clustering!

Remember to always refer to the official Kubernetes documentation for the most accurate and up-to-date information. Keep exploring, keep learning, and keep building amazing things with Kubernetes!