Install Longhorn In Kubernetes With Helm Chart

· 1385 words

My homelab runs k3s on a few small nodes. k3s comes with the local-path storage class, which is fine until a node goes down. A local-path volume is just a folder on one node, so the pod that uses it can only run on that node. When the node is off, the pod is stuck in Pending and the data is not reachable.

I wanted persistent volumes that survive a node failure without running a separate NAS or Ceph cluster. Longhorn does that with the disks the nodes already have, and it installs with a single Helm chart. In this guide I’ll show you how I install it, check that it works and give a pod a replicated volume.

What Is Longhorn

Longhorn is a distributed block storage system for Kubernetes.

It started at Rancher (now SUSE) and is a CNCF project. Longhorn takes the local disks on your nodes and turns them into replicated volumes. Every volume has one engine and several replicas, and each replica is stored on a different node. If a node dies, the volume keeps working from the replicas on the other nodes, and Longhorn rebuilds the missing replica when it can.

Longhorn replicates each volume across nodes: a pod on node 1 uses a volume whose replicas live on the disks of nodes 1, 2 and 3

It also gives you:

  1. Snapshots of a volume.
  2. Backups to S3 compatible storage or NFS.
  3. ReadWriteMany (RWX) volumes over NFSv4.
  4. A web UI to see volumes, nodes, replicas and backups.

This guide is based on Longhorn v1.13.0, which is the latest chart at the time of writing.

Prerequisites

Longhorn runs on every node, so every node needs a few things. From the Longhorn installation requirements:

Requirement Notes
Kubernetes v1.34 or later for Longhorn v1.13
Container runtime Any runtime Kubernetes supports, e.g. containerd (k3s uses containerd)
open-iscsi iscsid must be running and the iscsi_tcp module loaded
NFSv4 client Needed for RWX volumes and NFS backups
Tools bash, curl, findmnt, grep, awk, blkid, lsblk
Filesystem The host disk Longhorn uses should be ext4 or XFS
Helm v3.0 or later on your machine

The chart for v1.13.0 sets kubeVersion: >=1.34.0-0, so Helm will refuse to install it on an older cluster. If you are on an older Kubernetes version, either upgrade the cluster or install the v1.12 chart with --version 1.12.2, which supports Kubernetes v1.25 and later.

Check your cluster version first:

kubectl version

Install The Packages On Each Node

On Ubuntu or Debian, run this on every node:

sudo apt update
sudo apt install -y open-iscsi nfs-common
sudo systemctl enable --now iscsid
sudo modprobe iscsi_tcp

Make sure iscsid is active (running):

sudo systemctl status iscsid

The docs warn that open-iscsi version 2.1.12 causes volume attach failures. Use 2.1.11 or older, or 2.1.13 or newer. You can check the installed version with iscsiadm --version.

If you want encrypted volumes later, also install cryptsetup and dmsetup. I don’t need them in this guide.

Run The Preflight Check

Longhorn has a CLI called longhornctl that checks every node for you. It uses your kubeconfig, so run it from the machine where kubectl works. Download it for your CPU (use longhornctl-linux-arm64 on ARM64):

curl -sSfL -o longhornctl https://github.com/longhorn/cli/releases/download/v1.13.0/longhornctl-linux-amd64
chmod +x longhornctl

Then run the check:

./longhornctl check preflight

Read the result for each node and fix anything it reports as missing, such as a stopped iscsid or a missing NFS client. longhornctl can also install the missing packages for you with install preflight:

./longhornctl --kubeconfig ~/.kube/config --image longhornio/longhorn-cli:v1.13.0 install preflight

A Note For k3s

Longhorn works on k3s without extra settings. Since k3s v0.10.0 the kubelet root directory is /var/lib/kubelet, which is what Longhorn expects. You still need open-iscsi on each node like any other cluster.

Install Longhorn With Helm

Add the Longhorn chart repository and update it:

helm repo add longhorn https://charts.longhorn.io
helm repo update

Install the chart:

helm install longhorn longhorn/longhorn --namespace longhorn-system --create-namespace --version 1.13.0
  1. longhorn: the release name.
  2. longhorn/longhorn: the chart, from the longhorn repo we just added.
  3. --namespace longhorn-system: install everything in the longhorn-system namespace, which is the namespace the Longhorn docs use.
  4. --create-namespace: create the namespace if it doesn’t exist.
  5. --version 1.13.0: pin the chart version so you get the same install next time. Leave it out to get the latest.

Fewer Than Three Nodes

The default Longhorn storage class keeps 3 replicas of every volume, each on a different node. With only one or two nodes, Longhorn can’t place all the replicas and the volumes stay degraded. Set the replica count to the number of nodes you have with persistence.defaultClassReplicaCount:

helm install longhorn longhorn/longhorn --namespace longhorn-system --create-namespace --version 1.13.0 \
  --set persistence.defaultClassReplicaCount=2

If you already installed it, use helm upgrade with the same flags instead of helm install.

Verify The Installation

It takes a few minutes for all the pods to start. Watch them with:

kubectl -n longhorn-system get pods

Wait until every pod is Running and the READY column is full (e.g. 1/1, 3/3). You should see pods like longhorn-manager (one per node), longhorn-ui, longhorn-driver-deployer, instance-manager, engine-image and the CSI pods (csi-attacher, csi-provisioner, csi-resizer, csi-snapshotter, longhorn-csi-plugin). If a pod is stuck in CrashLoopBackOff, check its logs with kubectl -n longhorn-system logs <pod>. It’s often a missing prerequisite on that node.

Then check the storage classes:

kubectl get storageclass

You should see a storage class called longhorn.

Access The Longhorn UI

The UI runs behind the longhorn-frontend service, a ClusterIP service on port 80. The quickest way to open it is a port-forward from your machine:

kubectl -n longhorn-system port-forward svc/longhorn-frontend 8080:80

Open http://localhost:8080 in your browser. The dashboard shows the nodes, the disk space Longhorn can use and the volumes.

The UI has no authentication by default, and anyone who can reach it can delete your volumes. Don’t expose longhorn-frontend with a LoadBalancer or NodePort. If you need it outside the cluster, put it behind an Ingress with basic auth. The docs have guides for Traefik (the default ingress on k3s), NGINX and the Gateway API.

Create A Volume For A Pod

Now let’s use it. Save this as longhorn-test.yaml. It creates a 1 GiB PersistentVolumeClaim on the longhorn storage class and a small pod that writes to it:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: longhorn-test-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: longhorn
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: longhorn-test
spec:
  containers:
    - name: app
      image: busybox:1.36
      command: ["sh", "-c", "echo hello from longhorn > /data/hello.txt && sleep 3600"]
      volumeMounts:
        - name: data
          mountPath: /data
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: longhorn-test-pvc

Apply it:

kubectl apply -f longhorn-test.yaml

Check the claim:

kubectl get pvc longhorn-test-pvc

The STATUS should be Bound, with longhorn in the STORAGECLASS column. Once the pod is Running, read the file back:

kubectl exec longhorn-test -- cat /data/hello.txt

It should print hello from longhorn.

Now open the Volume tab in the UI. You’ll see a volume with a pvc-... name. It should be Attached and Healthy. Click on it to see its replicas and which node each replica is on. With three or more nodes, you should see three replicas on three different nodes.

When you’re done, clean up:

kubectl delete -f longhorn-test.yaml

The storage class uses reclaimPolicy: Delete by default, so deleting the PVC also deletes the Longhorn volume.

Make Longhorn The Default StorageClass On k3s

The Longhorn chart marks the longhorn storage class as the default (persistence.defaultClass: true). k3s also marks local-path as the default. With two default storage classes, a PVC that doesn’t set storageClassName can end up on either one, so only keep one default.

Check which classes have (default) next to their name:

kubectl get storageclass

To stop local-path being the default:

kubectl patch storageclass local-path -p '{"metadata": {"annotations": {"storageclass.kubernetes.io/is-default-class": "false"}}}'

k3s manages local-path with its own manifest. I haven’t confirmed whether a k3s restart or upgrade sets the annotation back. If you don’t need local-path at all, you can start the k3s server with --disable local-storage instead.

If you’d rather keep local-path as the default and only use Longhorn when you ask for it, install the chart with --set persistence.defaultClass=false.

After that, run kubectl get storageclass again. Only longhorn should show (default). A quick way to be sure it all works is to drain one node with kubectl drain <node> --ignore-daemonsets --delete-emptydir-data while a Deployment with one replica uses a Longhorn volume (a bare pod like the test above won’t be rescheduled), and watch the new pod start on another node with its data still there. Run kubectl uncordon <node> when you’re done.