Shared PersistenceVolumeClaim(PVC) across namespaces
Asked Answered
G

4

29

Is it possible to reference a PVC (in namespace-A) from namespace-B. Why I need that? I would like to allow the customer (private cloud) to point their volume through PVC, allowing them full control of the storage. At the Helm install time will ask for PVC information and will be used in the pod spec.

Glendaglenden answered 21/6, 2018 at 0:28 Comment(0)
W
21

It looks impossible, just because PersistentVolumeClaim is a namespaced object. You can look detailed answer here: https://mcmap.net/q/357596/-share-persistent-volume-claims-amongst-containers-in-kubernetes-openshift

Widen answered 21/6, 2018 at 4:4 Comment(0)
G
18

If you are using a ReadWriteMany-capable volume (like NFS/EFS), you can create multiple Persistent Volumes (PV) pointing to the same NFS volume, one for each namespace where you want to create a PVC. They can all use the same NFS volume at the same path, or specify different subPath to constrain them to certain directories.

Genro answered 24/11, 2020 at 20:38 Comment(2)
Hi. Is there any online example available for this? I am using Azure FS with RWMany. How do we "point volumes in different namespace to one pvc" ?Helainehelali
This worked for me and it's the simplest solution with the fewest moving parts. Just copy-paste your volume config into a new file with a different name: attribute and reference that volume in your new pvc.Suprematism
W
5

Just in case someone arrives here from Google: It will now be possible with Kubernetes 1.26 (in Alpha): https://kubernetes.io/docs/concepts/storage/persistent-volumes/#cross-namespace-data-sources

Warthog answered 30/1, 2023 at 13:4 Comment(2)
As far as I understand it this doesn't allow to share the volume but only to allow the volume from another namespace as data source. So it creates a separate, independent volume which is just initialized to the data from the other namespace at the beginning.Pernickety
Right - thank you for this clarification. I should have highlighted this in the original answer!Warthog
K
-2

Our solution as follows:

  • First of all,It requires that the persistentVolumeReclaimPolicy of the source PV must be Retain.
  • Secondly, We should add an annotation in the source PVC like that:
pvc-shared-namespaces: NS1, NS2
  • Thirdly, when we want to share PV by the source PVC, we can add two *annotation in the second PVC like that:
pvc-ref: pvc-1  # the name of the source PVC
pvc-ref-namespace: pvc-1-ns  # the namespace of the source PVC

Volume create as follows:

CSI takes out pvc-ref and pvc-ref-namespace from CreateVolumeRequest.Parameters in CreateVolume interface:

  1. Find out the source PVC based on pvc-ref and pvc-ref-namespace, take out pvc-shared-namespaces from the source PVC, and judge whether the namespace of the new PVC is in it, if yes, go to 2, otherwise refuse to create;
  2. Determine that the reclaimPolicy of the StorageClass of the new PVC is Retain, if yes, go to 3, otherwise refuse to create;
  3. Find the source PV from the source PVC, and construct a CreateVolumeResponse based on the source PV.

Note: The returned VolumeId is the Spec.CSI.VolumeHandle field of the source PV. After finding the source PV, you can determine whether the persistentVolumeReclaimPolicy of the source PV is Retain. If not, refuse to create.

Kopeisk answered 10/8, 2021 at 13:56 Comment(2)
Could you link to documentation? I find nothing on pvc-shared-namespaces over kubernetes official documentation, or even on google.Angulo
A nice idea, Jingyi, though the only place where this cool feature is mentioned is your comment here: github.com/kubernetes/kubernetes/issues/…. Wish it really would be a working solution, though it doesn't seems to be that. ;-(Sprag

© 2022 - 2024 — McMap. All rights reserved.