透過 Azure 容器儲存體使用磁碟區快照集

Azure 容器儲存體是雲端式磁碟區管理、部署和協調流程服務,專為容器原生建置。 本文說明如何擷取永續性磁碟區的時間點快照集,並使用新的永續性磁碟區宣告將其還原。

必要條件

  • 本文需要最新版本 (2.35.0 或更新版本) 的 Azure CLI。 請參閱如何安裝 Azure CLI。 若您使用的是 Azure Cloud Shell,即已安裝最新版本。 如果您計劃在本機執行命令,而不是在 Azure Cloud Shell 中執行命令,請務必使用系統管理權限來執行這些命令。
  • 您將需要 Azure Kubernetes Service (AKS) 叢集,且叢集節點的節點集區至少包含三部虛擬機器 (VM),每個節點至少有四個虛擬 CPU (vCPU)。
  • 本文假設您已在 AKS 叢集上安裝 Azure 容器儲存體,而且您已使用 Azure 磁碟暫時性磁碟 (本機儲存體) 建立存放集區和永續性磁碟宣告 (PVC)。 當您使用彈性 SAN 作為備份儲存體時,目前不支援磁碟區快照集。

建立磁碟區快照集類別

首先,您需要建立磁碟區快照集類別,以便在 YAML 資訊清單檔中定義磁碟區快照集的屬性,從而指定其屬性。 請遵循下列步驟來建立 Azure 磁碟的磁碟區快照集類別。

  1. 使用您慣用的文字編輯器來建立 YAML 資訊清單檔,例如 code acstor-volumesnapshotclass.yaml

  2. 貼入下列程式碼。 磁碟區快照集類別的 name 值可以是任意值。

    yml
    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshotClass
    metadata:
      name: csi-acstor-vsc
    driver: containerstorage.csi.azure.com
    deletionPolicy: Delete
    parameters:
      incremental: "true"  # available values: "true", "false" ("true" by default for Azure Public Cloud, and "false" by default for Azure Stack Cloud)
    
  3. 套用 YAML 資訊清單檔來建立磁碟區快照集類別。

    Azure CLI
    kubectl apply -f acstor-volumesnapshotclass.yaml
    

    建立完成時,您會看到如下訊息:

    輸出
    volumesnapshotclass.snapshot.storage.k8s.io/csi-acstor-vsc created
    

    您也可以執行 kubectl get volumesnapshotclass 來檢查磁碟區快照集類別是否已建立。 您應該會看到類似如下輸出:

    輸出
    NAME            DRIVER                            DELETIONPOLICY    AGE
    csi-acstor-vsc	 containerstorage.csi.azure.com	   Delete	           11s
    

建立磁碟區快照集

接下來,您將建立現有永續性磁碟區宣告的快照集,並套用在上一個步驟中建立的磁碟區快照集類別。

  1. 使用您慣用的文字編輯器來建立 YAML 資訊清單檔,例如 code acstor-volumesnapshot.yaml

  2. 貼入下列程式碼。 volumeSnapshotClassName 應為您在上一個步驟中建立的磁碟區快照集類別名稱。 針對 persistentVolumeClaimName,請使用要建立快照集的永續性磁碟區宣告名稱。 磁碟區快照集的 name 值可以是任意值。

    yml
    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshot
    metadata:
      name: azuredisk-volume-snapshot
    spec:
      volumeSnapshotClassName: csi-acstor-vsc
      source:
        persistentVolumeClaimName: azurediskpvc
    
  3. 套用 YAML 資訊清單檔以建立磁碟區快照集。

    Azure CLI
    kubectl apply -f acstor-volumesnapshot.yaml
    

    建立完成時,您會看到如下訊息:

    輸出
    volumesnapshot.snapshot.storage.k8s.io/azuredisk-volume-snapshot created
    

    您也可以執行 kubectl get volumesnapshot 來檢查磁碟區快照集是否已建立。 如果 READYTOUSE 指出 true,則可以繼續下一個步驟。

建立還原的永續性磁碟區宣告

