Building a Raspberry PI Based Kubernetes Cluster

This is a quick guide on building a Raspberry PI 4 Based Kubernetes cluster. For this ill be using 4 Raspberry Pi 4’s.

For starters we need to make sure we have our base image setup correctly. I am using Raspbian Lite for my install. Here are the steps i went through.

  • Static IP’s on all Pi’s:
    • This one is fairly simple, the file you need to edit is /etc/dhcpcd.conf . You need to add in the following (though changing it for your network range and changing the IP address per host:
      • static ip_address=192.168.0.x/24
      • static router=192.168.0.1
      • static domain_name_servers=192.168.0.x 192.168.0.x
  • Set hostnames, im using an internal DNS server with internal records so i have used the following:
    • master.k8s.x.x
    • node1.k8s.x.x
    • node2.k8s.x.x
    • node3.k8s.x.x
  • Set the pi user password to something proper.
  • Copy over SSH keys – just to make life easier for you!
  • Set the GPU memory to 16MB. These are headless servers, we dont need the GPU.
  • Finally, enable the container kernel features with the following:
    • cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory this needs to go at the end of /boot/cmdline.txt

Once all the above steps have been completed you will need to reboot each of the pi’s.

Configuring the Master Node

Lets get onto setting up the master node. The Rancher guys have made this really simple. You should be really careful when piping directly from curl, so i had a quick read through the script so im happy to run it. You should do so too.

All we need to do is run the following on the Master node:

curl -sfL https://get.k3s.io | sh - give it a couple of minutes to do its thing and thats it. Done. You can confirm that its running by doing a sudo systemctl status k3s

While we are still on the Master we need to grab our join key which is just a token we will use on our worker nodes to “authenticate” with the master.

Just run the following sudo cat /var/lib/rancher/k3s/server/node-token

This will display a long multicharacter string, copy this down.

Configuring the Worker Nodes

This is a simple as running 3 commands on each node.

The first is exporting the URL of our master node, the second exporting the join token from earlier and the third being being the install script.

  • Exporting the master URL:
    • export K3S_URL="https://192.168.0.70:6443" of course you will need to change the IP address to that of your own master.
  • Export the token:
    • export K3S_TOKEN="xxxxxxxxxxxxxx::node:xxxxxxxxxxxxx" again, you need to change the token to your own.
  • Run the installer on each of the nodes:
    • curl -sfL https://get.k3s.io | sh - This will install the k3s-agent on each of the nodes.

Finally, you can now confirm that all nodes have connected to your master with the following command (run from the master):

sudo kubectl get node -o wide you should get an output similar to this: