Build a Kubernetes (microk8s) cluster using Raspberry Pi


Posted on 2/7/2021


Kubernetes is also known as K8s, is an open-source container orchestrator originally build by Google. Kubernetes enables you to automate, scale and manage your container workloads.

Why Raspberry?

Raspberry Pi is a single-board computer developed by the Raspberry PI Foundation. Raspberry Pi's enable you to play with low cost "metal". In my initial Cluster setup, I used only the Raspberry Pi 4 Model B which comes with 4 GB of RAM which costs at the time of writing around 60€. Adding Power Supply and a Micro SD Card will end up (depending on choice) at around 75€ per Node.

Disclaimer: if you want to use Kubernetes outside of a home or lab scenario with production workloads I highly recommend you to look for a more reliable compute option. Maybe even consider Cloud options as those will save you a lot of struggle setting it up. Cloud Provider such as Azure, Digital Ocean (Note: this is an affinity link), Google, etc. has their own fully managed K8s offerings.

The initial setup

I started with an initial node count of 4 Raspberry Pi's which seemed for me the right amount of nodes to start. I used the following components (I rounded up the prices):

Name Amount Price Total
microSD Card 4 7€ 28€
Raspberry Pi 4 Modell B 4GB 4 60€ 240€
Power Supply 4 9€ 36€

Which sums up to 304€. Optionally you could add a heat sink, and cases which might add additional 20-30€ per node depending on your choice. Of course, you would also need network cables. I assume you might have some already.

After wiring up everything and connecting the Pi's to a switch I needed to flash the micro SD cards. I won't go into the process needed to flash a Raspberry Pi as there are plenty of pages out there explaining how.

Insert the SD card into your computer and use whatever tool you like to flash it. (I used Ubuntu 20.04 LTS). Afterward, I placed an empty "ssh" file into the boot partition as this enables SSH and I wanted to connect via SSH from the beginning. Inserting the micro SD cards into the PI's, powering them up, getting the IPs from my DHCP Server leases, setting up a new password, updating, setting static IP, and changing the hostname I was ready to go.

I will also skip the step of setting up static IPs, hostnames, etc. on the PIs as this should be quite known. Otherwise, just ask your search engine of choice ;-)

Installing Microk8s

Before we can install microk8s we need to enable c-groups for Kubernetes to work by editing the following file:

sudo nano /boot/firmware/cmdline.txt

Add the following options and reboot afterwards:

cgroup_enable=memory cgroup_memory=1

Installing Microk8s is quite straight forward after these steps. I used snap to install microk8s. Snap is a software packaging system by Canonical. As "snap" comes with Ubuntu already installed you only need to run the following command:

sudo snap install microk8s --classic --channel=1.20

After installing microk8s on all Nodes we can join them together into one cluster. On the primary Node run

sudo microk8s add-node

This will output a command with a generated token. Copy the command with your network IP to the other Nodes. Important: every node requires an own token. So you need to run add-node multiple times.

After all nodes are joined you are pretty much ready to have fun with Kubernetes. Check the status of the Kubernetes Cluster:

sudo microk8s status

sudo microk8s kubectl get nodes

Those commands should output the cluster health and show you all nodes.

Configure your cluster

Note: You can add your current user to the microk8s group, so you don't need to sudo every time. You need to log off and on again for those changes to work.

sudo usermod -a -G microk8s $USER
sudo chown -f -R $USER ~/.kube

As microk8s uses a namespaced kubectl to avoid conflicts with existing installations you can run the following command if you don't have kubectl installed to avoid to type always microk8s.kubectl

alias kubectl='microk8s kubectl'

Enable add-ons

Microk8s comes with the bare minimum installed. So you might want to install some add-ons The following list is from my experience what should be installed:

Depending on your network setup you want to configure an upstream DNS. To do this change the command to dns:172.16.1.254 (replace the IP with your upstream). Otherwise, Google DNS will be used.

sudo microk8s enable storage ingress dns

Those 3 add-ons will provide internal DNS resolution (service discovery), a basic storage class for persistent storage, and an ingress Nginx reverse proxy service.

At this stage, you are pretty done and ready to use Kubernetes.

Have fun :)