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?
- 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 interesting to read all the text here you can also watch this video below,
Install K3S on Raspberry Pi
Step 1: Setup Raspberry Pi SSH
- Plug your SDCard into your micro SDCard adapter.
- Now plug the adapter into your main PC.
- Download 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 click 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.
- 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 [email protected][IP_YOU_ASSIGN_TO_RASPBERRY_PI] $ sudo ssh [email protected]
- 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
Of course, this will disconnect your ssh, 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 -
- (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, 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 the step 1 and 2 to get another ssh Raspberry Pi with new Raspberry Pi
Note: Do not repeat step 3
- SSH into the new Raspberry Pi you want to add as the node
$ ssh [email protected][IP_YOU_ASSIGN_TO_RASPBERRY_PI] $ ssh [email protected]
- Install k3s on new node (knows as 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
- 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.