How to Install Kubernetes on Ubuntu 20.04 (Focal Fossa) part 2
--
In this blog post we will focus on installing Docker and Kubernetes. This is part 2 of two part series, before proceeding further please ensure you read and followed steps mentioned in Part 1 of the series (https://medium.com/@ahmad.md7/how-to-install-kubernetes-on-ubantu-20-04-focal-fossa-part-1-7a9826b96ee8).
Installing Docker
Docker is a software containerization platform to build and run applications based on containers .Containers provide lightweight execution environment which make use of host kernal resources. Containers are self sufficient , portable and scalable. You can read more about Docker in their website https://docs.docker.com/engine/
Step 1 : update your existing list of packages by running below command.
$sudo apt update
Step 2: Install few prerequisite packages which let apt use packages over HTTPS.
$sudo apt install apt-transport-https ca-certificates curl software-properties-common
Step 3: Add the key for the official Docker repository
$curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Step 4: Add the Docker repository to APT sources
if you are using Ubantu focal you use this command directly ,other wise use “$(lsb_release –cs)” which scans and returns the code name of your Ubuntu installation — in this case, focal. Also, the final word of the command — stable– is the type of Docker release.
$sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable”
or
$sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”
Step 5 : update the package database with the Docker packages from the newly added repository.
$sudo apt update
Step 6: Update policy to ensure you are installing from the Docker repository instead of the default Ubuntu repository.
$apt-cache policy docker-ce
Step 7: install Docker.
$sudo apt install docker-ce
Step 8 :Grant permissions to k8suser.
$sudo usermod -aG docker $(whoami)
Step 9:Verify docker is running…
$docker — version
Step 10 : if above step gives error , that means docker didn’t start automatically,run below command to start manually.
$sudo systemctl start docker
Step 11 : Enable docker to automatically run during boot.
$sudo systemctl enable docker
perform all these steps on rest of virtual machines.
Install Kubernetes
Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services. Kubernetes provides a framework to scale and run distributed systems resiliently and provides fail-over capabilities.
Kubernetes has lot more capabilities than this definition, you can read more about k8s from https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/
The Kubernetes scheduler determines the best available node on which to deploy newly created pods. If memory swapping is allowed on a host system, this can lead to performance and stability issues within Kubernetes. For this reason, Kubernetes requires that you disable swap in the host system
Step 1 : To disable swap, edit /etc/fstab file and comment out the line which includes entry either swap partition or swap file.To disable swap, edit /etc/fstab file and comment out the line which includes entry either swap partition or swap file.
$ sudo vi /etc/fstab
Step 2 : Run swapoff command to disable the swap on the fly
$sudo swapoff -a
Step 3 : set vm swapiness to zero ,Where 0 is the percent of swapiness. In this case swap can be used only if you are out of RAM (by default swap is used when more that 60% of RAM is full).
$sudo echo “vm.swappiness=0” | sudo tee — append /etc/sysctl.conf
Step 4: To enable the ip forwarding permanently, edit the file “/etc/sysctl.conf” and look for line “net.ipv4.ip_forward=1” and uncomment it.
$sudo vi /etc/sysctl.conf
Step 5: Apply configuration changes without reboot.
$sudo sysctl -p
Step 6 : Run below commands to add key and repository and finally install Kubelet,Kubeadm and Kubectl.
At time of writing this article , Ubuntu 16.04 (Xenial Xerus ) k8s repository was available but in future, when the k8s repository is available for Ubuntu 20.04 then replace xenial with focal word in ‘apt-add-repository’ command.
$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
$ sudo apt-add-repository “deb http://apt.kubernetes.io/ kubernetes-xenial main”
$ sudo apt update
$ sudo apt install -y kubelet kubeadm kubectl
Step 7 : On k8s Master node run below steps , once these steps are executed kubernetes master is initialized. Also output prints a kubeadm join command ,make a note of this join command .You will use this join command to join cluster nodes.
$sudo kubeadm init — pod-network.cidr=10.244.0.0/16 — apiserver-advertise-address=192.xxx.xxx.111
$sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$sudo chown $(id -u):$(id -g) $HOME/.kube/config
Step 8 : Join cluster
on cluster nodes run join command generated during Kubeadm initialization.
$sudo kubeadm join 192.168.188.111:6443 — token uf5pnv.shxaty3x1jr9nyda — discovery-token-ca-cert-hash sha256:4818d0d1f34c3179f366e92091b81cd19562bd83be4a25ed631c897d437f25e5
upon completion of above step run kubectl get nodes will show newly joined node
$kubectl get nodes
you will see nodes in Not Ready status.This is because you need to install POD net work for communication.
Step 8 : Deploy Calico Pod Network Add-on (Master Node).
POD network is used for communication between hosts and is necessary for k8s cluster to function properly.There are several choices for POD network, Calico and Flannel are easy to use. You can explore more options here https://kubernetes.io/docs/concepts/cluster-administration/networking/
From the master node, run the following command to download Calico pod network add-on.
curl https://docs.projectcalico.org/v3.14/manifests/calico.yaml -O
Look for below configuration in calico.yaml that was downloaded and update them to use correct Network Interface, in my case “enp0s8” is the name of host network . You can find this information by running “ifconfig” on
Kubernetes master node.
- name: IP_AUTODETECTION_METHOD
value: “interface=enp0s8”
- name: IP6_AUTODETECTION_METHOD
value: “interface=enp0s8”
Step 9 : Install calico POD network (Master node)
$kubectl apply -f calico.yaml
Step 10 : Once it has been deployed successfully then nodes status will become ready, let’s re-run kubectl command to verify nodes status
$kubectl get nodes
Step 10 : Run get pods for all namespaces , you should see all of them in running status ( this may take a while depending on your network connection)
kubectl get pods — all-namespaces
Step 11 : Deploy nginx webserver (Master Node)
$kubectl create deployment nginx-web — image=nginx
Step 12 : Verify nginx deployment
$kubectl get deployment.apps
$kubectl get pods
Step 12 : Scale nginx
Run below command to scale 4 instance of nginx
$kubectl scale — replicas=4 deployment nginx-web
You have successfully configured Kubernetes cluster and able to deploy nginx and scale it.