在 Kubernetes 中 pods 之间共享一个持久卷

Sharing a persistent volume between pods in Kubernetes

我们在 Kubernetes 中有两个 pods,为了便于对话,我们将其称为 pod1 和 pod2。我在 pod 1 上创建了 pv1 和 pvc1,它工作正常。 在我看来,文档对这种情况不够清楚,或者我找不到合适的 wiki。 如何从 pod2 访问 pv1 和 pvc1?

来自 k8s 文档:

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).

意思是在问题中描绘的场景中,如果 PodA_deployment.yaml 创建了卷声明:

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

然后 PodB 将能够像下面这样声明安装 pv:

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

在 PodB_deployment.yaml 中。 虽然它一目了然,一旦你理解它就有意义了,文档可以更好地解释它。

在多个节点上访问同一个PV并不像看起来那么容易。首先,根据您的用例,您需要一个支持文件系统来创建此共享存储。 EFS/NFS/GlusterFS 应该是理想的,kubernetes 卷插件支持其中的大部分。此外,您需要拥有具有正确访问模式的 PVC,例如:ReadWriteMany,以防您正在寻找多写架构。同样,在微服务世界中,您应该尽量避免此类用例或找到更好的替代方案,以使您的设计可扩展,无状态地扩展到可能的范围。 https://12factor.net/ 强调其中大部分原则。