Kubernetes Cluster on Raspberry Pi
Raspberry Pi clusters are not a novel idea, nor is installing Kubernetes on the cluster novel. The book “Kubernetes: Up and Running, 2nd edition” has an appendix on building a Raspberry Pi cluster, Jeff Geerling has his Raspberry Pi Dramble (https://www.pidramble.com/), and there are many other examples.
To start, let’s consider the hardware of my cluster. I used Jeff Geerling’s hardware as outlined on his Pi Dramble site to guide my hardware choices:
|Raspberry Pi 4B/4GB||4|
|Raspberry Pi PoE HAT||4|
|Samsung EVO+ 32 GB MicroSD||4|
|Yahboom Raspberry Pi Cluster Case||4|
|6” Cat6 Patch Cables||4|
|NETGEAR 5-port Gigabit PoE Switch||1|
Typically you power the Raspberry Pi 4B with a USB-C power supply but I like the idea of keeping the setup minimal so I sprung for the PoE HAT and PoE switch (the PoE HAT sits on top of the Raspberry Pi and allows the Pi to be powered through the PoE switch).
I also decided to purchase the BlinkStick Nano LED controllers to provide some external indication of individual Pi status.
Raspberry Pi Configuration
You will be doing the following steps on each of the Raspberry Pis. I recommend mapping out what you want their IP addresses and host names to be. I also recommend you do the following steps in order one Pi at a time.
You will want to plug the PoE HAT onto the Raspberry Pi and assemble the case. Then, I downloaded the Raspbian Buster Lite OS distro (https://www.raspberrypi.org/downloads/raspbian/). I used balenaEtcher (https://www.balena.io/etcher/) to write the image to the MicroSD card.
After flashing the Raspbian Lite image to the MicroSD card, I had to eject and re-insert the card in order for it to show as a volume on my computer. I’m use a MacOS computer and it named the volume ‘boot’. In order to allow SSH from Raspbian OS, you will need to create an empty file. Run the following command (assuming your volume name is the same):
This allows you to boot and access the headless Raspberry Pi (headless = without direct attached keyboard and monitor). You may now eject the MicroSD card from your computer and insert into your Raspberry Pi. Assuming you have a DHCP server to hand out IP addresses, simply plug the Raspberry Pi into the PoE switch and it will boot up. To find the IP, I checked my router (which also acts as my DHCP server) and found the IP for device named ‘RASPBERRYPI’. Suppose the IP address for your device is 192.168.1.5 – run the following command from a terminal window to connect:
The default username is ‘pi’ (as shown above) and the default password is ‘raspberry’. Do everyone a favor and change your password immediately. Run the following command:
Follow the menu prompts to change your password. Before leaving the configuration tool, I also changed the hostname. I used ‘k3s-master-1’ for this first node and then ‘node-1’, ‘node-2’, and ‘node-3’ for the rest of the devices. When you exit the tool, it will ask if you would like to reboot. Go ahead and reboot. There are two more configuration steps I performed before install K3s: set a static IP and setup the host file for name resolution to other Pis.
After the Pi has rebooted, I established connected via SSH to modify the hosts file by running the following command:
sudo vi /etc/hosts
This opens the hosts file using vi. If you are not familiar with vi, you will scroll to the line beginning with ‘127.0.0.1’ and press the ‘i’ key (which indicates you would like to insert a new line). I typed the intended IP address of node-1, followed by a tab, and then ‘node-1’. Press enter to move to the next line. Continue until you have a line for each of the hosts you intend to be part of the cluster. It should look similar to this image:
When you are done, press the ‘Esc’ key and type ‘:wq’ and press enter.
I used Visual Studio Code to assign the static IP since vi is not my favorite editor. You will need to install VS Code on your main computer if you haven’t already. If you don’t have VS Code, why not? It is free and a great editor! Visit https://code.visualstudio.com/. You will also need to install the ‘Remote-SSH’ extension within VS Code.
If you see that green icon in the bottom left, it means the extension is installed. Follow along with the video below to see how to enable a static IP on the Raspberry Pi using VS Code. You will be editing the ‘/etc/dhcpcd.conf’ file located on the Raspberry Pi by using an editor located on your main computer.
From the SSH session in your terminal window, you need to reboot the machine to cause the static IP to take effect by running:
The Raspberry Pi is now configured. You will want to run the steps above on each of the devices, making sure the IPs and host names are unique. Remember to connect to the Pi, you will need to use the new IP address.
I chose to use K3s instead of the full blown K8s distro. K3s (https://k3s.io/) is a very lightweight Kubernetes distribution. The installation and configuration is super simple so I will direct you to their documentation: https://rancher.com/docs/k3s/latest/en/quick-start/.
To confirm that everything is setup correctly, connect to the master via SSH and run:
sudo kubectl get nodes
The output should look like this:
You should now be ready to start using your Raspberry Pi Kubernetes cluster!