Everything as code.
Build a Raspberry Pi Kubernetes Cluster
If minikube is not enough, build your own datacenter, lowcost with Raspberry PIs, to play around with Kubernetes (K8s) on top of it.
Bill of material
The Kubernetes cluster will consist of the following components, to scale, just multiply it. The master node will have the WLAN and will act as gateway to the outside world, all other nodes are connected via the Ethernet hub.
- 4 Raspberry Pis (Pi-1, Pi-2, Pi-3, Pi-4 will work)
- 4 SDHC mem cards (8-16 GB)
- 4 Pi housing boxes (or build/print yourself)
- 4 CAT5 Ethernet cables
- 1 Ethernet 10/100 mbps Hub
- WLAN 1 Edimax USB card if not on one of the Pis
- 4 micros-USB loading cables
- 4-port USB loader or 4 1 port USB loader
- 8-port power connector strip
- piece of wood and many cable binders to mount it all on a shelf
will roughly cost 200 EUR in sum (your mileage may vary)
Flash the images
The Hypriot project has pre-defined Docker images. It can be downloaeded and flashed on the SD card, a detailed description is here (https://blog.hypriot.com/getting-started-with-docker-and-windows-on-the-raspberry-pi/ ). After this is done, the first node can be booted. Username: pirate Password: hypriot . Should be immediately changed with passwd .
Repair the keyboard
- If you need another keyboard (e.g. german) layout :
sudo apt-get install console-data
sudo dpkg-reconfigure console-data - Select pc / qwertz / German / latin 1 or whatever you need ;-)
Configure the WLAN
- lookup your WLAN:
sudo iwlist wlan0 scan | grep -i ssid - probably you need to bring the wlan up first with:
sudo ifconfig wlan0 up - enter the network name and password sudo wpa_passphrase ssid_networkname ssid_password > /etc/wpa_supplicant/wpa_supplicant.conf
- enhance the /etc/wpa_supplicant/wpa_supplicant.conf to fit to your envirnment, delete the # cleartext password line:
network={
ssid=”ssid_networkname”
#psk=”ssid_password”
psk=123456789012345678901234567890
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
group=CCMP
} - edit the file(s) in:
/etc/network/interfaces.d/ - add the following line:
auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf - After reboot, the network should be up and runnin sudo reboot
Expand the SDHC memory card
After Hypriot is installed, the root particition will be too small. A lot of diskspace is allocated on a separate partition on the card. By expanding the root partition of the card you can regain that ‘missing’ space!
This is the easiest way to do it:
- open the PI configuration screen (in the terminal CLI) by typing:
sudo raspi-config - run the update option just to make sure you have the latest version of the configuration software:
update - run the second option:
expand rootfs - click:
Finish - select ‘YES‘ when it asks for a reboot
Install and configure the DHCP server
Now we need to set up the LAN. We statically assign the 10.0.0.1 address.
- edit the file(s) in
/etc/network/interfaces.d/ - add the following line:
allow-hotplug eth0
iface eth0 inet static
address 10.0.0.1
netmask 255.255.255.0
broadcast 10.0.0.255
gateway 10.0.0.1
- Then we fetch the DHCP Server with
apt-get install isc-dhcp-server - After installation we need to configure the config file and Ethernet interface in
/etc/default/isc-dhcp-server - And then edit /etc/dhcp/dhcp.conf:
# dhcpd.conf
#
# configuration file for ISC dhcpd
#
option domain-name “cluster.north”;
option domain-name-servers 8.8.8.8, 8.8.4.4;
default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;
authoritative;
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.1 10.0.0.10;
option subnet-mask 255.255.255.0;
option broadcast-address 10.0.0.255;
option routers 10.0.0.1;
}
default-lease-time 600;
max-lease-time 7200; - We restart the DHCP server with
systemctl restart isc-dhcp-server.service - and check if everything went find, if necessary we reboot
journalctl -xe - To get some nice names for the machines , we change /etc/hostname and /etc/hosts (or permanently here /etc/cloud/templates/hosts.debian.tmpl ) to something similar like this
127.0.0.1 localhost
192.168.8.192 pi
10.0.0.1 kubernetes k8s
10.0.0.2 node-2
10.0.0.3 node-3
10.0.0.4 node-1
Set up NAT forwarding
To enable the cluster for outside connectivity, we enable IP forwarding in /etc/sysctl.conf with the line
net.ipv4.ip_forward=1
The following commands shuold enable the forwarding.
iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
iptables -A FORWARD -i wlan0 -o eth0 -m state –state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth0 -o wlan0 -J ACCEPT
One way to make them persistent is to get the iptables-persistent package via
apt-get install iptables-persistent
to save the current rules and to add the rules to /etc/iptables/rules.v4 by leaving out the iptables command and add 1 line in the nat section and 2 lines in the *filter before the commit at the end.
*nat
-A POSTROUTING -o wlan0 -j MASQUERADE
*filter
-A FORWARD -i wlan0 -o eth0 -m state –state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i eth0 -o wlan0 -j ACCEPT
Now we are done with the set-up of the primary (“master”) node.
Install Kubernetes
Install kubectl, kubelet and kubeadmin.
Add the encrption key.
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add –
Add the repository to the list, update and install.
echo “deb http://apt.kubernetes.io/ kubernetes-xenial main” >> /etc/apt/sources.list.d/Kubernetes.list
apt-get update
apt-get upgrade
apt-get install -y kubelet kubeadm kubectl kubernetes-cni
Install the cluster.
Setup networking.
Configure the UI.
Have fun :-)
Appendix Containers
Starting bash
docker run -t -i ubuntu:20.10 /bin/bash