简体   繁体   English

缩减后从 statefulset 中删除 Kubernetes 持久卷

[英]delete Kubernetes persistent volume from statefulset after scale down

I scaled my statefulset up to 4, and when scaling down to 1, I saw that I still have 4 persistent volumes with indexes from 0 to 3.我将 statefulset 扩展到 4,当缩小到 1 时,我看到我仍然有 4 个持久卷,索引从 0 到 3。

I also saw that the status of all of them is Bound I guess it is because I use it as stateful set, so it doesn't delete the volumes after scale down.我还看到它们的状态都是Bound我猜是因为我将它用作有状态集,所以它不会在缩小后删除卷。

I tried to manually delete one of tham (the one with index 2) because I was sure it will release my volume, so I used:我试图手动删除其中一个(索引为 2 的那个),因为我确信它会释放我的音量,所以我使用了:

kubectl delete persistentvolume <volume>

Well, that didn't help, it just made this volume to be in a terminating state forever... :/好吧,这并没有帮助,它只是让这个卷永远处于终止 state 中......:/

I have no idea how to remove this and all the other unused volumes now.我现在不知道如何删除这个和所有其他未使用的卷。

here is the volume configuration in stateful set yaml.这是状态集 yaml 中的卷配置。

  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: "default"
        resources:
          requests:
            storage: 7Gi

if I run如果我跑

kubectl get pvc --all-namespaces

I get我明白了

NAMESPACE    NAME                      STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
default      data-0         Bound    pvc-23af1aec-e385-4778-b0b0-56f1d1dfdfee   7Gi        RWO            default        4h5m
default      data-1         Bound    pvc-34625107-1352-4715-b12c-2fc6ff22ed08   7Gi        RWO            default        4h4m
default      data-2         Bound    pvc-15dbdb53-d951-465d-b9c3-ebadfcc3c725   7Gi        RWO            default        4h3m
default      data-3         Bound    pvc-d317657f-194a-4f4f-8c5f-dff2843b693f   7Gi        RWO            default        4h3m

if I run如果我跑

kubectl get --no-headers persistentvolumes

I get this:我明白了:

pvc-15dbdb53-d951-465d-b9c3-ebadfcc3c725   7Gi   RWO   Delete   Terminating   default/data-2            default         4h4m
pvc-23af1aec-e385-4778-b0b0-56f1d1dfdfee   7Gi   RWO   Delete   Bound         default/data-0            default         4h6m
pvc-34625107-1352-4715-b12c-2fc6ff22ed08   7Gi   RWO   Delete   Bound         default/data-1            default         4h5m
pvc-d317657f-194a-4f4f-8c5f-dff2843b693f   7Gi   RWO   Delete   Bound         default/data-3            default         4h3m

In statefulset, K8s doesn't delete PV or PVC by their own after termination of a pod automatically, It is to avoid further complication and for data safety.在 statefulset 中,K8s 不会在 Pod 自动终止后自行删除PVPVC ,这是为了避免进一步的复杂化和数据安全。 Thats why after doing scale down, we need to do it manually.Deleting the PVC after the pods have terminated will trigger deletion of the respective Persistent Volumes depending on the storage class and reclaim policy.这就是为什么在进行缩减后,我们需要手动进行。在 Pod 终止后删除PVC将触发删除相应的持久卷,具体取决于存储 class 和回收策略。

Please try to delete persistent volume claim or PVC instead of persistent volume .请尝试删除persistent volume claimPVC而不是persistent volume if you delete pvc it will automatically delete the respective pv .如果您删除pvc ,它将自动删除相应的pv

just run this command in your bash:只需在您的 bash 中运行此命令:

kubectl delete pvc data-3

REF 参考

There is a new feature introduced in k8s v1.23 [alpha] for this exact requirement. k8s v1.23 [alpha] 中引入了一个新特性来满足这个确切的要求。 Look for PVC retention policy in STS . 在 STS 中查找 PVC 保留策略

whenDeleted何时删除

configures the volume retention behavior that applies when the StatefulSet is deleted配置删除 StatefulSet 时应用的卷保留行为

whenScaled缩放时

configures the volume retention behavior that applies when the replica count of the StatefulSet is reduced;配置当 StatefulSet 的副本数减少时应用的卷保留行为; for example, when scaling down the set.例如,缩小集合时。

For each policy that you can configure, you can set the value to either Delete or Retain.对于您可以配置的每个策略,您可以将值设置为Delete 或 Retain。

Something like below would work for you.像下面这样的东西会为你工作。

  • On STS deletion - PVC, PV, cloud volume will be retained.在 STS 删除时 - PVC、PV、云量将被保留。
  • On STS scale down - PVC, PV, cloud volume will be deleted.在 STS 缩减时 - PVC、PV、云量将被删除。

below snippet from k8s docs:以下来自 k8s 文档的片段:

apiVersion: apps/v1
kind: StatefulSet
...
spec:
  persistentVolumeClaimRetentionPolicy:
    whenDeleted: Retain
    whenScaled: Delete
...

Along with this, Don't forget to set the PV reclaim policy to delete.除此之外,不要忘记将PV 回收策略设置为删除。 This will ensure that as soon as PVC gets deleted, even the PV along with backing volume would get deleted.这将确保一旦 PVC 被删除,即使 PV 和备份卷也会被删除。

refer this diagram for better understanding this.请参阅此以更好地理解这一点。

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

相关问题 带有 NFS 持久卷的 Kubernetes statefulset - Kubernetes statefulset with NFS persistent volume 在 Minikube 上的 Kubernetes StatefulSet 中添加持久卷 - Add persistent volume in Kubernetes StatefulSet on Minikube Kubernetes 删除后重新附加到相同的持久卷 - Kubernetes reattach to same persistent volume after delete 重新部署 kubernetes statefulset 是否保留持久卷声明? - Does re-deploying kubernetes statefulset preserve persistent volume claim? 有没有一种方法可以在kubernetes部署(或有状态集)中为每个Pod创建持久卷? - Is there a way to create a persistent volume per pod in a kubernetes deployment (or statefulset)? 在运行时可以按比例放大/缩小kubernetes statefulset的选项有哪些? - What are the options to scale up/scale down a kubernetes statefulset during runtime? Kubernetes - statefulSet 和卷权限 - Kubernetes - statefulSet and volume permissions 如何与另一个 StatefulSet 共享 StatefulSet 的持久卷? - How to share persistent volume of a StatefulSet with another StatefulSet? 在不丢失持久卷访问的情况下从 Deployment 迁移到 StatefulSet - Migrate from Deployment to StatefulSet without losing persistent volume access Kubernetes - 将数据写入 StatefulSet 卷 - Kubernetes - writing data to volume of StatefulSet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM