Kubernetes On Ubuntu 20.04: A Step-by-Step Guide

by Team 49 views
Kubernetes on Ubuntu 20.04: A Step-by-Step Guide

So, you want to dive into the world of Kubernetes on Ubuntu 20.04? Awesome! You've come to the right place. This guide will walk you through setting up a Kubernetes cluster on Ubuntu 20.04, making it super easy, even if you're relatively new to this whole container orchestration thing. We'll break it down into manageable steps, ensuring you understand each part of the process. Get ready to unleash the power of Kubernetes! Let's get started, guys!

Prerequisites

Before we jump in, let's make sure you have everything you need. Think of this as gathering your ingredients before you start cooking up a Kubernetes masterpiece.

  • Ubuntu 20.04 Servers: You'll need at least two Ubuntu 20.04 servers. One will act as the master node, and the other(s) will be worker nodes. For testing, you can use virtual machines. Ensure each server has a unique hostname and static IP address.
  • SSH Access: Make sure you can SSH into each of your servers. This will be your primary way of interacting with them.
  • Sudo Privileges: The user you're using to SSH into the servers should have sudo privileges. This allows you to run commands with administrative rights.
  • Internet Connection: Your servers will need an internet connection to download the necessary packages.
  • Basic Linux Knowledge: A basic understanding of Linux commands will be helpful.

Having these prerequisites in place will ensure a smooth and successful Kubernetes installation.

Step 1: Update and Upgrade Your Servers

First things first, let's ensure our Ubuntu servers are up-to-date. This is like warming up before a workout – it prepares your system for the tasks ahead. Open your terminal and SSH into each of your Ubuntu servers. Then, run the following commands:

sudo apt update
sudo apt upgrade -y

The sudo apt update command refreshes the package lists, ensuring you have the latest information about available packages. The sudo apt upgrade -y command then upgrades all installed packages to their newest versions. The -y flag automatically answers "yes" to any prompts, making the process non-interactive. This step is crucial for ensuring you have the latest security patches and bug fixes.

Step 2: Install Container Runtime (Docker)

Kubernetes needs a container runtime to run your containers. Docker is a popular choice, and we'll use it in this guide. Let's install Docker on all your servers. Run these commands:

sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker

The sudo apt install docker.io -y command installs Docker from the Ubuntu repositories. The -y flag again automatically answers "yes" to any prompts. The sudo systemctl start docker command starts the Docker service, and the sudo systemctl enable docker command ensures that Docker starts automatically on boot. After running these commands, verify that Docker is running correctly by running:

sudo docker run hello-world

If everything is set up correctly, you should see a message indicating that Docker successfully ran the hello-world image.

Step 3: Install Kubernetes Components (kubeadm, kubelet, kubectl)

Now, let's install the Kubernetes components: kubeadm, kubelet, and kubectl. kubeadm is a tool for bootstrapping Kubernetes clusters. kubelet is the agent that runs on each node in the cluster. kubectl is the command-line tool for interacting with the cluster. Run these commands on all your servers:

sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl
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
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

Let's break down these commands:

  • sudo apt update: Refreshes the package lists.
  • sudo apt install -y apt-transport-https ca-certificates curl: Installs necessary packages for securely accessing HTTPS repositories.
  • curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -: Adds the Kubernetes repository key to your system. This allows you to verify the authenticity of the packages you're downloading.
  • echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list: Adds the Kubernetes repository to your system's package sources.
  • sudo apt install -y kubelet kubeadm kubectl: Installs the kubelet, kubeadm, and kubectl packages.
  • sudo apt-mark hold kubelet kubeadm kubectl: Prevents these packages from being automatically updated. This is important because Kubernetes version compatibility is crucial.

Step 4: Initialize the Kubernetes Cluster (Master Node)

Now comes the exciting part: initializing the Kubernetes cluster on the master node. SSH into your designated master node and 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 that will be used for pods in the cluster. 10.244.0.0/16 is a common choice. After running this command, you'll see a lot of output. Pay close attention to the last few lines, as they contain important instructions for setting up kubectl and joining worker nodes to the cluster. It will look something like this:

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

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

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/networking/

Then you can join any number of worker nodes by running the following on each as root:

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

Follow the instructions to set up kubectl:

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

These commands copy the Kubernetes configuration file to your user's home directory and set the correct permissions. Now you can use kubectl to interact with the cluster.

Step 5: Deploy a Pod Network

A pod network allows pods to communicate with each other. We'll use Calico, a popular and easy-to-set-up pod network. 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 may take a few minutes for the network to be fully initialized. You can check the status of the pods by running:

kubectl get pods --all-namespaces

Wait until all Calico pods are in the Running state.

Step 6: Join Worker Nodes to the Cluster

Now it's time to add the worker nodes to the cluster. SSH into each of your worker nodes and run the kubeadm join command that was outputted during the kubeadm init step on the master node. It should look something like this:

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

Replace <master-node-ip>, <token>, and <hash> with the actual values from the kubeadm init output. After running this command on each worker node, they will join the cluster.

Step 7: Verify the Cluster

Finally, let's verify that the cluster is set up correctly. SSH into the master node and run the following command:

kubectl get nodes

You should see a list of all the nodes in the cluster, including the master node and all the worker nodes. The status of each node should be Ready. If all nodes are in the Ready state, congratulations! You've successfully set up a Kubernetes cluster on Ubuntu 20.04.

Troubleshooting

Sometimes, things don't go as planned. Here are some common issues and how to troubleshoot them:

  • Nodes Not Joining: Ensure that the kubeadm join command is run with the correct token and discovery token CA cert hash. Also, make sure that the worker nodes can communicate with the master node on port 6443.
  • Pods Not Starting: Check the pod logs for errors. You can use the kubectl logs <pod-name> -n <namespace> command to view the logs.
  • Network Issues: Ensure that the pod network is correctly configured. Check the status of the Calico pods using kubectl get pods --all-namespaces.
  • Firewall Issues: Ensure that the firewall is not blocking communication between the nodes. You may need to open ports 6443, 2379-2380, and 10250-10252 on the master node.

Conclusion

Setting up a Kubernetes cluster on Ubuntu 20.04 might seem daunting at first, but by following these steps, you can get it up and running in no time. Remember to pay attention to the details and troubleshoot any issues that arise. With your own Kubernetes cluster, you can now deploy and manage your containerized applications with ease. So, go ahead and explore the world of Kubernetes! Have fun, and happy deploying, guys! This comprehensive guide should give you a solid foundation for your Kubernetes journey. Good luck!