Skip to content

Instantly share code, notes, and snippets.

@Reclyptor
Last active March 3, 2026 07:01
Show Gist options
  • Select an option

  • Save Reclyptor/71377e71c3b999faeb58e065be380b69 to your computer and use it in GitHub Desktop.

Select an option

Save Reclyptor/71377e71c3b999faeb58e065be380b69 to your computer and use it in GitHub Desktop.
ZFS Volume Tagging for Kubernetes PVs

ZFS Volume Tagging for Kubernetes PVs

Overview

All iSCSI persistent volumes provisioned by democratic-csi on TrueNAS are tagged with custom ZFS user properties to identify their Kubernetes namespace and PVC claim name. This eliminates the need to reverse-engineer volume ownership via strings or other forensic methods during cluster rebuilds or migrations.

Properties

Property Description Example
k8s:namespace Kubernetes namespace the PVC belongs to plex
k8s:claim PVC claim name plex-config

Tagging a Volume

sudo /usr/sbin/zfs set k8s:namespace=<namespace> k8s:claim=<claim-name> primary/kubernetes/volumes/<pvc-uuid>

Listing All Tagged Volumes

sudo /usr/sbin/zfs list -t volume -r primary/kubernetes/volumes -o name,k8s:namespace,k8s:claim

Finding a Specific Service's Volume

sudo /usr/sbin/zfs list -t volume -r primary/kubernetes/volumes -o name,k8s:namespace,k8s:claim | grep <namespace>

Generating Tags from Kubernetes

To generate tagging commands for all iSCSI PVs in the current cluster:

kubectl get pv -o jsonpath='{range .items[?(@.spec.storageClassName=="iscsi-persistent-fs6712x")]}{.metadata.name}{"\t"}{.spec.claimRef.namespace}{"\t"}{.spec.claimRef.name}{"\n"}{end}' | while IFS=$'\t' read -r pv ns claim; do
  echo "sudo /usr/sbin/zfs set k8s:namespace=${ns} k8s:claim=${claim} primary/kubernetes/volumes/${pv}"
done

Finding Orphaned Volumes

Cross-reference TrueNAS zvols against Kubernetes PVs to find orphans:

# On TrueNAS: list all zvol names
sudo /usr/sbin/zfs list -t volume -r primary/kubernetes/volumes -o name -H | awk -F/ '{print $NF}'

# On workstation: list all iSCSI PV names
kubectl get pv -o jsonpath='{range .items[?(@.spec.storageClassName=="iscsi-persistent-fs6712x")]}{.metadata.name}{"\n"}{end}'

Any zvol present on TrueNAS but absent from Kubernetes is an orphan.

Notes

  • Tags are not automatically set by democratic-csi. They must be applied manually after PVC creation or during migration.
  • Tags survive ZFS snapshots, clones, and promotions.
  • NFS-based PVs (media shares, downloads) are static and not stored on ZFS zvols — they don't need tagging.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment