Configure mount options for pods that are backed by PersistentVolumes (NFS or other volume types) in OpenShift 4?

Solution Verified - Updated

Environment

  • Red Hat OpenShift Container Platform (RHOCP)
    • 4
  • Persistent Volumes Mount Options

Issue

  • Is it possible to configure mount options for volumes in OpenShift 4?
  • How to configure NFS mount options for pods backed by NFS PV?
  • When creating a pod backed by NFS Persistent Volume, default mount options are set. How to edit these NFS mount options?

Resolution

It is not possible to configure required mount options (for NFS or other volume types) directly in the pods, but they can be specified while creating a PersistentVolume by using the PV attribute spec.mountOptions, or using the mountOptions field from StorageClasses for dynamically provisioned PVs.
>Note: currently, Red Hat does not ship a NFS CSI Driver that supports dynamic provisioning of NFS volumes. .

Refer to the Diagnostic Steps section for examples for manually creating PersistentVolumes backed by NFS with customized mount options (like nfsvers and soft).

Root Cause

Disclaimer: Links contained herein to external website(s) are provided for convenience only. Red Hat has not reviewed the links and is not responsible for the content or its availability. The inclusion of any link to an external website does not imply endorsement by Red Hat of the website or their entities, products or services. You agree that Red Hat is not responsible or liable for any loss or expenses that may result due to your use of (or reliance on) the external site or content.

It is possible to configure mount options (for NFS or other volume types) directly in the PersistentVolumes via the spec.mountOptions field, or in the mountOptions field from StorageClasses for dynamically provisioned PersistentVolumes.
>Note: currently, dynamic provisioning NFS volumes is not supported in OpenShift 4.

Refer to the upstream Kubernetes documentation about the PersistentVolumes types supporting Content from kubernetes.io is not included.mount options, and to the StorageClasses Content from kubernetes.io is not included.mount options.

Diagnostic Steps

  • Check the description of the PersistentVolume spec.mountOptions:

    $ oc explain persistentvolume.spec.mountOptions
    KIND:     PersistentVolume
    VERSION:  v1
    
    FIELD:    mountOptions <[]string>
    
    DESCRIPTION:
         mountOptions is the list of mount options, e.g. ["ro", "soft"]. Not
         validated - mount will simply fail if one is invalid. More info:
    
  • Check the description of the StorageClass mountOptions:

    $ oc explain storageclass.mountOptions
    GROUP:      storage.k8s.io
    KIND:       StorageClass
    VERSION:    v1
    
    FIELD: mountOptions <[]string>
    
    DESCRIPTION:
        mountOptions controls the mountOptions for dynamically provisioned
        PersistentVolumes of this storage class. e.g. ["ro", "soft"]. Not validated
        - mount of the PVs will simply fail if one is invalid.
    
  • Example creating persistent volume with customized mountOptions:

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: nfs-pv001
    spec:
      capacity:
        storage: 5Gi
      accessModes:
      - ReadWriteMany
      mountOptions:
      - nfsvers=4.1
      - soft
      persistentVolumeReclaimPolicy: Retain
      nfs:
        path: /nfs
        server: bastion.srocp4.example.com
    
  • Create persistent volume claim with generated NFS volume:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: nfs-claim
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 5Gi
      volumeName: nfs-pv001
      storageClassName: ""
    
  • Create a pod backed by NFS volume :

    kind: Pod
    apiVersion: v1
    metadata:
      name: nfspod
    spec:
      containers:
        - name: app
          image: ubi9
          volumeMounts:
          - name: nfs-volume
            mountPath: /mnt/nfs
          command: ["/bin/sh"]
          args: ["-c", "sleep 500000"]
      volumes:
        - name: nfs-volume
          persistentVolumeClaim:
            claimName: nfs-claim
    
    $ oc get pods
    NAME           READY   STATUS    RESTARTS   AGE
    nfs-in-a-pod   1/1     Running   0          78m
    nfspod         1/1     Running   0          4m38s  <----
    
  • Verify if customized mountOptions are applied properly:

    $ oc rsh pod/nfspod 
    / # mount |grep nfs
    bastion.srocp4.example.com:/nfs on /mnt/nfs type nfs4 (rw,relatime,vers=4.1,rsize=524288,wsize=524288,namlen=255,soft,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.74.252.218,local_lock=none,addr=10.74.250.148)
    
    $ oc debug node/[node_name]
    [...]
    sh-5.1# chroot /host bash
    [root@node_name /]# mount | grep -i nfs
    bastion.srocp4.example.com:/nfs on /var/lib/kubelet/pods/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/volumes/kubernetes.io~nfs/nfs-volume type nfs4 (rw,relatime,vers=4.2,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.74.252.218,local_lock=none,addr=10.74.250.148)
    bastion.srocp4.example.com:/nfs on /var/lib/kubelet/pods/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/volumes/kubernetes.io~nfs/nfs-pv001 type nfs4 (rw,relatime,vers=4.1,rsize=524288,wsize=524288,namlen=255,soft,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.74.252.218,local_lock=none,addr=10.74.250.148)                          # <---- mounted with soft, 4.1 that is specified when creating nfs pv.
    
Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.