Skip to content

Instantly share code, notes, and snippets.

@bart-jansen
Last active January 28, 2022 10:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bart-jansen/f765f3596b32b286b69d082cc6e5b94d to your computer and use it in GitHub Desktop.
Save bart-jansen/f765f3596b32b286b69d082cc6e5b94d to your computer and use it in GitHub Desktop.
K3s Deployment on Raspberry Pi Cluster

K3s Deployment on Raspberry Pi Cluster

K3s is a lightweight version of Kubernetes (K8s) which makes it ideal for running on embedded devices like a Raspberry Pi. This guide explains how to setup a k3s cluster on your Raspberry PI's

Requirements

  • 2 or more Raspberry Pi devices (preferably RPI 4 with at least 2GB memory)
  • microSD/SSD flashed with Raspberry Pi OS Lite via Raspberry Pi Imager
  • All RPIs connected through ethernet or configured WiFi
  • SSH access enabled

Configure Raspberry Pi OS

Once you've flashed a microSD with the latest Raspberry Pi OS Lite with Raspberry Pi Imager, you have to plug it back in your PC and make some changes to it. Before you can access your RPI remotely, you have to create an empy ssh file in the root of your boot Volume:

touch /Volumes/boot/ssh

Enable cgroups by appending the following options at the end of /Volumes/boot/cmdline.txt:

cgroup_memory=1 cgroup_enable=memory

If you're running a 64-bit Raspberry PI (Raspberry Pi 3 or 4), you also need to configure the Raspberry Pi OS to run in 64-bit mode, by adding this to the bottom of /Volumes/boot/config.txt:

arm_64bit=1

cgroups, also known as control groups, are a feature in Linux to allow processes to be managed and monitored. This an essential kernel feature that allows the containerisation features that we require for K3s to work.

Configuration after first boot

Time to put the microSD card in your Raspberry Pi and let it boot. With SSH access enabled, we can connect to the pi via ssh, e.g.: ssh pi@192.168.0.x

The default password is raspberry, but it's good practice to change this right away by running:

passwd

Change the default hostname of your Raspberry Pi to a unique string (e.g. worker-01 or master-01). It's important that these hostnames are unique in your local network and you need to remember them when connecting your nodes to the cluster. This needs to be changed in two different places

sudo nano /etc/hostname

Change the default raspberrypi to e.g. worker-01

sudo nano /etc/hosts

Change the last value after 127.0.1.1:

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

127.0.1.1       worker-01

Optionally, you can also setup Static IPs for your devices.

Update and upgrade to get the latest security updates, and reboot to apply all the changes made:

sudo apt-get update && sudo apt-get upgrade  -y
sudo reboot

Deploy K3s

For k3s we need to setup one of the Raspberry Pi's as a master node and all other Pi's as worker nodes. The master node acts as an orchestrator and schedules all workloads.

Setup master node

We start with configuring one of the Raspberry Pi's as a master node.

Run k3s installer

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

Once installed, give it 1 or 2 minutes to successfully configure itself and then run the following command to see if your cluster is setup properly:

$ sudo kubectl get nodes
NAME        STATUS   ROLES                  AGE     VERSION
master-01   Ready    control-plane,master   2m14s   v1.22.5+k3s1

Once everything looks healthy, save the access token from the master node that the worker nodes can use to connect:

sudo cat /var/lib/rancher/k3s/server/node-token

Setup worker node(s)

Follow the same steps for each worker node up until you've reached the 'Setup master node' section and then run this for each of your worker nodes:

curl -sfL https://get.k3s.io | K3S_NODE_NAME="node01" K3S_URL="https://192.168.1.100:6443" K3S_TOKEN="token from above step" sh -

Replace these values to your :

  • K3S_NODE_NAME should match the hostname you gave your worker node
  • K3S_URL should match the internal IP of your master node (or its hostname)
  • K3s_TOKEN should match the token that you've obtained from your master node earlier

Once you've set up all your nodes, run this on any of the nodes to see if everything is setup properly:

pi@master-01:~ $ sudo kubectl get nodes
NAME          STATUS   ROLES                  AGE     VERSION
master-01     Ready    control-plane,master   6h17m   v1.22.5+k3s1
rpi-400       Ready    <none>                 10m     v1.22.5+k3s1
rpi2          Ready    <none>                 17s     v1.22.5+k3s1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment