Easily Install K3s Kubernetes Cluster on Raspberry PI
Posted April 12, 2024
K3s is lightweight. This means you are running an actual Kubernetes cluster but as lightweight. Now, you want to use K3s to create Kubernetes clusters within your Raspberry PI. In this case, you will need to correctly install K3s within your Raspberry PI and run your clusters.
In this guide, you will explore the world of Raspberry PI with K3s. You will learn how to perfectly install and create K3s clusters to expose lightweight Kubernetes clusters.
Here is a quick look at the key takeaways you will cover:
- Installing K3s master node on Raspberry.
- Adding Worker nodes to K3s Raspberry PI cluster.
- Creating K3s ingress controller as a K3s load balancer.
- How to manage K3s pods and nodes within Raspberry PI.
- Using K3s with Traefik and Nginx ingress Controllers.
Ready? Dive and get K3s running on Raspberry like a pro.
Requisites to Installing K3s on Raspberry PI
To get K3s Ready on your Raspberry PI ensure:
- At least to ready remote Raspberry PI. One for installing master k3s node and others as worker nodes.
- Ensure you have basic knowledge working with Kubernetes
Installing Single Node (Master) k3s on Raspberry PI
K3s has all the juices for Creating cluster units. It comes ready with Ingress Controllers and Dashboards to access your cluster. Traefik is the heart of Ingress and Kubernetes Dashboards, reverse proxy, and load balancer.
Now, when installing K3s on Raspberry, you might need to disable Traefik and ignore these features. Go ahead and install K3s to your master Raspberry PI using the following command:
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --disable=traefik" K3S_KUBECONFIG_MODE="644" sh -s -
Here is how this command works:
- The
curl -sfL https://get.k3s.io
command is the URL that fetches the K3s script to Raspberry INSTALL_K3S_EXEC="server --disable=traefik"
has--disable=traefik
to disable the Traefik service.server
tells Raspberry to run k3s to in server mode and as the Kubernetes master node.- K3S_KUBECONFIG_MODE=“644” sets file mode permissions for the Kubernetes kubeconfig file.
sh -s -
is your shell script.
Once you have the following command executed, go ahead and confirm you have K3s service running within your Raspberry PI:
systemctl status k3s
At the same time, the master node should be ready as follows:
kubectl get nodes
Your K3s Master Pods should be ready in your K3s kube-system namespace:
kubectl get pods -n kube-system
Installing k3s with Traefik on Raspberry PI
Based on your cluster, you might need to use Traefik as a reverse proxy, load balancer, or Dashboard management.
If this is your option, you should run the install command without --disable=traefik
as follows:
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server" K3S_KUBECONFIG_MODE="644" sh -s -
In this case, Raspberry will start K3s with Traefik as such:
kubectl get pods -n kube-system
To dive deeper into how Traefik works check the following guides and explore Traefik as a reverse proxy, load balancer, or for Dashboard management:
- How to Expose and Enable K3s with Traefik Dashboard
- How to Configure K3s with Traefik Ingress Controller
Managing K3s Raspberry cluster with K8s Lend
If you don’t have a dashboard to access and manage the created cluster, you now need to make a way to access your cluster (if you are not using the Traefik dashboard).
K8s Lend is a good option to access the K3s cluster. K3s Raspberry setting as saved in the /etc/rancher/k3s/k3s.yaml
path.
Dive into the dock and set up Lens to access K3s as follows:
Installing K3s Worker Nodes on Raspberry
Worker nodes run as agents. To install one, you must get a Master token and let K3s get installed on a different Raspberry as an agent.
In your Master PI, get the K3s tokes using the following command:
sudo cat /var/lib/rancher/k3s/server/node-token
Go to Worker Nodes and run your agent K3s cluster as follows:
curl -sfL https://get.k3s.io | K3S_URL=https://:master_ip_adress:6443 K3S_TOKEN=master_token sh -
You should replace the master_token with the above created token and master_ip_adress with the IP of the master PI.
Installing Nginx Ingress as K3s Raspberry Cluster Controller
If you install K3s without Traefik, you will need an Ingress Controller to create load balancers and reverse proxy to K3s clusters.
This is how to create a load balancer for the NGINX ingress controller:
Add NGINX as the ingress controller of the master PI using the following command:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.8.1/deploy/static/provider/baremetal/deploy.yaml
Now, check if the Nginx Ingress pods are ready:
kubectl get pods -A -o wide
Check the ingressclass object and confirm NGINX Ingress is ready:
kubectl -n ingress-nginx get ingressclasses
Then add the Ingress-NGINX controller annotation
kubectl -n ingress-nginx annotate ingressclasses nginx ingressclass.kubernetes.io/is-default-class="true"
First, get the NGINX service and check the Nodeport:
kubectl get services -n ingress-nginx
To test if the Ingress is working, you will need to send a curl request as follows:
curl <master-external-ip>:<node-port>
A response from nginx tells you the Ingress Controller is running.
You should go ahead and run a test application or your actual application with K3s Kubernetes and let the Nginx Ingress controller.
Here you will use the ingressClassName: nginx
to access the ingress:
---
kind: Deployment
---
kind: Service
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-server-ingress
spec:
ingressClassName: nginx
rules:
- host: test.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: test-server
port:
number: 800
Conclusion
I hope you can use K3s with Raspberry Pi and:
- Install K3s master and worker nodes on Raspberry PI.
- Create K3s ingress controller as a K3s load balancer.
- Use K3s with Traefik and Nginx ingress Controllers.