本文永久链接: https://www.xtplayer.cn/kubernetes/reuse-released-pv/

PV 回收策略

当用户不再使用其存储卷时,他们可以从 API 中将 PVC 对象删除,从而允许 该资源被回收再利用。PersistentVolume 对象的回收策略告诉集群,当其被 从申领中释放时如何处理该数据卷。 目前,数据卷可以被 Retained(保留)、Recycled(回收,Recycle 已被废弃)或 Deleted(删除)。

保留(Retain)

回收策略 Retain 使得用户可以手动回收资源。当 PersistentVolumeClaim 对象 被删除时,PersistentVolume 卷仍然存在,对应的数据卷被视为”已释放(released)”。 由于卷上仍然存在这前一申领人的数据,该卷还不能用于其他申领。 管理员可以通过下面的步骤来手动回收该卷:

  1. 删除 PersistentVolume 对象。与之相关的、位于外部基础设施中的存储资产 (例如 AWS EBS、GCE PD、Azure Disk 或 Cinder 卷)在 PV 删除之后仍然存在。
  2. 根据情况,手动清除所关联的存储资产上的数据。
  3. 手动删除所关联的存储资产;如果你希望重用该存储资产,可以基于存储资产的 定义创建新的 PersistentVolume 卷对象。

删除(Delete)

对于支持 Delete 回收策略的卷插件,删除动作会将 PersistentVolume 对象从 Kubernetes 中移除,同时也会从外部基础设施(如 AWS EBS、GCE PD、Azure Disk 或 Cinder 卷)中移除所关联的存储资产。 动态供应的卷会继承其 StorageClass 中设置的回收策略,该策略默认 为 Delete。 管理员需要根据用户的期望来配置 StorageClass;否则 PV 卷被创建之后必须要被 编辑或者修补。参阅更改 PV 卷的回收策略.

参考:https://kubernetes.io/zh/docs/concepts/storage/persistent-volumes/#reclaiming

准备测试 pv 和 pvc

  1. 点击 集群|存储|持久卷,点击右侧添加 pv:

  2. 进入任意项目,点击 PVC,然后点击右侧 添加 pvc

通过命令查看 pv 和 pvc yaml 配置

  • pv

    hxl@rancher:~$ kubectl get pv test-path-pv -oyaml

    apiVersion: v1
    kind: PersistentVolume
    metadata:
    annotations:
    field.cattle.io/creatorId: user-wf5x7
    pv.kubernetes.io/bound-by-controller: "yes"
    creationTimestamp: "2020-12-08T11:17:06Z"
    finalizers:
    - kubernetes.io/pv-protection
    labels:
    cattle.io/creator: norman
    name: test-path-pv
    resourceVersion: "24187470"
    selfLink: /api/v1/persistentvolumes/test-path-pv
    uid: 99f719ba-3314-48f3-9473-11e0b19a8806
    spec:
    accessModes:
    - ReadWriteOnce
    capacity:
    storage: 10Gi
    claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: test-path-pvc
    namespace: default
    resourceVersion: "24187465"
    uid: 88a75ff5-232a-44d5-8bbd-933fb325b80f
    hostPath:
    path: /tmp/test-path-pv
    type: ""
    persistentVolumeReclaimPolicy: Retain
    volumeMode: Filesystem
    status:
    phase: Bound
  • pvc

    hxl@rancher:~$ kubectl get pvc test-path-pvc -oyaml

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    annotations:
    field.cattle.io/creatorId: user-wf5x7
    pv.kubernetes.io/bind-completed: "yes"
    creationTimestamp: "2020-12-08T11:17:57Z"
    finalizers:
    - kubernetes.io/pvc-protection
    labels:
    cattle.io/creator: norman
    name: test-path-pvc
    namespace: default
    resourceVersion: "24187481"
    selfLink: /api/v1/namespaces/default/persistentvolumeclaims/test-path-pvc
    uid: 88a75ff5-232a-44d5-8bbd-933fb325b80f
    spec:
    accessModes:
    - ReadWriteOnce
    resources:
    requests:
    storage: 10Gi
    storageClassName: ""
    volumeMode: Filesystem
    volumeName: test-path-pv
    status:
    accessModes:
    - ReadWriteOnce
    capacity:
    storage: 10Gi
    phase: Bound

在 pv 的 spec.claimRef 可以看到对应 pvc 的具体信息,重要的有 name、namespace、uid。

测试删除 pvc

因为 pv 的 persistentVolumeReclaimPolicy 属性为 Retain,在删除 pvc 后 pv,会自动保留,所以 pv 状态变为 Released

编辑 pv

为了 pv 可以重新被挂载,需要执行 kubectl edit pv test-path-pv 命令,清理 spec.claimRef 中的内容,以使 pv 成为可用状态。

也可以执行以下命令去快速清除 pv 的 claimRef:

kubectl patch pv <pv-name> -p '{"spec":{"claimRef": null}}'

重新创建 pvc

进入任意项目,点击 PVC,然后点击右侧 添加 pvc