Kubernetes Cluster On Ubuntu 24: A Step-by-Step Guide

by Team 54 views
Kubernetes Cluster on Ubuntu 24: A Step-by-Step Guide

So, you're looking to dive into the world of Kubernetes on Ubuntu 24? Awesome! You've come to the right place. This guide will walk you through setting up a Kubernetes cluster on Ubuntu 24, step by step. We'll cover everything from preparing your Ubuntu servers to deploying your first application. Let's get started!

Prerequisites

Before we get our hands dirty, let's make sure we have all the necessary tools and configurations in place. Think of this as gathering your ingredients before you start cooking up a Kubernetes feast!

  • Ubuntu 24 Servers: You'll need at least two Ubuntu 24 servers. One will act as the master node, and the other(s) will be worker nodes. For a production environment, consider having at least three master nodes for high availability.
  • Internet Access: All servers should have internet access to download packages.
  • SSH Access: Ensure you can SSH into all the servers.
  • Root or Sudo Privileges: You'll need root or sudo privileges on all servers to install and configure the necessary software.
  • Unique Hostnames and Static IP Addresses: Each server should have a unique hostname and a static IP address. This is crucial for the cluster to function correctly.
  • Container Runtime: We’ll use containerd as our container runtime. It's lightweight and efficient.

With these prerequisites in place, we're ready to move on to the next stage. It's like having all your ingredients measured and ready to go – now the real fun begins!

Step 1: Preparing the Ubuntu Servers

Alright, let's get those Ubuntu servers prepped and ready for Kubernetes! This involves updating the system, installing necessary packages, and configuring some system settings. Trust me, it's not as daunting as it sounds. We’ll take it one step at a time.

Updating the System

First things first, let's update the package lists and upgrade the installed packages to their latest versions. Open your terminal and run these commands:

sudo apt update
sudo apt upgrade -y

This ensures that you have the latest security patches and bug fixes. It’s always a good practice to keep your system up to date. Think of it as giving your servers a regular health check-up!

Installing Required Packages

Next, we need to install some packages that Kubernetes requires. These include apt-transport-https, curl, and ca-certificates. Run the following command:

sudo apt install -y apt-transport-https curl ca-certificates
  • apt-transport-https: Allows apt to access repositories over HTTPS.
  • curl: A command-line tool for transferring data with URLs.
  • ca-certificates: Provides certificates for verifying SSL connections.

Adding the Kubernetes APT Repository

Now, let's add the Kubernetes APT repository so we can install Kubernetes components. First, download the Google Cloud public key:

sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg

Then, add the Kubernetes APT repository:

echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

Note: Even though the line above says kubernetes-xenial, it's compatible with Ubuntu 24. Don't worry, it's a common practice due to the way Kubernetes packages are built.

Updating APT Again

After adding the repository, update APT again to include the new packages:

sudo apt update

Disabling Swap

Kubernetes requires swap to be disabled. To disable swap, run:

sudo swapoff -a

To make this permanent, edit the /etc/fstab file:

sudo nano /etc/fstab

Comment out the line that contains swap by adding a # at the beginning of the line. Save the file and exit.

Configuring sysctl Settings

We need to configure some sysctl settings to allow bridged traffic to pass through the network. Create a file named /etc/sysctl.d/99-kubernetes-cri.conf with the following content:

sudo nano /etc/sysctl.d/99-kubernetes-cri.conf

Add these lines:

net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1

Save the file and apply the settings:

sudo sysctl --system

With these steps completed, your Ubuntu servers are now properly prepared for installing Kubernetes. Give yourself a pat on the back – you're making great progress!

Step 2: Installing Kubernetes Components

Now comes the exciting part – installing the Kubernetes components! We'll install kubelet, kubeadm, and kubectl on all nodes. These are the core tools we need to create and manage our cluster.

Installing kubelet, kubeadm, and kubectl

Run the following command to install these components:

sudo apt install -y kubelet kubeadm kubectl
  • kubelet: The agent that runs on each node and ensures that containers are running in a Pod.
  • kubeadm: A tool for bootstrapping Kubernetes clusters.
  • kubectl: The command-line tool for interacting with the Kubernetes API server.

