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

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

So, you want to dive into the world of Kubernetes and set up your own cluster on Ubuntu 20.04? Awesome! You've come to the right place. This guide will walk you through the entire process, step-by-step, making it easy to follow even if you're relatively new to Kubernetes. We'll cover everything from installing the necessary components to configuring your cluster and deploying your first application. Let's get started!

Prerequisites

Before we jump into the installation, let's make sure you have everything you need:

  • Ubuntu 20.04 Servers: You'll need at least two Ubuntu 20.04 servers. One will act as the master node, and the other will be a worker node. For a production environment, you'll likely want more worker nodes for redundancy and scalability.
  • Sudo Privileges: Make sure you have sudo privileges on all the servers. This will allow you to install and configure the necessary software.
  • Internet Connection: An internet connection is required to download the necessary packages.
  • Basic Linux Knowledge: A basic understanding of Linux commands and concepts will be helpful.

Step 1: Install Container Runtime (Docker)

Kubernetes needs a container runtime to run your applications. Docker is a popular choice, and we'll use it in this guide.

First, update your package index:

sudo apt update

Next, install Docker:

sudo apt install docker.io -y

Verify that Docker is installed and running:

sudo systemctl status docker

If Docker is not running, start it:

sudo systemctl start docker

Enable Docker to start on boot:

sudo systemctl enable docker

Step 2: Install kubeadm, kubelet, and kubectl

Now, let's install the Kubernetes components: kubeadm, kubelet, and kubectl. These tools are essential for creating and managing your Kubernetes cluster.

First, download the Google Cloud public signing key:

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

Add the Kubernetes apt repository:

cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

Update the package index again:

sudo apt update

Install kubeadm, kubelet, and kubectl:

sudo apt install -y kubelet kubeadm kubectl

Hold the packages to prevent accidental upgrades:

sudo apt-mark hold kubelet kubeadm kubectl

Repeat these steps on all your servers (master and worker nodes).

Step 3: Initialize the Kubernetes Cluster (Master Node)

Now, it's time to initialize the Kubernetes cluster on the master node. This process sets up the control plane components.

Run the following command:

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

Important: Take note of the kubeadm join command that is printed at the end of the output. You'll need this command to join the worker nodes to the cluster.

Configure kubectl to work with your cluster. Run these commands as your regular user (not as root):

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

Step 4: Install a Pod Network Add-on

Kubernetes requires a pod network add-on to enable communication between pods. We'll use Calico in this guide, but you can choose other options like Flannel or Weave Net.

Apply the Calico manifest:

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

Wait for the pods to be in the Running state. You can check the status with this command:

kubectl get pods --all-namespaces

Step 5: Join Worker Nodes to the Cluster

Now, let's join the worker nodes to the cluster. On each worker node, run the kubeadm join command that you noted down in Step 3.

It should look something like this:

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

If you don't have the kubeadm join command, you can get it on the master node by running:

sudo kubeadm token create --print-join-command

Step 6: Verify the Cluster

On the master node, verify that the worker nodes have joined the cluster:

kubectl get nodes

You should see your master node and worker nodes listed, with their status as Ready.

Step 7: Deploy a Sample Application

Let's deploy a simple application to test our cluster. We'll use the nginx image.

Create a deployment:

kubectl create deployment nginx --image=nginx

Expose the deployment as a service:

kubectl expose deployment nginx --port=80 --type=NodePort

Get the service details:

kubectl get service nginx

Find the NodePort value (e.g., 30000). You can access the nginx application by opening your browser and navigating to http://<worker-node-ip>:<nodeport>. You should see the default Nginx welcome page.

Diving Deeper: Understanding Each Step in Detail

Let's explore each step we've taken to build our Kubernetes cluster on Ubuntu 20.04, providing a more in-depth explanation of the commands and configurations involved. This will not only solidify your understanding but also equip you with the knowledge to troubleshoot potential issues and customize your cluster further.

Prerequisites: Setting the Stage for Success

Before you even think about installing Kubernetes, laying the right foundation is crucial. Imagine building a house on shaky ground – it's not going to end well, right? Similarly, having the correct prerequisites in place will save you from headaches down the line. We emphasized the need for at least two Ubuntu 20.04 servers. Why Ubuntu 20.04? It's a stable and widely supported distribution, making it an excellent choice for Kubernetes. Why at least two servers? Because one will be the master node, the brain of the operation, and the other will be a worker node, where your applications actually run. Think of it as the headquarters and the factory floor. Sudo privileges are a must because you'll be installing software and making system-level changes. Without sudo, you're basically asking for permission to do something, and the answer will always be no. An internet connection is also essential because we'll be downloading packages from various repositories. And finally, a basic understanding of Linux is like knowing the alphabet before writing a novel. You don't need to be a Linux guru, but knowing your way around the command line will be incredibly helpful.

Installing the Container Runtime (Docker): The Engine That Drives Your Apps

