Instructions on how to set up a Raspberry Pi cluster using k3s by Rancher.
Initial raspberry Pi setup using k3s.io — Kubernetes cluster on raspberry pi 3s!
sudo raspi-config
Uncomment to eth0 block to look like the following and save when done:
sudo nano /etc/dhcpcd.conf
interface eth0
static ip_address=x.x.x.y/24
static routers=x.x.x.1
static domain_name_servers=x.x.x.1
Confirm it worked by running:
sudo dphys-swapfile swapoff \
&& sudo dphys-swapfile uninstall \
&& sudo update-rc.d dphys-swapfile remove
sudo swapon --summary
Then add this to the first line in the file (DO NOT CREATE A NEW LINE)
sudo nano /boot/cmdline.txt
cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory
The Pis are now ready to run the cluster! :)
sudo reboot
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=v0.9.1 sh -
2. After this command finished the following will have been installed: kubectl, crictl, ctr, k3s-killall.sh, and k3s-uninstall.sh
3. Lets now grab the k3 key to allow worker nodes to join. Do this by running the following:
```bash
sudo cat /var/lib/rancher/k3s/server/node-token
curl -sfL https://get.k3s.io | K3S_URL=https://myserver:6443 K3S_TOKEN=mynodetoken INSTALL_K3S_VERSION=v0.9.1 sh -
2. You now have a ready to use worker node. Confirm its connected to the cluster by running the following command on the master node:
```bash
sudo kubectl get nodes
The most difficult part for me when setting up k3s was figuring out persistent storage. After lots of searching I found the best solution for my case was to use local storage by K3s. Later on I may switch to K3s Longhorn block storage solution. To get localstorage up and running, do the following:
sudo kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml
# Confirm with
sudo kubectl -n local-path-storage get pod
Below is an example PVC that will dynamically have a PV created on a node under this path: /opt/local-path-provisioner
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: local-path-pvc
namespace: default
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-path
resources:
requests:
storage: 2Gi
Here is a example pod using that PVC:
apiVersion: v1
kind: Pod
metadata:
name: volume-test
namespace: default
spec:
containers:
- name: volume-test
image: nginx:stable-alpine
imagePullPolicy: IfNotPresent
volumeMounts:
- name: volv
mountPath: /data
ports:
- containerPort: 80
volumes:
- name: volv
persistentVolumeClaim:
claimName: local-path-pvc
Please refrence the offical documentation for more information on this here: https://github.com/rancher/local-path-provisioner or go to https://rancher.com/docs/k3s/latest/en/storage
Below are some commands you will most likely be running a lot on your master node.
# Getting information about various parts of k8s
sudo kubectl get nodes
sudo kubectl get pods
sudo kubectl get pvc
sudo kubectl get service
sudo kubectl get pv
# Deploying and deleting various items from k8s
sudo kubectl apply -f some_k8_item.yaml
sudo kubectl delete -f some_k8_item.yaml
# Getting detailed information about pods
sudo kubectl describe pods
To get kubectl working on your local machine, first install kubectl by reading through the following documentation for your machine. https://kubernetes.io/docs/tasks/tools/install-kubectl
Next, we will need to setup your kubeconfig file by copying the config from the master to your machine.
# This is the IP of your master node
scp pi@x.x.x.y:/etc/rancher/k3s/k3s.yaml ~/.kube/config
To run functions as a servie on your cluster visit https://docs.openfaas.com/deployment/kubernetes/#c-deploy-using-kubectl-and-plain-yaml-for-development-only