-
Notifications
You must be signed in to change notification settings - Fork 29
K3s on Raspberry Pi
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.
- Raspberry Pi
- Docker & Kubernetes
- 4 x Raspberry Pi 3 B+
- 5 x MicroSD cards
- 4 x 1ft Micro USB cables
- 4 x 1ft Cat cables
- 1 x Multi-Pi Stackable Case
- 1 x Desktop USB Port Charger
- 1 x 8-Port Network Switch
Official images for Raspbian Stretch Lite is available to download from the Raspberry Pi website Downloads page.
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.
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
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.
passwd
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
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:
ls ~/.ssh
If you don't have an existing SSH key, you can Generate a new SSH key and adding it to the ssh-agent
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
sudo raspi-config
- Select Network Options
- Select
N1 Hostname
to change hostname
Go back to main menu
- Select option
4 Localisation Options
- Select
T1 Change Locale
to change Locale
Go back to Change Localisation menu
- Select
Change Timezone
to change your Timezone
Go back to Change Localisation menu
- Select
I4 Change Wi-fi Country
to your country
sudo reboot
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
sudo dphys-swapfile swapoff && \
sudo dphys-swapfile uninstall && \
sudo update-rc.d dphys-swapfile remove
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
sudo reboot
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
sudo k3s server &
Wait for k3s to start and to download the required images from the Kubernetes registry. This may take a few minutes.
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
K1089729d4ab5e51a44b1871768c7c04ad80bc6319d7bef5d94c7caaf9b0bd29efc::node:1fcdc14840494f3ebdcad635c7b7a9b7
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)
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.
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