Is it possible to have PVC's with the same name accross different namespace when using different PV's in Kubernetes?

9/22/2018

I have 2 different namespace: prod-01 and prod-02, What I want to do is build a copy of my prod-01 into prod-02 namespace keeping the same names for its pvcs, so that I don't have to maintain 2 sets of charts for each different namespace.

Here's how it looks like:

$ kubectl get ns | grep prod
prod-01          Active    178d
prod-02          Active    8d
$ 

As shown below, I have 2 pairs of pv's for each namespace:

$ kubectl get pv -o wide | grep prod
prod-01-db-pv                    50Gi       RWX            Retain           Bound       prod-01/app-db                                                                      164d
prod-01-nosql-db-pv              5Gi        RWX            Retain           Bound       prod-01/app-nosql-db                                                                149d
prod-02-db-pv                50Gi       RWX            Retain           Available   prod-02/app-db                                                          41m
prod-02-nosql-db-pv          5Gi        RWX            Retain           Available   prod-02/app-nosql-db                                                    19m
$ 

Here's how pvc's for prod-01 are being displayed:

$ kubectl get pvc --namespace=prod-01
NAME              STATUS    VOLUME               CAPACITY   ACCESS MODES   STORAGECLASS   AGE
app-db         Bound     prod-01-db-pv         50Gi       RWX                           164d
app-nosql-db   Bound     prod-01-nosql-db-pv   5Gi        RWX                           149d
$ 

And here's what I'm trying to accomplish:

$ kubectl get pvc --namespace=prod-02
NAME              STATUS    VOLUME                   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
app-db         Pending   prod-02-db-pv         0                                        2m
app-nosql-db   Pending   prod-02-nosql-db-pv   0                                        24m
$ 

As shown above, the pvc's for prod-02 namespace are stuck forever with Pending status.

Them when I change the pvc names on prod-02 to anything different, they bond as expected.

Which leads me to think I can't use the same names on pvc's even when they are in different namespaces and pointing to different pv's ... However, when searching the documentation, I could not find any evidence to this issue, and was wondering if I could be missing something.

So to put it simple, can I have multiple pvc's with the same name accross different namespaces (considering that they are using different pv's)?


Update: result of kubectl describe pvc

$ kubectl describe pvc app-db --namespace=prod-02
Name:          app-db
Namespace:     prod-02
StorageClass:  
Status:        Pending
Volume:        prod-02-db-pv
Labels:        <none>
Annotations:   <none>
Finalizers:    []
Capacity:      0
Access Modes:  
Events:        <none>
$ 

Also here's the output of kubectl get pvc:

$ kubectl get pvc app-db --namespace=prod-02  -o yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: 2018-09-22T22:00:34Z
  name: app-db
  namespace: prod-02
  resourceVersion: "43027607"
  selfLink: /api/v1/namespaces/prod-02/persistentvolumeclaims/app-db
  uid: ee81b951-beb2-11e8-b972-005056bbded7
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 50Gi
  volumeName: prod-02-db-pv
status:
  phase: Pending
$ 

And here are some details about the pv too:

$ kubectl get pv prod-02-db-pv --namespace=prod-02 -o yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  creationTimestamp: 2018-09-22T21:15:19Z
  name: prod-02-db-pv
  resourceVersion: "43020171"
  selfLink: /api/v1/persistentvolumes/prod-02-db-pv
  uid: 9c07d7a6-beac-11e8-b972-005056bbded7
spec:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 50Gi
  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: app-db
    namespace: prod-02
  nfs:
    path: /nfs_server/prod02/db
    server: 158.87.52.35
  persistentVolumeReclaimPolicy: Retain
status:
  phase: Available
$ 

Thanks in advance for the help!

-- Felipe Silveira
kubernetes
namespaces
persistent-volume-claims
persistent-volumes
storage

2 Answers

9/24/2018

Found the cause of my problem in the following link from OpenShift docs:

Turns out the problem was related to the wrong use of claimRefs in the yaml file used to create the prod-02 pv's, here's the full explanation:

Specifying a volumeName in your PVC does not prevent a different PVC from binding to the specified PV before yours does. Your claim will remain Pending until the PV is Available.

Specifying a claimRef in a PV does not prevent the specified PVC from being bound to a different PV. The PVC is free to choose another PV to bind to according to the normal binding process. Therefore, to avoid these scenarios and ensure your claim gets bound to the volume you want, you must ensure that both volumeName and claimRef are specified.

So right after fixing the claimRefs and recreating my pv's, the pvc started to get bonded as expected even with the same name used on other namespaces :-)

-- Felipe Silveira
Source: StackOverflow

9/23/2018

PVC is a namespaced resource but not PV. ie., you can have multiple PVC's with same name across difference namespaces.

There might be issues in the way you have configured the pv.

Can you make sure you are using the right ip address in pv configuration just under nfs attribute:

nfs:
path: /nfs_server/prod01/db
server: 158.87.52.35
-- Harish Anchu
Source: StackOverflow