從快照還原磁碟區

本文說明如何建立磁碟區快照,然後使用快照還原磁碟區。本文說明適用於使用 vSphere CSI 驅動程式的叢集。

事前準備

請參閱「使用 vSphere Container Storage 介面驅動程式」。

確認叢集有名為 standard-rwo 的 StorageClass,且已安裝 vSphere CSI 驅動程式。

您的 vSphere 版本、ESXi 和 vCenter Server 必須為 7.0 Update 3 以上版本。詳情請參閱「排解儲存空間問題」。

基本步驟說明

本文件提供的練習主要步驟如下:

  1. 建立 PersistentVolumeClaim。
    建立 PersistentVolumeClaim,要求 standard-rwo 儲存空間類別。接著,叢集會動態佈建 PersistentVolume,並將其與 PersistentVolumeClaim 建立關聯。
  2. 建立 Deployment。
    建立具有一個 Pod 的部署。Pod 會根據您的 PersistentVolumeClaim 指定磁碟區。Pod 中的單一容器會在 /hello/ 掛接磁碟區。
  3. 將檔案寫入 Pod 磁碟區。
    在 Pod 磁碟區中建立名為 hello.txt 的檔案。檔案內容為「Hello World!」。
  4. 建立 VolumeSnapshot。
    建立 VolumeSnapshot,擷取 Pod 磁碟區的狀態。
  5. 損毀檔案。
    變更 hello.txt 檔案,使其看起來像已毀損的檔案。檔案內容現在是「Hello W-corrupted-file-orld!」
  6. 使用快照還原磁碟區。
    建立第二個 PersistentVolumeClaim,並將 VolumeSnapshot 做為資料來源。編輯 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

可建立部署作業

以下是部署的資訊清單:

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

在上述資訊清單中,您可以看到新 PersistentVolume 聲明的資料來源是先前建立的 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!

疑難排解

如需疑難排解指南,請參閱「排解儲存空間問題」。