10.1 C
Amsterdam

How To Install Kubernetes Cluster On Ubuntu 20.04 Using K3s

Today we are going to learn that how to install Kubernetes Cluster on Ubuntu 20.04 using K3s. Before proceeding towards the installation process, let’s have a short introduction about the today article.

Introduction:

Kubernetes used to manage the docker containers across multiple cloud & virtualization platform. Kubernetes was designed by the Google. And k3s is used to reduce the higher load of k8sdeployment. So, here we’ll cover the installation of Kubernetes on Ubuntu 20.04 using k3s.

Setup Requirements:

We’ll need one Master node & the 2 worker nodes in order to perform our installation.

Master Node:

It handles the cluster, API calls and other process.

Workers Node:

Used to control the workloads, where pods deployed & applications run.

So, here we’ll be using one Master node & two worker nodes in order to complete our installation guide.

Step 1: Update Your System

As usual we do, update your system to have the latest updates installed.

sudo apt update && sudo apt upgrade -y

Once the update finished, reboot your system.

sudo systemctl reboot

Step 2: Mapping the Hosts On Ubuntu 20.04

Now, map the master and worker nodes on each host. To do this edit the file /etc/hosts/. And update according to hosts IP’s.

sudo nano /etc/hosts
172.16.10.3 master
172.16.10.4 worker01
172.16.10.10 worker02

Step 3: Install Docker On Ubuntu 20.04

Fire the below commands in your terminal to install docker on Ubuntu 20.04 as needed to use the Kubernetes. So, make sure docker is installed and running properly.

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
sudo apt update
sudo apt install docker-ce -y

Note: Make sure to perform this action(docker installation) on all hosts.

And then start & enable docker services.

sudo systemctl start docker
sudo systemctl enable docker

Verify the status of docker by typing

sabi@ubuntu:~$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2020-11-30 08:30:39 CET; 19s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 9275 (dockerd)
Tasks: 8
Memory: 35.6M
CGroup: /system.slice/docker.service
└─9275 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Nov 30 08:30:39 ubuntu dockerd[9275]: time="2020-10-30T08:30:39.460569874+01:00" level=warning msg="Your kernel does not support cgroup rt runtime"
Nov 30 08:30:39 ubuntu dockerd[9275]: time="2020-10-30T08:30:39.460672730+01:00" level=warning msg="Your kernel does not support cgroup blkio weight"
Nov 30 08:30:39 ubuntu dockerd[9275]: time="2020-10-30T08:30:39.460820506+01:00" level=warning msg="Your kernel does not support cgroup blkio weight_device"
Nov 30 08:30:39 ubuntu dockerd[9275]: time="2020-10-30T08:30:39.461286028+01:00" level=info msg="Loading containers: start."
Nov 30 08:30:39 ubuntu dockerd[9275]: time="2020-10-30T08:30:39.620849970+01:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0>
Nov 30 08:30:39 ubuntu dockerd[9275]: time="2020-10-30T08:30:39.717281156+01:00" level=info msg="Loading containers: done."
Nov 30 08:30:39 ubuntu dockerd[9275]: time="2020-10-30T08:30:39.754253372+01:00" level=info msg="Docker daemon" commit=4484c46d9d graphdriver(s)=overlay2 version>
Nov 30 08:30:39 ubuntu dockerd[9275]: time="2020-10-30T08:30:39.755461813+01:00" level=info msg="Daemon has completed initialization"
Nov 30 08:30:39 ubuntu systemd[1]: Started Docker Application Container Engine.

In order to avoid using sudo whenever you type docker commands, simply add your user to docker group. This can be done by typing.

sudo usermod -aG docker ${USER}
newgrp docker

Step 4: Setup the Master K3s Node

First of all we’ll setup the master node. On this node we’ll install the k3s sevice. To install k3s hit the given command in your terminal.

curl -sfL https://get.k3s.io | sh -s - --docker

You’ll see the similar output:

