Kubernetes Cluster On Ubuntu 20.04: A Comprehensive Guide

by Team 58 views
Kubernetes Cluster on Ubuntu 20.04: A Comprehensive Guide

Hey everyone! Building a Kubernetes cluster on Ubuntu 20.04 can seem like a mountain to climb, but trust me, it's totally doable, and it opens up a whole new world of possibilities for deploying and managing your applications. In this comprehensive guide, we'll break down everything you need to know, from the initial setup to deploying your first containerized application. We'll cover the basics, step-by-step instructions, and even some best practices to ensure your cluster runs smoothly. So, grab a coffee (or your favorite beverage), and let's dive into setting up your very own Kubernetes cluster. We'll explore the crucial steps, covering everything from installing essential tools like kubeadm, kubelet, and kubectl, to configuring the networking and deploying a sample application. Building a Kubernetes cluster on Ubuntu 20.04 is a great way to understand how to containerize and manage your applications efficiently. You'll gain valuable experience in automating deployments, scaling applications, and managing resources effectively. Furthermore, understanding Kubernetes can significantly boost your career prospects, as it's a highly sought-after skill in the tech industry. Whether you're a beginner or have some experience with containerization, this guide is designed to help you succeed. The setup described here uses kubeadm, which is a tool that simplifies the bootstrapping process of a Kubernetes cluster. Kubeadm automates many of the complex configurations, making it easier for you to get up and running quickly. We'll go through each command and configuration step by step, so you can follow along with confidence. We'll also touch on essential topics like networking, security, and monitoring, ensuring that your Kubernetes cluster is robust and production-ready. By the end of this guide, you'll have a fully functional Kubernetes cluster on Ubuntu 20.04, ready to host your containerized applications. Let's make this journey together, starting with the very first steps!

Prerequisites: What You'll Need

Before we jump into the installation process, let's make sure we have everything we need. Setting up a Kubernetes cluster on Ubuntu 20.04 requires a few key elements, so it's best to be prepared. First off, you'll need at least two Ubuntu 20.04 virtual machines (VMs). One will serve as the master node, and the other(s) will be worker nodes. You can use tools like VirtualBox, VMware, or even cloud providers like AWS, Google Cloud, or Azure to create these VMs. Make sure your VMs have at least 2GB of RAM and 2 CPUs. While 1GB RAM might technically work, you'll likely encounter performance issues. It’s always better to be safe than sorry, so aim for at least 2GB. Also, ensure that your VMs have a stable internet connection for downloading the necessary packages. You’ll need a user account with sudo privileges on each VM. This is essential for installing and configuring the various Kubernetes components. Having sudo access allows you to make system-level changes. Next, we have to disable swap memory. Kubernetes doesn’t play well with swap, so it’s best to disable it. It's often recommended to disable swap to improve performance and prevent unexpected behavior. You'll also need to have a container runtime installed, like Docker or containerd, on each node. The container runtime is responsible for running your containerized applications. We’ll show you how to install Docker in the next section. Finally, we must ensure that the time is synchronized across all the nodes. Time synchronization is crucial for the proper functioning of Kubernetes, and slight time discrepancies can lead to issues. You can use ntp or chrony for this purpose. So, make sure you meet all these prerequisites, and you’ll be in a great position to start building your Kubernetes cluster! With these prerequisites in place, we can ensure that your cluster operates smoothly and efficiently. We will cover each of these requirements in detail in the next sections.

Step 1: Setting Up Your Ubuntu 20.04 VMs

Alright, let's get those Ubuntu 20.04 VMs ready! First things first, spin up those VMs. You can do this using your preferred virtualization tool or cloud provider. Make sure you can SSH into each VM. SSH access is vital for interacting with your VMs remotely. This is where you will execute commands to configure and manage your cluster. Now, it's time to update your package lists and upgrade existing packages on each VM. This ensures that you have the latest software versions and security patches. Open a terminal on each VM and run the following commands. Start by updating the package lists:

sudo apt update

Next, upgrade the existing packages:

sudo apt upgrade -y

After upgrading the packages, you'll need to install the necessary packages to allow apt to use packages over HTTPS. This is crucial for installing Kubernetes components from their official repositories. Run the following command:

sudo apt install -y apt-transport-https ca-certificates curl gnupg

With that, let's disable swap, which is essential. Kubernetes doesn’t function well with swap enabled. To disable swap, first, check if swap is enabled by running sudo swapon --show. If swap is enabled, you'll need to disable it. First, turn off the swap:

sudo swapoff -a

Then, edit the /etc/fstab file to comment out the swap entry so it doesn't re-enable on reboot. Open the /etc/fstab file with a text editor like nano or vim:

sudo nano /etc/fstab

Comment out any lines that start with swap. For example, change UUID=... swap swap defaults 0 0 to #UUID=... swap swap defaults 0 0. Save the file and exit the editor. Finally, you have to ensure that the time is synchronized across all your VMs. Time drift can cause issues with Kubernetes, especially with certificates and scheduling. Install and configure ntp or chrony to synchronize time across all your nodes.

sudo apt install -y chrony

After installing, chrony should automatically synchronize the time. You can verify the time sync status by running chronyc tracking. Once you've completed these steps on each VM, your Ubuntu 20.04 VMs are ready for the next phase: installing the container runtime and Kubernetes components.

Step 2: Installing Docker (Container Runtime)