Pinning Package Versions

To prevent accidental upgrades, it's a good idea to pin the versions of these packages:

sudo apt-mark hold kubelet kubeadm kubectl

This ensures that the packages won't be automatically updated when you run apt upgrade. This can help prevent unexpected issues caused by new versions.

Step 3: Initializing the Kubernetes Cluster (Master Node)

Okay, it's time to initialize the Kubernetes cluster on the master node! This process sets up the control plane, which manages the cluster.

Initializing the Cluster with kubeadm

Run the following command on the master node to initialize the cluster:

sudo kubeadm init --pod-network-cidr=10.244.0.0/16
  • --pod-network-cidr: Specifies the range of IP addresses for Pods. We're using 10.244.0.0/16, which is the default for Calico, a popular networking solution.

This command will take a few minutes to run. When it's finished, it will output a kubeadm join command. Make sure to copy this command, as you'll need it to join the worker nodes to the cluster. It will look something like this:

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

Configuring kubectl

To use kubectl with the cluster, you need to configure it to connect to the API server. Run the following commands:

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

Now you can run kubectl get nodes to see the nodes in your cluster. You should see the master node in the NotReady state, because we haven't installed a pod network yet.

Step 4: Installing a Pod Network

A pod network allows Pods to communicate with each other. We'll use Calico, a popular and powerful networking solution.

Installing Calico

Run the following command to install Calico:

kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml

This command applies the Calico manifest, which creates the necessary resources to run Calico in your cluster. It might take a few minutes for all the Calico pods to become ready.

Verifying the Pod Network

Run kubectl get pods -n kube-system to see the pods in the kube-system namespace. You should see Calico pods running. Once all the Calico pods are in the Running state, run kubectl get nodes again. The master node should now be in the Ready state.

Step 5: Joining Worker Nodes to the Cluster

Now it's time to add the worker nodes to the cluster. Remember that kubeadm join command you copied earlier? You'll need it now. Run that command on each worker node.

Joining the Worker Nodes

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

This command tells the worker node to join the cluster managed by the master node. It might take a few minutes for the worker node to join the cluster.

Verifying the Worker Nodes

Run kubectl get nodes on the master node to see the nodes in your cluster. You should now see the worker nodes in the Ready state.

Step 6: Deploying Your First Application

Congratulations! You now have a working Kubernetes cluster. Let's deploy a simple application to test it out.

Creating a Deployment

We'll deploy a simple Nginx web server. Create a file named nginx-deployment.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

This deployment creates two replicas of the Nginx web server.

Applying the Deployment

Run the following command to apply the deployment:

kubectl apply -f nginx-deployment.yaml

Creating a Service

To access the Nginx web server, we need to create a service. Create a file named nginx-service.yaml with the following content:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: LoadBalancer

Note: If you're running Kubernetes in a cloud environment like AWS, Azure, or GCP, the LoadBalancer type will provision a load balancer for you. If you're running Kubernetes on-premises, you might need to use a different service type, such as NodePort or ClusterIP, and configure an external load balancer separately.

Applying the Service

Run the following command to apply the service:

kubectl apply -f nginx-service.yaml

Accessing the Application

Run kubectl get service nginx-service to get the external IP address of the service. Open your web browser and navigate to that IP address. You should see the default Nginx welcome page.

Conclusion

And there you have it! You've successfully set up a Kubernetes cluster on Ubuntu 24 and deployed your first application. This is just the beginning, of course. There's a whole world of Kubernetes features and concepts to explore. But you've taken the first step, and that's the most important thing. Happy clustering!

Further Exploration

  • Kubernetes Documentation: The official Kubernetes documentation is a great resource for learning more about Kubernetes.
  • Calico Documentation: Learn more about Calico and its features.
  • Kubernetes Tutorials: There are many online tutorials and courses that can help you deepen your understanding of Kubernetes.

Keep experimenting, keep learning, and keep building amazing things with Kubernetes!