本頁說明如何使用磁碟區快照備份及還原永久磁碟儲存空間。
如需簡介,請參閱「關於 Kubernetes 磁碟區快照」。
需求條件
如要在 GKE 上使用磁碟區快照,必須符合下列規定:
使用支援快照的 CSI 驅動程式。樹內 Persistent Disk 驅動程式不支援快照。如要建立及管理快照,您必須使用與基礎
PersistentVolumeClaim
(PVC) 相同的 CSI 驅動程式。如要建立永久磁碟 (PD) 磁碟區快照,請使用 Compute Engine Persistent Disk CSI 驅動程式。執行 GKE 1.18.10-gke.2100 以上版本,或 1.19.3-gke.2100 以上版本的新 Linux 叢集,預設會安裝 Compute Engine Persistent Disk CSI 驅動程式。您也可以在現有叢集上啟用 Compute Engine Persistent Disk CSI 驅動程式。
使用控制層版本 1.17 以上。如要在
VolumeSnapshot
中使用 Compute Engine 永久磁碟 CSI 驅動程式,請使用 GKE 1.17.6-gke.4 以上版本。
- 擁有現有的
PersistentVolumeClaim
,可做為快照使用。快照來源使用的PersistentVolume
必須由 CSI 驅動程式管理。如要確認您使用的是 CSI 驅動程式,請檢查PersistentVolume
規格是否含有driver: pd.csi.storage.gke.io
或filestore.csi.storage.gke.io
的csi
區段。如果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
,取得最新版本。
建立及使用磁碟區快照
本文中的範例說明如何執行下列工作:
- 建立
PersistentVolumeClaim
和Deployment
。 - 將檔案新增至
Deployment
使用的PersistentVolume
。 - 建立
VolumeSnapshotClass
來設定快照。 - 建立
PersistentVolume
的磁碟區快照。 - 刪除測試檔案。
- 將
PersistentVolume
還原至您建立的快照。 - 確認還原作業是否成功。
如要使用磁碟區快照,請完成下列步驟:
- 建立
VolumeSnapshotClass
物件,指定快照的 CSI 驅動程式和刪除政策。 - 建立
VolumeSnapshot
物件,要求現有PersistentVolumeClaim
的快照。 - 在
PersistentVolumeClaim
中參照VolumeSnapshot
,將磁碟區還原至該快照,或使用快照建立新磁碟區。
建立 PersistentVolumeClaim
和 Deployment
如要建立
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 驅動程式的儲存空間類別。套用資訊清單:
kubectl apply -f my-pvc.yaml
如要建立
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
套用資訊清單:
kubectl apply -f my-deployment.yaml
檢查
Deployment
的狀態:kubectl get deployment hello-app
Deployment
可能需要一段時間才能準備就緒。您可以執行上述指令,直到看到類似下列內容的輸出:NAME READY UP-TO-DATE AVAILABLE AGE hello-app 1/1 1 1 2m55s
在磁碟區中新增測試檔案
列出
Deployment
中的Pods
:kubectl get pods -l app=hello-app
輸出結果會與下列內容相似:
NAME READY STATUS RESTARTS AGE hello-app-6d7b457c7d-vl4jr 1/1 Running 0 2m56s
在
Pod
中建立測試檔案:kubectl exec POD_NAME \ -- sh -c 'echo "Hello World!" > /usr/share/hello/hello.txt'
將
POD_NAME
替換為Pod
的名稱。確認檔案是否存在:
kubectl exec POD_NAME \ -- sh -c 'cat /usr/share/hello/hello.txt'
輸出結果會與下列內容相似:
Hello World!
建立 VolumeSnapshotClass
物件
建立 VolumeSnapshotClass
物件,指定 CSI 驅動程式和磁碟區快照的 deletionPolicy
。建立 VolumeSnapshot
物件時,可以參照 VolumeSnapshotClass
物件。
將下列資訊清單儲存為
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
。
套用資訊清單:
kubectl apply -f volumesnapshotclass.yaml
建立 VolumeSnapshot
VolumeSnapshot
物件是現有 PersistentVolumeClaim
物件快照的要求。建立 VolumeSnapshot
物件時,GKE 會自動建立並繫結 VolumeSnapshotContent
物件,這是叢集中的資源,例如 PersistentVolume
物件。
將下列資訊清單儲存為
volumesnapshot.yaml
。apiVersion: snapshot.storage.k8s.io/v1 kind: VolumeSnapshot metadata: name: my-snapshot spec: volumeSnapshotClassName: my-snapshotclass source: persistentVolumeClaimName: my-pvc
套用資訊清單:
kubectl apply -f volumesnapshot.yaml
建立
Volume
快照後,GKE 會在叢集中建立對應的VolumeSnapshotContent
物件。這個物件會儲存VolumeSnapshot
物件的快照和繫結。您不會直接與VolumeSnapshotContents
物件互動。確認 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
刪除測試檔案
刪除您建立的測試檔案:
kubectl exec POD_NAME \ -- sh -c 'rm /usr/share/hello/hello.txt'
確認檔案已不存在:
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
以使用新的聲明。
確認您使用的是磁碟或映像檔快照,兩者差異如下:
- 磁碟快照:頻繁建立快照,但很少還原。
- 映像檔快照:不常建立快照,但經常還原。建立映像檔快照的速度也可能比磁碟快照慢。
詳情請參閱「快照頻率限制」。瞭解快照類型有助於排解問題。
檢查
VolumeSnapshot
:kubectl describe volumesnapshot SNAPSHOT_NAME
volumeSnapshotClassName
欄位會指定快照類別。kubectl describe volumesnapshotclass SNAPSHOT_CLASS_NAME
snapshot-type
參數會指定snapshots
或images
。如未提供,則預設值為snapshots
。如果沒有快照類別 (例如快照是靜態建立),請檢查
VolumeSnapshotContents
。sh kubectl describe volumesnapshotcontents SNAPSHOT_CONTENTS_NAME
輸出內容中的快照控制代碼格式會顯示快照類型,如下所示: *projects/PROJECT_NAME/global/snapshots/SNAPSHOT_NAME
:磁碟快照projects/PROJECT_NAME/global/images/IMAGE_NAME
:圖片快照
將下列資訊清單儲存為
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
套用資訊清單:
kubectl apply -f pvc-restore.yaml
更新
my-deployment.yaml
檔案,使用新的PersistentVolumeClaim
:... volumes: - name: my-volume persistentVolumeClaim: claimName: pvc-restore
套用更新後的資訊清單:
kubectl apply -f my-deployment.yaml
確認快照已成功還原
取得 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 中填入磁碟區。
找出快照名稱。
Google Cloud 控制台
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
將下列
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
套用資訊清單:
kubectl apply -f restored-snapshot.yaml
將下列
VolumeSnapshotContent
資訊清單儲存為restored-snapshot-content.yaml
。請將snapshotHandle
欄位替換為您的專案 ID 和快照名稱。volumeSnapshotRef.name
和volumeSnapshotRef.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
套用資訊清單:
kubectl apply -f restored-snapshot-content.yaml
將下列
PersistentVolumeClaim
資訊清單儲存為restored-pvc.yaml
。 Kubernetes 儲存空間控制器會尋找名為VolumeSnapshot
的restored-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
套用資訊清單:
kubectl apply -f restored-pvc.yaml
請將下列
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
套用資訊清單:
kubectl apply -f restored-pod.yaml
確認檔案是否已還原:
kubectl exec restored-pod -- sh -c 'cat /demo/data/hello.txt'
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。
刪除
VolumeSnapshot
:kubectl delete volumesnapshot my-snapshot
刪除
VolumeSnapshotClass
:kubectl delete volumesnapshotclass my-snapshotclass
刪除
Deployment
:kubectl delete deployments hello-app
刪除
PersistentVolumeClaim
個物件:kubectl delete pvc my-pvc pvc-restore
後續步驟
- 請參閱 Kubernetes Volume Snapshot 說明文件。
- 瞭解音量擴展。
- 瞭解如何手動安裝 CSI 驅動程式。
- 瞭解 GKE 的區塊儲存空間 (永久磁碟)。