Provide NFS Storage to Kubernetes
Goal
Give the cluster storage that any node can mount and several pods can share (ReadWriteMany), served over NFS by a Debian machine or a TrueNAS server, first as one static volume, then with a provisioner that creates a folder per claim.
Prerequisites
- A machine with the storage: a Debian host with large disks, or a TrueNAS server.
- A static IP for that machine (see Set a Static IP Address on Debian with ifupdown).
kubectland Helm on the admin workstation.
Steps
Export the Storage
- Debian server: install the server, create the folder and export it to the cluster's subnet only:
sudo apt-get install -y nfs-kernel-server sudo mkdir -p /mnt/media echo "/mnt/media <cluster-subnet>/24(rw,sync,no_subtree_check,no_root_squash)" | sudo tee -a /etc/exports sudo exportfs -a && sudo systemctl restart nfs-kernel-server - Or TrueNAS: create a dataset for Kubernetes (Datasets → Add Dataset), give
rootfull control of it, then share it (Shares → Unix (NFS) Shares → Add). Under Advanced Options, set Maproot User and Maproot Group toroot, and restrict the share to the cluster's network.
Prepare Every Node
- Install the NFS client on every node, control plane included. The kubelet mounts NFS with the host's
mount.nfs; without it, pods hang inContainerCreatingwithfsconfig() failed:sudo apt-get install -y nfs-common
Option A: One Static Volume
- Declare the share as a PersistentVolume and bind a claim to it by name:
apiVersion: v1 kind: PersistentVolume metadata: {name: nfs-media-pv} spec: capacity: {storage: 1000Gi} # informational for NFS accessModes: [ReadWriteMany] persistentVolumeReclaimPolicy: Retain # deleting the claim keeps the files nfs: {server: <nfs-server-ip>, path: /mnt/media} --- apiVersion: v1 kind: PersistentVolumeClaim metadata: {name: nfs-media-pvc, namespace: media} spec: accessModes: [ReadWriteMany] resources: {requests: {storage: 1000Gi}} volumeName: nfs-media-pv - Mount the claim in each pod that needs it. To give an app one folder of the share, mount the share and select the folder with
subPath, rather than pointing the PV at a subfolder:volumeMounts: - {name: media, mountPath: /data/media/movies, subPath: movies}
Option B: A Folder per Claim, Created on Demand
- Install the provisioner with Helm, pointing it at the export:
Addhelm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/ helm install nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner \ -n kube-system --set nfs.server=<nfs-server-ip> --set nfs.path=<exported-path> \ --set storageClass.name=nfs-client--set storageClass.defaultClass=trueonly if claims without astorageClassNameshould land on NFS. - From then on, a claim with
storageClassName: nfs-clientgets its own folder on the share, with no PV to write.
Verify
Check one layer at a time, from the network up, so a failure points at its layer.
- On a node:
/usr/sbin/showmount -e <nfs-server-ip>lists the export (full path:/usr/sbinisn't on a regular user's PATH overssh host 'cmd'). The macOSshowmountfails against TrueNAS withProgram/version mismatch, a client quirk: test from a node, or mount from the Mac with-o vers=3,resvport. - On every node, a host mount accepts writes:
sudo mount -t nfs <nfs-server-ip>:<export> /mnt && sudo touch /mnt/.test-$(hostname) && ls -la /mnt | head sudo rm -f /mnt/.test-*; sudo umount /mnt - A pod on each node mounts it through the kubelet, without any provisioner (add a toleration for tainted nodes):
kubectl run nfs-test --image=busybox:1.36 --restart=Never --overrides='{"spec":{"nodeName":"<node>","volumes":[{"name":"nfs","nfs":{"server":"<nfs-server-ip>","path":"<export>"}}],"containers":[{"name":"nfs-test","image":"busybox:1.36","command":["sh","-c","ls -la /data | head"],"volumeMounts":[{"name":"nfs","mountPath":"/data"}]}]}}' kubectl logs nfs-test; kubectl delete pod nfs-test # STATUS was Completed - For a media share: library files are hardlinks into the downloads folder, not copies (see A Hardlink Can't Cross Mounts, So Downloads and Media Libraries Must Share One Mount):
sudo find <mount>/movies <mount>/tv -type f -size +100M -links +1 | wc -l # hardlinked sudo find <mount>/movies <mount>/tv -type f -size +100M -links 1 | wc -l # standalone copies - A test claim binds:
kubectl apply -f - <<'EOF' apiVersion: v1 kind: PersistentVolumeClaim metadata: {name: test-claim} spec: accessModes: [ReadWriteMany] storageClassName: nfs-client resources: {requests: {storage: 1Mi}} EOF kubectl get pvc test-claim # STATUS: Bound - A new folder for the claim appears on the server; delete the test claim afterwards.
Troubleshooting
- Claims stay
Pending: read the provisioner's logs (kubectl logs -n kube-system -l app=nfs-subdir-external-provisioner). "Permission denied" means root is squashed on the export. mount.nfs: access denied by serverwhen the PV points at a subfolder: the server accepts only the exported path. Mount the export and usesubPath(step 5).- Apps can't write in their folder: the folder belongs to root and the app runs as another UID (1000 in linuxserver.io images). See Kubernetes Storage Failures Usually Start on the Host or the NAS.
- SQLite-based apps report "database is locked": keep their databases off NFS (see SQLite Needs Reliable File Locks, So Its Database Belongs on Local Disk, Not on NFS).
References
- A PersistentVolumeClaim Requests Storage and a StorageClass Provisions It - PV, PVC and StorageClass.
- Kubernetes Storage Failures Usually Start on the Host or the NAS - the traps behind most failures here.
- Kubernetes MOC - hub for Kubernetes notes.
- Kubernetes documentation: Volumes, nfs - the
nfsvolume type. - Kubernetes documentation: Using subPath - mounting one folder of a volume.
- nfs-subdir-external-provisioner - the provisioner and its Helm values.
- exports(5) man page -
no_root_squash,anonuid/anongid,syncandno_subtree_check. - A Hardlink Can't Cross Mounts, So Downloads and Media Libraries Must Share One Mount - why downloads and libraries share one export and one mount.