Creating a Persistent Volume

A Persistent Volume (PV) is a piece of storage in the cluster provisioned by administrators or is dynamically provisioned using Storage Classes.
You can create a Persistent Volume (PV) using the following command:
kubectl apply -f storage-class.yaml
kubectl apply -f pv.yaml
To check whether the Persistent Volume (PV) is created succesfully, use the following command:
For Kubernetes:
kubectl get pv
Sample output:
# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE spe-volume 100Gi RWX Retain Available spe-storage 5s
For OpenShift:
oc get pv
Sample output:
# oc get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE spe-volume 100Gi RWX Retain Available spe-storage 2d20h
A Persistent Volume can be of various types:
Persistent Volume Types
Type
Example
NFS share
storage-class.yaml
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: spe-storage provisioner: kubernetes.io/no-provisioner parameters: server: xx.xxx.xxx.xx path: /share readOnly: "false"
pv.yaml
apiVersion: v1 kind: PersistentVolume metadata: name: spe-volume spec: capacity: storage: 100Gi volumeMode: Filesystem accessModes: - ReadWriteMany persistentVolumeReclaimPolicy: Retain storageClassName: spe-storage nfs: path: "/nfsshare" server: xx.xxx.xxx.xx
Azure File share
storage-class.yaml
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: spe-storage provisioner: file.csi.azure.com # replace with "kubernetes.io/azure-file" if aks version is less than 1.21 allowVolumeExpansion: true mountOptions: - dir_mode=0777 - file_mode=0777 - uid=1000 - gid=1000 - mfsymlinks - cache=strict - actimeo=30 parameters: skuName: Premium_LRS
pv.yaml
apiVersion: v1 kind: PersistentVolume metadata: name: spe-volume spec: capacity: storage: 100Gi accessModes: - ReadWriteMany persistentVolumeReclaimPolicy: Retain storageClassName: spe-storage csi: driver: file.csi.azure.com readOnly: false volumeHandle: unique-volumeid # make sure this volumeid is unique for every identical share in the cluster volumeAttributes: resourceGroup: resourceGroupName # optional, only set this when storage account is not in the same resource group as node shareName: aksshare nodeStageSecretRef: name: azure-secret namespace: default mountOptions: - dir_mode=0777 - file_mode=0777 - uid=1000 - gid=1000 - mfsymlinks - cache=strict - nosharesock - nobrl
Azure File Share as NFS
storage-class.yaml
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: spe-storage provisioner: file.csi.azure.com allowVolumeExpansion: true parameters: protocol: nfs
pv.yaml
apiVersion: v1 kind: PersistentVolume metadata: annotations: pv.kubernetes.io/provisioned-by: file.csi.azure.com name: spe-volume spec: capacity: storage: 100Gi accessModes: - ReadWriteMany storageClassName: spe-storage persistentVolumeReclaimPolicy: Retain csi: driver: file.csi.azure.com readOnly: false # make sure volumeid is unique for every identical share in the cluster # the # character is reserved for internal use volumeHandle: account-name_file-share-name volumeAttributes: resourceGroup: EXISTING_RESOURCE_GROUP_NAME # optional, only set this when storage account is not in the same resource group as agent node storageAccount: EXISTING_STORAGE_ACCOUNT_NAME shareName: EXISTING_FILE_SHARE_NAME # only file share name, don't use full path protocol: nfs