為 PersistentVolume 建立快照

您可以在 GKE 叢集中,使用 Kubernetes 磁碟區快照功能來處理永久磁碟區和動態佈建

磁碟區快照可讓您在特定時間點建立磁碟區副本。您可以使用這個副本,將磁碟區還原至先前的狀態,或是佈建替代磁碟區。

您可以使用下列元件佈建及附加磁碟區快照:

需求條件

如要在 Azure 上的 GKE 使用磁碟區快照,必須符合下列條件:

  • 使用支援快照的 Container Storage Interface (CSI) 驅動程式的磁碟區。GKE on Azure 預設使用的 Azure 磁碟和 Azure 檔案驅動程式支援快照。

    如需支援快照的所有 CSI 驅動程式清單,請參閱 Kubernetes 說明文件中的「其他功能」驅動程式資料欄。

  • 擁有現有的 PersistentVolumeClaim,可做為快照使用。快照來源使用的 PersistentVolume 必須由 CSI 驅動程式管理。如要確認您使用的是 CSI 驅動程式,請檢查 PersistentVolume 規格是否含有 driver: disk.csi.azure.comfile.csi.azure.comcsi 區段。如果您的叢集透過 CSI 驅動程式動態佈建 PersistentVolumes,如以下各節所述,則由 CSI 驅動程式管理。

事前準備

建立及使用磁碟區快照

本文中的範例說明如何執行下列工作:

  1. 建立範例 PersistentVolumeClaimPod
  2. 建立 VolumeSnapshot
  3. 還原磁碟區快照
  4. 確認還原作業是否成功

如要使用磁碟區快照,請完成下列步驟:

  1. 建立 VolumeSnapshot 物件,要求現有 PersistentVolumeClaim 的快照。
  2. PersistentVolumeClaim 中參照 VolumeSnapshot,將磁碟區還原至該快照,或使用快照建立新磁碟區。

建立範例 PersistentVolumeClaimPod

  1. 如要建立 PersistentVolumeClaim 物件,請將下列資訊清單儲存為 example-pvc.yaml

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

    對於 spec.storageClassName,您可以指定任何使用支援的 CSI 驅動程式的儲存空間類別。本範例使用預設的 standard-rwo 儲存空間類別。

  2. 套用資訊清單:

    kubectl apply -f example-pvc.yaml
    
  3. 建立 Pod,將目前的日期和時間寫入磁碟區。如要建立 Pod,請將下列資訊清單儲存為 snapshot-shell.yaml

    apiVersion: v1
    kind: Pod
    metadata:
      name: snapshot-shell
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: linux
        image: ubuntu:bionic
        command: ["/bin/sh"]
        args: ["-c", "echo $(date -u) >> /data/out.txt"]
        volumeMounts:
        - name: snapshot-volume
          mountPath: /data
      restartPolicy: Never
      volumes:
      - name: snapshot-volume
        persistentVolumeClaim:
          claimName: example-pvc
    
  4. 套用資訊清單:

    kubectl apply -f snapshot-shell.yaml
    
  5. 檢查 Pod 的狀態:

    kubectl get pod snapshot-shell
    

    Pod 可能需要一段時間才能執行及完成。您可以執行上述指令,直到看到類似下列內容的輸出:

    NAME             READY   STATUS      RESTARTS   AGE
    snapshot-shell   0/1     Completed   0          24s
    

建立 VolumeSnapshot

VolumeSnapshot 物件是現有 PersistentVolumeClaim 物件快照的要求。建立 VolumeSnapshot 物件時,叢集會自動建立並繫結 VolumeSnapshotContent 物件,後者是叢集中的資源,例如 PersistentVolume 物件。

  1. 將下列資訊清單儲存為 volumesnapshot.yaml

    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshot
    metadata:
      name: example-snapshot
    spec:
      source:
        persistentVolumeClaimName: example-pvc
    
  2. 套用資訊清單:

    kubectl apply -f volumesnapshot.yaml
    

    建立磁碟區快照後,叢集會建立對應的 VolumeSnapshotContent 物件。這個物件會儲存 VolumeSnapshot 物件的快照和繫結。您不會直接與 VolumeSnapshotContents 物件互動。

  3. 確認叢集是否已建立 VolumeSnapshotContents 物件:

    kubectl get volumesnapshotcontents
    

    輸出結果會與下列內容相似:

    NAME                                               AGE
    snapcontent-cee5fb1f-5427-11ea-a53c-42010a1000da   55s
    

確認磁碟區快照已準備就緒

建立磁碟區快照內容後,您在 VolumeSnapshotClass 中指定的 CSI 驅動程式會在對應的儲存系統上建立快照。叢集在儲存系統上建立快照並繫結至 VolumeSnapshot 物件後,即可使用快照。您可以執行下列指令來檢查狀態:

kubectl get volumesnapshot \
  -o custom-columns='NAME:.metadata.name,READY:.status.readyToUse'

如果快照已可使用,輸出內容會與下列內容相似:

NAME                    READY
example-snapshot        true

還原磁碟區快照

您可以在 PersistentVolumeClaim 中參照 VolumeSnapshot,佈建含有現有磁碟區資料的新磁碟區,或將磁碟區還原至您在快照中擷取的狀態。

如要在 PersistentVolumeClaim 中參照 VolumeSnapshot,請將 dataSource 欄位新增至 PersistentVolumeClaim

在本例中,您會在新的 VolumeSnapshot 中參照您建立的 VolumeSnapshot,並建立掛接 PersistentVolumeClaim 的 Pod。PersistentVolumeClaim

  1. 將下列資訊清單儲存為 pvc-restore.yaml

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
     name: pvc-restore
    spec:
     dataSource:
       name: example-snapshot
       kind: VolumeSnapshot
       apiGroup: snapshot.storage.k8s.io
     storageClassName: standard-rwo
     accessModes:
       - ReadWriteOnce
     resources:
       requests:
         storage: 1Gi
    
  2. 套用資訊清單:

    kubectl apply -f pvc-restore.yaml
    
  3. 啟動臨時 Pod,將 PVC 掛接至 Pod,然後將 out.txt 的內容列印至記錄檔。

    將下列資訊清單儲存為 restore-log.yaml

    apiVersion: v1
    kind: Pod
    metadata:
      name: restore-verify
    spec:
      volumes:
        - name: restore-data
          persistentVolumeClaim:
            claimName: pvc-restore
      containers:
        - name: shell-container
          image: ubuntu:bionic
          volumeMounts:
            - mountPath: "/data"
              name: restore-data
          command: [ "/bin/sh" ]
          args: ["-c", "cat /data/out.txt", "exit", "1"]
      restartPolicy: Never
    
  4. 套用資訊清單:

    kubectl apply -f restore-log.yaml
    

確認快照已成功還原

您在上一步建立的 Pod 會從快照讀取資料。如要查看快照中的資料,請使用 kubectl logs 指令。

kubectl logs restore-verify

輸出內容應包含快照的時間戳記。

清除所用資源

如要避免系統收取本頁所用資源的費用,請按照下列步驟操作。

  1. 刪除 VolumeSnapshot

    kubectl delete volumesnapshot example-snapshot
    

  2. 刪除臨時 Pod:

    kubectl delete -f restore-log.yaml
    
  3. 刪除 Pod:

    kubectl delete -f snapshot-shell.yaml
    
  4. 刪除 PersistentVolumeClaim 個物件:

    kubectl delete pvc example-pvc pvc-restore
    

後續步驟