Kubernetes has become an essential tool for container orchestration, and running it on Raspberry Pi can be an excellent way to learn and experiment with the technology. This guide will walk you through the process of setting up a Kubernetes cluster on Raspberry Pi using K3s, a lightweight Kubernetes distribution.
Prerequisites
Before you begin, ensure you have the following:
- At least two Raspberry Pi 4 devices with 4GB RAM or more
- Raspberry Pi OS (64-bit) or Ubuntu Server installed on each Pi. If you haven't installed an OS on your Pis yet, I recommend Raspberry Pi's very own Installation Guide.
- All Raspberry Pis connected to the same network
- MicroSD cards (8GB or larger) for each Pi
- Adequate power supply for each Pi
- SSH enabled on all Raspberry Pis. If you need instructions on how to do this, I recommend this guide.
Installation Steps
-
Find out the IP Address of the Pi that will serve as your Kubernetes master node.
The master node is our Kubernetes Control Plane. It manages the overall state of the cluster:
hostname -I -
SSH into the Pi that will serve as your Master Node. The master node is the primary control point for your Kubernetes cluster.
ssh <Username>@<MASTER_IP_ADDRESS> -
Perform system updates and get the latest security patches:
sudo apt update && sudo apt upgrade -y -
Configure cgroups for Kubernetes Compatibility. We need to allow Kubernetes to allocate and track CPU, memory, and other resources:
sudo nano /boot/cmdline.txt
Add: cgroup_memory=1 cgroup_enable=memory -
Reboot Raspberry Pi. To ensure the new cgroup configuration settings are applied, reboot your Pi:
sudo reboot -
Install K3s on Master Node. K3s is a lightweight Kubernetes distribution optimized for resource-constrained environments, making it ideal for usage with Raspberry Pi. It has a reduced memory footprint compared to full Kubernetes. Install it with:
curl -sfL https://get.k3s.io | sh - -
Verify Kubernetes Installation. To confirm the successful installation of Kubernetes and verify the setup of the basic cluster, run:
sudo kubectl get nodes
This lists nodes in the cluster. For now, it should only show the master node.
-
Retrieve Cluster Join Token. Kubernetes uses security credentials, known as tokens, to authenticate worker nodes and enable them to securely join the cluster. The token that allows worker nodes to join the cluster is called the Cluster Join Token, and it is unique to each cluster. To retrieve the Join Token, run:
sudo cat /var/lib/rancher/k3s/server/node-token
Save this somewhere secure before the next step. -
Join Worker Nodes to Cluster
To transform additional Raspberry Pis into Kubernetes worker nodes, run (for each other Pi/worker node):
curl -sfL https://get.k3s.io | K3S_URL=https://<MASTER_IP>:6443 K3S_TOKEN=<NODE_TOKEN> sh -
This installs K3s on each worker and connects the workers to the master node. -
Verify Cluster Composition
To confirm that the cluster has been set up correctly, you can check if your master node and worker nodes have been registered with:
sudo kubectl get nodes
This should show your master and worker nodes.
Congratulations! You have now not only installed Kubernetes on Raspberry Pi, but also set up a functional local Kubernetes cluster, with which you can deploy and manage containerized applications. 🥳
In the next post of the series, I will talk you through how to deploy Prometheus on Raspberry Pi, a powerful open-source monitoring and alerting toolkit!
Troubleshooting
If your worker nodes are failing to show up on the master node, it might be because your raspberry pis all have the same hostname, if you haven't changed them. To fix this problem, give each pi its own unique hostname by editing two files; hostname and hosts.
Open the hostname file with a text editor, like nano:
sudo nano /etc/hostname
You'll see the current hostname. Replace it with the new unique hostname you want to set.
Save and close the file (in nano, press Ctrl+X, then Y, and hit Enter).
Next, you'll need to update the hosts file. Open it by typing:
sudo nano /etc/hosts
Find the line that looks like 127.0.1.1 raspberrypi and replace raspberrypi with the new hostname you chose.
Save and close the file. Finally, reboot your Raspberry Pi for the changes to take effect:
sudo reboot
Top comments (0)