Okay, here is a tutorial to help you install K3s, a lightweight Kubernetes distribution, on your Raspberry Pi(s) for a home setup. This guide covers prerequisites, installation for a single node (server) or multiple nodes (server + agents), and basic verification.
Assumptions:
- You have one or more Raspberry Pi devices (Model 3B+ or newer, preferably Pi 4B or 5 for better performance).
- You have flashed Raspberry Pi OS (Lite version recommended, 64-bit is preferable if your Pi supports it) onto SD cards for each Pi.
- You have SSH access enabled on your Raspberry Pis and can connect to them from your computer.
- Your Raspberry Pis are connected to your home network (Ethernet is recommended over Wi-Fi for stability).
Phase 1: Prerequisites (Perform on each Pi)
-
Update System: Connect to your Pi via SSH and update the package lists and installed packages:
sudo apt update
sudo apt full-upgrade -y
sudo reboot
Wait for the Pi to reboot and reconnect via SSH.
-
Set Unique Hostnames (Recommended for Multi-Node): Give each Pi a unique hostname (e.g., k3s-server, k3s-agent-1).
sudo hostnamectl set-hostname k3s-server # Or k3s-agent-1, etc.
Edit the hosts file to reflect this:
sudo nano /etc/hosts
Ensure the line starting with 127.0.1.1 points to your new hostname. Save and exit (Ctrl+X, then Y, then Enter).
-
Configure Static IPs (Highly Recommended): For a stable cluster, static IPs are essential. The method depends on your network setup. You can either:
- Configure DHCP reservation on your router (assigning a fixed IP to each Pi's MAC address). This is often the easiest way.
- Configure static IPs directly on each Pi (e.g., by editing
/etc/dhcpcd.conf or using network management tools). Refer to Raspberry Pi OS documentation for details.
- Make a note of the IP address assigned to each Pi.
-
Enable cgroups for Container Support: K3s requires specific control groups (cgroups) to be enabled for memory management.
Phase 2: K3s Installation
You have two main options: a single-node cluster (all on one Pi) or a multi-node cluster (one server Pi, one or more agent Pis).
Option A: Single-Node K3s Cluster (Server Only)
-
Run the K3s Installer: On the Pi you designated as the server (or your only Pi), run the official K3s installation script:
curl -sfL https://get.k3s.io | sh -
This command downloads the script and executes it, installing the K3s server components and containerd.
-
Check Service Status: Verify that the K3s service is running:
sudo systemctl status k3s
You should see it as active (running). Press q to exit the status view.
Option B: Multi-Node K3s Cluster (Server + Agent(s))
-
Install K3s Server (on the Server Pi): Follow steps 1 and 2 from Option A on the Pi you designated as the server.
-
Get Server IP and Token (on the Server Pi):
-
Install K3s Agent (on each Agent Pi):
-
Check Agent Service Status: On the agent Pi, verify the agent service is running:
sudo systemctl status k3s-agent
You should see it as active (running). Press q to exit.
-
Repeat Step 3 for any additional agent Pis you want to add to the cluster.
Phase 3: Verification and Access
-
Verify Nodes (on the Server Pi): On the server Pi, use kubectl (the Kubernetes command-line tool, installed with K3s) to see the nodes in your cluster:
sudo kubectl get nodes -o wide
- For a single-node setup, you'll see only the server node listed with the
control-plane,master role.
- For a multi-node setup, you should see the server node and all agent nodes you added. It might take a minute or two for new agents to appear with a
Ready status.
-
Access Kubeconfig: To manage your cluster from your main computer (instead of SSHing into the server Pi), you need the kubeconfig file.
- On the server Pi, the config file is located at
/etc/rancher/k3s/k3s.yaml.
- You need to read this file:
sudo cat /etc/rancher/k3s/k3s.yaml.
- Copy the entire content of this file.
- On your local machine, save this content into a file, typically at
~/.kube/config. If that file already exists (for other clusters), you can save it elsewhere (e.g., ~/.kube/config-k3s-rpi) and merge it or use the KUBECONFIG environment variable: export KUBECONFIG=~/.kube/config:~/.kube/config-k3s-rpi.
- Important: Inside the copied
k3s.yaml content, find the line starting with server:. It will likely have https://127.0.0.1:6443 or https://localhost:6443. You must change this IP address to the actual static IP address of your K3s server Pi (e.g., https://192.168.1.100:6443) so your local machine can reach it.
- Ensure your local machine has
kubectl installed.
- Test access from your local machine:
kubectl get nodes. You should see the same output as when you ran it on the server Pi.
Next Steps:
- Install Helm: A package manager for Kubernetes.
- Deploy Applications: Try deploying a simple application like Nginx or use tools like Arkade to install common apps.
- Explore Ingress: Set up an Ingress controller (Traefik is included with K3s) for external access to your services.
- Persistent Storage: Configure storage solutions suitable for Raspberry Pi (e.g., NFS, Longhorn if resources allow, or hostPath for simple cases).
- Monitoring: Set up monitoring tools like Prometheus and Grafana.
Enjoy your Raspberry Pi Kubernetes cluster! Remember that RPi resources are limited, so manage your deployments accordingly.</your_node_token></server_ip></your_node_token></server_ip>