Sharing a persistent volume between pods in Kubernetes

Accessing the same PV on multiple node is not that easy as it looks. First of all, based on your your use case, you need a supporting filesystem underneath for creating this shared storage. An EFS/NFS/GlusterFS should be ideal and kubernetes volume plugin support most of these. Additionally you need to have PVC with the right access mode Eg : ReadWriteMany, in case you are looking for a multi-write architecture. Again, in a micro service world, you should try to avoid such use cases or find better alternatives to make your design scalable, stateless to a possible extend. https://12factor.net/ emphasize most of these principles.


From the k8s documentation:

A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator. It is a resource in the cluster just like a node is a cluster resource. PVs are volume plugins like Volumes, but have a lifecycle independent of any individual pod that uses the PV. This API object captures the details of the implementation of the storage, be that NFS, iSCSI, or a cloud-provider-specific storage system.

A PersistentVolumeClaim (PVC) is a request for storage by a user. It is similar to a pod. Pods consume node resources and PVCs consume PV resources. Pods can request specific levels of resources (CPU and Memory). Claims can request specific size and access modes (e.g., can be mounted once read/write or many times read-only).

Meaning that in the scenario pictured in the question, if PodA_deployment.yaml creates a volume claim:

volumeMounts:
- name: myapp-data-pv-1
  mountPath: /home/myappdata/mystuff

then PodB will be able to mount the pv making a claim like the following:

volumes:
   - name: myapp-data-pv-1
     persistentVolumeClaim:
       claimName: myapp-data-pvc-1

in PodB_deployment.yaml. While it's clear once and it makes sense once you get to understand it, the documentation could explain it better.