Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing a persistent volume between pods in Kubernetes

We have two pods in Kubernetes that for sake of conversation we'll call pod1 and pod2. I created pv1 and pvc1 on pod 1 and it's working fine. In my opinion, the documentation is not clear enough about this scenario or I couldn't find the right wiki. How can I access pv1 and pvc1 from pod2?

like image 934
AR1 Avatar asked Sep 07 '17 16:09

AR1


People also ask

Can persistent volume be shared?

Kubernetes persistent volumes provide data storage for stateful applications. They abstract a storage system's implementation from how it's consumed by your pods. A persistent volume could store data locally, on a network share, or in a block storage volume provided by a cloud vendor.

Can volume be shared between pods?

This means that an EBS volume can be pre-populated with data, and that data can be shared between pods. Note: You must create an EBS volume by using aws ec2 create-volume or the AWS API before you can use it.

Can multiple pods use the same persistent volume claim?

Creating the Persistent Volume Claim There is a one-to-one mapping of PVs and PVCs. However, multiple pods in the same project can use the same PVC. This is the use case we are highlighting in this example. The claim name is referenced by the pod under its volumes section.


2 Answers

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.

like image 159
AR1 Avatar answered Sep 16 '22 15:09

AR1


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.

like image 40
anrajme Avatar answered Sep 17 '22 15:09

anrajme