Mastering Kubernetes Persistent Storage: PV, PVC, and StorageClass with NFS
This guide walks through installing NFS, configuring PersistentVolumes, PersistentVolumeClaims, and StorageClasses in Kubernetes, explains their key fields and lifecycle, demonstrates dynamic provisioning with an NFS provisioner, and shows how to use the resulting PVC in a pod.
Install Storage System
Common storage options for Kubernetes include NFS, Ceph, GlusterFS, FastDFS. This guide uses NFS and shows how to install it.
Install Service
$ yum install nfs-utils rpcbind -yCreate Shared Directory
$ mkdir /data/k8s -pConfigure NFS Export
$ vim /etc/exports
/data/k8s *(rw,sync,no_root_squash)Start Services
$ systemctl start rpcbind
$ systemctl start nfs
$ systemctl enable rpcbind
$ systemctl enable nfsTest
$ showmount -e 192.168.205.128
Export list for 192.168.205.128:
/data/k8s *All nodes must install the NFS client.
PersistentVolume (PV)
PV is an abstraction of underlying storage, created and managed by administrators. It can be backed by Ceph, NFS, etc.
Example PV definition:
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv01
labels:
storage: pv
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
persistentVolumeReclaimPolicy: Recycle
nfs:
path: /data/k8s
server: 192.168.205.128Key fields:
accessModes : ReadWriteOnce, ReadOnlyMany, ReadWriteMany.
capacity : storage size.
persistentVolumeReclaimPolicy : Retain, Delete, Recycle.
PV status can be Available, Bound, Released, or Failed.
PersistentVolumeClaim (PVC)
PVC expresses a user's storage request and consumes a PV. Use kubectl explain pvc for details.
Example PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-test
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1GiKey spec fields: accessModes, resources.requests.storage, dataSource, selector, storageClassName, volumeMode, volumeName.
After creation, check status:
$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-test Bound my-pv01 1Gi RWO <em>(none)</em> ...If a second PVC is created without an available PV, its status will be Pending until a matching PV is created.
StorageClass
StorageClass automates PV creation. It defines storage properties and the provisioner to use.
Example StorageClass for NFS:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs
provisioner: rookieops/nfsFields include provisioner, parameters, and reclaimPolicy (default Delete, also Retain). The StorageClass name is used by PVCs to request dynamic provisioning.
Create it with kubectl apply -f sc.yaml and verify:
$ kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs rookieops/nfs Delete Immediate false ...Dynamic PVC using the StorageClass:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-from-sc
spec:
accessModes:
- ReadWriteOnce
storageClassName: nfs
resources:
requests:
storage: 1GiAfter applying, a PV is automatically created and bound.
One StorageClass can be marked as default by adding the annotation storageclass.kubernetes.io/is-default-class: "true". Only one default is allowed.
To use the PVC in a Pod:
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: nfs-pvc
mountPath: /mnt
restartPolicy: Never
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: pvc-from-scExec into the pod and write to /mnt to verify persistence.
Summary
While Kubernetes encourages stateless applications, stateful workloads require persistent storage. Understanding PV, PVC, and StorageClass, and how to configure NFS provisioners, is essential for reliable data persistence.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
