Provide NFS Storage to Kubernetes

In progress Kubernetes Linux

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

Steps

Export the Storage

  1. 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
    
  2. Or TrueNAS: create a dataset for Kubernetes (Datasets → Add Dataset), give root full control of it, then share it (Shares → Unix (NFS) Shares → Add). Under Advanced Options, set Maproot User and Maproot Group to root, and restrict the share to the cluster's network.

Prepare Every Node

  1. Install the NFS client on every node, control plane included. The kubelet mounts NFS with the host's mount.nfs; without it, pods hang in ContainerCreating with fsconfig() failed:
    sudo apt-get install -y nfs-common
    

Option A: One Static Volume

  1. 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
    
  2. 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

  1. Install the provisioner with Helm, pointing it at the export:
    helm 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
    
    Add --set storageClass.defaultClass=true only if claims without a storageClassName should land on NFS.
  2. From then on, a claim with storageClassName: nfs-client gets 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/sbin isn't on a regular user's PATH over ssh host 'cmd'). The macOS showmount fails against TrueNAS with Program/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

References