Install K3S on Raspberry Pi, Nowadays Kubernetes is the most killer skill in IT, computer networking, or even in programming, I’ve been interested in building a Kubernetes cluster of Raspberry Pi(s). With Raspberry, we use k3s instead of k8s because k3s is lightweight and easy to set up.
So here in this tutorial, I am going to set up a cluster of 8 raspberries.
Pre-requirements for this setup?
- Raspberry
- 32GB SDCard
- Power supply for raspberry
- A micro SDCard adapter
What is K3S?
K3s is a lightweight version of Kubernetes that is all in a binary of less than 100 MB. K3s is developed by Rancher Labs on Feb 26, 2019. It’s highly available, fully CNCF (Cloud Native Computing Foundation) certified Kubernetes distribution designed for production workloads in unattended, resource-constrained, remote locations or inside IoT appliances.
Here is the Video tutorial for this.
If you aren’t interested to read all the text here you can also watch this video below,
Install K3S on Raspberry Pi
Follow the given 3-step process.
Step 1: Setup Raspberry Pi SSH
- Plug your SDCard into your micro SDCard adapter.
- Now plug the adapter into your main PC.
- Download the Raspberry Pi imager from their download page.
- Open Raspberry Pi Imager
- Click on “Choose Image” and then “Raspberry Pi OS (other)”
- Then choose “Raspberry Pi OS Lite”
- Now choose your micro SDCard by clicking “Choose Storage”
- Then click on write, (confirm the write by clicking on yes)
- When it finishes remove your SDCard from your computer and plug it into your Raspberry Pi.
- Boot your Raspberry Pi
- Wait (5 min) to boot up Raspberry Pi.
- Now again unplug the SDCard from Raspberry Pi and plug it back into your computer.
- This time when you plug this SDCard into your PC you see a “Boot” disk, open it
- Here look for the “cmdline.txt” file and open it with your perfected text editor.
- Add the following line to the end of the line.
cgroup_memory=1 cgroup_enable=memory ip=[ENTER_AN_IP_FOR_YOUR_RASPBERRY_PI]::[DEFULT_GATEWAY]:[SUBNET_MASK]:[NAME_FOR_RASPBERRY_PI]:[NETWORK_INTERFACE]:off
E.g:
cgroup_memory=1 cgroup_enable=memory ip=192.168.1.10::192.168.1.1:255.255.255.0:rpimaster:eth0:off
- Save this file and close
- Open another file labeled “config.txt”
- Add the following line to the button of the file.
arm_64bit=1
- Save this file and close
Now one last thing is to enable SSH on Raspberry Pi
- To do that open Powershell in Windows and terminal in Linux and Mac.
- Navigate to the “boot” drive
- Exclude the following command
# For windows
new-item ssh
# For Linux and Mac
touch ssh
- Unplug the micro SDCard from the PC and plug it into your Raspberry Pi .
- Boot the Raspberry Pi
Step 2: Prepare for K3S
- SSH into your Raspberry Pi using the following command.
$ sudo ssh pi@[IP_YOU_ASSIGN_TO_RASPBERRY_PI]
$ sudo ssh pi@192.168.1.10
- Enter the password for Raspberry Pi
In most cases, the default password is “raspberry”
- Enable IPTABLE (requied for k3s)
$ sudo iptables -F
- Reboot the system once
$ reboot
Of course, this will disconnect your ssh, and reconnect after 5 min.
Step 3: Install K3S in Raspberry Pi
- Download and install k3s by executing the following command
$ sudo curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE=”644” sh -
This command download and install k3s on your system
- (Optional) Verify K3s is properly installed by executing the following command
$ sudo kubectl get nodes
Step 4: (Optional) Add more nodes (machines or Raspberry Pi)
NOTE: If you have only one Raspberry Pi then you properly skip this step, this step is all about adding more machines or Raspberry Pi in your k3s cluster.
- Now you need a master token from your first node or Raspberry Pi, and execute the following command.
$ sudo cat /var/lib/rancher/k3s/server/node-token
# My Output:
K1089729d4ab5e51a44b1871768c7c04ad80bc6319d7bef5d94c7caaf9b0bd29efc::node:1fcdc14840494f3ebdcad635c7b7a9b7
- Copy the output (called a token)
- Respect step 1 and 2 to get another ssh Raspberry Pi with a new Raspberry Pi
Note: Do not repeat step 3
- SSH into the new Raspberry Pi you want to add as the node
$ ssh pi@[IP_YOU_ASSIGN_TO_RASPBERRY_PI]
$ ssh pi@192.168.1.11
- Install k3s on the new node (known as the worker node)
Execute the following command
$ sudo curl -sfL https://get.k3s.io | K3S_TOKEN=”[THE_TOKEN_YOU_COPIED]” K3S_URL=”https://[YOUR_FRIST_RASPBERRY_PI]:6443” K3S_NODE_NAME=”[NAME_YOU_WANT]” sh -
# E.g:
$ sudo curl -sfL https://get.k3s.io | K3S_TOKEN=”K1089729d4ab5e51a44b1871768c7c04ad80bc6319d7bef5d94c7caaf9b0bd29efc::node:1fcdc14840494f3ebdcad635c7b7a9b7” K3S_URL=”https://192.168.1.10:6443” K3S_NODE_NAME=”rpi_worker_1” sh -
- Close the ssh by executing the ‘exit’ command
$ exit
- To make sure your node is added to the master node, go to the master node and execute the following command
$ sudo kubectl get node
Congrats you successfully configure your cluster now you can do all the crazy stuff of Kubernetes.
Now that I’ve got this cluster up and running, I can start playing around with all sorts of Kubernetes features and even use it for production purposes. It’s going to be a lot of fun experimenting with it all.
You also learned quite a bit about setting up Raspberry Pi and installing K3s on it. You even figured out how to add more nodes to the cluster and obtain the necessary tokens for it. It’s been a great learning experience overall.
If you’re looking for a fun project to try out, I highly recommend building your own Kubernetes cluster using Raspberry Pi. Not only will you get to work with Kubernetes firsthand, but you’ll also get to experiment with the capabilities of Raspberry Pi – it’s a win-win!
If you have any questions or comments, I’d love to hear from you. Just drop them in the comment section below. Happy coding, everyone!
2 thoughts on “Install K3S on Raspberry Pi [Complete setup]”
How is storage handled on the cluster? Is, for example, the container for Minecraft replicated across all worker nodes, or is it migrated from one node to the next if the cluster decides to move it? I’m working on collecting the resources (RPIs) to build a K3s cluster, but was hoping there was a way to use shared storage, like a M.2 to USB HDD. Is there a better way to attach shared storage without a single point of failure?
This might answer your question if you haven’t already gotten one….
https://llimon.github.io/post/k3s-nfs/