K3OS Raspberry Pi Deployment and Ecosystem

The implementation of K3s on Raspberry Pi hardware represents a critical intersection between edge computing and container orchestration. While standard Kubernetes is often too resource-heavy for single-board computers, K3s provides a lightweight alternative. However, the underlying operating system choice determines whether the cluster is a stable production-like environment or a source of constant troubleshooting. k3OS emerges as a specialized Linux distribution designed specifically to host K3s, shifting the management paradigm from imperative manual installation to declarative configuration. This architectural shift eliminates common pitfalls associated with general-purpose distributions, such as Raspberry Pi OS, particularly concerning networking and DNS resolution. By treating the operating system as a component of the cluster, k3OS allows for a preconfigured image approach where the system is defined in a YAML file before it ever touches the hardware.

The k3OS Architectural Paradigm

k3OS is not a general-purpose operating system; it is a purpose-built distribution engineered to host K3s. The primary distinction lies in its configuration model. In a traditional deployment, an administrator installs an OS, updates packages, and then runs an installation script. In k3OS, the entire configuration is declared using a YAML file.

This declarative approach means that the environment is immutable and reproducible. Instead of manually configuring the system after booting, the user defines the desired state in a configuration file, and k3OS ensures the system boots into that state. This significantly reduces the "drift" often seen in home lab clusters where different nodes might have slightly different package versions or configuration tweaks.

The impact for the user is a drastic reduction in "hoop-jumping." In standard Raspberry Pi OS environments, DNS resolution inside pods can become a significant failure point, often requiring complex manual interventions to ensure that the second or third node in a cluster can resolve internal service names. k3OS mitigates these issues by integrating the K3s requirements directly into the OS layer.

Image Generation and Deployment Workflow

Deploying k3OS on Raspberry Pi does not follow the standard "flash and configure" workflow of Raspberry Pi OS. Instead, it utilizes a build-process that integrates the configuration into the image itself.

A known method for generating these images involves a script created by Sjors Gielen. This tool allows users to generate k3OS images specifically for the Raspberry Pi architecture.

The process for utilizing this image generator is as follows:

  • Clone the repository at https://github.com/sgielen/picl-k3OS-image-generator.git.
  • Collect the unique MAC address of each Raspberry Pi device intended for the cluster.
  • Create a specific YAML configuration file for each device within the config/ folder. The naming convention for these files must strictly follow the MAC address of the hardware, for example, config/6A:4A:19:32:8A:50.yaml.
  • The script utilizes the MAC address to map the specific YAML configuration to the corresponding physical Raspberry Pi device.

Once the configuration files are prepared, the image is generated by running the following command:

./build-image.sh raspberrypi

This command produces an image file named picl-k3OS-v0.10.0-raspberrypi.img. This image is then flashed to a microSD card using tools such as Balena Etcher. Upon the first boot, the image performs several initialization tasks, which may take a few minutes before the device becomes responsive to network requests.

k3OS Configuration and Management

The simplicity of k3OS lies in its configuration file. Unlike traditional Linux administration, which requires editing multiple files in /etc/, k3OS centralizes its settings.

The primary keys available in the config.yaml include the hostname and SSH authorization. For instance, a first node might be configured with:

hostname: k3s-1

Regarding security and access, k3OS employs a restricted user model. The distribution primarily utilizes a single user named rancher. The root account is systematically disabled, possesses no password, and is never assigned an SSH key. This design minimizes the attack surface of the node.

SSH access is managed through authorized keys. Users can add their public keys to the configuration file to allow the rancher user to connect. Furthermore, k3OS supports a streamlined integration with GitHub, allowing SSH keys to be obtained via the format github:${USERNAME}.

To integrate additional nodes into a k3OS cluster, the administrator must retrieve the token from the primary server node. This is achieved by executing the following command:

ssh [email protected] sudo cat /var/lib/rancher/k3s/server/node-token

(Note: The IP address 192.168.1.1 must be replaced with the actual IP of the server node).

K3s Installation on Raspberry Pi OS

While k3OS offers a declarative approach, many users prefer Raspberry Pi OS, especially on the Raspberry Pi 5. The Raspberry Pi 5 is positioned as a powerhouse for lightweight Kubernetes distributions due to its increased processing capabilities and RAM options.

The hardware prerequisites for a stable K3s environment on Raspberry Pi 5 include:

  • A Raspberry Pi 5 with at least 4GB of RAM, though 8GB is recommended for larger workloads.
  • A high-quality microSD card of 32GB or larger, or an SSD connected via USB for improved boot and I/O performance.
  • A stable power supply and network connection.

The software prerequisites include Raspberry Pi OS (64-bit recommended) and SSH access.

The installation process on Raspberry Pi OS involves several critical steps to ensure Kubernetes can manage memory and processes correctly.

First, the system must be updated and essential tools installed:

sudo apt update -y
sudo apt install -y curl wget

A critical requirement for Kubernetes is cgroup support. Without this, K3s will not function correctly. To enable this, the user must edit the cmdline.txt file:

sudo nano /boot/firmware/cmdline.txt

The following parameters must be added to the end of the line, ensuring they remain on the same line:

cgroup_memory=1 cgroup_enable=memory

After saving the file and rebooting the system via sudo reboot, K3s can be installed using the official installation script:

curl -sfL https://get.k3s.io | sh -

By default, this command installs K3s as a server (control plane), resulting in a single-node cluster. The installation can be verified by checking the service status:

sudo systemctl status k3s

Once verified, the cluster can be tested using kubectl:

sudo kubectl get nodes

To expand the cluster, a worker node can be added by retrieving the server token via sudo cat /var/lib/rancher/k3s/server/node-token and running the following on the worker node:

