
If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.
A persistent volume (PV) is an OpenShift resource that points to some sort of storage, such as an LVM volume or a CIFS share or an NFS share, whereas a persistent volume claim (PVC) is a reference to a persistent volume, used by a pod.
A JSON or YAML file that contains key value pairs used to create an object, such as a config map, deployment, a project, a pod, a route, a secret, a service, et cetera. These files are known as templates. The oc explain command can be used to get the list of keys that can be used in the JSON or YAML template file.
oc explain persistentvolumes
And then more details on each key can be displayed.
oc explain persistentvolumes.spec
storageClassName must be an exact match of the NAME of the Storage Class returned by the oc get storageclass command.
The csi (Container Storage Interface) driver must be an exact match of the PROVISIONER of the Storage Class.
persistentVolumeReclaimPolicy must be an exact match of the RECLAIMPOLICY of the Storage Class.
~]$ oc get storageclass
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
file-storage (default) csi.trident.netapp.io Delete Immediate true 377d
thin kubernetes.io/vsphere-volume Delete Immediate false 381d
efs-sc efs.csi.aws.com Delete Immediate false 20d
accessMode can contain one or more of the following:
- ReadWriteOnce (RWO) - The volume may only be mounted on a single node
- ReadWriteOncePod (RWOP) - The volume may only be mounted on a single pod
- ReadOnlyMany (ROX) - The volume may be mounted on different nodes
- ReadWriteMany (RWX) - The volume can be mounted on different nodes
volumeMode can be either:
- Filesystem (default, files are stored anywhere on the storage device)
- Block (files are stored in a dediated block on the storage device, which may result in improved read/write performance)
labels or claimRef are Optional, and can be used to bind Persistent Volume Claims to the Persistent Volume
- claimRef can be used to bind the Persistent Volume to Persistent Volume Claims
- metadata.labels can be used to create a custom label (key / value pair) that would then be used to bind the Persistent Volume Claim to the Persistent Volume
The following is the bare minimum that is required to create a Persistent Volume.
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-persistent-volume
spec:
accessModes:
- ReadOnlyMany
capacity:
storage: 1Gi
Here is the YAML that could be used to create a Persistent Volume that uses the csi.trident.netapp.io (Trident NetApp CSI) storage class.
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-trident-netapp-csi-persistent-volume
spec:
accessModes:
- ReadWriteMany
capacity:
storage: 1Gi
claimRef:
apiVersion: v1
kind: PersistentVolumeClaim
name: my-trident-netapp-csi-persistent-volume-claim
namespace: my_project
csi:
driver: csi.trident.netapp.io
volumeHandle: my-trident-netapp-csi-persistent-volume-claim
persistentVolumeReclaimPolicy: Delete
storageClassName: file-storage
volumeMode: Filesystem
Here is the YAML that could be used to create a Persistent Volume that uses the efs.csi.aws.com (Amazon Web Services Elastic File System CSI) storage class.
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-persistent-volume
labels:
name: my-persistent-volume
spec:
accessModes:
- ReadOnlyMany
- ReadWriteMany
- ReadWriteOnce
capacity:
storage: 1Gi
csi:
driver: efs.csi.aws.com
claimRef:
kind: PersistentVolumeClaim
namespace: default
name: my-persistent-volume-claim
apiVersion: v1
persistentVolumeReclaimPolicy: Delete
storageClassName: efs-sc
volumeMode: Filesystem
Here is how you could create a Persistent Volume for an NFS share.
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-persistent-volume
labels:
name: my-persistent-volume
spec:
accessModes:
- ReadOnlyMany
- ReadWriteMany
- ReadWriteOnce
capacity:
storage: 1Gi
nfs:
path: /mnt
server: 198.19.255.74
persistentVolumeReclaimPolicy: Retain
The oc apply or oc create command with the -f or --filename option can be used to create the pod using the template JSON or YAML file.
The oc replace command can be used to replace a pod using a new or updated template JSON or YAML file.
The oc edit command can be used to update a pods template YAML file.
~]$ oc create -f create_pv.yml
persistentvolume/my-persistent-volume created
The oc get persistentvolumes (or oc get pv) command should list the persistent volume.
~]$ oc get persistentvolumes
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
my-persistent-volume 1Gi RWX Retain Bound default/pvc-sv1 efs-sc 20d
The oc describe pv command can be used to display the details of a Persistent Volume. In this example, the details of the pvc001 Persistent Volume will be displayed.
~]$ oc describe pv my-persistent-volume
Name: my-persistent-volume
Labels: <none>
Annotations: pv.kubernetes.io/bound-by-controller: yes
Finalizers: [kubernetes.io/pv-protection]
StorageClass: efs-sc
Status: Bound
Claim: default/pvc-sv1
Reclaim Policy: Retain
Access Modes: RWX
VolumeMode: Filesystem
Capacity: 1Gi
Node Affinity: <none>
Message:
Source:
Type: CSI (a Container Storage Interface (CSI) volume source)
Driver: efs.csi.aws.com
FSType:
VolumeHandle: fs-0123cdef::fsap-0123456789abcdef
ReadOnly: false
VolumeAttributes: <none>
Events: <none>
If you create a Persistent Volume that uses the csi.trident.netapp.io (Trident NetApp CSI) storage class, the tridentctl CLI can be used to show that there is a volume that corresponds to the Persistent Volume.
The NAME of the volume will be an exact match of volumeHandle in the YAML you used to create the Persistent Volume.
volumeHandle: my-trident-netapp-csi-persistent-volume-claim
~]$ tridentctl get volume --namespace trident
+----------------------------------------------------+---------+---------------+----------+--------------------------------------+--------+---------+
| NAME | SIZE | STORAGE CLASS | PROTOCOL | BACKEND UUID | STATE | MANAGED |
+----------------------------------------------------+---------+---------------+----------+--------------------------------------+--------+---------+
| my-trident-netapp-csi-persistent-volume-handle | 1.0 GiB | file-storage | file | f1975316-c73e-4d4e-bd15-c810e31fee10 | online | true |
+----------------------------------------------------+---------+---------------+----------+--------------------------------------+--------+---------+
Did you find this article helpful?
If so, consider buying me a coffee over at