简体   繁体   English

如何在 GKE 上使用 Helm 重用动态配置的 PersistentVolumes?

[英]How can you reuse dynamically provisioned PersistentVolumes with Helm on GKE?

I am trying to deploy a helm chart which uses PersistentVolumeClaim and StorageClass to dynamically provision the required sotrage.我正在尝试部署一个使用PersistentVolumeClaimStorageClass来动态提供所需存储的 helm chart。 This works as expected, but I can't find any configuration which allows a workflow like这按预期工作,但我找不到任何允许像这样的工作流程的配置

helm delete xxx

# Make some changes and repackage chart

helm install --replace xxx

I don't want to run the release constantly, and I want to reuse the storage in deployments in the future.我不想不断地运行发布,我想在未来的部署中重用存储。

Setting the storage class to reclaimPolicy: Retain keeps the disks, but helm will delete the PVC and orphan them.将存储类设置为reclaimPolicy: Retain保留磁盘,但 helm 将删除 PVC 并将其孤立。 Annotating the PVC's so that helm does not delete them fixes this problem, but then running install causes the error注释 PVC 以便 helm 不删除它们修复了这个问题,但是运行安装会导致错误

Error: release xxx failed: persistentvolumeclaims "xxx-xxx-storage" already exists

I think I have misunderstood something fundamental to managing releases in helm.我想我误解了掌舵管理发布的一些基本知识。 Perhaps the volumes should not be created in the chart at all.也许根本不应该在图表中创建交易量。

PersistenVolumeClain creating just a mapping between your actualPersistentVolume and your pod. PersistenVolumeClain只是在您的实际PersistentVolume和您的 pod 之间创建一个映射。

Using "helm.sh/resource-policy": keep annotation for PV is not the best idea, because of that remark in a documentation :使用"helm.sh/resource-policy": keep注释不是最好的主意,因为文档中的注释:

The annotation "helm.sh/resource-policy": keep instructs Tiller to skip this resource during a helm delete operation.注释“helm.sh/resource-policy”:keep 指示 Tiller 在 helm delete 操作期间跳过此资源。 However, this resource becomes orphaned.但是,此资源变为孤立资源。 Helm will no longer manage it in any way. Helm 将不再以任何方式管理它。 This can lead to problems if using helm install --replace on a release that has already been deleted, but has kept resources.如果在已删除但保留资源的版本上使用 helm install --replace ,这可能会导致问题。

If you will create a PV manually after you will delete your release, Helm will remove PVC, which will be marked as "Available" and on next deployment, it will reuse it.如果您在删除发布后手动创建 PV,Helm 将删除 PVC,PVC 将被标记为“可用”,并在下一次部署时重新使用它。 Actually, you don't need to keep your PVC in the cluster to keep your data.实际上,您不需要将 PVC 保留在集群中以保留您的数据。 But, for making it always using the same PV, you need to use labels and selectors .但是,为了让它始终使用相同的 PV,您需要使用labels 和 selectors

For keep and reuse volumes you can:对于保留和重用卷,您可以:

  1. Create PersistenVolume with the label, as an example, for_app=my-app and set "Retain" policy for that volume like this:使用标签创建 PersistenVolume,例如for_app=my-app并为该卷设置“保留”策略,如下所示:
apiVersion: v1
kind: PersistentVolume
metadata:
  name: myappvolume
  namespace: my-app
  labels:
    for_app: my-app
spec:
  persistentVolumeReclaimPolicy: Retain
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  1. Modify your PersistenVolumeClaim configuration in Helm.在 Helm 中修改 PersistenVolumeClaim 配置。 You need to add a selector for using only PersistenVolumes with a label for_app=my-app .您需要添加一个选择器以仅使用带有标签for_app=my-app的 PersistenVolumes。
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myappvolumeclaim
  namespace: my-app
spec:
  selector:
    matchLabels:
      for_app: my-app
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

So, now your application will use the same volume each time when it started.因此,现在您的应用程序每次启动时都将使用相同的卷。

But, please keep in mind, you may need to use selectors for other apps in the same namespace for preventing using your PV by them.但是,请记住,您可能需要为同一命名空间中的其他应用程序使用选择器,以防止它们使用您的 PV。

Actually, I'd suggest using StateFul sets and VolumeClaimTemplates: https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/实际上,我建议使用 StateFul 集和 VolumeClaimTemplates: https ://kubernetes.io/docs/concepts/workloads/controllers/statefulset/

The example there should speak for itself..那里的例子应该不言自明..

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

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