容器服务 创建快照和使用快照来恢复卷

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

相关产品
容器服务 产品页,含版本规格、价格与购买入口。

一、前提条件

·已创建1.18或以上版本的 TKE 集群。

·已安装最新版的 CBS-CSI 组件。

二、创建 VolumeSnapshotClass

1. 使用以下 YAML,创建 VolumeSnapshotClass 对象。

apiVersion: snapshot.storage.k8s.io/v1beta1kind: VolumeSnapshotClassmetadata: name: cbs-snapclassdriver: com.tencent.cloud.csi.cbsdeletionPolicy: Delete

2. 执行以下命令创建 VolumeSnapshotClass 对象。

vim VolumeSnapshotClass.yamlkubectl apply -f VolumeSnapshotClass.yaml

3. 执行以下命令查看 VolumeSnapshotClass 是否创建成功。

$ kubectl get volumesnapshotclassNAME DRIVER DELETIONPOLICY AGEcbs-snapclass com.tencent.cloud.csi.cbs Delete 17m

三、创建 PVC 快照 VolumeSnapshot

1. 这篇以 new-snapshot-demo 快照名为例,使用以下 YAML 创建 VolumeSnapshot 对象。

注意:说明:

注意:请将以下 YAML 中的 PVC 名称 csi-pvc 替换为你集群下实际需要创建快照的 PVC 名称。

apiVersion: snapshot.storage.k8s.io/v1beta1kind: VolumeSnapshotmetadata: name: new-snapshot-demospec: volumeSnapshotClassName: cbs-snapclass source: persistentVolumeClaimName: csi-pvc

2. 执行以下命令,检查 VolumeSnapshot 和 VolumeSnapshotContent 对象是否创建成功。若 READYTOUSE 为 true,则创建成功。

$ kubectl get volumesnapshotNAME READYTOUSE SOURCEPVC SOURCESNAPSHOTCONTENT RESTORESIZE SNAPSHOTCLASS SNAPSHOTCONTENT CREATIONTIME AGEnew-snapshot-demo true www1-ivantestweb-0 10Gi cbs-snapclass snapcontent-ea11a797-d438-4410-ae21-41d9147fe610 22m 22m

$ kubectl get volumesnapshotcontentNAME READYTOUSE RESTORESIZE DELETIONPOLICY DRIVER VOLUMESNAPSHOTCLASS VOLUMESNAPSHOT AGEsnapcontent-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/v1beta1kind: VolumeSnapshotContentmetadata: 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-a32e3bdef3d6spec: 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-41d9147fe610status: creationTime: 1604480319000000000 readyToUse: true restoreSize: 10737418240 snapshotHandle: snap-e406fc9m

四、从快照恢复数据到新 PVC

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

注意:说明:

注意:请将以下代码中的 StorageClass 名称 cbs-csi 替换为实际集群对应的 StorageClass 名称。

apiVersion: v1kind: PersistentVolumeClaimmetadata: name: restore-testspec: 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-testNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGErestore-test Bound pvc-80b98084-29a3-4a38-a96c-2f284042cf4f 10Gi RWO cbs-csi 97s

$ kubectl get pv pvc-80b98084-29a3-4a38-a96c-2f284042cf4f -oyaml

apiVersion: v1kind: PersistentVolumemetadata: 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-71538d50293aspec: 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: Filesystemstatus: phase: Bound

注意:说明:

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

赞 (0)
上一篇 4天前
下一篇 4天前