How to build your own Raspberry Pi Kubernetes Cluster
Overview
Kubernetes is a very powerful platform to scale your applications, and the Raspberry Pi is a low-cost computer with excellent power efficiencty you can use to run tasks without breaking the bank. If you put the two together, you can have a low-cost and scalable platform for Kubernetes on the Raspberry Pi. In this video, we take a look at how to create a Pi-powered Kubernetes cluster.
Relevant Links |
---|
Original Video |
Raspbian Downloads |
Errata
Pods created do not have access to the Internet. The instructions on this page have been fixed, but as a result, one command will be changed from the video and one new command was added that was not present in the video.
What You'll Need
- At least two Raspberry Pi boards (Pi 4 is best)
- A Raspberry-certified power supply for each
- An SD card with decent speed
- SD card flashed with Raspbian (Lite edition preferred)
Set-up Process (do the following on each Raspberry Pi)
Edit the host name
Edit /etc/hosts and /etc/hostname on the SD card to the actual name of the instance
For example:
k8s-master k8s-worker-01
(Or whatever naming scheme you wish)
Configure boot options
Edit /boot/cmndline.txt and add:
cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory
Note: Add that to the end of the first line, do not create a new line.
Install all updates
sudo apt update && sudo apt dist-upgrade
Disable swap
sudo dphys-swapfile swapoff sudo dphys-swapfile uninstall sudo apt purge dphys-swapfile
Reboot
Reboot each Pi:
sudo reboot
Install Docker
curl -sSL get.docker.com | sh sudo usermod -aG docker jay
Set Docker daemon options
Edit the daemon.json file (this file most likely won't exist yet)
sudo vim /etc/docker/daemon.json
{ "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2" }
== Enable routing
Find the following line in the file:
/etc/sysctl.conf
#net.ipv4.ip_forward=1
Uncomment that line.
Reboot again
sudo reboot
Add Kubernetes repository
sudo vim /etc/apt/sources.list.d/kubernetes.list
Add:
deb http://apt.kubernetes.io/ kubernetes-xenial main
Add the GPG key to the Pi:
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
Install required Kubernetes packages
sudo apt update sudo apt install kubeadm kubectl kubelet
Note: If you get errors with the first command, wait a few minutes and try again.
Master-only
Initialize Kubernetes
Run:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
Note: The video, and the previous version of this article recommended the following command:
kubeadm init --pod-network-cidr=10.17.0.0/16 --service-cidr=10.18.0.0/24 --service-dns-domain=mydomain.com
While that can work, it goes against the best practice for flannel. Source. Thank you to the viewer that brought this to my attention, but I cannot find his comment to credit him.
Once this runs, you will get some output that will include the join command, but don't join nodes yet. Copy this somewhere for later.
Set up config directory
The previous command will give you three additional commands to run, most likely these:
mkdir -p ~.kube sudo cp /etc/kubernetes/admin.conf ~/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
Go ahead and run those, but if it recommends different commands, run those instead.
Install flannel network driver
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Note: The lack of sudo is intentional
Make sure all the pods come up
kubectl get pods --all-namespaces
Join worker nodes to the cluster
Once all of the pods have come up, run the join command on each worker node. This command was provided in an earlier step.
Check status of nodes
See if the nodes have joined successfully, run the following command a few times until everything is ready:
kubectl get nodes