14 Steps to Install kubernetes on Ubuntu 20.04(bento/ubuntu-20.04), 18.04(hashicorp/bionic64)

(Note : - This article has been updated for installing kubernetes on ubuntu 20.04)

In this article on How to Install kubernetes on Ubuntu 20.04 and18.04 we are going to setup Kubernetes cluster along with kubeadm on Ubuntu 20.04 (The same steps can be used for the Ubuntu 18.04 as well as 16.04)

We will start setting up everything from the scratch on virtual machine. Since we are setting up kubernetes cluster, so we will be provisioning two virtual machine - kubernetes master(kmaster) and kubernetes kworker(kworker). The only pre-requisite which you need to do is to install Vagrant and Virtual Box before.

We are going to setup two nodes for kubernetes cluster

  1. Master node
  2. Worker node

Prerequisites

  1. Reading time is about 20 minutes
  2. Vagrant 2.2.15 or latest - For installation instruction click here
  3. VM VirtualBox - For installation instruction click here



Step 1 - Start your vagrant box

As a minimum requirement for kubernetes installation we need -

  1. Master Node - 2 cpus, 2 GB Memory
  2. Worker Node - 1 cpu, 1 GB Memory

Use following Vagrantfile or at least create a Vagrantfile and copy the following configuration into it -

 1Vagrant.configure("2") do |config|
 2  config.vm.define "master" do |master|
 3    master.vm.box_download_insecure = true    
 4    master.vm.box = "bento/ubuntu-20.04"        ## For ubuntu 18.04 use - hashicorp/bionic64
 5    master.vm.network "private_network", ip: "100.0.0.1"
 6    master.vm.hostname = "master"
 7    master.vm.provider "virtualbox" do |v|
 8      v.name = "master"
 9      v.memory = 2048
10      v.cpus = 2
11    end
12  end
13
14  config.vm.define "worker" do |worker|
15    worker.vm.box_download_insecure = true 
16    worker.vm.box = "bento/ubuntu-20.04"        ## For ubuntu 18.04 use - hashicorp/bionic64
17    worker.vm.network "private_network", ip: "100.0.0.2"
18    worker.vm.hostname = "worker"
19    worker.vm.provider "virtualbox" do |v|
20      v.name = "worker"
21      v.memory = 1024
22      v.cpus = 1
23    end
24  end
25
26end


Start you virtual boxes by starting up your vagrant box -

1vagrant up 

**Note - If you are interested in setting the kubernetes cluster on Ubuntu 18.04 then replace worker.vm.box = "bento/ubuntu-20.04" with worker.vm.box = "hashicorp/bionic64" **


Step 2 - Update host files on both master and worker node

After starting the vagrant box now we need to login into the virtual machine using the command vagrant ssh master

master node - SSH into the master node

1vagrant ssh master

Add host entry for master as well as worker node

1sudo vi /etc/hosts
1100.0.0.1 master.jhooq.com master
2100.0.0.2 worker.jhooq.com worker

worker node - SSH into the master node

1vagrant ssh worker

Add host entry for master as well as worker node

1sudo vi /etc/hosts
1100.0.0.1 master.jhooq.com master
2100.0.0.2 worker.jhooq.com worker

Test the worker node by sending from master

1ping worker
1PING worker.jhooq.com (100.0.0.2) 56(84) bytes of data.
264 bytes from worker.jhooq.com (100.0.0.2): icmp_seq=1 ttl=64 time=0.462 ms
364 bytes from worker.jhooq.com (100.0.0.2): icmp_seq=2 ttl=64 time=0.686 ms

Test the master node by sending from worker

1ping master
2PING master.jhooq.com (100.0.0.1) 56(84) bytes of data.
364 bytes from master.jhooq.com (100.0.0.1): icmp_seq=1 ttl=64 time=0.238 ms
464 bytes from master.jhooq.com (100.0.0.1): icmp_seq=2 ttl=64 time=0.510 ms


Step 3 - Install Docker on both master and worker node

You need to install Docker on both the node.

So run the following installation command on both the nodes

1sudo apt-get update
1sudo apt install docker.io

Enable and start docker

1sudo systemctl enable docker
1Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
1sudo systemctl start  docker

Check the docker service status

1sudo systemctl status docker
1● docker.service - Docker Application Container Engine
2   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
3   Active: active (running) since Thu 2020-04-23 19:10:59 UTC; 4s ago
4     Docs: https://docs.docker.com


Step 4 - Disable the firewall and turnoff the "swapping"

We need to disable firewall as well as swapping on master as well as worker node. Because to install kubernetes we need to disable the swapping on both the nodes

1sudo ufw disable
1Firewall stopped and disabled on system startup
1sudo swapoff -a

Step 5 - Install "apt-transport-https" package

To download the kubernetes and its public we need to install "apt-transport-https" package on both master as well as worker node

1sudo apt-get update && sudo apt-get install -y apt-transport-https


Step 6 - Download the public keys

We need to have the public keys for accessing packages on Google Cloud.

So run the following command to get the public keys on both master as well as worker node

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

Step 7 - Add kubernetes repo

As a next step we need to add the kubernetes repo to both master as well as worker node

1sudo bash -c 'echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list'

Step 8 - Install kubernetes

Now after adding the kubernetes repo we need to install the kubernetes on both mater as well as worker node

1sudo apt-get update && sudo apt-get install -y kubelet kubeadm kubectl

Step 9 - Enable and Start kubelet

Alright now we have installed the kubernetes, now we need to enable the kubelet support for both master as well worker node

1sudo systemctl enable kubelet
1sudo systemctl start kubelet

Step 10 - Initialize the kubernetes cluster

Okay now we have reach to point where we have done all the prerequisite for initializing the kubernetes cluster.

Let's run the kubernetes initialization command on only on master

1sudo kubeadm init --apiserver-advertise-address=100.0.0.1

Note down kubeadm join command which we are going to use from worker node to join the master node using token. (Note : - Followig command will be different for you, do not try copy the following command)

1sudo kubeadm join 100.0.0.1:6443 --token g2bsw7.5xr3bqc21eqyc6r7 --discovery-token-ca-cert-hash sha256:39b2b0608b9300b3342a8d0a0e9204c8fc74d45b008043a810f94e4f1fb8861f

Step 11 - Move kube config file to current user (only run on master)

To interact with the kubernetes cluster and to user kubectl command, we need to have the kube config file with us.

Use the following command to get the kube config file and put it under working directory.

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

Step 12 - Apply CNI from kube-flannel.yml(only run on master)

After the master of the cluster is ready to handle jobs and the services are running, for the purpose of making containers accessible to each other through networking, we need to set up the network for container communication.

Get the CNI(container network interface) configuration from flannel

1wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

Step 13 - Join worker nodes to master(only run on worker)

In the Step 10 we generated the token and kubeadm join command.

Now we need to use that join command from our worker node

1sudo kubeadm join 100.0.0.1:6443 --token g2bsw7.5xr3bqc21eqyc6r7     --discovery-token-ca-cert-hash sha256:39b2b0608b9300b3342a8d0a0e9204c8fc74d45b008043a810f94e4f1fb8861f
 1W0423 19:27:00.344480   18268 join.go:346] preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set.
 2preflight] Running pre-flight checks
 3	WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
 4preflight] Reading configuration from the cluster...
 5preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
 6kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.18" ConfigMap in the kube-system namespace
 7kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
 8kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
 9kubelet-start] Starting the kubelet
10kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
11
12This node has joined the cluster:
13* Certificate signing request was sent to apiserver and a response was received.
14* The Kubelet was informed of the new secure connection details.
15
16Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

Step 14 - Check the nodes status(only run on master)

To check the status of the nodes use

1kubectl get nodes
1NAME     STATUS   ROLES    AGE   VERSION
2master   Ready    master   26m   v1.18.2
3worker   Ready    <none>   63s   v1.18.2

(Note - In case you see the status of the nodes not ready then I would recommend to check the these troubleshooting step as well as flannel settings)


Summary

So this was our beginner tutorial which involves around - 14 Steps to Install kubernetes on Ubuntu 18.04 and 16.04.

With that we can conclude what we have achieved -

  • Congratulations we have successfully installed kubernetes on Ubuntu 18.04
  • After installing kubernetes we are successfully join the worker nodes with master.

Troubleshooting kube-flannel.yml

Note - But since we are working on the VMs so we need to check our Ethernet interfaces first.
Look out for the Ethernet i.e. eth1 which has a ip address 100.0.0.1(this is the ip address which we used in vagrant file)

1ip a s
11: lo: <LOOPBACK,UP,LOWER_UP>
22: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
3    link/ether 08:00:27:bb:14:75 brd ff:ff:ff:ff:ff:ff
4    inet 10.0.2.15
53: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
6    link/ether 08:00:27:fb:48:77 brd ff:ff:ff:ff:ff:ff
7    inet 100.0.0.1
84: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP>

Now we need to add the extra args for eth1 in kube-flannel.yml

1vi kube-flannel.yml

Searche for - "flanneld"

In the args section add : - -iface=eth1

1- --iface=eth1
2        args:
3        - --ip-masq
4        - --kube-subnet-mgr
5        - --iface=eth1

Apply the flannel configuration

1kubectl apply -f kube-flannel.yml
 1podsecuritypolicy.policy/psp.flannel.unprivileged created
 2clusterrole.rbac.authorization.k8s.io/flannel created
 3clusterrolebinding.rbac.authorization.k8s.io/flannel created
 4serviceaccount/flannel created
 5configmap/kube-flannel-cfg created
 6daemonset.apps/kube-flannel-ds-amd64 created
 7daemonset.apps/kube-flannel-ds-arm64 created
 8daemonset.apps/kube-flannel-ds-arm created
 9daemonset.apps/kube-flannel-ds-ppc64le created
10daemonset.apps/kube-flannel-ds-s390x created

Troubleshooting Error

How to fix – [ERROR Swap]: running with swap on is not supported. Please disable swap .


Learn more On Kubernetes -

  1. Setup kubernetes on Ubuntu
  2. Setup Kubernetes on CentOs
  3. Setup HA Kubernetes Cluster with Kubespray
  4. Setup HA Kubernetes with Minikube
  5. Setup Kubernetes Dashboard for local kubernetes cluster
  6. Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
  7. How to use Persistent Volume and Persistent Volume Claims in Kubernetes
  8. Deploy Spring Boot Microservice on local Kubernetes cluster
  9. Deploy Spring Boot Microservice on Cloud Platform(GCP)
  10. Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
  11. CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
  12. kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
  13. How to setup kubernetes jenkins pipeline on AWS?
  14. Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
  15. How to fix kubernetes pods getting recreated?
  16. How to delete all kubernetes PODS?
  17. How to use Kubernetes secrets?
  18. Share kubernetes secrets between namespaces?
  19. How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
  20. Delete Kubernetes POD stuck in terminating state?

Posts in this Series