Learning how to create a Kubernetes cluster from scratch with some Raspberry Pis.
This cluster is 3 nodes:
Component | Quantity |
---|---|
UbiGear Ethernet Cable Crimper Kit + 100 RJ45 | 1 |
Note: These instructions are for Macs. Much of this setup is following the Kubernetes on Raspbian Lite guide by Alex Ellis.
-
Download Raspbian Stretch Lite.
-
Flash SD Card with the unzipped raspbian image. Etcher is an open-source Electron app for flashing OS images to SD cards and USB drives.
brew cask install etcher
-
Enable ssh by placing an empty file on the sd card:
touch /Volumes/boot/ssh
- Note: may need to mount the sd card after flashing. Example:
diskutil mount /dev/disk2s1
. - Insert flashed sd card in to pi.
- Note: may need to mount the sd card after flashing. Example:
-
SSH in to the Pi directly from Ethernet adapter.
- Enable Internet Sharing
- Raspbian should have Avahi Daemon running, allowing for
connection with
raspberrypi.local
:
ping raspberrypi.local ssh [email protected] # default pi password: raspberry
If the above doesn't work, look for the ethernet adapter (bridge100) inet address:
ifconfig # install nmap and discover ethernet devices brew install nmap sudo nmap -n -sn 192.168.2.1/24 ssh [email protected]
- Note: ipaddress may differ
-
Set up static IP address:
# Find local network settings: ip -4 addr show | grep global # Find address of router (or gateway): ip route | grep default | aw '{print $3}' # Find the address of DNS server (likely same as gateway): cat /etc/resolv.conf # List network interface names: ls /sys/class/net/
Edit
/etc/dhcpcd.conf
:# example static IP configuration: interface eth0 static ip_address=192.168.3.2/24 static routers=192.168.3.1
Reboot:
sudo reboot
-
Setup Locale and modify hostname to (e.g.
k8s-master
) usingraspi-config
util and reboot.sudo raspi-config
Note:
- Select locales with
spacebar
in raspi-config. - SSH in to rasspberry pi with
<hostname>.local
now (e.g.ssh [email protected]
).
- Select locales with
-
Setup Docker
curl -sSL get.docker.com | sh sudo usermod pi -aG docker newgrp docker
-
Turn off swap space (required for K8s)
sudo dphys-swapfile swapoff sudo dphys-swapfile uninstall sudo update-rc.d dphys-swapfile remove
-
Enable cgroups in
/boot/cmdline.txt
and reboot:bflags="$(head -n1 /boot/cmdline.txt) cgroup_enable=cpuset cgroup_enable=memory" echo $bflags | sudo tee /boot/cmdline.txt
Note:
cgroup_memory=1
might be needed for some pi3 models.sudo reboot
-
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list sudo apt-get update sudo apt-get install -y kubeadm
Note: Continue on to the Worker Nodes setup below for non-master nodes.
-
Initialize master node
sudo kubeadm init --token-ttl=0 # takes ~10 mins mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
-
Save the generated
join-token
for the other nodes:# example sudo kubeadm join 192.168.3.2:6443 --token l3m1rn.vyo4bpefx51upqzw --discovery-token-ca-cert-hash sha256:1ba58581a3a95c795fd603894c4ff7f7a205004c20cc17e1cbe62a870019d267
-
Verify everything is running (system pods might show
Pending
for a while) and install addons:kubectl --namespace=kube-system get pods
- See the installing addons doc
- Run
kubectl apply -f [podnetwork].yaml
with one of the addons to deploy it to the cluster.
-
Install a network driver like Weave Net:
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
-
Copy cluster
config
to local machine to connect with cluster withoutssh
ing in to the master node:# from mac scp [email protected]:~/.kube/config ~/.kube/config-pi export KUBECONFIG=$KUBECONFIG:$HOME/.kube/config:$HOME/.kube/config-pi kubectl config use-context kubernetes-pi # whatever context you've named for your pi cluster config
Find the other pi's on the ethernet switch with arp -a
, or sudo nmap -n -sn 192.168.3.1/24
(ip may differ).
- Repeat the general setup from above for each worker node.
- Change hostnames to
k8s-worker-n
viasudo raspi-config
. After rebooting, it should be possible tossh
in without ips:ssh [email protected] ssh [email protected]
- Join the nodes to the cluster:
sudo kubeadm join 192.168.3.2:6443 --token l3m1rn.vyo4bpefx51upqzw --discovery-token-ca-cert-hash sha256:1ba58581a3a95c795fd603894c4ff7f7a205004c20cc17e1cbe62a870019d267
- Verify cluster is set up:
kubectl get nodes
Note: run this command from master node.. see step 5 from the Master Node setup above.
-
Clone the visualizer app serve using
kubectl proxy
:git clone https://github.com/raghur/gcp-live-k8s-visualizer.git kubectl proxy --www=path/to/gcp-live-k8s-visualizer
-
Navigate to http://localhost:8001/static/
-
Create the rolebinding listed on the dashboard access control readme
kubectl apply -f dashboard-admin.yaml
-
Deploy the dashboard:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/alternative/kubernetes-dashboard.yaml
-
Allow access to dashboard through master node, by changing the default dashboard service type from
ClusterIP
toNodePort
kubectl -n kube-system edit service kubernetes-dashboard