Is it possible to have 1 pod with multiple PVC of different namespaces attached

3/8/2021

Example:

Namespace: a

 PVC: a-pvc
 Pod: main-pod-to-work-with

Mounts:

a-pvc; name: a-pvc-mount; path: /pvc/a-files
b-pvc; name: b-pvc-mount; path: /pvc/b-files
c-pvc; name: c-pvc-mount; path: /pvc/c-files

Namespace: b

 PVC: b-pvc

Namespace: c

 PVC: c-pvc
-- Vrushali
kubernetes
kubernetes-pod

1 Answer

3/8/2021

As per Persistent Volume docs:

A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes.

A PersistentVolumeClaim (PVC) is a request for storage by a user.

Bit lower in documentation you have Binding part, where you can find information:

Once bound, PersistentVolumeClaim binds are exclusive, regardless of how they were bound. A PVC to PV binding is a one-to-one mapping, using a ClaimRef which is a bi-directional binding between the PersistentVolume and the PersistentVolumeClaim.

Pod and PVC are namespaced resources. It mean's that if you have pod in default namespace, pvc also must be in the same namespace.

$ kubectl api-resources
NAME                              SHORTNAMES   APIVERSION                        NAMESPACED   KIND
...
persistentvolumeclaims            pvc          v1                                true         PersistentVolumeClaim
persistentvolumes                 pv           v1                                false        PersistentVolume
pods                              po           v1                                true         Pod
...

If you would create pod in default namespace, it won't be albe to see resources in other namespace. That's why you need to specify namespace in some commends. If you won't specify it, your output will be from default namespace.

$ kubectl get po
No resources found in default namespace.
$ kubectl get po -n kube-system
NAME                                                        READY   STATUS    RESTARTS   AGE
event-exporter-gke-564fb97f9-wtx9w                          2/2     Running   0          9m2s
fluentbit-gke-8tcm6                                         2/2     Running   0          8m48s
fluentbit-gke-cdm2w                                         2/2     Running   0          8m51s

Test

Based on Configure a Pod to Use a PersistentVolume for Storage documentation you have steps to create PV, PVC and Pods.

$ kubectl get po
NAME            READY   STATUS    RESTARTS   AGE
task-pv-pod     1/1     Running   0          5m20s
task-pv-pod-2   1/1     Running   0          4m43s
task-pv-pod-3   1/1     Running   0          2m40s

Few pods can use the same PVC. But all pods and PVC are in the same namespace. As pod and pvc resources, it's impossible to use pod from one namespace to use resource from another namespace.

Conclusion

Kubernetes namespaced resources are visible only for resources in the same namespace (like pod or pvc) In that situation you have specify namespace using --namespace <namespace> or -n <namespace>. There are also cluster-wide resources like PersistentVolume which don't require to specify name as they are visible in whole cluster. To get list of resources to check if it's namespaced you can use command $ kubectl api-resources.

In one particular namespace, one PVC can be used by a few pods. However it is impossible to one pod have few PVC from another namespaces.

If this didn't answer your question, please elaborate it.

-- PjoterS
Source: StackOverflow