How To

Install Kubernetes Cluster on Ubuntu using K3s

Kubernetes is a container orchestration tool for hybrid cloud platforms. It was designed by Google. Kubernetes allows users to manage docker containers across different cloud and virtualization platforms. K3s is a lightweight kubernetes deployment binary that is meant to reduce the ‘heavily loaded’ k8s deployment. K3s is being developed by Rancher.

Original content from computingforgeeks.com - post 75879

[ebook product=”2″ theme=”purple”]

In this post we shall cover how to install a Kubernetes cluster on Ubuntu LTS using k3s. You can check out how to install k8s on Ubuntu with kubeadm for an alternative.

Kubernetes architecture involves a Master node and Worker Nodes. Their functions are as follows:

  • Master – controls the cluster, API calls, e.t.c.
  • Workers – these handles the workloads, where the pods are deployed and applications ran. They can be added and removed from the cluster.

So to setup a k3s cluster you need at least two hosts, the master node and one worker node. In this post we shall be using one master node and two worker nodes.

We need to prepare our hosts to be able to run k3s in a cluster. Use the following steps to install k3s cluster on Ubuntu:

1. Update Ubuntu system

With your servers installed with Ubuntu, update and upgrade them:

sudo apt update
sudo apt -y upgrade && sudo systemctl reboot

2. Map the hostnames on each node

Make sure you have the hostnames mapped on each node. This is by adding the IP and hostname of each node in the /etc/hosts file of each host.

In our setup, it is as follows:

$ sudo vim /etc/hosts
172.16.10.3 master
172.16.10.4 worker01
172.16.10.10 worker02

This has to be done on all the hosts for you to use DNS names.

3. Install Docker on Ubuntu

The next step is to install docker on on the hosts. As discussed before, Kubernetes is used to manage Docker containers on hybrid cloud infrastructure. Thus we need to have docker up and running on all the nodes before we can setup K3s.

Add Docker APT repository:

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"

Install Docker CE on Ubuntu:

sudo apt update && sudo apt install docker-ce -y

This has to be done on all the hosts including the master node. After successful installation, start and enable the service.

sudo systemctl start docker
sudo systemctl enable docker

You can also check if the service is started and is running:

 systemctl status docker

Add your user to Docker group to avoid typing sudo everytime you run docker commands.

sudo usermod -aG docker ${USER}
newgrp docker

4. Setup the Master k3s Node

In this step, we shall install and prepare the master node. This involves installing the k3s service and starting it.

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

Run the command above to install k3s on the master node. The script installs k3s and starts it automatically.

[INFO]  Finding release for channel stable
[INFO]  Using v1.29.6+k3s2 as release
[INFO]  Downloading hash https://github.com/k3s-io/k3s/releases/download/v1.29.6+k3s2/sha256sum-amd64.txt
[INFO]  Downloading binary https://github.com/k3s-io/k3s/releases/download/v1.29.6+k3s2/k3s
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Skipping installation of SELinux RPM
[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

To check if the service installed successfully, you can use:

systemctl status k3s

You can check if the master node is working by :

sudo kubectl get nodes -o wide

The output should be something like this:

NAME    STATUS   ROLES                  AGE   VERSION        INTERNAL-IP      EXTERNAL-IP   OS-IMAGE           KERNEL-VERSION     CONTAINER-RUNTIME
noble   Ready    control-plane,master   15s   v1.29.6+k3s2   188.40.183.232   <none>        Ubuntu 24.04 LTS   6.8.0-38-generic   docker://27.1.1

5. Allow ports on firewall

We need to allow ports that will will be used to communicate between the master and the worker nodes. The ports are 443 and 6443.

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

You need to extract a token form the master that will be used to join the nodes to the master.

On the master node:

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

You will then obtain a token that looks like:

K1078f2861628c95aa328595484e77f831adc3b58041e9ba9a8b2373926c8b034a3::server:417a7c6f46330b601954d0aaaa1d0f5b

6. Install k3s on worker nodes

The next step is to install k3s on the worker nodes. Run the commands below to install k3s on worker nodes:

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

Where master_IP is the IP of the master node and join_token is the token obtained from the master. e.g:

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

You can verify if the k3s-agent on the worker nodes is running by:

sudo systemctl status k3s-agent

To verify that our nodes have successfully been added to the cluster, run :

sudo kubectl get nodes

Your output should look like:

 kubectl get nodes -o wide

This shows that we have successfully setup our k3s cluster ready to deploy applications to it.

7. Deploy Addons to K3s

K3s is a lightweight kubernetes tool that doesn’t come packaged with all the tools but you can install them separately.

Install Helm Commandline tool on k3s

  • Download the latest version of Helm commandline tool from this page.
  • Extract the tar file using tar -xvcf <downloaded-file>
  • Move the binary file to /usr/local/bin/helm
sudo mv linux-amd64/helm /usr/local/bin/helm

Check version

helm version

Add the helm chart repository to allow installation of applications using helm:

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

8. Deploy an application on k3s

We can now deploy a test application on the K3s cluster.

Deploy Nginx Web-proxy on K3s

Nginx can be used as a web proxy to expose ingress web traffic routes in and out of the cluster.

We can install nginx web-proxy using Helm:

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

We can test if the application has been installed by:

kubectl get pods -n kube-system -l app=nginx-ingress -o wide

The output should be like:

root@master:~# 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   <none>           <none>

We have successfully deployed nginx web-proxy on k3s.

how to setup kubernetes on ubuntu 20 using k3s

9. Removing k3s (Bonus)

To remove k3s on the worker nodes, execute:

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

To remove k3s on the master node, execute:

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

Conclusion

In this article, we have been able to deploy k3s on a self hosted environment. We have also deployed helm and nginx on our k3s cluster.

You can find more articles on Kubernetes below:

Related Articles

Ubuntu How To Upgrade from Ubuntu 22.04 to Ubuntu 23.10 Debian Install and Use Websploit on Ubuntu | Debian Ansible Install Apache Tomcat using Ansible on Ubuntu / CentOS / Debian Containers Ship Kubernetes Logs to External Elasticsearch with Filebeat and Fluentd

3 thoughts on “Install Kubernetes Cluster on Ubuntu using K3s”

Leave a Comment

Press ESC to close