So this past weekend, I did a little more work on one of my Raspberry PIs. Over the past few years Raspberry PIs have grown in the computing power and memory available. So as part of that, with the Raspberry PI 4, and moving forward. They can actually support running a flavor of kubernetes on the device.
So the biggest question, is “Why?” If we look at this from the perspective of what can you do with kubernetes on a Raspberry Pi, or several Raspberry Pis. For me there are a lot of potential options here for fun projects.
Some examples are:
IoT Projects:
One of the great uses for a Raspberry PI is to implement different IoT projects. The cheap cost, and easy of breadboarding means that I can get sensors connected to this thing pretty quickly. Now that alone doesn’t justify the need for Kubernetes. But one of the bigger benefits here is that by implementing Kubernetes on my RPi, I can then make it possible to leverage kubernetes container orchestration to manage updates to the device and managing the many processes I want to run on this device.
For example, if I had a RPi with several sensors enabled. I could potentially have multiple applications that would use the sensor output in different ways. Maybe at certain times of day, or certain conditions I want to enable different functionality. Then I could simply schedule pod deployments to support.
Additionally, by running Kubernetes, and leveraging docker containers. As I have new applications, I just need to push the images to the registry, and then deploy via either YAML or Helm Chart.
Home Automation
The favorite topic of IoT projects, and hobbiests everywhere, Home Automation. Lots of people use RPis for home automation. The benefit of using RPis is that I can very quickly and cheaply build a cluster to support different applications I want to run as part of building out my own home automation. Once I have a foundational cluster, I can deploy many different applications and update configuration of the RPis extremely easily from a remote terminal.
Development Environment:
If you want to developer for Kubernetes, you really need a sandbox to work in. You’re options are that you could stand up a cluster at home, you can work with and learn how to use Kubernetes. The benefit being this is easy to get running.
What is k3s?
So I’ve talked a lot about Kubernetes, and the question then becomes what exactly is k3s? K3s, is a version of Kubernetes that has been optimized to support running production workloads in unattended and/or resource constrained environments. Additionally k3s is a very small binary, and very easy to install.
The other reason, and this is important, is that it’s been optimized foor ARM64 and ARMv7 hardware. This is important because a lot of application development currently is done on amd64, but ARM64 is a preferred platform for hardware and IoT work. Many devices are built using ARM64 boards due to their computing power and ability to manage power better.
This is because ARM leverages Advanced RISC, which means that the instruction set is reduced. It simplifies the instructions sent to the CPU which makes it easier for embedded systems.
How do I install K3s on RPi?
So now we get to “how” do we do this? The steps are actually pretty simple. Really it boils down to the following steps:
- Flash the OS onto an SD Card.
- (Optional) if you are doing a cluster, you need to enable a static ip.
- Disable swap
- CGroup Configuration
- Install k3s
So let’s dive into how we can do these:
Flash the OS onto an SD Card:
For this, the process is well documented on the RaspberryPi.com. You can find documented steps here.
Enable Static IP:
If you wanted to setup a raspberry pi cluster, you can do that by accessing the Raspberry Pi via a terminal (I recommend VSCode, instructions here). Then execute the following:
sudo nano /etc/dhcpcd.conf
# Update the parameters to your router ip-address
interface eth0
static ip_address=192.168.1.85/24
static routers=192.168.1.254
# Hit Ctrl+X and "Y" to save changes
Disable Swap:
Swap is part of how linux manages resources, and ideally we want to see Kubernetes manage that. So to support this run the following:
# 1. Turn off swap temporary.
sudo swapoff -a
# 2. To turn of swap permanently we need to update the `CONF_SWAPSIZE` in `dphys-swapfile` file to `0`
sudo nano /etc/dphys-swapfile
# 3. set
CONF_SWAPSIZE=0
# 4. select control + X and save the changes.
CGroup Configuration:
CGroups are used by linux to manage resource access on hardware. And in order to get K3s installed, we need to update the configuration:
# 1. Open the cmdline.txt file
sudo nano /boot/cmdline.txt
#2. Add below into THE END of the current line
cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory
# 3. Save the file and reboot
sudo reboot
Install k3s
Finally the process of installing k3s, is as simple as running the following command:
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --disable=traefik --flannel-backend=host-gw --tls-san=192.168.1.85 --bind-address=192.168.1.85 --advertise-address=192.168.1.85 --node-ip=192.168.1.85 --cluster-init" sh -s -
For more detailed information on the install, see the documentation.