用户指南

最佳实践

API 文档

创建快照和使用快照来恢复卷

最近更新时间:2022-03-18 11:32:20

操作场景

如需为 PVC 数据盘创建快照来备份数据,或者将备份的快照数据恢复到新的 PVC 中,可以通过 CBS-CSI 插件来实现,本文将介绍如何利用 CBS-CSI 插件实现 PVC 的数据备份与恢复。

前提条件

  • 已创建1.18或以上版本的 TKE 集群
  • 已安装最新版的 CBS-CSI 组件。

操作步骤

备份PVC

创建 VolumeSnapshotClass

  1. 使用以下 YAML,创建 VolumeSnapshotClass 对象。示例如下:
    apiVersion: snapshot.storage.k8s.io/v1beta1
    kind: VolumeSnapshotClass
    metadata: 
      name: cbs-snapclass
    driver: com.tencent.cloud.csi.cbs
    deletionPolicy: Delete
  2. 执行以下命令查看 VolumeSnapshotClass 是否创建成功。示例如下:
    $ kubectl get volumesnapshotclass
    NAME            DRIVER                      DELETIONPOLICY   AGE
    cbs-snapclass   com.tencent.cloud.csi.cbs   Delete           17m

创建 PVC 快照 VolumeSnapshot

  1. 本文以 new-snapshot-demo 快照名为例,使用以下 YAML 创建 VolumeSnapshot 对象。示例如下:

    apiVersion: snapshot.storage.k8s.io/v1beta1
    kind: VolumeSnapshot
    metadata:
      name: new-snapshot-demo
    spec:
      volumeSnapshotClassName: cbs-snapclass
      source:
        persistentVolumeClaimName: csi-pvc

  2. 执行以下命令,查看 Volumesnapshot 和 Volumesnapshotcontent 对象是否创建成功,若 READYTOUSE 为 true,则创建成功。示例如下:

    $ kubectl get volumesnapshot
    NAME                READYTOUSE   SOURCEPVC            SOURCESNAPSHOTCONTENT   RESTORESIZE   SNAPSHOTCLASS   SNAPSHOTCONTENT                                    CREATIONTIME   AGE
    new-snapshot-demo   true         www1-ivantestweb-0                           10Gi          cbs-snapclass   snapcontent-ea11a797-d438-4410-ae21-41d9147fe610   22m            22m
    $ kubectl get volumesnapshotcontent
    NAME                                               READYTOUSE   RESTORESIZE   DELETIONPOLICY   DRIVER                      VOLUMESNAPSHOTCLASS   VOLUMESNAPSHOT      AGE
    snapcontent-ea11a797-d438-4410-ae21-41d9147fe610   true         10737418240   Delete           com.tencent.cloud.csi.cbs   cbs-snapclass         new-snapshot-demo   22m

  3. 执行以下命令,可以获取 Volumesnapshotcontent 对象的快照 ID,字段是 status.snapshotHandle(如下为 snap-e406fc9m),可以根据快照 ID 在 容器服务控制台 确认快照是否存在。示例如下:

    $ kubectl get volumesnapshotcontent snapcontent-ea11a797-d438-4410-ae21-41d9147fe610 -oyaml
    apiVersion: snapshot.storage.k8s.io/v1beta1
    kind: VolumeSnapshotContent
    metadata:
      creationTimestamp: "2020-11-04T08:58:39Z"
      finalizers:
      - snapshot.storage.kubernetes.io/volumesnapshotcontent-bound-protection
      name: snapcontent-ea11a797-d438-4410-ae21-41d9147fe610
      resourceVersion: "471437790"
      selfLink: /apis/snapshot.storage.k8s.io/v1beta1/volumesnapshotcontents/snapcontent-ea11a797-d438-4410-ae21-41d9147fe610
      uid: 70d0390b-79b8-4276-aa79-a32e3bdef3d6
    spec:
      deletionPolicy: Delete
      driver: com.tencent.cloud.csi.cbs
      source:
        volumeHandle: disk-7z32tin5
      volumeSnapshotClassName: cbs-snapclass
      volumeSnapshotRef:
        apiVersion: snapshot.storage.k8s.io/v1beta1
        kind: VolumeSnapshot
        name: new-snapshot-demo
        namespace: default
        resourceVersion: "471418661"
        uid: ea11a797-d438-4410-ae21-41d9147fe610
    status:
      creationTime: 1604480319000000000
      readyToUse: true
      restoreSize: 10737418240
      snapshotHandle: snap-e406fc9m

从快照恢复数据到新 pvc

  1. 本文以上述 步骤 中创建的 VolumeSnapshot 的对象名为 new-snapshot-demo 为例,使用以下 YAML 从快照恢复卷。示例如下:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: restore-test
    spec:
      storageClassName: cbs-csi
      dataSource:
        name: new-snapshot-demo
        kind: VolumeSnapshot
        apiGroup: snapshot.storage.k8s.io
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 10Gi

  2. 执行以下命令,查看恢复的 PVC 已成功创建,从 PV 中可以查看到对应的 diskid(如下为 disk-gahz1kw1)。示例如下:

    $ kubectl get pvc restore-test
    NAME           STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
    restore-test   Bound    pvc-80b98084-29a3-4a38-a96c-2f284042cf4f   10Gi       RWO            cbs-csi        97s
    $ kubectl get pv pvc-80b98084-29a3-4a38-a96c-2f284042cf4f -oyaml
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      annotations:
        pv.kubernetes.io/provisioned-by: com.tencent.cloud.csi.cbs
      creationTimestamp: "2020-11-04T12:08:25Z"
      finalizers:
      - kubernetes.io/pv-protection
      name: pvc-80b98084-29a3-4a38-a96c-2f284042cf4f
      resourceVersion: "474676883"
      selfLink: /api/v1/persistentvolumes/pvc-80b98084-29a3-4a38-a96c-2f284042cf4f
      uid: 5321df93-5f21-4895-bafc-71538d50293a
    spec:
      accessModes:
      - ReadWriteOnce
      capacity:
        storage: 10Gi
      claimRef:
        apiVersion: v1
        kind: PersistentVolumeClaim
        name: restore-test
        namespace: default
        resourceVersion: "474675088"
        uid: 80b98084-29a3-4a38-a96c-2f284042cf4f
      csi:
        driver: com.tencent.cloud.csi.cbs
        fsType: ext4
        volumeAttributes:
          diskType: CLOUD_PREMIUM
          storage.kubernetes.io/csiProvisionerIdentity: 1604478835151-8081-com.tencent.cloud.csi.cbs
        volumeHandle: disk-gahz1kw1
      nodeAffinity:
        required:
          nodeSelectorTerms:
          - matchExpressions:
            - key: topology.com.tencent.cloud.csi.cbs/zone
              operator: In
              values:
              - ap-beijing-2
      persistentVolumeReclaimPolicy: Delete
      storageClassName: cbs-csi
      volumeMode: Filesystem
    status:
      phase: Bound

    说明:

    如果 StorageClass 使用了拓扑感知(先调度 Pod 再创建 PV),即指定 volumeBindingMode: WaitForFirstConsumer,则需要先部署 Pod(需挂载 PVC)才会触发创建 PV(从快照创建新的 CBS 并与 PV 绑定)。

目录