pv是release状态,如何恢复pvc使用数据

目录

    • 前言
      • 确认pv存在数据
      • 删除pod、删除pv
      • pv状态是Released,不能被pvc绑定
      • 将Released状态的pv设为可用Available
    • 总结

前言

环境:k8s v1.22.6、centos7
本文演示当一个pv是released状态时,即pvc删除了,pv就会变成releases状态,本篇讲演示如何重新使用该pv的数据:

确认pv存在数据

[root@master01 ~]# kubectl get pv pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                   STORAGECLASS       REASON   AGE
pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d   1Gi        RWO            Delete           Bound    default/mysql-mysql-0   nfs-storageclass            36d

#确认pv对应的pod是mysql,mysql是持久化数据的
[root@master01 ~]# kubectl exec -it mysql-0 -- bash
root@mysql-0:/# mysql -uroot -pAa123456
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| ry-cloud           |
| ry-config          |
| sys                |
+--------------------+
mysql> use ry-config
mysql> show tables;
+----------------------+
| Tables_in_ry-config  |
+----------------------+
| config_info          |
| config_info_aggr     |
| config_info_beta     |
+----------------------+

删除pod、删除pv

#现在删除pvc,这样pv就会变成released状态,但是我们发现pv的策略是Delete,所以需要先将pv的策略设为Retain
[root@master01 ~]# kubectl edit pv pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d
[root@master01 ~]# kubectl get pv pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                   STORAGECLASS       REASON   AGE
pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d   1Gi        RWO            Retain           Bound    default/mysql-mysql-0   nfs-storageclass            36d
#删除pvc,但是删除pvc之前需要删除对应的pod,这是k8s的存储机制决定的,这样没有pod使用pvc时,pvc才能删除成功
[root@master01 ~]# kubectl  scale  sts mysql  --replicas=0
[root@master01 ~]# kubectl delete  pvc mysql-mysql-0
#现在查看pv的状态,状态变成Released
[root@master01 ~]# kubectl get pv pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM                   STORAGECLASS       REASON   AGE
pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d   1Gi        RWO            Retain           Released   default/mysql-mysql-0   nfs-storageclass            36d

pv状态是Released,不能被pvc绑定

#现在启动一个mysql pod,我们发现,sc会重新创建pv而不是使用之前的pv,因为之前的pv状态是Released,Released状态的pv是不能被pvc绑定的
[root@master01 ~]# kubectl scale  sts mysql --replicas=1
statefulset.apps/mysql scaled
[root@master01 ~]# kubectl get pv  | grep mysql
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM                                                             STORAGECLASS       REASON   AGE
pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d   1Gi        RWO            Retain           Released   default/mysql-mysql-0                                             nfs-storageclass            36d
pvc-9583652d-dea1-4212-8a39-4b845fcbe0ce   1Gi        RWO            Delete           Bound      default/mysql-mysql-0                                             nfs-storageclass            84s
#删除刚才创建的pvc、pv,恢复一下
kubectl scale  sts mysql --replicas=0
kubectl delete  pvc mysql-mysql-0                                                       

将Released状态的pv设为可用Available

#将Released状态的pv设为可用
[root@master01 ~]# kubectl edit pv pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS       REASON   AGE
pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d   1Gi        RWO            Retain           Available           nfs-storageclass            36d
#删除下面这段声明
  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: mysql-mysql-0
    namespace: default
    resourceVersion: "1196703"
    uid: 7df76867-615a-42ac-ad8d-9a3102d2ad8d

#现在pv的状态已经是Available状态了
[root@master01 ~]# kubectl get pv pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS       REASON   AGE
pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d   1Gi        RWO            Retain           Available           nfs-storageclass            36d
#创建一个mysql pod
[root@master01 ~]# kubectl scale  sts mysql --replicas=1
#pv变成绑定状态了
[root@master01 ~]# kubectl get pv pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                   STORAGECLASS       REASON   AGE
pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d   1Gi        RWO            Retain           Bound    default/mysql-mysql-0   nfs-storageclass            36d
#pvc也是绑定状态
[root@master01 ~]# kubectl get pvc mysql-mysql-0 
NAME            STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS       AGE
mysql-mysql-0   Bound    pvc-7df76867-615a-42ac-ad8d-9a3102d2ad8d   1Gi        RWO            nfs-storageclass   29s
#可以看到pvc已经绑定了pv,这说明pod可以继续使用pv里面的数据了
#验证pod可以继续使用pv里面的数据
[root@master01 ~]# kubectl exec -it mysql-0 -- bash
root@mysql-0:/# mysql -uroot -pAa123456
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| ry-cloud           |	#数据库存在
| ry-config          |	#数据库存在
| sys                |
+--------------------+

总结

以上使用sts创建的pod进行测试的,sts的yaml中会自动创建pvc:

kubectl get sts mysql -oyaml
  volumeClaimTemplates:
  - apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      creationTimestamp: null
      name: mysql
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi
      storageClassName: nfs-storageclass
      volumeMode: Filesystem
    status:
      phase: Pending
然后创建的pvc绑定到了状态是 Available的pv,这样就实现了重新使用pv的数据。
以上,我们总结如下:
不管是deployment创建的pod,还是sts创建的pod,首先需要将pv设为Retain(防止删除pvc时pv被删除),然后将pv状态设为Available状态,即删
除pv中claimRef相关字段,然后创建一个pvc(不管是手动创建的还是sts中使用pvc模板创建的),这个pvc的访问模式和容量大小要与pv的访问模式和
大小相匹配,这样pvc才能与pv进行绑定成功。

你可能感兴趣的:(kubernetes,pv恢复数据,k8s)