Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes NFS Persistent Volumes - multiple claims on same volume? Claim stuck in pending?

Use case:

I have a NFS directory available and I want to use it to persist data for multiple deployments & pods.

I have created a PersistentVolume:

apiVersion: v1 kind: PersistentVolume metadata:   name: nfs-pv spec:   capacity:     storage: 10Gi   accessModes:     - ReadWriteMany   nfs:     server: http://mynfs.com     path: /server/mount/point 

I want multiple deployments to be able to use this PersistentVolume, so my understanding of what is needed is that I need to create multiple PersistentVolumeClaims which will all point at this PersistentVolume.

kind: PersistentVolumeClaim apiVersion: v1 metaData:   name: nfs-pvc-1   namespace: default spec:   accessModes:     - ReadWriteMany   resources:     requests:       storage: 50Mi 

I believe this to create a 50MB claim on the PersistentVolume. When I run kubectl get pvc, I see:

NAME        STATUS     VOLUME    CAPACITY    ACCESSMODES   AGE nfs-pvc-1   Bound      nfs-pv    10Gi        RWX           35s 

I don't understand why I see 10Gi capacity, not 50Mi.

When I then change the PersistentVolumeClaim deployment yaml to create a PVC named nfs-pvc-2 I get this:

NAME        STATUS     VOLUME    CAPACITY    ACCESSMODES   AGE nfs-pvc-1   Bound      nfs-pv    10Gi        RWX           35s nfs-pvc-2   Pending                                        10s 

PVC2 never binds to the PV. Is this expected behaviour? Can I have multiple PVCs pointing at the same PV?

When I delete nfs-pvc-1, I see the same thing:

NAME        STATUS     VOLUME    CAPACITY    ACCESSMODES   AGE nfs-pvc-2   Pending                                        10s 

Again, is this normal?

What is the appropriate way to use/re-use a shared NFS resource between multiple deployments / pods?

like image 453
John Avatar asked May 26 '17 14:05

John


People also ask

Can a persistent volume have multiple claims?

The mapping between persistentVolume and persistentVolumeClaim is always one to one. Even When you delete the claim, PersistentVolume still remains as we set persistentVolumeReclaimPolicy is set to Retain and It will not be reused by any other claims.

Can multiple PVC use same PV?

Once a PV is bound to a PVC, that PV is essentially tied to the PVC's project and cannot be bound to by another PVC. There is a one-to-one mapping of PVs and PVCs. However, multiple pods in the same project can use the same PVC.

What is the difference between persistent volume and persistent volume claim in Kubernetes?

PVCs are requests for those resources and also act as claim checks to the resource. So a persistent volume (PV) is the "physical" volume on the host machine that stores your persistent data. A persistent volume claim (PVC) is a request for the platform to create a PV for you, and you attach PVs to your pods via a PVC.


2 Answers

Basically you can't do what you want, as the relationship PVC <--> PV is one-on-one.

If NFS is the only storage you have available and would like multiple PV/PVC on one nfs export, use Dynamic Provisioning and a default storage class.

It's not in official K8s yet, but this one is in the incubator and I've tried it and it works well: https://github.com/kubernetes-incubator/external-storage/tree/master/nfs-client

This will enormously simplify your volume provisioning as you only need to take care of the PVC, and the PV will be created as a directory on the nfs export / server that you have defined.

like image 151
Volker Kerkhoff Avatar answered Sep 21 '22 09:09

Volker Kerkhoff


From: https://docs.openshift.org/latest/install_config/storage_examples/shared_storage.html

As Baroudi Safwen mentioned, you cannot bind two pvc to the same pv, but you can use the same pvc in two different pods.

volumes: - name: nfsvol-2   persistentVolumeClaim:     claimName: nfs-pvc-1 <-- USE THIS ONE IN BOTH PODS    
like image 43
Javier Salmeron Avatar answered Sep 19 '22 09:09

Javier Salmeron