Rleased pv reclaim

Backgroud

在k8s上部署了一个nginx,用作图片服务器。应用了pvc作为容器存储。
处于某种目的,需要对该 pod 进行 Namespace 的迁移。即从 default 迁移到其它 Namespace. podconfigmapsvc 迁移很简单,在新的 Namespace 里面在创建一个即可。
但是 pvc 则有些不同。首先 pv volume类型为 Hostpath ,只能使用 RWO 模式,因此不能直接采用在新 Namespace 中新建 pvc 在由 k8s controller 自动关联到该 pv 。因此只能先将 pv 从原来的 pvc 中解除绑定。
解除绑定过程很简单,根据官方文档中的描述,删除 pv 绑定的 pvc 即可。

When a user is done with their volume, they can delete the PVC objects from the API which allows reclamation of the resource. The volume is considered “released” when the claim is deleted, but it is not yet available for another claim. The previous claimant’s data remains on the volume which must be handled according to policy.

这样一来 pv 就从 Bond 切换到了 Released 状态。但出于 Release 状态的 pv 是无法被 reclaim 的,这段在官方文档中也有描述。

A volume will be in one of the following phases:
Available – a free resource that is not yet bound to a claim
Bound – the volume is bound to a claim
Released – the claim has been deleted, but the resource is not yet reclaimed by the cluster
Failed – the volume has failed its automatic reclamation
The CLI will show the name of the PVC bound to the PV.

Solution

经过大量实践,最终发现出于 Released 状态的 pv ,可通过:

 kubectl replace -f xxx.yaml

重新加载 pv 配置后,将状态位清除。同时存储于 pv 中的内容也不会被删除,再次关联到新的 pvc 上时,原来保存的内容依旧可用。由此可达到 pv 复用的效果。

Reference

https://kubernetes.io/docs/concepts/storage/persistent-volumes/#introduction

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