kubernetes pv reclaim

本文介绍如何解决Kubernetes中Persistent Volume (PV)在删除Persistent Volume Claim (PVC)后进入Released状态的问题,并通过kubectl replace命令实现PV的复用。
部署运行你感兴趣的模型镜像

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

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

### 创建基于 NFS 的静态 PVKubernetes 中创建基于 NFS 的静态 PersistentVolume (PV),需要手动定义一个 YAML 文件来描述 NFS 存储的配置。以下是具体实现方法: #### 1. 配置 NFS 静态 PV 以下是一个示例 YAML 文件,用于定义一个静态 NFS PV: ```yaml apiVersion: v1 kind: PersistentVolume metadata: name: nfs-static-pv spec: capacity: storage: 10Gi accessModes: - ReadWriteMany persistentVolumeReclaimPolicy: Retain nfs: server: <nfs-server-ip> # 替换为实际的 NFS 服务器 IP 地址[^4] path: "/exports/data" # 替换为实际的 NFS 共享路径 ``` 上述 YAML 文件中: - `capacity` 定义了存储容量,这里设置为 10Gi。 - `accessModes` 指定了访问模式,`ReadWriteMany` 表示允许多个 Pod 同时读写该存储[^2]。 - `persistentVolumeReclaimPolicy` 设置为 `Retain`,表示当 PVC 被删除时保留数据[^3]。 - `nfs.server` 和 `nfs.path` 分别指定 NFS 服务器的 IP 地址和共享路径。 #### 2. 创建静态 PV 使用以下命令将上述 YAML 文件应用到 Kubernetes 集群中: ```bash kubectl apply -f nfs-static-pv.yaml ``` 可以通过以下命令查看是否成功创建了 PV: ```bash kubectl get pv ``` 输出示例: ```plaintext NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE nfs-static-pv 10Gi RWX Retain Available 1m ``` #### 3. 创建 PVC 并绑定到静态 PV 接下来,创建一个 PersistentVolumeClaim (PVC) 来绑定到上述静态 PV: ```yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: nfs-static-pvc spec: accessModes: - ReadWriteMany resources: requests: storage: 10Gi ``` 应用此 YAML 文件: ```bash kubectl apply -f nfs-static-pvc.yaml ``` 通过以下命令验证 PVC 是否成功绑定到 PV: ```bash kubectl get pvc ``` 输出示例: ```plaintext NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE nfs-static-pvc Bound nfs-static-pv 10Gi RWX 1m ``` #### 4. 在 Pod 中使用 PVC 最后,在 Pod 或 Deployment 的 YAML 文件中挂载该 PVC: ```yaml apiVersion: v1 kind: Pod metadata: name: nfs-pod spec: containers: - name: busybox image: busybox command: ["sleep", "10000"] volumeMounts: - mountPath: "/mnt/nfs" name: nfs-volume volumes: - name: nfs-volume persistentVolumeClaim: claimName: nfs-static-pvc ``` 应用此 YAML 文件: ```bash kubectl apply -f nfs-pod.yaml ``` 验证 Pod 是否正常运行: ```bash kubectl get pods ``` 输出示例: ```plaintext NAME READY STATUS RESTARTS AGE nfs-pod 1/1 Running 0 1m ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值