install Kubernetes featured image

How to Install and Use Kubernetes on Ubuntu 20.04

Introduction

Kubernetes is an open-source tool that is crucial in container orchestration. Kubernetes works by orchestrating and managing clusters at scale across various cloud environments or even on-premise servers. A cluster is a set of hosts meant for running containerized applications and services. A cluster needs a minimum of two nodes to work – one master node and a worker node. Keeping scalability in mind, you have the option to expand the cluster with as many worker nodes as required.

A node in Kubernetes refers to a server. A master node is a server that manages the state of the cluster. Worker nodes are servers that run the workloads – these are typically containerized applications and services.

This guide will walk you through the steps of installing and deploying a Kubernetes cluster consisting of two nodes on Ubuntu 20.04. As mentioned, having two nodes is the most basic configuration when working with Kubernetes. You also have the option to add more worker nodes once you understand the fundamentals. We will further show you how to link the two servers to allow the master node to control the worker node.

To test our configuration, we will deploy a Docker container running the Nginx webserver to the cluster. This is a typical real-life application of Kubernetes. You will learn more about some defining components of Kubernetes such as kubectl and kubeadm as we go along. It is also advisable to first get acquainted with our tutorial on getting to know Kubernetes tool-kit basics to familiarize yourself with the basics of the Kubernetes platform.

Now, let’s start!

Prerequisites

You will need to provision two servers, running on Ubuntu 20.04. For best performance, the minimum system requirements for Kubernetes are 2GB of RAM and 2 CPUs. You may follow steps 1 to 4 of this step-by-step tutorial to help you set up your Ubuntu server on CloudSigma. One server will be the master node, the other will be the worker node. We have aptly named our two servers as kubernetes-master and kubernetes-worker. This makes it easier to follow along with the tutorial. However, you are free to choose the hostnames you prefer.

  • Ensure you add a user with sudo privileges on both nodes that we will use to run the commands as outlined in the tutorial above. Follow this tutorial on configuring the Linux sudoers file for instructions.

  • Network connectivity – the servers in the cluster should be able to communicate. When you deploy your VMs from CloudSigma, they will be connected to the internet with a public IP by default. If you are working from a local network, you may have to edit your /etc/hosts file in each server and link them appropriately.

  • You will need to install and enable Docker on each of the nodes. Kubernetes relies on a container runtime to run containers in pods. While there are other container platforms to choose from, we will be using Docker in this tutorial. Docker will provide the runtime environment needed by Ubuntu. You may follow steps 1, 2, and 3 of our tutorial on installing and operating Docker.

Step 1: Install Kubernetes

In this step, we will be installing Kubernetes. Just like you did with Docker in the prerequisites, you must run the commands in both nodes to install Kubernetes. Use ssh to login into both nodes and proceed. You will start by installing the apt-transport-https package which enables working with http and https in Ubuntu’s repositories. Also, install curl as it will be necessary for the next steps. Execute the following command:

Then, add the Kubernetes signing key to both nodes by executing the command:

Next, we add the Kubernetes repository as a package source on both nodes using the following command:

After that, update the nodes:

  • Install Kubernetes tools

Once the update completes, we will install Kubernetes. This involves installing the various tools that make up Kubernetes: kubeadm, kubelet, kubectl, and kubernetes-cni. These tools are installed on both nodes. We define each tool below:

  • kubelet – an agent that runs on each node and handles communication with the master node to initiate workloads in the container runtime. Enter the following command to install kubelet:

  • kubeadm – part of the Kubernetes project and helps initialize a Kubernetes cluster. Enter the following command to install the kubeadm:

  • kubectl – the Kubernetes command-line tool that allows you to run commands inside the Kubernetes clusters. Execute the following command to install kubectl:

  • kubernetes-cni – enables networking within the containers ensuring containers can communicate and exchange data. Execute the following command to install:

Optionally, you can install all four in a single command:

Step 2: Disabling Swap Memory

Kubernetes fails to function in a system that is using swap memory. Hence, it must be disabled in the master node and all worker nodes. Execute the following command to disable swap memory:

This command disables swap memory until the system is rebooted. We have to ensure that it remains off even after reboots. This has to be done on the master and all worker nodes. We can do this by editing the fstab file and commenting out the /swapfile line with a #. Open the file with the nano text editor by entering the following command:

Inside the file, comment out the swapfile line as shown in the screenshot below:

install Kubernetes fstab swap disable

If you do not see the swapfile line, just ignore it. Save and close the file when you are done editing. Follow the same process for both nodes. Now, swap memory settings will remain off, even after your server reboots.

Step 3: Setting Unique Hostnames

Your nodes must have unique hostnames for easier identification. If you are deploying a cluster with many nodes, you can set it to identify names for your worker nodes such as node-1, node-2, etc. As we had mentioned earlier, we have named our nodes as kubernetes-master and kubernetes-worker. We have set them at the time of creating the server. However, you can adjust or set yours if you had not already done so from the command line. To adjust the hostname on the master node, run the following command:

On the worker node, run the following command:

You may close the current terminal session and ssh back into the server to see the changes.

Step 4: Letting Iptables See Bridged Traffic

For the master and worker nodes to correctly see bridged traffic, you should ensure net.bridge.bridge-nf-call-iptables is set to 1 in your config. First, ensure the br_netfilter module is loaded. You can confirm this by issuing the command:

Optionally, you can explicitly load it with the command:

Now, you can run this command to set the value to 1:

Step 5: Changing Docker Cgroup Driver

By default, Docker installs with cgroupfs as the cgroup driver. Kubernetes recommends that Docker should run with systemd as the driver. If you skip this step and try to initialize the kubeadm in the next step, you will get the following warning in your terminal:

On both master and worker nodes, update the cgroupdriver with the following commands:

Then, execute the following commands to restart and enable Docker on system boot-up:

Once that is set, we can proceed to the fun stuff, deploying the Kubernetes cluster!

Step 6: Initializing the Kubernetes Master Node

The first step in deploying a Kubernetes cluster is to fire up the master node. While on the terminal of your master node, execute the following command to initialize the kubernetes-master:

If you execute the above command and your system doesn’t match the expected requirements, such as minimum RAM or CPU as explained in the Prerequisites section, you will get a warning and the cluster will not start:

install Kubernetes InitError

Note: If you are building for production, it’s a good idea to always meet the minimum requirements for Kubernetes to run smoothly. However, if you are doing this tutorial for learning purposes, then you can add the following flag to the kubeadm init command to ignore the error warnings:
sudo kubeadm init --ignore-preflight-errors=NumCPU,Mem --pod-network-cidr=10.244.0.0/16

The screenshot below shows that the initialization was successful. We have also added a flag to specify the pod network with the IP 10.244.0.0, It’s the default IP that the kube-flannel uses. We will discuss more on the pod network in the next step.

install Kubernetes Kubeadm Init

In the output, you can see the kubeadm join command (we’ve hidden our IP address) and a unique token that you will run on the worker node and all other worker nodes that you want to join onto this cluster. Next, copy-paste this command as you will use it later in the worker node.

In the output, Kubernetes also displays some additional commands that you should run as a regular user on the master node before you start to use the cluster. Let’s run these commands:

We have now initialized the master node. However, we also have to set up the pod network on the master node before we join the worker nodes.

Step 7: Deploying a Pod Network

A pod network facilitates communication between servers and it’s necessary for the proper functioning of the Kubernetes cluster. You can read more about Kubernetes Cluster Networking from the official docs. We will be using the Flannel pod network for this tutorial. Flannel is a simple overlay network that satisfies the Kubernetes requirements.

Before we deploy the pod network, we need to check on the firewall status. If you have enabled the firewall after following step 5 of the tutorial on setting up your Ubuntu server, you must first add a firewall rule to create exceptions for port 6443 (the default port for Kubernetes). Run the following ufw commands on both master and worker nodes:

After that, you can run the following two commands to deploy the pod network on the master node:

This may take a couple of seconds to a minute depending on your environment to load up the flannel network. Run the following command to confirm that everything is fired up:

The output of the command should show all services status as running if everything was successful:

install Kubernetes Pod Status

You can also view the health of the components using the get component status command:

install Kubernetes Component Status

This command has a short form cs:

Component Status Short

If you see the unhealthy status, modify the following files and delete the line at (spec->containers->command) containing this phrase - --port=0 :

Do the same for this file:

Finally, restart the Kubernetes service:

Step 8: Joining Worker Nodes to the Kubernetes Cluster

With the kubernetes-master node up and the pod network ready, we can join our worker nodes to the cluster. In this tutorial, we only have one worker node, so we will be working with that. If you have more worker nodes, you can always follow the same steps as we will explain below to join the cluster.

First, log into your worker node on a separate terminal session. You will use your kubeadm join command that was shown in your terminal when we initialized the master node in Step 6. Execute the command:

You should see similar output like the screenshot below when it completes joining the cluster:

Worker Join

Once the joining process completes, switch the master node terminal and execute the following command to confirm that your worker node has joined the cluster:

In the screenshot from the output of the command above, we can see that the worker node has joined the cluster:

install Kubernetes K8S Node Status

Step 9: Deploying an Application to the Kubernetes Cluster

At this point, you have successfully set up a Kubernetes cluster. Let’s make the cluster usable by deploying a service to it. Nginx is a popular web server boasting incredible speeds even with thousands of connections. We will deploy the Nginx webserver to the cluster to prove that you can use this setup in a real-life application.

Execute the following command on the master node to create a Kubernetes deployment for Nginx:

You can view the created deployment by using the describe deployment command:

Nginx Deployment

To make the nginx service accessible via the internet, run the following command:

NodePort Svc Create

The command above will create a public-facing service for the Nginx deployment. This being a nodeport deployment, Kubernetes assigns the service a port in the range of 32000+.

You can get the current services by issuing the command:

NodePort Svc Status

You can see that our assigned port is 32264. Take note of the port displayed in your terminal to use in the next step.

To verify that the Nginx service deployment is successful, issue a curl call to the worker node from the master. Replace your worker node IP and the port you got from the above command:

You should see the output of the default Nginx index.html:

Curl Nginx Svc

Optionally, you can visit the worker node IP address and port combination in your browser and view the default Nginx index page:

install Kubernetes Nginx Webpage

You can delete a deployment by specifying the name of the deployment. For example, this command will delete our deployment:

We have now successfully tested our cluster!

Conclusion

In this tutorial, you have learned how to install a Kubernetes cluster on Ubuntu 20.04. You set up a cluster consisting of a master and worker node. You were able to install the Kubernetes toolset, created a pod network, and joined the worker node to the master node. We also tested our concept by doing a basic deployment of an Nginx webserver to the cluster. This should work as a foundation to working with Kubernetes clusters on Ubuntu.

While we only used one worker node, you can extend your cluster with as many nodes as you wish. If you would like to get deeper into DevOps with automation tools like Ansible, we have a tutorial that delves into provisioning Kubernetes cluster deployments with Ansible and Kubeadm, check it out. If you want to learn how to deploy a PHP application on a Kubernetes cluster check this tutorial.

Happy Computing!