現在,您可以建立新的永續性磁碟區宣告,以使用磁碟區快照集作為資料來源。

  1. 使用您慣用的文字編輯器來建立 YAML 資訊清單檔,例如 code acstor-pvc-restored.yaml

  2. 貼入下列程式碼。 storageClassName 必須符合您在建立原始永續性磁碟區時所使用的儲存類別。 例如,如果您使用暫時性磁碟 (本機 NVMe),而非用於後端記憶體的 Azure 磁碟,請將 storageClassName 變更為 acstor-ephemeraldisk。 針對資料來源的 name 值,請使用您在上一個步驟中建立的磁碟區快照集名稱。 永續性磁碟區宣告的中繼資料 name 值可以是任意值。

    yml
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-azuredisk-snapshot-restored
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: acstor-azuredisk
      resources:
        requests:
          storage: 100Gi
      dataSource:
        name: azuredisk-volume-snapshot
        kind: VolumeSnapshot
        apiGroup: snapshot.storage.k8s.io
    
  3. 套用 YAML 資訊清單檔以建立 PVC。

    Azure CLI
    kubectl apply -f acstor-pvc-restored.yaml
    

    建立完成時,您會看到如下訊息:

    輸出
    persistentvolumeclaim/pvc-azuredisk-snapshot-restored created
    

    您也可以執行 kubectl describe pvc pvc-azuredisk-snapshot-restored 來檢查永續性磁碟區是否已建立。 您應看到狀態為 [擱置],以及訊息「waiting for first consumer to be created before binding」

提示

如果您已建立還原的永續性磁碟區宣告,並想要再次套用 yaml 檔案以更正錯誤或進行變更,您必須先刪除舊的永續性磁碟區宣告,接著再次套用 yaml 檔案:kubectl delete pvc <pvc-name>

刪除原始 Pod (選用)

在建立新的 Pod 之前,您可能想要刪除用於建立快照集的原始 Pod。

  1. 執行 kubectl get pods 以列出 Pod。 請確定您要刪除的 Pod 正確無誤。
  2. 若要移除 Pod,請執行 kubectl delete pod <pod-name>

使用還原的快照集建立新的 Pod

接下來,使用還原的永續性磁碟區宣告建立新的 Pod。 使用 Fio (彈性 I/O 測試器) 建立 Pod,以進行基準測試和工作負載模擬,並指定永續性磁碟區的掛接路徑。

  1. 使用您慣用的文字編輯器來建立 YAML 資訊清單檔,例如 code acstor-pod2.yaml

  2. 貼入下列程式碼。 永續性磁碟區宣告 claimName 應為您所建立還原快照集永續性磁碟區宣告的名稱。 Pod 的中繼資料 name 值可以是任意值。

    yml
    kind: Pod
    apiVersion: v1
    metadata:
      name: fiopod2
    spec:
      nodeSelector:
        acstor.azure.com/io-engine: acstor
      volumes:
        - name: diskpv
          persistentVolumeClaim:
            claimName: pvc-azuredisk-snapshot-restored
      containers:
        - name: fio
          image: nixery.dev/shell/fio
          args:
            - sleep
            - "1000000"
          volumeMounts:
            - mountPath: "/volume"
              name: diskpv
    
  3. 套用 YAML 資訊清單檔以部署 Pod。

    Azure CLI
    kubectl apply -f acstor-pod2.yaml
    

    您應該會看到如下輸出:

    輸出
    pod/fiopod2 created
    
  4. 檢查 Pod 是否正在執行,且永續性磁碟區宣告已成功繫結至 Pod:

    Azure CLI
    kubectl describe pod fiopod2
    kubectl describe pvc pvc-azuredisk-snapshot-restored
    
  5. 檢查 Fio 測試以查看其目前狀態:

    Azure CLI
    kubectl exec -it fiopod2 -- fio --name=benchtest --size=800m --filename=/volume/test --direct=1 --rw=randrw --ioengine=libaio --bs=4k --iodepth=16 --numjobs=8 --time_based --runtime=60
    

您現在已從還原的永續性磁碟區宣告部署新的 Pod,並可在 Kubernetes 工作負載上使用。

另請參閱