In this tutorial, we will show you how to install Kubernetes on Ubuntu 20.04 LTS. For those of you who didn’t know, Kubernetes, often referred to as K8s, is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It was originally developed by Google and later donated to the Cloud Native Computing Foundation (CNCF). Kubernetes has become the industry standard for managing containerized workloads, offering features like load balancing, self-healing, and automatic scaling.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of Kubernetes on a Ubuntu 20.04 (Focal Fossa) server. You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint.
Prerequisites
- A server running one of the following operating systems: Ubuntu 20.04, 18.04, and any other Debian-based distribution like Linux Mint.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- A network connection or internet access.
- Kubernetes can be resource-intensive, so it’s recommended to have a system with at least 2 CPU cores and 4GB of RAM. For production environments, you’ll need more resources depending on your workload.
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, you can harm your system if you’re not careful when acting as the root.
Install Kubernetes on Ubuntu 20.04 LTS Focal Fossa
Step 1. Before we can proceed with the Kubernetes installation, we need to ensure that our Ubuntu system is up-to-date and has the necessary dependencies installed. Open a terminal and run the following commands:
sudo apt update sudo apt upgrade sudo apt install apt-transport-https curl
These commands will update the package lists and upgrade any outdated packages on your system.
Step 2. Installing Docker.
Kubernetes relies on a container runtime to run and manage containers. In this guide, we’ll be using Docker as the container runtime. Run the following commands to install Docker on your Ubuntu system:
sudo apt install docker.io
Once the Docker is installed ensure that it is enabled to start after reboot:
sudo systemctl enable docker sudo systemctl start docker
You can verify the Docker installation:
docker --version
Step 3. Installing Kubernetes on Ubuntu 20.04.
Now we add the Kubernetes signing key:
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
Next, we’ll add the Kubernetes package repository:
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
After that, run the following command to install Kubernetes:
sudo apt install kubeadm kubelet kubectl kubernetes-cni
Step 4. Disable swap memory.
Running Kubernetes requires that you disable swap:
sudo swapoff -a
To permanently disable swap, comment out or remove the swap line on /etc/fstab
file:
sudo nano /etc/fstab
... # # / was on /dev/sda2 during curtin installation /dev/disk/by-uuid/f3d575c6-e46go-4472-b406-30dmeilana965a / ext4 defaults 0 0 #/swap.img none swap sw 0 0
Step 5. Set hostnames.
Now give hostnames to each node. In our scenario, we’re using the hostnames master-node and master-node to easily differentiate our hosts and identify their roles:
sudo hostnamectl set-hostname master-node sudo hostnamectl set-hostname slave-node
Step 6. Initialize Kubernetes master server.
First, enter the following command on your master node:
sudo kubeadm init
Next, run those three commands on the master node:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
Step 7. Deploy a pod network.
The next step is to deploy a pod network. Run the following two commands on the master node:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel-rbac.yml
You can use the kubectl
command to confirm that everything is up and ready:
kubectl get pods --all-namespaces
Step 8. Join the Kubernetes cluster.
Now our cluster is ready to have the worker nodes join. Use the kubeadm join
command retrieved earlier from the Kubernetes master node initialization output to join your Kubernetes cluster:
sudo kubeadm join 192.168.77.21:6443 --token 1exb8s.2t4k3bramnoamo --discovery-token-ca-cert-hash sha256:72gama4918cf2cute19356c9a402fb609263adad48c13797d0cba2341
Confirm that master-node
is now part of our Kubernetes cluster with this command:
kubectl get nodes
Step 9. Deploying a service on the Kubernetes cluster.
In these steps, we are ready to deploy a service into the Kubernetes cluster. Run the following two commands on your master node:
kubectl run --image=nginx nginx-server --port=80 --env="YOUR-DOMAIN=cluster" kubectl expose deployment nginx-server --port=80 --name=nginx-http
You should now see a new Nginx docker container deployed on your worker node:
sudo docker ps
Finally, you can see a running list of all available services running in your cluster with the following command, issued from the Kubernetes master node:
kubectl get svc
Congratulations! You have successfully installed Kubernetes. Thanks for using this tutorial for installing Kubernetes on Ubuntu 20.04 LTS Focal Fossa systems. For additional help or useful information, we recommend you check the official Kubernetes website.