K3s Cluster in a Weekend – Part 3 – Longhorn
Longhorn provides native block storage for Kubernetes clusters and can be used with K3s. The K3s worker nodes have a secondary disk attached which will be used by Longhorn to provision storage. Disks attached to the nodes must be prepped.
First, we will wipe the second disk attached to the node.
worker-01:~# wipefs -a /dev/<second_disk>
Now, format the disk as ext4.
worker-01:~# mkfs.ext4 /dev/<second_disk>
Create a directory to mount the disk. Longhorn will use this directory for data storage.
worker-01:~# mkdir /longhorn-storage
Mount the disk, then add it to /etc/fstab
to persist reboot.
worker-01:~# mount /dev/<second_disk> /longhorn-storage/
worker-01:~# echo UUID\=$(findmnt -n -o UUID,TARGET,FSTYPE,OPTIONS /longhorn-storage) 0 0 | tee -a /etc/fstab
open-iscsi and a NFSv4 client is required for Longhorn.
worker-01:~# apt install -y nfs-common open-iscsi
Now, we are ready to install Longhorn using Helm. Additional configuration flags, such as, defaultDataPath can be passed to Helm as described in customize default settings.
helm repo add longhorn https://charts.longhorn.io; helm repo update
helm install longhorn longhorn/longhorn --namespace longhorn-system --create-namespace --set defaultSettings.defaultDataPath="/longhorn-storage"
We can access the Longhorn UI by creating a service from the control node.
control-01:~$ kubectl apply -f longhorn-svc-lb.yaml
longhorn-svc-lb.yaml
---
apiVersion: v1
kind: Service
metadata:
name: longhorn-svc-lb
namespace: longhorn-system
spec:
selector:
app: longhorn-ui
type: LoadBalancer
loadBalancerIP: 10.33.0.210
ports:
- name: http
protocol: TCP
port: 80
targetPort: http