Skip to content

K3s on Raspberry Pi

Syed Sayem edited this page Mar 29, 2019 · 1 revision

Learn how you can build your own Kubernetes cluster on Raspberry Pi. In this post, I will explains how to install Raspbian Stretch Lite image on an SD card. You will need another computer with an SD card reader to install the image.

 

Table of contents

top  

Step 1: Materials

top  

Step 2: Download the image

Official images for Raspbian Stretch Lite is available to download from the Raspberry Pi website Downloads page.

top  

Step 3: Burn Your Image on Sd

You will need to use an image writing tool to install the image you have downloaded on your SD card.

Etcher is a graphical SD card writing tool that works on Mac OS, Linux and Windows, and is the easiest option for most users. Etcher also supports writing images directly from the zip file, without any unzipping required. To write your image with Etcher:

  • Download Etcher and install it.
  • Connect an SD card reader with the SD card inside.
  • Open Etcher and select from your hard drive the Raspberry Pi .img or .zip file you wish to write to the SD card.
  • Select the SD card you wish to write your image to.
  • Review your selections and click 'Flash!' to begin writing data to the SD card.

top  

Step 4: Enable SSH & WiFi

Etcher automatically ejects the drive when the flashing procedure is completed, so you may have to remove and reinsert the microSD card in your computer.

  • to enable SSH access, create an empty file called “ssh” (no file extension!) and put it on the microSD card (/boot)
  • to enable WiFi, create a file called “wpa_supplicant.conf” with following content, on the microSD card (/boot):
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
  ssid="<your_ssid>"
  psk="<your_password>"
  key_mgmt=WPA-PSK
}

More information on How to Setup Wi-Fi On Your Raspberry Pi via the Command Line

top  

Securing a Raspberry Pi

The security of your Raspberry Pi is important. Gaps in security leave your Raspberry Pi open to hackers who can then use it without your permission.

Change your default password
passwd
Changing your username

To add a new user, enter:

sudo adduser sayem

To add them to the sudo group to give them sudo permissions:

sudo adduser sayem sudo

Force sudo to require a password, enter:

sudo nano /etc/sudoers.d/010_pi-nopasswd

and change the pi entry (or whichever usernames have superuser rights) to:

sayem ALL=(ALL) PASSWD: ALL

More information on Securing your Raspberry Pi

top  

Password-less SSH Login

It is possible to configure your Raspberry Pi to allow your computer to access it without providing a password each time you try to connect. To do this you need to generate an SSH key:

Check for existing SSH keys
ls ~/.ssh
Generate a new SSH key

If you don't have an existing SSH key, you can Generate a new SSH key and adding it to the ssh-agent

Copy your public key to your Raspberry Pi
ssh-copy-id <USERNAME>@<IP-ADDRESS>

Alternatively, if the ssh-copy-id is not available on your system, you can copy the file manually over SSH:

cat ~/.ssh/id_rsa.pub | ssh <USERNAME>@<IP-ADDRESS> 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'

Now try ssh <USER>@<IP-ADDRESS> and you should connect without a password prompt.

More information on Passwordless SSH access

top  

Change hostname, (ex. k8s-master)

sudo raspi-config
Change hostname
  • Select Network Options
  • Select N1 Hostname to change hostname
Change Localisation

Go back to main menu

  • Select option 4 Localisation Options
  • Select T1 Change Locale to change Locale
Change Timezone

Go back to Change Localisation menu

  • Select Change Timezone to change your Timezone
Change Wi-fi country

Go back to Change Localisation menu

  • Select I4 Change Wi-fi Country to your country

Restart your Raspberry Pi

sudo reboot

top  

Install Docker:

It’s not required to install Docker since K3s uses containerd, but it's useful to be able to verify that the pods are actually running. Docker can be installed quickly by running the following command:

curl -sSL get.docker.com | sh && \
sudo usermod -aG docker sayem \
newgrp docker

top  

We need to then disable swap. Kubernetes requires swap to be disabled.

sudo dphys-swapfile swapoff && \
  sudo dphys-swapfile uninstall && \
  sudo update-rc.d dphys-swapfile remove

top  

Enable container features

We need to enable container features in the kernel, edit /boot/cmdline.txt and add the following to the end of the line:

cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory

top  

Restart your Raspberry Pi

sudo reboot

top  

Install Kubernetes tools:

The current version of k3s is v0.2.0, but you can always visit the releases page to check for a newer version.

On one of the nodes log in and do the following:

wget https://github.com/rancher/k3s/releases/download/v0.2.0/k3s-armhf && \
  chmod +x k3s-armhf && \
  sudo mv k3s-armhf /usr/local/bin/k3s

top  

Master Node Setup

sudo k3s server &

Wait for k3s to start and to download the required images from the Kubernetes registry. This may take a few minutes.

You should get an output similar to
sayem@raspi1:~ $ INFO[2019-03-14T01:08:42.601602978-04:00] Starting k3s v0.2.0 (2771ae1) 

Now, press Enter and run the following command:

sudo cat /var/lib/rancher/k3s/server/node-token
Grab the join key:
K1089729d4ab5e51a44b1871768c7c04ad80bc6319d7bef5d94c7caaf9b0bd29efc::node:1fcdc14840494f3ebdcad635c7b7a9b7

top  

Troubleshooting

If you get an error like the one below, you might need to reboot your Raspberry Pi.

ERRO[2019-03-14T01:00:25.214592577-04:00] Failed to find memory cgroup, you may need to add "cgroup_memory=1 cgroup_enable=memory" to your linux cmdline (/boot/cmdline.txt on a Raspberry Pi)

top  

Join a worker

Now log into another Raspberry Pi like raspi2, raspi3, raspi4 and download the binary if you haven't already done so.

You can download it by running the following command:

wget https://github.com/rancher/k3s/releases/download/v0.2.0/k3s-armhf && \
  chmod +x k3s-armhf && \
  sudo mv k3s-armhf /usr/local/bin/k3s

Now join the worker to the server with the following:

$ export NODE_TOKEN="Paste The Join Key here"
$ export SERVER_IP="https://192.168.1.6:6443"
$ sudo -E k3s agent -s ${SERVER_IP} -t ${NODE_TOKEN} &

That's it.

 

List your nodes

Now go back to server terminal and run the following command:

sudo k3s kubectl get node -o wide
NAME     STATUS   ROLES    AGE     VERSION
raspi1   Ready    <none>   6m59s   v1.13.4-k3s.1
raspi2   Ready    <none>   2m15s   v1.13.4-k3s.1

top  

Deployment

top  

Clone this wiki locally