為 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.com
或file.csi.azure.com
的csi
區段。如果您的叢集透過 CSI 驅動程式動態佈建 PersistentVolumes,如以下各節所述,則由 CSI 驅動程式管理。
事前準備
建立及使用磁碟區快照
本文中的範例說明如何執行下列工作:
如要使用磁碟區快照,請完成下列步驟:
- 建立
VolumeSnapshot
物件,要求現有 PersistentVolumeClaim 的快照。 - 在
PersistentVolumeClaim
中參照VolumeSnapshot
,將磁碟區還原至該快照,或使用快照建立新磁碟區。
建立範例 PersistentVolumeClaim
和 Pod
如要建立
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
儲存空間類別。套用資訊清單:
kubectl apply -f example-pvc.yaml
建立 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
套用資訊清單:
kubectl apply -f snapshot-shell.yaml
檢查 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
物件。
將下列資訊清單儲存為
volumesnapshot.yaml
。apiVersion: snapshot.storage.k8s.io/v1 kind: VolumeSnapshot metadata: name: example-snapshot spec: source: persistentVolumeClaimName: example-pvc
套用資訊清單:
kubectl apply -f volumesnapshot.yaml
建立磁碟區快照後,叢集會建立對應的
VolumeSnapshotContent
物件。這個物件會儲存VolumeSnapshot
物件的快照和繫結。您不會直接與VolumeSnapshotContents
物件互動。確認叢集是否已建立
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
將下列資訊清單儲存為
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
套用資訊清單:
kubectl apply -f pvc-restore.yaml
啟動臨時 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
套用資訊清單:
kubectl apply -f restore-log.yaml
確認快照已成功還原
您在上一步建立的 Pod 會從快照讀取資料。如要查看快照中的資料,請使用 kubectl logs
指令。
kubectl logs restore-verify
輸出內容應包含快照的時間戳記。
清除所用資源
如要避免系統收取本頁所用資源的費用,請按照下列步驟操作。
刪除
VolumeSnapshot
:kubectl delete volumesnapshot example-snapshot
刪除臨時 Pod:
kubectl delete -f restore-log.yaml
刪除 Pod:
kubectl delete -f snapshot-shell.yaml
刪除
PersistentVolumeClaim
個物件:kubectl delete pvc example-pvc pvc-restore
後續步驟
- 請參閱 Kubernetes Volume Snapshot 說明文件。
- 安裝其他 CSI 驅動程式。