Building a kubernetes cluster on raspberry pi using k3s

Building a kubernetes cluster on raspberry pi using k3s

On a smaller raspberry pi where you don't have that much resources (usually 1-2 gb of ram) you don't have the resources to run a full fledged kubernetes cluster. There is a project for providing fully working lightweight kubernetes cluster using rancher's k3s.

Prerequisites

In order to be able to install k3s on raspberry pi you need one or more raspberry pi's, having raspberrian or ubuntu loaded up into the sdcard of the pi's. A fully working networking.

Installation

Installation of the k3s is fairly simple. Let's assume we have two raspberry pi's working as two different nodes. One that will be the manager and the other the worker node of our cluster (I was told that it is politically incorrect to use the terminology master and slaves). First of all you need to make sure that you have these two option added to /boot/firmware/cmdline.txt cgroup_enable=memory cgroup_memory=1:

# cat /boot/firmware/cmdline.txt 
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=LABEL=writable rootfstype=ext4 elevator=deadline rootwait fixrtc quiet splash cgroup_enable=memory cgroup_memory=1

Once you added the options reboot your pi's to activate the options.
On the master you will be running the following command line:

#curl -sfL https://get.k3s.io | sh -
[INFO]  Finding release for channel stable
[INFO]  Using v1.20.4+k3s1 as release
[INFO]  Downloading hash https://github.com/k3s-io/k3s/releases/download/v1.20.4+k3s1/sha256sum-amd64.txt
[INFO]  Downloading binary https://github.com/k3s-io/k3s/releases/download/v1.20.4+k3s1/k3s
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Creating /usr/local/bin/kubectl symlink to k3s
[INFO]  Creating /usr/local/bin/crictl symlink to k3s
[INFO]  Creating /usr/local/bin/ctr symlink to k3s
[INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
[INFO]  Creating uninstall script /usr/local/bin/k3s-uninstall.sh
[INFO]  env: Creating environment file /etc/systemd/system/k3s.service.env
[INFO]  systemd: Creating service file /etc/systemd/system/k3s.service
[INFO]  systemd: Enabling k3s unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
[INFO]  systemd: Starting k3s

After a short while if you are running the kubectl get nodes you'll end up with something like this:

# kubectl get nodes
NAME      STATUS   ROLES                  AGE     VERSION
manager   Ready    control-plane,master   4m46s   v1.20.4+k3s1

Now it's time to install the slave. For this we need to look into the /var/lib/rancher/k3s/server/node-token and copy the node token from the file:

# cat /var/lib/rancher/k3s/server/node-token
    K1073b3f02527159c80edf837727ae35cde5169707a7be0ecc91ca5a852e1e2c76f::server:c86cf36c0577ece1ef6d2cf072ece1c4

Now login to each of the worker nodes and run the following commands:

# curl -sfL https://get.k3s.io | K3S_URL=https://<manager ip address>:6443 K3S_TOKEN=K1073b3f02527159c80edf837727ae35cde5169707a7be0ecc91ca5a852e1e2c76f::server:c86cf36c0577ece1ef6d2cf072ece1c4 sh -
[INFO]  Finding release for channel stable
[INFO]  Using v1.20.4+k3s1 as release
[INFO]  Downloading hash https://github.com/k3s-io/k3s/releases/download/v1.20.4+k3s1/sha256sum-amd64.txt
[INFO]  Downloading binary https://github.com/k3s-io/k3s/releases/download/v1.20.4+k3s1/k3s
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Creating /usr/local/bin/kubectl symlink to k3s
[INFO]  Creating /usr/local/bin/crictl symlink to k3s
[INFO]  Creating /usr/local/bin/ctr symlink to k3s
[INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
[INFO]  Creating uninstall script /usr/local/bin/k3s-agent-uninstall.sh
[INFO]  env: Creating environment file /etc/systemd/system/k3s-agent.service.env
[INFO]  systemd: Creating service file /etc/systemd/system/k3s-agent.service
[INFO]  systemd: Enabling k3s-agent unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s-agent.service → /etc/systemd/system/k3s-agent.service.
[INFO]  systemd: Starting k3s-agent

Again after a few minutes if you run kubect get nodes on the manager you will see both nodes in Ready state:

# kubectl get nodes
NAME      STATUS   ROLES                  AGE     VERSION
manager   Ready    control-plane,master   4m46s   v1.20.4+k3s1
worker    Ready                           4m10s.  v1.20.4+k3s1

Now your kubernetes cluster is ready to be used. The previous commands are working on most of the linux environments not only on raspberry pi's. Therefore you can create a small kubernetes cluster on small virtual machines with not a whole bunch of resources.