OpenShift - Mount a Persistent Volume in a container

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.

 

The oc describe pvc command can be used to display the details of a Persistent Volume Claim. Notice this Persistent Volume Claim has two access modes, ROX and RWX. More on this in a moment.

  • ReadOnlyMany (ROX) - The volume may be mounted on different nodes, read only
  • ReadWriteMany (RWX) - The volume may be mounted on different nodes, read and write
~]$ oc describe pvc my-persistent-volume-claim
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:  ROX,RWX
VolumeMode:    Filesystem
Used By:       my-app-2mp2k

 

The Mounts option in a YAML file being used by a deployment can be used to mount a directory in the container to a directory on the node (virtual machine) that the container is running on.

As an example, let's say you want to store the data in the /var/data directory in the container to /usr/local/openshift/foo/data on the node (virtual machine) that the container is running on. In this example, the YAML file being used by the deployment would use mounts to specify the directories.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-openshift
spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: hello-openshift
        image: openshift/hello-openshift:latest
        ports:
        - containerPort: 80
        mounts: /var/data from /usr/local/openshift/foo/data (rw)

 

Or, the volumeMounts option can be used to mount a Persistent Volume Claim.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-openshift
spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: hello-openshift
        image: openshift/hello-openshift:latest
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /var/data
          name: my-volume
      volumes:
      - name: my-volume
        persistentVolumeClaim:
          claimName: my-persistent-volume-claim

 

If the Persistent Volume Claim only has a single access mode, such as Read Write Many (RWX), the volume will use the access mode. On the other hand, if the Persistent Volume Claim has both Read Only Many (ROX) and Read Write Many (RWX), the volume will default to use Read Write Many (RWX). In this scenario, readOnly: true must be used to mount the volume as read only.

spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: hello-openshift
        image: openshift/hello-openshift:latest
        ports:
        - containerPort: 80
        volumeMounts:
          mountPath: /var/data
          name: my-volume
          readOnly: true

 

The oc get pvc command can be used to list the Persistent Volume Claims.

~]$ oc get pvc
NAME                STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
foo-pvc             Bound    pvc-4c3498b8-286b-46c2-8b1f-057cd38d5295   10Gi       RWO            file-storage   256d

 

Or a volume can be data stored in a Secret.

volumes:
  name: my-secret
  secret:
    secretName: my-secret

 

Or as an empty directory.

volumes:
  emptyDir: {},
  name: 'empty'

 

Assuming the volume was successfully mounted, the oc exec command can be used to validate that the volume has been mounted in the pod. Notice in this example that the /var/data directory that was mounted is owned by 99 instead of root, as an indication that /var/data is a mounted volume.

~]$ oc exec pod/my-pod-hp5w4 -- ls -l /var
total 4
drwxr-xr-x. 2 root root    6 Apr 23  2020 adm
drwxr-xr-x. 1 root root   33 Apr 15  2021 cache
drwxrwxrwx. 2   99   99 4096 Aug  2 10:24 data
drwxr-xr-x. 2 root root    6 Apr 23  2020 db
drwxr-xr-x. 2 root root    6 Apr 23  2020 empty
drwxr-xr-x. 2 root root    6 Apr 23  2020 ftp
drwxr-xr-x. 2 root root    6 Apr 23  2020 games
drwxr-xr-x. 2 root root    6 Apr 23  2020 gopher
drwxr-xr-x. 3 root root   18 Apr 14  2021 kerberos
drwxr-xr-x. 1 root root   60 Apr 15  2021 lib
drwxr-xr-x. 2 root root    6 Apr 23  2020 local
lrwxrwxrwx. 1 root root   11 Apr 14  2021 lock -> ../run/lock
drwxr-xr-x. 1 root root   93 Apr 15  2021 log
lrwxrwxrwx. 1 root root   10 Apr 23  2020 mail -> spool/mail
drwxr-xr-x. 2 root root    6 Apr 23  2020 nis
drwxr-xr-x. 2 root root    6 Apr 23  2020 opt
drwxr-xr-x. 2 root root    6 Apr 23  2020 preserve
lrwxrwxrwx. 1 root root    6 Apr 14  2021 run -> ../run
drwxr-xr-x. 5 root root   41 Apr 14  2021 spool
drwxrwxrwt. 1 root root    6 Apr 15  2021 tmp
drwxr-xr-x. 2 root root    6 Apr 23  2020 yp

 




Did you find this article helpful?

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

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |