Search code examples
kubernetesdevopskubernetes-helmnfspersistent-storage

is namespace mandatory while defining claimRef under k8s PersistentVolume manifest file?


Below is my scenario. I have an NFS setup and it will be used to create PV. and then use PVC to bind the volume. Now, Consider I want to bind particular PV/PVC together irrespective of where PVC will be created. As far as I tried I could not bind PV/PVC without bringing namespace into the picture. Since I use helm charts for deployment and the namespace can be anything (use can create/use any namespace) hence I do not want to restrict PV to look for PVC only in one namespace, rather bind to matching PVC from any namespace.

nfs-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  claimRef:
    name: nfs-pvc
    namespace: default   # This is something I wanna get rid off
  nfs:
    path: /apps/exports
    server: <nfs-server-ip>

nfs-pvc.yaml #This one I should be able to create in any namespace and attach to the above PVC.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
  namespace: fhir
spec:
  volumeName: nfs-pv
  storageClassName: nfs
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

I have tried without giving the namespace option in PV, but it didn't work. enter image description here

Any help on this would be much appreciated.


Solution

  • I think that is not possible because :

    • PVC is a namespaced resource and PV is not a namespaced resource.
    kubectl api-resources | grep 'pv\|pvc\|NAME'
    NAME                              SHORTNAMES   APIVERSION                             NAMESPACED   KIND
    persistentvolumeclaims            pvc          v1                                     true         PersistentVolumeClaim
    persistentvolumes                 pv           v1                                     false        PersistentVolume
    
    • So there can be multiple PVCs with the same 'name' across multiple namespaces. so when we are mentioning the name of the pvc under claimRef we need to mention the name space as well.