K3s: A lightweight Kubernetes
Introduction
K3s is a lightweight Kubernetes distribution that is designed to be easy to install and use. It is a great way to run a Kubernetes cluster in a home lab.
In this documentation, I assume you're running a Linux server on Debian.
Requirements
You can find a list of requirements on the K3s website. Basically the most important thing is to disable the firewall system to not overlap with the k3s network.
Installation
To install k3s on multiple nodes, its is preferable to use a configuration management tool like Ansible.
Here we'll see how to install k3s on a single node manually for simplicity. K3s has 2 roles:
- Server (K3s Control plane)
- Agent (K3s client/workers)
On large cluster, you generally have 3 (or more) dedicated nodes for the control plane and the rest for the workers. But here we'll keep it simple and run the control plane and the worker on the same node.
Start by creating a file to configure the kubelet:
Then run this command with root privileges:
etcd-expose-metrics
is used to expose the etcd metrics to the Prometheus server.flannel-backend=none
is used to disable the flannel network plugin because I prefer using Cilium for the network policy.disable-network-policy
required by Cilium.disable=traefik
you can keep it, but I prefer letting Qovery handle the ingress with Nginx.disable=metrics-server
same here, I prefer using Qovery for the metrics.disable servicelb
we'll use metallb for the load balancer.bind-address=0.0.0.0
is used to bind the kubelet to all interfaces.
Graceful shutdown
I personaly don't like how k3s shutdown the server when a reboot is triggered. It's not graceful and dangerous if you're hosting stateful apps likes databases.
To gracefully shutdown the k3s server, you can use this script:
Then create a systemd service to run it on reboot/shutdown:
Finally set execution permissions and enable the service:
You can now reboot the server and see that pods are gracefully shutdown: