使用磁碟區快照備份 Persistent Disk 儲存空間


本頁說明如何使用磁碟區快照備份及還原永久磁碟儲存空間。

如需簡介,請參閱「關於 Kubernetes 磁碟區快照」。

需求條件

如要在 GKE 上使用磁碟區快照,必須符合下列規定:

  • 使用支援快照的 CSI 驅動程式。樹內 Persistent Disk 驅動程式不支援快照。如要建立及管理快照,您必須使用與基礎 PersistentVolumeClaim (PVC) 相同的 CSI 驅動程式。

  • 使用控制層版本 1.17 以上。如要在 VolumeSnapshot 中使用 Compute Engine 永久磁碟 CSI 驅動程式,請使用 GKE 1.17.6-gke.4 以上版本。

  • 擁有現有的 PersistentVolumeClaim,可做為快照使用。快照來源使用的 PersistentVolume 必須由 CSI 驅動程式管理。如要確認您使用的是 CSI 驅動程式,請檢查 PersistentVolume 規格是否含有 driver: pd.csi.storage.gke.iofilestore.csi.storage.gke.iocsi 區段。如果 PersistentVolume 是由 CSI 驅動程式動態佈建 (如下列章節所述),則由 CSI 驅動程式管理。

限制

Compute Engine 建立磁碟快照的限制也適用於 GKE。

最佳做法

在 GKE 上使用永久磁碟 Volume 快照時,請務必遵循 Compute Engine 磁碟快照的最佳做法

事前準備

開始之前,請確認你已完成下列工作:

  • 啟用 Google Kubernetes Engine API。
  • 啟用 Google Kubernetes Engine API
  • 如要使用 Google Cloud CLI 執行這項工作,請安裝初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行 gcloud components update,取得最新版本。

建立及使用磁碟區快照

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

  1. 建立 PersistentVolumeClaimDeployment
  2. 將檔案新增至 Deployment 使用的 PersistentVolume
  3. 建立 VolumeSnapshotClass 來設定快照
  4. 建立 PersistentVolume 的磁碟區快照
  5. 刪除測試檔案
  6. PersistentVolume 還原至您建立的快照
  7. 確認還原作業是否成功

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

  1. 建立 VolumeSnapshotClass 物件,指定快照的 CSI 驅動程式和刪除政策。
  2. 建立 VolumeSnapshot 物件,要求現有 PersistentVolumeClaim 的快照。
  3. PersistentVolumeClaim 中參照 VolumeSnapshot,將磁碟區還原至該快照,或使用快照建立新磁碟區。

建立 PersistentVolumeClaimDeployment

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

    Persistent Disk

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

    這個範例使用 Compute Engine 永久磁碟 CSI 驅動程式預設安裝的 standard-rwo 儲存空間類別。詳情請參閱「使用 Compute Engine 永久磁碟 CSI 驅動程式」。

    對於 spec.storageClassName,您可以指定任何使用支援的 CSI 驅動程式的儲存空間類別。

  2. 套用資訊清單:

    kubectl apply -f my-pvc.yaml
    
  3. 如要建立 Deployment,請將下列資訊清單儲存為 my-deployment.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: hello-app
    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: sdk-volume
              mountPath: /usr/share/hello/
          volumes:
          - name: sdk-volume
            persistentVolumeClaim:
              claimName: my-pvc
    
  4. 套用資訊清單:

    kubectl apply -f my-deployment.yaml
    
  5. 檢查 Deployment 的狀態:

    kubectl get deployment hello-app
    

    Deployment 可能需要一段時間才能準備就緒。您可以執行上述指令,直到看到類似下列內容的輸出:

    NAME        READY   UP-TO-DATE   AVAILABLE   AGE
    hello-app   1/1     1            1           2m55s
    

在磁碟區中新增測試檔案

  1. 列出 Deployment 中的 Pods

    kubectl get pods -l app=hello-app
    

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

    NAME                         READY   STATUS    RESTARTS   AGE
    hello-app-6d7b457c7d-vl4jr   1/1     Running   0          2m56s
    
  2. Pod 中建立測試檔案:

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

    POD_NAME 替換為 Pod 的名稱。

  3. 確認檔案是否存在:

    kubectl exec POD_NAME \
        -- sh -c 'cat /usr/share/hello/hello.txt'
    

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

    Hello World!
    

建立 VolumeSnapshotClass 物件

建立 VolumeSnapshotClass 物件,指定 CSI 驅動程式和磁碟區快照的 deletionPolicy。建立 VolumeSnapshot 物件時,可以參照 VolumeSnapshotClass 物件。

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

    Persistent Disk

    如果叢集執行 1.21 以上版本,請使用 v1 API 版本。

    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshotClass
    metadata:
      name: my-snapshotclass
    driver: pd.csi.storage.gke.io
    deletionPolicy: Delete
    

    在這個例子中:

    • CSI 驅動程式會使用 driver 欄位佈建快照。在本範例中,pd.csi.storage.gke.io 使用的是 Compute Engine 永久磁碟 CSI 驅動程式

    • deletionPolicy 欄位會告知 GKE,在刪除繫結的 VolumeSnapshot 物件時,如何處理 VolumeSnapshotContent 物件和基礎快照。指定 Delete 可刪除 VolumeSnapshotContent 物件和基礎快照。如要保留 VolumeSnapshotContent 和基礎快照,請指定 Retain

      如要使用自訂儲存位置,請將 storage-locations 參數新增至快照類別。如要使用這個參數,叢集必須採用 1.21 以上版本。

      apiVersion: snapshot.storage.k8s.io/v1
      kind: VolumeSnapshotClass
      metadata:
        name: my-snapshotclass
      parameters:
        storage-locations: us-east2
      driver: pd.csi.storage.gke.io
      deletionPolicy: Delete
      
    • 如要建立磁碟映像檔,請在 parameters 欄位中新增下列項目:

      parameters:
        snapshot-type: images
        image-family: IMAGE_FAMILY
      

      IMAGE_FAMILY 替換為您偏好的映像檔系列名稱,例如 preloaded-data

  2. 套用資訊清單:

    kubectl apply -f volumesnapshotclass.yaml
    

建立 VolumeSnapshot

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

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

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

    kubectl apply -f volumesnapshot.yaml
    

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

  3. 確認 GKE 已建立 VolumeSnapshotContents 物件:

    kubectl get volumesnapshotcontents
    

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

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

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

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

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

NAME               READY
my-snapshot        true

刪除測試檔案

  1. 刪除您建立的測試檔案:

    kubectl exec POD_NAME \
        -- sh -c 'rm /usr/share/hello/hello.txt'
    
  2. 確認檔案已不存在:

    kubectl exec POD_NAME \
        -- sh -c 'cat /usr/share/hello/hello.txt'
    

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

    cat: /usr/share/hello/hello.txt: No such file or directory
    

還原磁碟區快照

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

如要在 PersistentVolumeClaim 中參照 VolumeSnapshot,請將 dataSource 欄位新增至 PersistentVolumeClaim。無論 VolumeSnapshotContents 是指磁碟映像檔或快照,都使用相同程序。

在本例中,您會在新的 PersistentVolumeClaim 中參照您建立的 VolumeSnapshot,並更新 Deployment 以使用新的聲明。

  1. 確認您使用的是磁碟或映像檔快照,兩者差異如下:

    • 磁碟快照:頻繁建立快照,但很少還原。
    • 映像檔快照:不常建立快照,但經常還原。建立映像檔快照的速度也可能比磁碟快照慢。

    詳情請參閱「快照頻率限制」。瞭解快照類型有助於排解問題。

    檢查 VolumeSnapshot

    kubectl describe volumesnapshot SNAPSHOT_NAME
    

    volumeSnapshotClassName 欄位會指定快照類別。

    kubectl describe volumesnapshotclass SNAPSHOT_CLASS_NAME
    

    snapshot-type 參數會指定 snapshotsimages。如未提供,則預設值為 snapshots

    如果沒有快照類別 (例如快照是靜態建立),請檢查 VolumeSnapshotContentssh kubectl describe volumesnapshotcontents SNAPSHOT_CONTENTS_NAME 輸出內容中的快照控制代碼格式會顯示快照類型,如下所示: * projects/PROJECT_NAME/global/snapshots/SNAPSHOT_NAME:磁碟快照

    • projects/PROJECT_NAME/global/images/IMAGE_NAME:圖片快照
  1. 將下列資訊清單儲存為 pvc-restore.yaml

    Persistent Disk

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-restore
    spec:
      dataSource:
        name: my-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. 更新 my-deployment.yaml 檔案,使用新的 PersistentVolumeClaim

    ...
    volumes:
    - name: my-volume
      persistentVolumeClaim:
        claimName: pvc-restore
    
  4. 套用更新後的資訊清單:

    kubectl apply -f my-deployment.yaml
    

確認快照已成功還原

  1. 取得 GKE 為更新的 Deployment 建立的新 Pod 名稱:

     kubectl get pods -l app=hello-app
    

確認測試檔案是否存在:

   kubectl exec NEW_POD_NAME \
       -- sh -c 'cat /usr/share/hello/hello.txt'

NEW_POD_NAME 替換為 GKE 建立的新 Pod 名稱。

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

   Hello World!

匯入現有快照

您可以使用在目前叢集外部建立的現有磁碟區快照,手動佈建 VolumeSnapshotContents 物件。舉例來說,您可以使用在其他叢集中建立的Google Cloud 資源快照,在 GKE 中填入磁碟區。

  1. 找出快照名稱。

    Google Cloud 控制台

    前往 https://console.cloud.google.com/compute/snapshots

    Google Cloud CLI

    執行下列指令:

    gcloud compute snapshots list
    

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

    NAME                                           DISK_SIZE_GB  SRC_DISK                                                     STATUS
    snapshot-5e6af474-cbcc-49ed-b53f-32262959a0a0  1             us-central1-b/disks/pvc-69f80fca-bb06-4519-9e7d-b26f45c1f4aa READY
    
  2. 將下列 VolumeSnapshot 資訊清單儲存為 restored-snapshot.yaml

    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshot
    metadata:
      name: restored-snapshot
    spec:
      volumeSnapshotClassName: my-snapshotclass
      source:
        volumeSnapshotContentName: restored-snapshot-content
    
  3. 套用資訊清單:

    kubectl apply -f restored-snapshot.yaml
    
  4. 將下列 VolumeSnapshotContent 資訊清單儲存為 restored-snapshot-content.yaml。請將 snapshotHandle 欄位替換為您的專案 ID 和快照名稱。volumeSnapshotRef.namevolumeSnapshotRef.namespace 都必須指向先前建立的 VolumeSnapshot,雙向繫結才會有效。

    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshotContent
    metadata:
      name: restored-snapshot-content
    spec:
      deletionPolicy: Retain
      driver: pd.csi.storage.gke.io
      source:
        snapshotHandle: projects/PROJECT_ID/global/snapshots/SNAPSHOT_NAME
      volumeSnapshotRef:
        kind: VolumeSnapshot
        name: restored-snapshot
        namespace: default
    
  5. 套用資訊清單:

    kubectl apply -f restored-snapshot-content.yaml
    
  6. 將下列 PersistentVolumeClaim 資訊清單儲存為 restored-pvc.yaml。 Kubernetes 儲存空間控制器會尋找名為 VolumeSnapshotrestored-snapshot,然後嘗試尋找或動態建立 PersistentVolume 做為資料來源。接著,您可以在 Pod 中使用這個 PVC,存取還原的資料。

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

    kubectl apply -f restored-pvc.yaml
    
  8. 請將下列 Pod 資訊清單儲存為 restored-pod.yaml,請參閱PersistentVolumeClaim。CSI 驅動程式會佈建 PersistentVolume,並從快照填入資料。

    apiVersion: v1
    kind: Pod
    metadata:
      name: restored-pod
    spec:
      containers:
      - name: busybox
        image: busybox
        args:
        - sleep
        - "3600"
        volumeMounts:
        - name: source-data
          mountPath: /demo/data
      volumes:
      - name: source-data
        persistentVolumeClaim:
          claimName: restored-pvc
          readOnly: false
    
  9. 套用資訊清單:

    kubectl apply -f restored-pod.yaml
    
  10. 確認檔案是否已還原:

    kubectl exec restored-pod -- sh -c 'cat /demo/data/hello.txt'
    

清除所用資源

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

  1. 刪除 VolumeSnapshot

    kubectl delete volumesnapshot my-snapshot
    
  2. 刪除 VolumeSnapshotClass

    kubectl delete volumesnapshotclass my-snapshotclass
    
  3. 刪除 Deployment

    kubectl delete deployments hello-app
    
  4. 刪除 PersistentVolumeClaim 個物件:

    kubectl delete pvc my-pvc pvc-restore
    

後續步驟