Set Up A Kubernetes Cluster On Ubuntu 24.04
Hey everyone! 👋 If you're looking to dive into the world of container orchestration with Kubernetes, you've come to the right place! This guide will walk you through setting up a Kubernetes cluster on Ubuntu 24.04, the latest and greatest from Ubuntu. We'll cover everything from the initial setup to deploying your first application. So, grab your favorite beverage, and let's get started!
Prerequisites: What You'll Need Before You Start
Before we jump into the fun stuff, let's make sure you have everything you need. You'll want to have a solid foundation for this setup. Here's what you'll need:
- Ubuntu 24.04 Machines: You'll need at least two Ubuntu 24.04 machines – one for the control plane (master node) and at least one for a worker node. More worker nodes will give you more resources to use. Make sure you have root or sudo access on all machines. You can create virtual machines, use cloud instances (like AWS EC2, Google Compute Engine, or Azure VMs), or even use physical machines. It's totally up to you! The main thing is that they can communicate with each other over the network.
- Network Connectivity: Your machines need to be able to communicate with each other. Make sure there are no firewalls blocking traffic between them, especially on ports used by Kubernetes (like 6443 for the Kubernetes API server, 10250 for kubelet, etc.). It's a good idea to have a private network for your cluster if possible, especially if you're dealing with sensitive data.
- A Package Manager: Ubuntu uses
aptas its package manager. Make sure it's up to date. You will need this to install all the necessary packages. - Sufficient Resources: Each machine should have at least 2 GB of RAM and 2 CPUs. For production environments, you'll want more resources, obviously. More RAM and CPU cores will give you more space to play with your pods. Also, ensure you have enough disk space for container images and application data. SSDs are generally preferred for performance.
- Basic Understanding of Linux: A basic understanding of Linux commands and concepts will be helpful. Knowing how to navigate the command line, edit files, and manage services will make your life much easier.
- Internet Access: Your machines need internet access to download packages and container images. If you are behind a proxy, make sure to configure it correctly before proceeding.
- Container Runtime: Kubernetes needs a container runtime to run your containers. We will use containerd, but Docker is also an option. We'll set this up during the installation process.
Alright, with these prerequisites in place, we're ready to get started. Let's start with the installation of Kubernetes on Ubuntu 24.04!
Step 1: Updating and Configuring Your Ubuntu Machines
First things first, update your system. Before installing anything, it’s always a good idea to update your package lists and upgrade existing packages. This ensures you have the latest versions of everything and that the system is secure. Open a terminal on each of your Ubuntu 24.04 machines and run the following commands:
sudo apt update
sudo apt upgrade -y
The -y flag automatically answers 'yes' to any prompts.
Next, disable swap. Kubernetes doesn't work well with swap enabled, so we need to disable it. Run the following commands on all of your machines:
sudo swapoff -a
sudo sed -i '/ swap / s/^/#/' /etc/fstab
The first command disables swap immediately. The second command comments out the swap entry in /etc/fstab so that swap is disabled on reboot.
Now, configure the hostname. Each machine in your Kubernetes cluster needs a unique hostname. You can set the hostname using the hostnamectl command. Replace your-master-node and your-worker-node with the actual hostnames you want to use for each machine. For the master node:
sudo hostnamectl set-hostname your-master-node
And for the worker node(s):
sudo hostnamectl set-hostname your-worker-node
You might need to reboot your machines for the hostname changes to take effect, but it's not always necessary. After setting the hostnames, it's essential to configure the /etc/hosts file on each machine to map the hostnames to their IP addresses. This helps with internal communication within the cluster. Open the /etc/hosts file with a text editor (like nano or vim):
sudo nano /etc/hosts
Add the IP addresses and hostnames of your machines. For example:
127.0.0.1 localhost
<master_node_ip> your-master-node
<worker_node_ip> your-worker-node
Make sure to replace <master_node_ip> and <worker_node_ip> with the actual IP addresses of your master and worker nodes, respectively. Save the file and close the editor. Now all your machines are correctly updated.
Step 2: Installing Containerd (The Container Runtime)
Kubernetes needs a container runtime to run your containerized applications. Containerd is a lightweight, industry-standard container runtime, and it's what we'll be using in this guide. Run the following commands on all of your machines:
sudo apt install containerd
This command installs containerd. Now, we need to configure containerd to work with Kubernetes. Create a configuration file at /etc/containerd/config.toml:
sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml
Next, edit the configuration file to include the following settings. We're telling containerd to use systemd for cgroup management. Open the file with a text editor:
sudo nano /etc/containerd/config.toml
Find the `[plugins.