Kubernetes NFS Mount Setup for Persistent Storage
Requirements
Section titled “Requirements”-
Install nfs-common on all kubernetes nodes
sudo apt install -y nfs-common -
Install NFS CSI driver
-
An NFS share setup from something like TrueNAS, Synology, etc.
Setup persistent volume
Section titled “Setup persistent volume”-
Create a yaml file called
nfs-pv.yamlwith the following:apiVersion: v1kind: PersistentVolumemetadata:name: nfsspec:capacity:storage: 20GiaccessModes:- ReadWriteManystorageClassName: nfsnfs:server: 192.168.1.24 # IP of NFS Serverpath: "/mnt/TREPOOL/TRESHARE" # NFS mount path -
Apply file
kubectl apply -f nfs-pv.yaml
Setup persistent volume claim
Section titled “Setup persistent volume claim”-
Create a yaml file called
nfs-pvc.yamlwith the following:apiVersion: v1kind: PersistentVolumeClaimmetadata:name: nfsspec:accessModes:- ReadWriteManystorageClassName: nfsresources:requests:storage: 20Gi -
Apply file
kubectl apply -f nfs-pvc.yaml
Test with NGINX web server
Section titled “Test with NGINX web server”-
Create a yaml file called
nfs-web.yamlwith the following:apiVersion: apps/v1kind: Deploymentmetadata:name: nfs-webspec:replicas: 1selector:matchLabels:app: nfs-webtemplate:metadata:labels:app: nfs-webspec:containers:- name: nfs-webimage: nginxports:- name: webcontainerPort: 80volumeMounts:- name: nfsmountPath: /usr/share/nginx/html # NFS path in podvolumes:- name: nfspersistentVolumeClaim:claimName: nfs -
Apply file
kubectl apply -f nfs-web.yaml -
Get the name of the pod. eg:
nfs-web-7bc97bcf48-5brq6kubectl get pods -
Exec into the pod to verify nfs is mounted and files are available.
kubectl exec -it nfs-web-7bc97bcf48-5brq6 -- /bin/bash -
cd into NFS path from the deployment and list the directory to verify files.
cd /usr/share/nginx/html