How to Build a Raspberry Pi 4B Kubernetes Cluster

Jul 12th 2020

By Robert Kirchner JR


my raspberry pi cluster

In this tutorial, we will be building a Kubernetes computer cluster out of five Raspberry Pi 4B single board computers.

Kubernetes?

Kubernetes is a platform for building scalable software application environments with computer clusters. You can read more here.

Raspberry Pi

The Raspberry Pi is a line of single board computers(SBC) developed by the Raspberry Pi Foundation, with a focus on computer programming and electronics education.

Raspberry Pis enjoy lots of popularity from builders and tinkers across the world. I will even admit that they are one of my favorite toys. They are used in applications from nano application servers, to remote soil moisture sensors.

The Raspberry Pi 4B

The Pi 4B is the latest generation of single board computers in the Raspberry Pi family. Enjoying these stats:

  • Quad-core 1.5Ghz ARM64 SoC
  • Up to 8GB DRR4 Low Power Memory
  • 2.4GHz and 5GHz AC WiFi
  • Low Energy Bluetooth
  • Dual 4K Micro HDMI Outputs
  • USB 3.0
  • Gigabit Ethernet

You can read our unboxing review here.

The Raspberry Pi 4B offers whole new potential in the world of nano application servers.

The Build

Materials

Here is a list of the materials we used for our experiment:

  • Windows 10 PC
  • 5 Raspberry Pi 4B 4GB(You could use any combination of Pi 4Bs)
  • 1 Anker 60W 6 Port USB Charging Stations
  • 5 Sandisk Ultra 16GB MicroSD Cards
  • 256 2.5 inch SATA 3 Solid State Drive
  • USB 3.0 2.5 inch External SATA Enclosure
  • 8 port TP-Link Gigabit Ethernet Switch
  • USB 3.0 SD Card Reader

k3s

Rancher k3s is a lightweight Kubernetes distribution that seems the most well suited for the Raspberry Pi. You can find it here.

Format SSD

Next we need to format the USB SSD that the master node will use. Format the SSD to ext4 format using your favorite disk tool, we will use Minitool Partition Wizard.

Ubuntu Server

The good folks at Ubuntu have put together a 64 bit Ubuntu Server distribution just for the Pi 4B. You can fin it here. You will need to download the ARM64 Preinstalled Server image.

Flashing Ubuntu to SD

We will make two master SD Cards, one for the master, and one for the slaves. Will will then just flash copies of these for the rest of the cards.

You will need to get the Rufus USB Disk Flashing Tool. Now load the SD card in the reader adapter and insert it into your USB port.

Open Rufus and select your SD card under Device.

Next, under Boot selection, navigate to the Ubuntu Server image that you downloaded previously

Leave all the other other settings alone, make sure that you have backed up all the data on your SD card, and press the Start button.

A prompt about erasing the SD card will come up, again, make sure your data is backed up, and press the continue button.

Do this for your two master SD cards.

OS Setup for Nodes

The USB SSD will be controlled by our master node. We will need to setup the SSD to mount at boot, and to configure a static ip address.

Now insert your SD card, ethernet cord, HDMI, and USB SSD enclosure into the pi that you have selected to be the master node of your Kubernetes cluster, and plug in the power cord.

Go through the dialog to set up your admin account.

Now we will run system updates


sudo apt update
sudo apt upgrade

Set Static IP

Next we will setup our Raspberry Pis to have static IP addresses from 192.168.1.100 to 192.168.1.104 when connected to our 8 port switch.

Navigate to /etc/netplan and remove all files in the directory.

Next, using your favorite text editor(we will use nano for ease), create a new file in this folder


sudo nano /etc/netplan/my-net-config.yaml

Now add this contents:


network:
  version: 2
  ethernets:
    eth0:
      dhcp4: false
      addresses: [192.168.1.100/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 4.4.4.4]

Where gateway4 is the address of our router.

Save the file(ctrl o), exit(ctrl x) and reboot your pi.

We now need to enable memory cgroups that is required by Kubernetes.

Run command the following command and reboot


sudo sed -i '$ s/$/ cgroup_enable=memory cgroup_memory=1/' /boot/firmware/nobtcmd.txt

Now repeat the process for the second master SD card, using another IP address that is not in your desired Kubernetes cluster range. This will be the final base for our worker node SD.

Master Node USB SSD

We will create a folder that our SSD will mount to at boot


mkdir /ssd

We will access the SSD from this directory

Now we need to setup auto mount, we will edit fstab


sudo nano /etc/fstab

Insert this line to the bottom of the file


UUID=0e281f8d-1117-451d-9b1b-b07405a631bc /media auto nosuid,nodev,nofail 0 0

Where the UUID is the ID for your drive from


sudo blkid /dev/sdb2 | awk -F'"' '{print $2}'

Reboot

Backup Master Node SD Card

Now use Rufus to backup this image for your master node.

Install k3s on Master Node

Now we will install Kubernetes. The k3s install is very easy, involving only a simple cURL call to download and run the install script.


curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 644

Now to verify that everything is right, you should run this command and see something similar:


kubectl version

Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.3-k3s.2", GitCommit:"e7e6a3c4e9a7d80b87793612730d10a863a25980", GitTreeState:"clean", BuildDate:"2019-11-18T18:31:23Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/arm64"}
Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.3-k3s.2", GitCommit:"e7e6a3c4e9a7d80b87793612730d10a863a25980", GitTreeState:"clean", BuildDate:"2019-11-18T18:31:23Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/arm64"}

kubectl get no -o wide

NAME          STATUS   ROLES    AGE     VERSION         INTERNAL-IP    EXTERNAL-IP   OS-IMAGE       KERNEL-VERSION      CONTAINER-RUNTIME
kube-rpi-cp   Ready    master   3m10s   v1.16.3-k3s.2   192.168.0.42   <none>        Ubuntu 19.10   5.3.0-1014-raspi2   containerd://1.3.0-k3s.5

You now have a working single node Raspberry Pi Kubernetes cluster!

Install k3s Kubernetes on Worker Nodes

To install Kubernetes on your worker nodes, make a copy one at a time of the worker SD card master.

Master Node Token

We need to retrieve our k3s token from our master node.

On the master, run:


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

Take note of the entire output.

Set Worker IP

After you flash each SD card(you must do this one at a time to avoid ip address conflicts), you must set its unique IP address.

Let's edit are net config file:


sudo nano /etc/netplan/my-net-config.yaml

Change the address in the address field to the next IP in your range. Ex 192.168.1.101

Save(ctrl o), exit(ctrl x) and reboot.

Add Worker to Cluster

Now, to add a worker to our cluster, we run a simple command:


curl -sfL https://get.k3s.io | K3S_URL=$k3s_server K3S_TOKEN=$k3s_token sh -

Where K3S_URL = https://our-master-node-local-ip:6443 and K3S_TOKEN = the output we received from running the cat command on our master node.

That Easy

You now have your own Kubernetes cluster, ready to do Kubernetes stuff, whatever that might be for you.

Comments




Navagation