Setting Up K3s on Raspberry Pi

Setting Up K3s on Raspberry Pi

Raspberry Pi Kubernetes Cluster (Part2)

This is part 2 of my small project setting up and learning to use a Kubernetes cluster. In part one, I build the hardware for the cluster, check the article. Now we have to configure all the elements of the system. The first step is creating the K3s cluster. K3s is the lighter version of the main K8s Kubernetes system. K3s is designed for use in situations with fewer system resources like the one we have here.

Flash Raspbian image

The first step is flashing the basic Raspbian Lite image into our microSD cards for every node in the cluster. We are going to use the raspberry pi imager. This will allow us to preconfigure the following parameters allowing us to access the raspberry pi via ssh immediately.

  • Hostname

  • Enable SSH

  • Username/Password

  • Locale

  • Wireless network (if needed)

Configuring cmdline.txt file (Cgroups and StaticIP)

After burning the Raspbian image we have to set advanced options, this can be made by modifying the cmdline.txt file on the microSD card.

Open cmdline.txt and got to the end of the line and after rootwait type the following:

cmdline.txt
rootwait cgroup_memory=1 cgroup_enable=memory ip=192.168.11.182::192.168.11.1:255.255.255.0:rocinanteSlave1:eth0:off quiet

Disable Wi-Fi and Bluetooth

In my system and going to connect the nodes to the network via ethernet, and because of this I have no use for the onboard Wi-Fi and Bluetooth. To save some power and heat I will disable both of them. This is accomplished by adding this two lines to the config.txt file.

config.txt
dtoverlay=disable-wifi dtoverlay=disable-bt

Repeat all operations in the rest of the nodes of the cluster.

Configure master

Update Node

Run the sudo apt update and sudo apt dist-upgrade to ensure that everything is up to date.

Personal configuration

Configure any personal settings such as text editors, folder structure etc.

Enable IP tables

First enable Iptables with the following command and reboot the system.

sudo iptables -F

Install K3s

Then to install K3s there is an script available on the web so we will run that script with the following command

sudo curl -sfL get.k3s.io | K3S_KUBECONFIG_MODE="644" sh -s

After the script finishes we can run the command kubectl get nodes and we should see something like this.

NAME STATUS ROLES AGE VERSION

rocinantemaster Ready control-plane,master 2m v1.25.7+k3s1

If everything is OK the master is already configured, at last in order to configure the slaves we need a token from the master. To get that token we run the following command.

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

Configure slaves

Update, personalize and enable IP tables

We first execute the first 3 steps the same way as we did in the master.

Install K3s

To install K3s in the slaves, we will use the same script we use in the master but the parameters will be different. Use the following command after replacing the token and the IP with yours.

sudo curl -sfL get.k3s.io | K3S_TOKEN="K10a9821f6b37e219d22f27c1e04e1e46e9d381c84030ef43a117a9a73adbb5559b::server:cc0af4ae34e02afcec5691776f3ea7cf" K3S_URL="https://192.168.11.181:6443" sh -s

Check configuration correct

After configuring all we execute the command kubectl get nodes in the master, you should get something like this:

NAME STATUS ROLES AGE VERSION

rocinanteslave1 Ready 3m17s v1.25.7+k3s1

rocinantemaster Ready control-plane,master 20m v1.25.7+k3s1

rocinanteslave2 Ready 103s v1.25.7+k3s1

rocinanteslave3 Ready 34s v1.25.7+k3s1

Deploy test payload

To test that everything is OK we will deploy an nginx application. The objective of this is to check the cluster, run the following command

kubectl create deployment nginx --image=nginx

After some time we will execute the kubectl get deployments command and get the following answer:

NAME READY UP-TO-DATE AVAILABLE AGE

nginx 1/1 1 1 49s

After that we execute the following command to delete the deployment and leave an empty cluster.

kubectl delete --all deployments

Did you find this article valuable?

Support Artic blog by becoming a sponsor. Any amount is appreciated!