Add MountOptions to Kubernetes ingress rule from cli

11/10/2021

I have a PV/PVC in my kubernetes cluster.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  nfs:
    path: /tmp
    server: 172.17.0.2

I want to externally add mountOptios to all PVs like below.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /tmp
    server: 172.17.0.2

Is there any way I can achieve this using kubectl cli like we add annotations to ingress rules and pods?

-- Ujjawal Khare
kubernetes
persistent-volume-claims
persistent-volumes

1 Answer

11/12/2021

You can use kubectl patch command to add mountOptions to existing PV in the cluster:

kubectl patch pv pv0003 --patch '{"spec": {"mountOptions": ["hard","nfsvers=4.1"]}}'

If you want to add mountOptions to every PV in the cluster you can use simple bash for loop and kubectl patch command:

for pv in $(kubectl get pv --no-headers -o custom-columns=":metadata.name"); do kubectl patch pv $pv --patch '{"spec": {"mountOptions": ["hard","nfsvers=4.1"]}}'; done
-- Mikolaj S.
Source: StackOverflow