Kubernetes Cluster mit dem Raspberry Pi (2/3/4) mit K3s
This guide demonstrates how to set up a Raspberry Pi cluster using Kubernetes. My setup involves six Raspberry Pis:
- 2x Raspberry Pi 2 ARMv7
- 2x Raspberry Pi 3 ARMv8
- 2x Raspberry Pi 4 ARMv8
Important Note:
The Raspberry Pi 2 Model B V1.2 is compatible due to its 64-bit core. The Raspberry Pi 2 Model B V1 is not compatible as it has a 32-bit core. The BCM2837 chip, developed for the Raspberry Pi Foundation, offers better performance even in 32-bit mode compared to the BCM2836. Therefore, only the RPi 2 Model B V1.2 will work.
Step 1: Hardware Setup
Additional Hardware Required:
- 1 GBit 8-port switch
- Network cables in various colors
- 6-port USB Anker charger with suitable USB cables
- Acrylic Raspberry Pi stack
- 64GB MicroSD cards
Step 2: Operating System Installation
- Download and install the Raspberry Pi Imager from the official website.
- For each Pi, select the appropriate model:
- Choose Raspberry Pi OS Lite (64-Bit) as the image.
- Select the SD card.
- Click "Next" and then "Edit Settings" to assign a name (e.g., Pi2-purple, Pi2-blue, etc.), set a username, and password.
- Click "Yes" and wait for the SD card to be ready. Insert it into the Raspberry Pi and repeat for each device.
Step 3: Configuring Raspberry Pis
-
Connect to each Pi via SSH (IP address can be found in your router settings).
-
Edit the network configuration:
sudo nano /etc/dhcpcd.confAdd:
interface eth0 static ip_address=192.168.178.200/24 static routers=192.168.178.254Save and exit by pressing Control + X.
-
Alternatively, assign a static IP via your router. The IP range for the cluster is 192.168.178.200-192.168.178.206.
-
Disable swap:
sudo swapoff -aTo permanently disable swap:
sudo nano /etc/dphys-swapfile CONF_SWAPSIZE=0Save and exit.
-
Configure cgroups:
sudo nano /boot/firmware/cmdline.txtAppend to the first line:
cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memorySave and exit.
-
Reboot the system:
sudo reboot
Step 4: Installing Kubernetes with K3s
Installing the Kubernetes Master Node:
-
Choose the most powerful Raspberry Pi as the master node. In this example, it's Raspberry Pi4-red with IP 192.168.178.200.
-
Connect via SSH and run:
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --disable=traefik --flannel-backend=host-gw --tls-san=192.168.178.200 --bind-address=192.168.178.200 --advertise-address=192.168.178.200 --node-ip=192.168.178.200 --cluster-init" sh - -
After installation, the master node starts automatically. The configuration file is located at
/etc/rancher/k3s/k3s.yaml. -
Retrieve the node token needed to add worker nodes:
sudo cat /var/lib/rancher/k3s/server/node-token
Installing Kubernetes Worker Nodes:
- Connect to the first Raspberry Pi designated as a worker node via SSH (e.g., 192.168.178.201).
- Execute the following command, replacing
PUT-THE-TOKEN-HEREwith the actual token:curl -sfL https://get.k3s.io | K3S_URL=https://192.168.178.200:6443 K3S_TOKEN="PUT-THE-TOKEN-HERE" sh - - Repeat the process for all remaining Raspberry Pis.
Conclusion
Congratulations, your Raspberry Pi Kubernetes cluster is now set up! For further steps, consider configuring Lens for cluster management or enrolling in a beginner's course on Kubernetes.
Citations: [1] https://everythingdevops.dev/step-by-step-guide-creating-a-kubernetes-cluster-on-raspberry-pi-5-with-k3s/ [2] https://anthonynsimon.com/blog/kubernetes-cluster-raspberry-pi/ [3] https://blog.alexellis.io/self-hosting-kubernetes-on-your-raspberry-pi/ [4] https://rpi4cluster.com/k3s-design-goals/ [5] https://solved.scality.com/solved/how-i-made-a-kubernetes-cluster-with-five-raspberry-pis/ [6] https://blog.hypriot.com/post/setup-kubernetes-raspberry-pi-cluster/ [7] https://ubuntu.com/tutorials/how-to-kubernetes-cluster-on-raspberry-pi [8] https://ikarus.sg/kubernetes-with-k3s/
