简体   繁体   English

持久卷和持久卷声明在 kubernetes 中如何相互绑定

[英]How persistent volume and persistence volume claim bound each other in kubernetes

I am working on creating persistence volume & persistence volume claim in kubernetes.我正在努力在 kubernetes 中创建持久性卷和持久性卷声明。 Both below configuration working fine and I am able to store the data in persistence volume storage path.以下配置都工作正常,我能够将数据存储在持久卷存储路径中。

I created persistence volume我创建了持久性卷

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-vol
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi #Size of the volume
  accessModes:
    - ReadWriteOnce #type of access
  hostPath:
    path: "/mnt/data" #host location
---

and Persistence volume claim:和持久性卷声明:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---

Here there is no connection between persistence volume & persistence volume claim in above configuration files.在这里,上述配置文件中的持久性卷和持久性卷声明之间没有联系。 How both are bound to each other.两者如何相互绑定。

Persistence volume & persistence volume claim持久性卷和持久性卷声明

Say in deployment.yml, we can point the name of persistence volume claim.说在deployment.yml中,我们可以指出持久化卷声明的名称。 So that POD -> PVC -> PV -> host machine storage location.这样POD -> PVC -> PV -> 主机存储位置。

Could anyone help me to understand the how persistence volume & persistence volume claim bound to each other by above configuration files.任何人都可以帮助我了解持久性卷和持久性卷声明如何通过上述配置文件相互绑定。

In a nutshell binding between PV and PVC is decided by matching capacity and accessModes .简而言之,PV 和 PVC 之间的绑定是由匹配capacityaccessModes决定的。 Since you have 1Gi and ReadWriteOnce in both PV and PVC the binding was successful.由于您在 PV 和 PVC 中都有1GiReadWriteOnce ,因此绑定成功。

From the docs here这里的文档

A user creates, or in the case of dynamic provisioning, has already created, a PersistentVolumeClaim with a specific amount of storage requested and with certain access modes.用户创建,或者在动态配置的情况下,已经创建了一个 PersistentVolumeClaim,它具有特定的存储量和特定的访问模式。 A control loop in the master watches for new PVCs, finds a matching PV (if possible), and binds them together. master 中的控制循环监视新的 PVC,找到匹配的 PV(如果可能),并将它们绑定在一起。 If a PV was dynamically provisioned for a new PVC, the loop will always bind that PV to the PVC.如果为新的 PVC 动态配置了 PV,则循环将始终将该 PV 绑定到 PVC。 Otherwise, the user will always get at least what they asked for, but the volume may be in excess of what was requested.否则,用户将始终至少得到他们所要求的,但数量可能超过所要求的。 Once bound, PersistentVolumeClaim binds are exclusive, regardless of how they were bound.一旦绑定,PersistentVolumeClaim 绑定是独占的,无论它们是如何绑定的。 A PVC to PV binding is a one-to-one mapping, using a ClaimRef which is a bi-directional binding between the PersistentVolume and the PersistentVolumeClaim. PVC 到 PV 的绑定是一对一的映射,使用 ClaimRef,它是 PersistentVolume 和 PersistentVolumeClaim 之间的双向绑定。

Claims will remain unbound indefinitely if a matching volume does not exist.如果不存在匹配的卷,则声明将无限期保持不受约束。 Claims will be bound as matching volumes become available.当匹配的数量可用时,索赔将受到约束。 For example, a cluster provisioned with many 50Gi PVs would not match a PVC requesting 100Gi.例如,配置有许多 50Gi PV 的集群不会匹配请求 100Gi 的 PVC。 The PVC can be bound when a 100Gi PV is added to the cluster集群添加100Gi PV时可以绑定PVC

Do note that the storage classes(manual) in both the pv and pvc are the same which is one of the reasons they are bound.if they are different, then the pvc will go to pending status.请注意,pv 和 pvc 中的存储类(手动)是相同的,这是它们被绑定的原因之一。如果它们不同,那么 pvc 将 go 进入挂起状态。 It's imperative that they are the same to be bound.它们必须相同才能被绑定。 Hope this helps, You can also refer to this thread for various ways to bind.希望对您有所帮助,您也可以参考此线程了解各种绑定方式。 Can a PVC be bound to a specific PV? PVC可以绑定到特定的PV吗?

PVC documentation:https://kubernetes.io/docs/concepts/storage/persistent-volumes/ PVC 文档:https://kubernetes.io/docs/concepts/storage/persistent-volumes/

PVCs don't necessarily have to request a class. PVC 不一定要请求 class。 A PVC with its storageClassName set equal to "" is always interpreted to be requesting a PV with no class, so it can only be bound to PVs with no class (no annotation or one set equal to ""). storageClassName 设置为“”的 PVC 总是被解释为请求一个没有 class 的 PV,因此它只能绑定到没有 class 的 PV(没有注释或一组等于“”)。 A PVC with no storageClassName is not quite the same and is treated differently by the cluster, depending on whether the DefaultStorageClass admission plugin is turned on.一个没有 storageClassName 的 PVC 不太一样,会被集群区别对待,这取决于 DefaultStorageClass 准入插件是否开启。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM