|NO.Z.00250|——————————|CloudNative|——|KuberNetes&存储进阶.V16|——|Ceph.v10|ceph测试|PVC快照|

一、PVC快照
### --- PVC快照说明

~~~     注意:PVC快照功能需要k8s 1.17+
~~~     块存储快照具体原理和注意事项参考5节点方案
二、创建snapshotClass
### --- 切换到创建目录

[root@k8s-master01 cephfs]# cd /root/rook/cluster/examples/kubernetes/ceph/csi/rbd
[root@k8s-master01 rbd]# kubectl create -f snapshotclass.yaml 
volumesnapshotclass.snapshot.storage.k8s.io/csi-rbdplugin-snapclass created
三、创建快照
### --- 首先在之前创建的MySQL容器里创建一个文件夹,并创建一个文件

[root@k8s-master01 rbd]# kubectl get po
NAME                               READY   STATUS    RESTARTS   AGE
busybox                            1/1     Running   2          130m
wordpress-mysql-6965fc8cc8-fvtrt   1/1     Running   0          69m
### --- 进入容器并创建目录和文件并写入内容

[root@k8s-master01 rbd]# kubectl exec -ti wordpress-mysql-6965fc8cc8-fvtrt -- bash
root@wordpress-mysql-6965fc8cc8-fvtrt:/# ls
bin  boot  dev  docker-entrypoint-initdb.d  entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@wordpress-mysql-6965fc8cc8-fvtrt:/# cd /var/lib/mysql
root@wordpress-mysql-6965fc8cc8-fvtrt:/var/lib/mysql# ls
auto.cnf  ib_logfile0  ib_logfile1  ibdata1  lost+found  mysql  performance_schema
root@wordpress-mysql-6965fc8cc8-fvtrt:/var/lib/mysql# mkdir test_snapshot
root@wordpress-mysql-6965fc8cc8-fvtrt:/var/lib/mysql# ls
auto.cnf  ib_logfile0  ib_logfile1  ibdata1  lost+found  mysql  performance_schema  test_snapshot
root@wordpress-mysql-6965fc8cc8-fvtrt:/var/lib/mysql# echo "test for snapshot" > test_snapshot/1.txt
root@wordpress-mysql-6965fc8cc8-fvtrt:/var/lib/mysql# cat test_snapshot/1.txt 
test for snapshot

四、修改snapshot.yaml文件的source pvc为创建的MySQL pvc:

~~~     # 修改为mysql-pv-claim

[root@k8s-master01 rbd]# kubectl get pvc
NAME             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS      AGE
mysql-pv-claim   Bound    pvc-a22e54cf-9d96-4070-b3a7-90a2fa73c249   30Gi       RWO            rook-ceph-block   71m
[root@k8s-master01 rbd]# vim snapshot.yaml 
spec:
  volumeSnapshotClassName: csi-rbdplugin-snapclass
  source:
    persistentVolumeClaimName: mysql-pv-claim

五、创建快照及查看状态

[root@k8s-master01 rbd]# kubectl  create -f snapshot.yaml
volumesnapshot.snapshot.storage.k8s.io/rbd-pvc-snapshot created
[root@k8s-master01 rbd]# kubectl  create -f snapshot.yaml
volumesnapshot.snapshot.storage.k8s.io/rbd-pvc-snapshot created
[root@k8s-master01 rbd]# kubectl get volumesnapshotclass
NAME                      DRIVER                       DELETIONPOLICY   AGE
csi-rbdplugin-snapclass   rook-ceph.rbd.csi.ceph.com   Delete           4m47s
[root@k8s-master01 rbd]# kubectl get volumesnapshot
NAME               READYTOUSE   SOURCEPVC        SOURCESNAPSHOTCONTENT   RESTORESIZE   SNAPSHOTCLASS             SNAPSHOTCONTENT                                    CREATIONTIME   AGE
rbd-pvc-snapshot   true         mysql-pv-claim                           30Gi          csi-rbdplugin-snapclass   snapcontent-6168fda6-e2dd-484a-bab7-dc36d4db3515   19s            33s

