Bootstrap FreeKB - OpenShift - Create Persistent Volume Claim
OpenShift - Create Persistent Volume Claim

Updated:   |  OpenShift articles

If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.

A persistent volume (PV) is the "physical" volume (such as a hard disk drive or solid state drive) on the host machine (node) that stores your persistent data, 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 persistentvolumeclaims

 

And then more details on each key can be displayed.

oc explain persistentvolumeclaims.spec

 

storageClassName must be an exact match of one of the storage classes returned by the oc get storageclass command.

~]$ 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

 

volumeName must be an exact match of the Persistent Volume that the Persistent Volume Claim should be bound to.

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)

 

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-persistent-volume-claim
  namespace: foo
spec:
  accessModes:
  - ReadOnlyOnce
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: file-storage
  volumeMode: Block
  volumeName: my-persistent-volume

 

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_pvc.yml 
persistentvolumeclaim/my-persistent-volume-claim created

 

The oc get persistentvolumeclaims (or oc get pvc) command will return the list of persistent volume claims.

~]$ oc get pvc --output wide
NAME                         STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE      VOLUMEMODE
my-persistent-volume-claim   Bound    pvc-2db07c57-e282-48e7-bfb1-4cbd7245c25e   1Gi        RWO,RWX        file-storage   3m29s    Filesystem

 

labels or claimRef in the Persistent Volume YAML can be used to bind Persistent Volume Claims to a 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
kind: PersistentVolume
apiVersion: v1
metadata:
  name: my-persistent-volume
  labels: 
    name: my-persistent-volume
spec:
  capacity:
    storage: 1Gi
  csi:
    driver: efs.csi.aws.com
  accessModes:
    - ReadOnlyMany
    - ReadWriteMany
    - ReadWriteOnce
  claimRef:
    kind: PersistentVolumeClaim
    namespace: my-project
    name: my-persistent-volume-claim
  persistentVolumeReclaimPolicy: Delete
  storageClassName: efs-sc
  volumeMode: Filesystem

 

And the oc get persistentvolumes (or oc get pv) command should list the persistent volume.

~]$ oc get pv pvc-2db07c57-e282-48e7-bfb1-4cbd7245c25e
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                            STORAGECLASS   REASON   AGE
pvc-2db07c57-e282-48e7-bfb1-4cbd7245c25e   1Gi        RWO            Delete           Bound    foo/my-persistent-volume-claim   file-storage            19m

 

The oc describe pvc command can be used to display the details of a Persistent Volume Claim. In this example, the details of the pvc001 Persistent Volume Claim will be displayed.

~]$ oc describe pvc
Name:          my-persistent-volume-claim
Namespace:     foo
StorageClass:  file-storage
Status:        Bound
Volume:        pvc-2db07c57-e282-48e7-bfb1-4cbd7245c25e
Labels:        <none>
Annotations:   pv.kubernetes.io/bind-completed: yes
               pv.kubernetes.io/bound-by-controller: yes
               volume.beta.kubernetes.io/storage-provisioner: csi.trident.netapp.io
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      1Gi
Access Modes:  RWO,RWX
VolumeMode:    Filesystem
Used By:       <none>

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 78883c in the box below so that we can be sure you are a human.