Now, let’s get Docker installed on our Ubuntu 20.04 VMs. Docker is the most popular container runtime and makes it easy to manage containers in your Kubernetes cluster. First, update the apt package index:

sudo apt update

Next, install Docker:

sudo apt install docker.io -y

After the installation is complete, start the Docker service and enable it to start on boot:

sudo systemctl start docker
sudo systemctl enable docker

To ensure that Docker is running correctly, verify its status:

sudo systemctl status docker

You should see an output indicating that the Docker service is active and running. Now, configure Docker to use the cgroupfs driver. This is required for Kubernetes. Create the Docker configuration directory if it doesn't exist:

sudo mkdir -p /etc/docker

Then, create or edit the /etc/docker/daemon.json file. If the file doesn’t exist, create it. Add the following content to the file:

{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "jsonfile",
  "log-opts": {
    "max-size": "10m"
  },
  "storage-driver": "overlay2"
}

Save the file and restart the Docker service for the changes to take effect:

sudo systemctl restart docker

Finally, add your user to the Docker group so you can run Docker commands without using sudo (optional, but recommended):

sudo usermod -aG docker $USER
groupadd docker
newgrp docker

Now, Docker should be set up correctly on your Ubuntu 20.04 VMs. The next step involves installing the Kubernetes components.

Step 3: Installing Kubernetes Components

Alright, let’s get the core Kubernetes components installed. We’ll need kubeadm, kubelet, and kubectl. First, add the Kubernetes repository key. This ensures that you’re downloading packages from a trusted source. Run the following command:

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

Next, add the Kubernetes repository. Create a file /etc/apt/sources.list.d/kubernetes.list and add the following line. This will tell apt where to find the Kubernetes packages:

echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list

Update the package index again to include the Kubernetes repository:

sudo apt update

Now, install kubeadm, kubelet, and kubectl:

sudo apt install -y kubelet kubeadm kubectl

Next, hold the kubelet package to prevent it from being automatically upgraded. This is important because the versions of kubeadm, kubelet, and kubectl should align. Run the following command:

sudo apt-mark hold kubelet

With these steps completed, the necessary Kubernetes components are now installed on your VMs. We're getting closer to setting up our cluster! The next step is to initialize the Kubernetes control plane on your master node.

Step 4: Initializing the Kubernetes Cluster (Master Node)

Okay, time to initialize the Kubernetes cluster on your master node. This involves configuring the control plane components. On your master node, run the following command to initialize the cluster. Replace <YOUR_POD_NETWORK> with a CIDR range for your pod network. This example uses 10.244.0.0/16. This is a crucial step that sets up the core components of the cluster. Important: Choose a pod network CIDR that doesn't conflict with your existing network. It is important to remember this!

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

After running this command, you will see a lot of output, which includes the necessary commands you'll need to join worker nodes to the cluster. This is important to remember! This command sets up the control plane, which manages the Kubernetes cluster. You might see some warnings, but as long as there are no errors, you're good to go. It is recommended to save the output of this command, as it includes important details and instructions for joining worker nodes. Make sure to note any warnings or errors. Once the initialization is complete, you need to configure kubectl so you can connect to your cluster. This allows you to interact with the cluster from your machine. To configure kubectl, run the commands provided in the output of the kubeadm init command. Typically, it involves copying a configuration file to your ~/.kube directory:

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

Next, install a network add-on. Kubernetes requires a networking add-on so that pods can communicate with each other. This is crucial for enabling networking within the cluster. Common choices include Calico, Flannel, and Weave Net. For this guide, we'll use Calico. Apply the Calico manifest to your cluster:

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

After applying the Calico manifest, wait for the pods to become ready. You can check the status of the pods with the command kubectl get pods -n kube-system. The kube-system namespace hosts core Kubernetes components. After all pods are in the Running state, your cluster is almost ready to go! Next, on the master node, you might notice that the nodes are in the NotReady state. This is because the taint on the master node prevents workloads from being scheduled on it. Remove the taint from the master node. This lets you schedule pods on the master node. Run the following command:

kubectl taint nodes <your-master-node-name> node-role.kubernetes.io/master:NoSchedule-

To find your master node name, run kubectl get nodes. With the cluster initialized, kubectl configured, the network add-on applied, and the master node ready, your control plane is now fully set up. Next, we’ll move on to joining worker nodes.

Step 5: Joining Worker Nodes to the Cluster

Alright, let’s get those worker nodes connected to your Kubernetes cluster. On each of your worker nodes, you’ll need to run the kubeadm join command. Remember the output from your kubeadm init command on the master node? That output includes a kubeadm join command that you’ll use here. On each worker node, run the kubeadm join command provided by kubeadm init on the master node. This command will connect your worker nodes to the control plane. Make sure to copy the full command. It usually looks something like this:

sudo kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>

Execute the command on each worker node. This will register the worker nodes with the master node. After the command executes, the worker node will attempt to join the cluster. Give it a moment to complete. This process may take a minute or two. Once the worker nodes have successfully joined the cluster, you can verify their status by running the following command on your master node:

kubectl get nodes

You should see all your nodes listed, and their status should be Ready. If the status is NotReady, wait a few minutes and check again. If the issue persists, check the logs on the worker nodes using journalctl -xef kubelet. After successfully joining the worker nodes, your Kubernetes cluster is almost ready to host your applications. The next step is to deploy a sample application to test your cluster.

Step 6: Deploying a Sample Application

Time to see your Kubernetes cluster in action! Let's deploy a simple