六、指定快照创建PVC:如果想要创建一个具有某个数据的PVC,可以从某个快照恢复:

[root@k8s-master01 rbd]# cat pvc-restore.yaml 
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: rbd-pvc-restore
spec:
  storageClassName: rook-ceph-block
  dataSource:
    name: rbd-pvc-snapshot
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 30Gi
### --- 注意:dataSource为快照名称,storageClassName为新建pvc的storageClass,
~~~     storage的大小不能低于原pvc的大小, 

[root@k8s-master01 rbd]# kubectl create -f pvc-restore.yaml 
persistentvolumeclaim/rbd-pvc-restore created
[root@k8s-master01 rbd]# kubectl get pvc
NAME              STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS      AGE
mysql-pv-claim    Bound    pvc-a22e54cf-9d96-4070-b3a7-90a2fa73c249   30Gi       RWO            rook-ceph-block   82m
rbd-pvc-restore   Bound    pvc-9b93b53b-f96b-42ea-852f-3357df4135a4   30Gi       RWO            rook-ceph-block   4s

七、PVC快照数据效验

### --- 创建一个容器,挂载该PVC,查看是否含有之前的文件:

[root@k8s-master01 rbd]# vim restore-check-snapshot-rbd.yaml 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: check-snapshot-restore
spec:
  selector:
    matchLabels:
      app: check 
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: check 
    spec:
      containers:
      - image: alpine:3.8
        name: check
        command:
        - sh
        - -c
        - sleep 36000
        volumeMounts:
        - name: check-mysql-persistent-storage
          mountPath: /mnt
      volumes:
      - name: check-mysql-persistent-storage
        persistentVolumeClaim:
          claimName: rbd-pvc-restore
### --- 创建容器

[root@k8s-master01 rbd]# kubectl  create -f restore-check-snapshot-rbd.yaml
deployment.apps/check-snapshot-restore created
### --- 查看数据是否存在

[root@k8s-master01 rbd]# kubectl  get po -owide
NAME                                      READY   STATUS    RESTARTS   AGE     IP               NODE           NOMINATED NODE   READINESS GATES
busybox                                   1/1     Running   2          160m    172.17.125.1     k8s-node01     <none>           <none>
check-snapshot-restore-64b85c5f88-sgtzt   1/1     Running   0          2m15s   172.27.14.210    k8s-node02     <none>           <none>
wordpress-mysql-6965fc8cc8-fvtrt          1/1     Running   0          99m     172.25.244.207   k8s-master01   <none>           <none>
### --- 查看test-snapshot目录是否存在

[root@k8s-master01 rbd]# kubectl exec -ti check-snapshot-restore-64b85c5f88-sgtzt -- ls /mnt
test_snapshot
### --- 查看创建的文件及文件内容

[root@k8s-master01 rbd]# kubectl exec -ti check-snapshot-restore-64b85c5f88-sgtzt -- ls /mnt/test_snapshot
1.txt
[root@k8s-master01 rbd]# kubectl exec -ti check-snapshot-restore-64b85c5f88-sgtzt -- cat /mnt/test_snapshot/1.txt
test for snapshot
### --- 测试无误后清理数据(snapshotclass可以不删除,
~~~     后期创建rbd快照直接用该snapshotclass即可):

[root@k8s-master01 rbd]# kubectl  delete -f restore-check-snapshot-rbd.yaml -f pvc-restore.yaml -f snapshot.yaml 
deployment.apps "check-snapshot-restore" deleted
persistentvolumeclaim "rbd-pvc-restore" deleted
volumesnapshot.snapshot.storage.k8s.io "rbd-pvc-snapshot" deleted

 

 

 

 

 

 

 

 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(45)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示