LinuxTutorialsUbuntu

How To Install Kubernetes on Ubuntu 20.04 LTS

Install Kubernetes on Ubuntu 20.04

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, the popular open-source container orchestration platform, has revolutionized the way applications are deployed and managed in the cloud-native era. Its powerful features, such as automated scaling, self-healing, and efficient resource utilization, have made it an essential tool for DevOps teams worldwide.

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 to download the Avidemux package.
  • A non-root sudo user or access to the root user. We recommend acting as a non-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. To begin, update and upgrade the Ubuntu packages on all the nodes using the following commands:

sudo apt update
sudo apt upgrade
sudo apt install apt-transport-https curl

Step 2. Installing Docker.

Follow the command to install Docker CE on Ubuntu 20.04:

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.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button