从快照恢复卷

本文档介绍了如何创建卷快照,然后使用快照恢复卷。此处的说明适用于使用 vSphere CSI 驱动程序的集群。

准备工作

阅读使用 vSphere 容器存储接口驱动程序

验证集群是否具有一个名为 standard-rwo 的 StorageClass,以及是否已安装 vSphere CSI 驱动程序。

您的 vSphere 版本、ESXi 和 vCenter Server 必须为 7.0 Update 3 或更高版本。如需了解详情,请参阅排查存储问题

步骤概述

以下是本文档中提供的练习的主要步骤:

  1. 创建 PersistentVolumeClaim。
    创建请求 standard-rwo 存储类别的 PersistentVolumeClaim。然后,集群会动态预配 PersistentVolume 并将其与 PersistentVolumeClaim 关联。
  2. 创建 Deployment
    创建包含一个 Pod 的 Deployment。Pod 会根据 PersistentVolumeClaim 指定卷。Pod 中的一个容器会将卷装载到 /hello/
  3. 将文件写入 Pod 卷。
    在 Pod 卷中创建一个名为 hello.txt 的文件。该文件的内容为“Hello World!”。
  4. 创建 VolumeSnapshot。
    创建一个捕获 Pod 卷状态的 VolumeSnapshot。
  5. 损坏此文件。
    更改 hello.txt 文件,使其看起来像已损坏的文件。该文件的内容现在为“Hello W-corrupted-file--orld!”
  6. 使用快照恢复卷。
    创建第二个将 VolumeSnapshot 用作其数据源的 PersistentVolumeClaim。修改 Deployment,使其卷与新 PersistentVolumeClaim 关联。然后,验证 hello.txt 文件是否已恢复。

创建一个 PersistentVolumeClaim

以下是 PersistentVolumeClaim 的清单:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: standard-rwo

在上述清单中,您可以看到 storageClassName 设置为 standard-rwo。这是与 vSphere CSI 驱动程序关联的存储类别。

将此清单保存在名为 my-pvc.yaml 的文件中。创建并查看 PersistentVolumeClaim:

kubectl --kubeconfig CLUSTER_KUBECONFIG apply -f my-pvc.yaml

kubectl --kubeconfig CLUSTER_KUBECONFIG get pvc my-pvc

在输出中,您可以看到 PersistentVolumeClaim 已绑定到动态预配的 PersistentVolume。例如,以下输出显示名为 my-pvc 的 PersistentVolumeClaim 已绑定到名为 pvc-467d211c-26e4-4d69-aaa5-42219aee6fd5 的 PersistentVolume:

my-pvc   Bound    pvc-467d211c-26e4-4d69-aaa5-42219aee6fd5  …  standard-rwo   100s

创建 Deployment

以下是 Deployment 的清单:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  selector:
    matchLabels:
      app: hello-app
  template:
    metadata:
      labels:
        app: hello-app
    spec:
      containers:
      - name: hello-app
        image: google/cloud-sdk:slim
        args: [ "sleep", "3600" ]
        volumeMounts:
        - name: my-volume
          mountPath: /hello/
      volumes:
      - name: my-volume
        persistentVolumeClaim:
          claimName: my-pvc

在本练习中,您需要了解上述 Deployment 清单的以下要点:

  • Pod 通过指定您之前创建的 PersistentVolumeClaim my-pvc 来请求存储。

  • Pod 具有一个容器,该容器会将卷装载到 /hello/

将此清单保存到名为 my-deployment.yaml 的文件中,然后创建 Deployment:

kubectl --kubeconfig CLUSTER_KUBECONFIG apply -f my-deployment.yaml

Deployment 具有一个 Pod。获取此 Pod 的名称:

kubectl --kubeconfig CLUSTER_KUBECONFIG get pods

记下 Pod 名称。例如,在以下输出中,Pod 名称为 my-deployment-7575c4f5bf-r59nt

my-deployment-7575c4f5bf-r59nt   1/1     Running   0          65s

在 Pod 卷中创建一个文件,然后查看此文件。

kubectl --kubeconfig CLUSTER_KUBECONFIG \
    exec POD_NAME \
    -- sh -c 'echo "Hello World!" > /hello/hello.txt'

kubectl --kubeconfig CLUSTER_KUBECONFIG \
    exec POD_NAME \
    -- sh -c 'cat /hello/hello.txt'

输出会显示文件 /hello/hello.txt 的内容:

Hello World!

创建快照

以下是 VolumeSnapshot 的清单:

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: my-snapshot
spec:
  volumeSnapshotClassName: csi-vsphere-snapshot-class
  source:
    persistentVolumeClaimName: my-pvc

将此清单保存到名为 my-snapshot.yaml 的文件中,然后创建 VolumeSnapshot:

kubectl --kubeconfig CLUSTER_KUBECONFIG apply -f my-snapshot.yaml

损坏卷中的文件

更改 hello.txt 的内容,使其看起来已损坏:

kubectl --kubeconfig CLUSTER_KUBECONFIG \
    exec POD_NAME \
    -- sh -c 'echo "Hello W-corrupted-file-orld!" > /hello/hello.txt'

kubectl --kubeconfig CLUSTER_KUBECONFIG \
    exec POD_NAME \
    -- sh -c 'cat /hello/hello.txt'

在输出中,您可以看到此文件已更改:

Hello W-corrupted-file-orld!

恢复

以下是第二个 PersistentVolumeClaim 的清单:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc-2
spec:
  storageClassName: standard-rwo
  dataSource:
    name: my-snapshot
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

在上述清单中,您可以看到新 PersistentVolumeClaim 的数据源是您之前创建的 VolumeSnapshot。

将此清单保存在名为 my-pvc-2.yaml 的文件中。创建并查看 PersistentVolumeClaim:

kubectl --kubeconfig CLUSTER_KUBECONFIG apply -f my-pvc-2.yaml

kubectl --kubeconfig CLUSTER_KUBECONFIG get pvc my-pvc-2

打开 Deployment 进行修改:

kubectl --kubeconfig CLUSTER_KUBECONFIG edit deployment my-deployment

my-pvc 更改为 my-pvc-2,然后关闭编辑器:

…
  volumes:
  - name: my-volume
    persistentVolumeClaim:
    claimName: my-pvc-2

Deployment 会删除 Pod,并创建使用新 PersistentVolumeClaim 的新 Pod。

等待几分钟,然后获取新 Pod 的名称:

kubectl --kubeconfig CLUSTER_KUBECONFIG get pods

验证 Pod 卷是否已恢复:

kubectl --kubeconfig CLUSTER_KUBECONFIG \
   exec NEW_POD_NAME \
   -- sh -c 'cat /hello/hello.txt'

输出显示该卷已恢复:

Hello World!

问题排查

如需获取问题排查指导,请参阅排查存储问题