[英]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 自动终止后自行删除PV
或PVC
,这是为了避免进一步的复杂化和数据安全。 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 claim
或PVC
而不是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
You can set the retention policy to define the behavior for whenDeleted
and whenScaled
.您可以设置保留策略来定义whenDeleted
和whenScaled
的行为。
Check the same here:在这里检查相同:
Kubernetes 1.23
allows this with: https://kubernetes.io/blog/2021/12/16/kubernetes-1-23-statefulset-pvc-auto-deletion/ Kubernetes 1.23
允许这样做: https://kubernetes.io/blog/2021/12/16/kubernetes-1-23-statefulset-pvc-auto-deletion/
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.像下面这样的东西会为你工作。
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.