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


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

建立 Kubernetes 磁碟區快照等同於建立 Filestore 備份。詳情請參閱「關於 Kubernetes 磁碟區快照」。

需求條件

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

  • 您必須部署 Filestore CSI 驅動程式。僅支援下列 Filestore 服務層級

    • 搭配 GKE 1.21 以上版本的基本 HDD
    • 基本傳統硬碟 (100 GiB 至 63.9 TiB),搭配 GKE 1.33 以上版本
    • 搭配 GKE 1.21 以上版本的 Basic SSD
    • 區域 (1 TiB 至 9.75 TiB),搭配 GKE 1.31 以上版本
    • 可用區 (10 TiB 至 100 TiB),搭配 GKE 1.27 以上版本
    • Enterprise 版 (GKE 1.25 以上版本)
  • 使用控制層版本 1.17 以上版本。如要在 VolumeSnapshot 中使用 Filestore CSI 驅動程式,請使用適用於服務層級的 GKE 版本號碼。

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

限制

  • 快照磁碟區的大小限制與一般磁碟區相同。舉例來說,基本傳統硬碟級別的 Filestore 快照大小必須大於或等於 1 TiB。

  • Filestore CSI 驅動程式不支援區域 Filestore 服務層級的動態佈建或備份工作流程:

  • 每個執行個體一次只能備份一個共用項目。就儲存集區而言,從兩個不同 Filestore 執行個體的兩個不同共用區發出的備份要求,會同時執行。

  • 單一共用備份只能還原至單一共用磁碟區。使用 Filestore CSI 驅動程式時,您只能將單一共用磁碟區還原至新的 Filestore 執行個體。

    • 新執行個體必須使用與備份相同的服務層級。
    • 新執行個體的容量下限必須與備份相同。
  • 不支援對來源或現有 Filestore 執行個體執行 Filestore backup restore 作業。如需完整的功能限制清單,請參閱「Filestore 備份功能限制」。

  • 不支援多重共用備份。

事前準備

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

  • 啟用 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

    Filestore

     apiVersion: v1
     kind: PersistentVolumeClaim
     metadata:
       name: my-pvc
     spec:
       storageClassName: enterprise-rwx
       accessModes:
       - ReadWriteMany
       resources:
         requests:
           storage: 1Ti
    

    這個範例會建立企業級 Filestore PVC。詳情請參閱「透過 Filestore CSI 驅動程式存取 Filestore 執行個體」。

    對於 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

    Filestore

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

    在這個例子中:

    • CSI 驅動程式會使用 driver 欄位佈建快照。在本範例中,filestore.csi.storage.gke.io 會使用 Filestore CSI 驅動程式
    • deletionPolicy 欄位會告知 GKE,在刪除繫結的 VolumeSnapshot 物件時,如何處理 VolumeSnapshotContent 物件和基礎快照。指定 Delete 可刪除 VolumeSnapshotContent 物件和基礎快照。如要保留 VolumeSnapshotContent 和基礎快照,請指定 Retain
  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

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

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

    Filestore

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-restore
    spec:
      dataSource:
        name: my-snapshot
        kind: VolumeSnapshot
        apiGroup: snapshot.storage.k8s.io
      storageClassName: enterprise-rwx
      accessModes:
      - ReadWriteMany
      resources:
        requests:
          storage: 1Ti
    
  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: filestore.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: enterprise-rwx
      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
    

後續步驟