How To Install and Use Kubernetes On Ubuntu 20.04 - CloudSigma
How To Install and Use Kubernetes On Ubuntu 20.04 - CloudSigma
(HTTPS://WWW.CLOUDSIGMA.COM/)
Introduction
Kubernetes (https://kubernetes.io/) is an open-source tool that is crucial in container
orchestration (https://docs.docker.com/get-started/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 (https://www.cloudsigma.com/how-to-install-
operate-docker-on-ubuntu-in-the-public-cloud/).
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
con�guration 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.
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
(https://www.cloudsigma.com/how-to-set-up-your-ubuntu-18-04-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 con�guring the Linux
sudoers �le for instructions (https://www.cloudsigma.com/con�guring-the-linux-sudoers-
�le/).
• Network connectivity – the servers in the cluster should be able to communicate. When
you deploy your VMs from CloudSigma (https://zrh.cloudsigma.com/ui/4.0/login), 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 (https://linuxize.com/post/how-to-edit-
your-hosts-�le/) �le 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 (https://www.cloudsigma.com/how-to-install-operate-
docker-on-ubuntu-in-the-public-cloud/).
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:
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 de�ne 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:
1 sudo swapoff -a
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 �le with the nano text editor by entering the following command:
Inside the �le, comment out the swapfile line as shown in the screenshot below:
If you do not see the swap�le line, just ignore it. Save and close the �le when you are done
editing. Follow the same process for both nodes. Now, swap memory settings will remain off,
even after your server reboots.
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:
You may close the current terminal session and ssh back into the server to see the changes.
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 �rst step in deploying a Kubernetes cluster is to �re 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:
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 �ag to the kubeadm init command to
ignore the error warnings:
cidr=10.244.0.0/16
The screenshot below shows that the initialization was successful. We have also added a �ag
to specify the pod network with the IP 10.244.0.0, It’s the default IP that the kube-�annel
uses (https://blog.laputa.io/kubernetes-�annel-networking-6a1cb1f8ec7c). We will discuss
more on the pod network in the next step.
In the output, you can see the command (we’ve hidden our IP address) and a
kubeadm join
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:
1 mkdir -p $HOME/.kube
2 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
3 sudo chown $(id -u):$(id -g) $HOME/.kube/config
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.
Before we deploy the pod network, we need to check on the �rewall status. If you have
enabled the �rewall after following step 5 of the tutorial on setting up your Ubuntu server
(https://www.cloudsigma.com/how-to-set-up-your-ubuntu-18-04-server/), you must �rst add
a firewall rule to create exceptions for port 6443 (the default port for Kubernetes). Run
the following ufw (https://wiki.ubuntu.com/UncomplicatedFirewall) 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
�annel network. Run the following command to con�rm that everything is �red up:
The output of the command should show all services status as running if everything was
successful:
You can also view the health of the components using the get component status command:
1 kubectl get cs
If you see the unhealthy status, modify the following �les and delete the line at
(spec->containers->command) containing this phrase - --port=0 :
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:
Once the joining process completes, switch the master node terminal and execute the
following command to con�rm 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:
Execute the following command on the master node to create a Kubernetes deployment
(https://kubernetes.io/docs/concepts/workloads/controllers/deployment/) for Nginx:
You can view the created deployment by using the describe deployment command:
To make the nginx service accessible via the internet, run the following command:
The command above will create a public-facing service for the Nginx deployment. This being a
nodeport (https://kubernetes.io/docs/concepts/services-networking/service/) deployment,
Kubernetes assigns the service a port in the range of 32000+ .
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:
1 curl your-kubernetes-worker-ip:32264
Optionally, you can visit the worker node IP address and port combination in your browser
and view the default Nginx index page:
You can delete a deployment by specifying the name of the deployment. For example, this
command will delete our deployment:
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
(https://www.ansible.com/), we have a tutorial that delves into provisioning Kubernetes
cluster deployments with Ansible and Kubeadm (https://www.cloudsigma.com/how-to-
create-a-kubernetes-cluster-using-kubeadm-on-ubuntu-18-04/), check it out. If you want to
learn how to deploy a PHP application on a Kubernetes cluster check this tutorial
(https://www.cloudsigma.com/deploy-a-php-application-on-a-kubernetes-cluster-with-
ubuntu-18-04/).
Happy Computing!
About Latest
CLOUD (HTTPS://WWW.CLOUDSIGMA.COM/TAG/CLOUD/)
CONTAINERIZATION (HTTPS://WWW.CLOUDSIGMA.COM/TAG/CONTAINERIZATION/)
CONTAINERS (HTTPS://WWW.CLOUDSIGMA.COM/TAG/CONTAINERS/)
DEVOPS (HTTPS://WWW.CLOUDSIGMA.COM/TAG/DEVOPS/)
DOCKER (HTTPS://WWW.CLOUDSIGMA.COM/TAG/DOCKER/)
KUBERNETES (HTTPS://WWW.CLOUDSIGMA.COM/TAG/KUBERNETES/)
TUTORIAL (HTTPS://WWW.CLOUDSIGMA.COM/TAG/TUTORIAL/)
UBUNTU (HTTPS://WWW.CLOUDSIGMA.COM/TAG/UBUNTU/)
H O M E ( H T T P S : // W W W. C LO U D S I G M A .C O M / )
L E G A L ( H T T P S : / / W W W . C L O U D S I G M A . C O M / L E G A L- S W I T Z E R L A N D / )
F E AT U R E S ( H T T P S : // W W W. C LO U D S I G M A .C O M / F E AT U R E S / )
I A A S P R I C I N G ( H T T P S : // W W W. C LO U D S I G M A .C O M / P R I C I N G / )
A B O U T C LO U D S I G M A ( H T T P S : // W W W. C LO U D S I G M A .C O M /A B O U T/ )
LO C AT I O N S ( H T T P S : // W W W. C LO U D S I G M A .C O M / C LO U D - LO C AT I O N S / )
PA R T N E R S ( H T T P S : // W W W. C LO U D S I G M A . C O M / C LO U D - H O S T I N G - PA R T N E R - P R O G R A M / )
S TAT U S ( H T T P : // S TAT U S .C LO U D S I G M A . C O M )
C L O U D T U TO R I A L S ( H T T P S : // W W W. C L O U D S I G M A . C O M / C O M M U N I T Y/ T U TO R I A L S / )
Q U E S T I O N S ( H T T P S : // W W W. C L O U D S I G M A . C O M / C O M M U N I T Y/ Q U E S T I O N S - A N D - A N S W E R S / )
B LO G ( H T T P S : // W W W. C LO U D S I G M A .C O M / B LO G / )
(https://www.facebook.com/CloudSigma)
(https://twitter.com/CloudSigma)
(https://www.linkedin.com/company/cloudsigma-ag)
(https://www.youtube.com/cloudsigma)
(https://www.instagram.com/cloudsigmahq/)
(https://www.cloudsigma.com/feed/)
( h t t p s : //m a r ke t p l a c e . i n t e l . c o m /s /p a r t n e r /a 5 S 3 b 0 0 0 0 0 0 2 k E 0 E A I /c l o u d s i g m a ? l a n g u a g e = e n _ U S &
wapkw=cloudsigma) ( h t t p s : // w w w. c l o u d s i g m a . c o m /c l o u d s i g m a - w i t h -
hpe-delivers-innovative-cloud-services-around-the-globe/)
( h t t p s : // w w w. c l o u d s i g m a . c o m / i s o - 2 7 0 0 1 - i n f o r m a t i o n - s e c u r i t y - c e r t i f i e d - c l o u d / )
( h t t p s : // w w w. c l o u d s i g m a . c o m / i s o - 2 7 0 1 7 - c l o u d - s e c u r i t y - c e r t i f i e d - c l o u d / )
( h t t p s : // w w w. c l o u d s i g m a . c o m / i s o - 2 7 0 1 8 - p r i v a c y - p r o t e c t i o n - c e r t i f i e d - c l o u d / )
( h t t p s : // w w w. c l o u d s i g m a . c o m /g e a n t - p r e f e r r e d - c l o u d - p a r t n e r // )
( h t t p s : // w w w. c l o u d s i g m a . c o m /e u - g d p r - c o m p l i a n t - c l o u d / )
( h t t p s : // w w w. c l o u d s i g m a . c o m /p c i - d s s - c o m p l i a n t - c l o u d / ) ( h t t p s : // w w w. c l o u d s i g m a . c o m
/s t a r - l eve l - 1 - r e g i s t e r e d - p u b l i c - c l o u d / ) ( h t t p s : // w w w. c l o u d s i g m a . c o m /e b a -
recommendations-compliant-cloud/)
First Name
Email address:
© 2022 CloudSigma AG