[INFO] Finding release for channel stable
[INFO] Using v1.18.9+k3s1 as release
[INFO] Downloading hash https://github.com/rancher/k3s/releases/download/v1.18.9+k3s1/sha256sum-amd64.txt
[INFO] Downloading binary https://github.com/rancher/k3s/releases/download/v1.18.9+k3s1/k3s
[INFO] Verifying binary download
[INFO] Installing k3s to /usr/local/bin/k3s
[INFO] Creating /usr/local/bin/kubectl symlink to k3s
[INFO] Creating /usr/local/bin/crictl symlink to k3s
[INFO] Skipping /usr/local/bin/ctr symlink to k3s, command exists in PATH at /usr/bin/ctr
[INFO] Creating killall script /usr/local/bin/k3s-killall.sh
[INFO] Creating uninstall script /usr/local/bin/k3s-uninstall.sh
[INFO] env: Creating environment file /etc/systemd/system/k3s.service.env
[INFO] systemd: Creating service file /etc/systemd/system/k3s.service
[INFO] systemd: Enabling k3s unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
[INFO] systemd: Starting k3s

Make sure the installation is successfull.

sudo systemctl status k3s
● k3s.service - Lightweight Kubernetes
Loaded: loaded (/etc/systemd/system/k3s.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2020-11-30 08:32:46 CET; 53s ago
Docs: https://k3s.io
Process: 11151 ExecStartPre=/sbin/modprobe br_netfilter (code=exited, status=0/SUCCESS)
Process: 11152 ExecStartPre=/sbin/modprobe overlay (code=exited, status=0/SUCCESS)
Main PID: 11153 (k3s-server)
Tasks: 0
Memory: 420.9M
CGroup: /system.slice/k3s.service
└─11153 /usr/local/bin/k3s server --docker
Nov 30 08:33:32 ubuntu k3s[11153]: I1030 08:33:32.532935 11153 reconciler.go:196] operationExecutor.UnmountVolume started for volume "helm-traefik-token-dmfz6">
Nov 30 08:33:32 ubuntu k3s[11153]: I1030 08:33:32.533481 11153 reconciler.go:196] operationExecutor.UnmountVolume started for volume "values" (UniqueName: "kub>
Nov 30 08:33:32 ubuntu k3s[11153]: W1030 08:33:32.533849 11153 empty_dir.go:453] Warning: Failed to clear quota on /var/lib/kubelet/pods/d1fae0b1-f3ac-481a-b6a>
Nov 30 08:33:32 ubuntu k3s[11153]: I1030 08:33:32.538649 11153 operation_generator.go:782] UnmountVolume.TearDown succeeded for volume "kubernetes.io/configmap>
Nov 30 08:33:32 ubuntu k3s[11153]: I1030 08:33:32.555370 11153 operation_generator.go:782] UnmountVolume.TearDown succeeded for volume "kubernetes.io/secret/d1>
Nov 30 08:33:32 ubuntu k3s[11153]: I1030 08:33:32.653113 11153 reconciler.go:319] Volume detached for volume "helm-traefik-token-dmfz6" (UniqueName: "kubernete>
Nov 30 08:33:32 ubuntu k3s[11153]: I1030 08:33:32.653137 11153 reconciler.go:319] Volume detached for volume "values" (UniqueName: "kubernetes.io/configmap/d1f>
Nov 30 08:33:33 ubuntu k3s[11153]: W1030 08:33:33.978774 11153 pod_container_deletor.go:77] Container "23d34e0acde3f4ab7e41d99e9cff16e7ba8cd76122158dfc18b64ae0>
Nov 30 08:33:33 ubuntu k3s[11153]: W1030 08:33:33.997821 11153 pod_container_deletor.go:77] Container "a52fe91aa3e7b137ebe4dd993e358c382f96b5df342cb1c098acadb6>
Nov 30 08:33:35 ubuntu k3s[11153]: W1030 08:33:35.022454 11153 pod_container_deletor.go:77] Container "2b9f302427059276bc2e1d012db5cbcaeb72ed373cb52218edb6eb4b