curl -sfL https://get.k3s.io | K3S_URL=https://<SERVER_IP>:6443 K3S_TOKEN=<TOKEN> sh -

k3OS Build and Version Management

For advanced users and developers, k3OS can be built from source. The build process requires Docker and the execution of the make command. All resulting artifacts are stored in the ./dist/artifacts directory.

The kernel source is maintained separately at https://github.com/rancher/k3os-kernel, which also requires Docker and make for compilation.

To test k3OS in a virtual environment on Linux, users can run the following command:

./scripts/run

This launches a VM of k3OS in the terminal. To exit the VM, the user must use the sequence CTRL+a c to enter the qemu console, and then press q to quit.

Version management in k3OS is handled through environment variables. If K3OS_VERSION is not specified, the system defaults to the latest release. To upgrade the rootfs or kernel, the following commands are used:

export K3OS_VERSION=v0.10.0
/usr/share/rancher/k3os/scripts/k3os-upgrade-rootfs
/usr/share/rancher/k3os/scripts/k3os-upgrade-kernel

It is important to note that these scripts were deprecated as of v0.9.0, although they remain present in the /usr/share/rancher/k3os/scripts directory. Additionally, when performing an overlay installation on Raspberry Pi (using the original Raspbian kernel), the k3os-upgrade-kernel script will exit with a warning and perform no action, as the original distro kernel continues to be utilized.

Hardware Constraints and Memory Analysis

A recurring challenge in deploying K3s on Raspberry Pi hardware is the memory bottleneck. The experience varies significantly across different Raspberry Pi models.

Raspberry Pi Model RAM Capability K3s Performance Status Analysis
Pi Zero 2 ~512MB Failed Daemon fails to start due to insufficient memory.
Pi 3 A+ ~512MB Unusable Memory is exhausted within minutes of startup.
Pi 3 B+ 1GB Minimal/Viable Considered the absolute minimum for functional operation.
Pi 4 / Pi 5 4GB - 8GB High Performance Capable of running server and worker nodes efficiently.

The memory crisis is exacerbated by the use of swap. On Raspberry Pi devices, enabling swap is widely discouraged. This is because the I/O performance of microSD cards is extremely poor. When the system begins to swap, the disk I/O bottleneck "kills" the system, leading to catastrophic performance degradation.

For users attempting to run K3s on severely memory-constrained devices like the Pi Zero 2, standard installations are impossible. Even attempts to use an HA (High Availability) cluster configuration where master nodes are schedulable fail on Pi Zero 2s, although such configurations are viable on Pi 3s and Pi 4s.

Advanced Control Plane Optimization

To mitigate the resource load on the Raspberry Pi nodes, particularly regarding the database, users can externalize the control plane database.

By default, K3s utilizes an embedded etcd. While efficient for many, etcd can suffer from performance issues on slow disks, such as SD cards used in Raspberry Pis. To alleviate this, K3s supports external databases.

Supported external database options include:

  • PostgreSQL
  • MySQL
  • MariaDB
  • External etcd

By running a database like PostgreSQL in a Docker container on a more powerful machine (such as a Mac), the Raspberry Pi nodes are relieved of the heavy lifting associated with the Control Plane DB. This allows the limited RAM and CPU of the Pi to be dedicated to running containerized workloads rather than managing the cluster state.

Summary of Implementation Paths

Depending on the goals of the user, there are two primary paths for implementing a K3s cluster on Raspberry Pi hardware.

The first path is the k3OS approach. This is intended for users who want a "set and forget" experience. By using a declarative YAML configuration and a specialized image, users avoid the common DNS and networking pitfalls associated with general-purpose operating systems. This path is ideal for those who want their cluster to behave like a managed appliance.

The second path is the Raspberry Pi OS approach. This is intended for users who need a full-featured operating system alongside their cluster. This path requires manual configuration of cgroups via cmdline.txt and manual installation of K3s. It offers more flexibility but requires more maintenance and troubleshooting, particularly regarding system updates and networking.

Comparative Analysis of Deployment Strategies

The choice between k3OS and Raspberry Pi OS is fundamentally a choice between an immutable infrastructure and a mutable one.

k3OS implements a pattern similar to "Infrastructure as Code." The use of MAC-address-mapped YAML files means that the configuration is bound to the hardware. This eliminates the need for post-installation scripts and ensures that every node is deployed in a known state. The impact is a more stable environment, particularly for DNS, which is often the primary failure point in Raspberry Pi clusters.

In contrast, the Raspberry Pi OS deployment is an additive process. The user starts with a base OS and adds layers of configuration. While this allows for more customization—such as running non-Kubernetes applications directly on the OS—it introduces the risk of configuration drift. If the cgroup settings are not applied correctly, or if an update reverts a system setting, the cluster may fail.

From a resource perspective, both methods are subject to the physical limitations of the Raspberry Pi. The memory analysis proves that regardless of the OS, K3s requires a minimum of 1GB of RAM to be usable. The attempt to run K3s on Pi Zero 2 hardware demonstrates that the K3s daemon will not even start if the memory is insufficient, regardless of the distribution.

The most effective way to scale these clusters is to address the I/O and memory bottlenecks. Moving from microSD cards to USB SSDs significantly improves etcd performance. Furthermore, externalizing the database to a PostgreSQL or MySQL instance removes the most resource-intensive part of the control plane from the Raspberry Pi, enabling more stable operation on mid-range hardware like the Pi 3 B+.

Sources

  1. Chris Woolum
  2. PicoCluster
  3. k3os GitHub
  4. SUSE Forums
  5. Andre Veiga

Related Posts