Kubernetes doesn't directly run your applications. Instead, it relies on a container runtime to do the heavy lifting. Think of the container runtime as the engine of your car. Kubernetes provides the steering wheel and navigation, but the engine is what actually makes the car move. Docker is a popular container runtime, and it's what we used in this guide. We started by updating the package index using sudo apt update. This is like checking for updates on your phone – it ensures you have the latest information about available software. Then, we installed Docker using sudo apt install docker.io -y. The -y flag automatically answers 'yes' to any prompts, saving you from having to manually confirm the installation. After installation, we verified that Docker was running using sudo systemctl status docker. Systemctl is the systemd control manager, and it's used to manage system services like Docker. If Docker wasn't running, we started it using sudo systemctl start docker. Finally, we enabled Docker to start on boot using sudo systemctl enable docker. This ensures that Docker will automatically start whenever your server restarts, preventing your applications from going offline.

Installing kubeadm, kubelet, and kubectl: The Kubernetes Toolkit

These three components are the core of your Kubernetes toolkit. kubeadm is like the construction foreman – it's used to bootstrap your Kubernetes cluster. It handles all the complex configuration behind the scenes. kubelet is like the construction worker – it's an agent that runs on each node and manages the containers running on that node. It listens to instructions from the master node and ensures that the containers are running as they should. kubectl is like the project manager – it's a command-line tool that allows you to interact with your Kubernetes cluster. You can use it to deploy applications, manage resources, and monitor the health of your cluster. We added the Kubernetes apt repository to our system using curl and sudo tee. This tells our system where to find the Kubernetes packages. Then, we updated the package index again and installed the three components using sudo apt install -y kubelet kubeadm kubectl. Finally, we held the packages to prevent accidental upgrades using sudo apt-mark hold kubelet kubeadm kubectl. This is important because upgrading these components without careful planning can break your cluster.

Initializing the Kubernetes Cluster (Master Node): Laying the Foundation

This step is where the magic really begins. We initialized the Kubernetes cluster on the master node using 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 the pods in your cluster. Pods are the smallest deployable units in Kubernetes, and they need IP addresses to communicate with each other. The output of this command includes a kubeadm join command, which we used later to join the worker nodes to the cluster. It's crucial to save this command because you'll need it. We then configured kubectl to work with our cluster by copying the /etc/kubernetes/admin.conf file to our home directory and changing the ownership. This file contains the credentials that kubectl needs to authenticate with the Kubernetes API server. Without this configuration, kubectl wouldn't be able to talk to your cluster.

Installing a Pod Network Add-on: Connecting the Dots

Kubernetes needs a way to connect the pods running on different nodes. This is where pod network add-ons come in. Think of them as the plumbing system of your cluster – they allow pods to communicate with each other regardless of which node they're running on. We used Calico in this guide, but other popular options include Flannel and Weave Net. We applied the Calico manifest using kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml. A manifest is a YAML file that describes the desired state of your Kubernetes resources. In this case, the Calico manifest defines the resources needed to install and configure Calico. We then waited for the pods to be in the Running state. This ensures that Calico is properly installed and configured.

Joining Worker Nodes to the Cluster: Expanding the Team

This step is where we added the worker nodes to our cluster. We ran the kubeadm join command on each worker node, using the command that we saved earlier. This command tells the worker node to connect to the master node and join the cluster. The master node then authenticates the worker node and allows it to join. If you lost the kubeadm join command, you can regenerate it on the master node using sudo kubeadm token create --print-join-command.

Verifying the Cluster: Making Sure Everyone's On Board

After joining the worker nodes, we verified that they were successfully added to the cluster using kubectl get nodes. This command lists all the nodes in your cluster, along with their status. The status should be Ready for all nodes, indicating that they are healthy and able to run pods. If a node is not in the Ready state, there may be an issue with the node or its connection to the master node.

Deploying a Sample Application: Putting It All to the Test

Finally, we deployed a simple application to test our cluster. We used the nginx image, which is a popular web server. We created a deployment using kubectl create deployment nginx --image=nginx. A deployment is a declarative way to manage your applications in Kubernetes. It tells Kubernetes how many replicas of your application to run and how to update them. We then exposed the deployment as a service using kubectl expose deployment nginx --port=80 --type=NodePort. A service is an abstraction that provides a stable IP address and port for your application. The NodePort type exposes the service on a specific port on each node in the cluster. We then got the service details using kubectl get service nginx and found the NodePort value. We could then access the nginx application by opening our browser and navigating to http://<worker-node-ip>:<nodeport>. If you saw the default Nginx welcome page, congratulations! You have successfully deployed an application to your Kubernetes cluster.

Conclusion

And there you have it! You've successfully created a Kubernetes cluster on Ubuntu 20.04. This is just the beginning of your Kubernetes journey. There's much more to learn, but you now have a solid foundation to build upon. Experiment with different configurations, deploy more complex applications, and explore the vast ecosystem of Kubernetes tools and resources. The possibilities are endless! Keep exploring, keep learning, and have fun!