Make sure that Master node is running properly.

sudo kubectl get nodes -o wide

Output:

sabi@ubuntu20:~$ sudo kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master Ready master 3m2s v1.18.9+k3s1 172.16.10.3 Ubuntu 20.04.1 LTS 5.4.0-52-generic docker://19.3.8

Step 5: Allow Firewall Access

Allow the firewall access to the 443 and 6443 ports in order to start the communication b/w the master & worker nodes.

sudo ufw allow 6443/tcp
sudo ufw allow 443/tcp

Now, run the below command in your terminal to get a tocken from the master node that will be later used on worker node to join the master node.

sudo cat /var/lib/rancher/k3s/server/node-token

Output:

K1078f2861628c95aa328595484e77f831adc3b58041e9ba9a8b2373926c8b034a3::server:417a7c6f46330b601954d0aaaa1d0f5b

Step 6: Install K3s Service On Worker Nodes

Now, install the k3s service on worker nodes & connect them to the master node.

curl -sfL http://get.k3s.io | K3S_URL=https://<master_IP>:6443 K3S_TOKEN=<join_token> sh -s - --docker

master_IP: IP of Master Node
join_token: Token generated on Master Node in above step.

curl -sfL http://get.k3s.io | K3S_URL=https://172.16.10.3:6443 K3S_TOKEN=K1078f2861628c95aa328595484e77f831adc3b58041e9ba9a8b2373926c8b034a3::server:417a7c6f46330b601954d0aaaa1d0f5b sh -s - --docker

Make sure that k3s-agent is running properly on worker nodes by typing.

sudo systemctl status k3s-agent

Fire the given command to verify that worker nodes are added to cluster.

sudo kubectl get nodes

You’ll see the similar output upon success.

sabi@ubuntu20:~$ kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master Ready master 14m v1.18.9+k3s1 172.16.10.3 Ubuntu 20.04.1 LTS 5.4.0-52-generic docker://19.3.8
worker02 Ready 90s v1.18.9+k3s1 172.16.10.10 Ubuntu 20.04.1 LTS 5.4.0-52-generic docker://19.3.8
worker01 Ready 41s v1.18.9+k3s1 172.16.10.4 Ubuntu 18.04.5 LTS 4.15.0-122-generic docker://19.3.6

Install Helm CLI Tool On k3s

Visit the link to download the latest version of Helm.

And extract the downloaded file with the help of tar.

tar -xvcf <downloaded-file>

Then move the binary file to /usr/local/bin/helm

mv linux-amd64/helm /usr/local/bin/helm

Verify the version

heml version

Finally add helm repo in order to allow installation with helm.

helm repo add stable https://charts.helm.sh/stable
helm repo update

Deploy Application On K3s

Here we’ll add Nginx as test purposes.

Run the below command to install nginx using helm

helm install nginx-ingress stable/nginx-ingress --namespace kube-system \
--set defaultBackend.enabled=false

Verify the added application by typing

kubectl get pods -n kube-system -l app=nginx-ingress -o wide
sabi@ubuntu20:~$ kubectl get pods -n kube-system -l app=nginx-ingress -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-ingress-controller-56547fb57-vsjdm 1/1 Running 0 28s 10.42.2.6 worker01

So, it shows that we’ve successfully deployed nginx on k3s.

How To Install Kubernetes Cluster On Ubuntu 20.04 Using K3s

Removing K3s

In order to remove k3s on the worker nodes, hit

sudo /usr/local/bin/k3s-agent-uninstall.sh
sudo rm -rf /var/lib/rancher

To remove k3s on master node, type

sudo /usr/local/bin/k3s-uninstall.sh
sudo rm -rf /var/lib/rancher

So, this is how you can install kubernetes on Ubuntu 20.04 using k3s.

- Advertisement -

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Donate to Osradar

Latest article

x