本指南說明如何將 (GKE) 連線的 Parallelstore 執行個體中的資料備份至 Cloud Storage 值區,並設定 GKE CronJob,自動排程備份資料,以防資料遺失。本指南也會說明如何復原 Parallelstore 執行個體的資料。
事前準備
請按照「從 GKE 建立及連線至 Parallelstore 執行個體」一文的說明,設定 GKE 叢集和 Parallelstore 執行個體。
資料備份
下一節說明如何設定 GKE CronJob,持續將 GKE 叢集中的 Parallelstore 執行個體資料備份到其他位置,避免資料遺失。
連線至 GKE 叢集
取得 GKE 叢集的憑證:
gcloud container clusters get-credentials CLUSTER_NAME \
--project=PROJECT_ID \
--location=CLUSTER_LOCATION
更改下列內容:
- CLUSTER_NAME:GKE 叢集名稱。
- PROJECT_ID:專案 ID。 Google Cloud
- CLUSTER_LOCATION:包含叢集的 Compute Engine 可用區。您的叢集必須位於 Parallelstore CSI 驅動程式的支援區域。
提供必要權限
您的 GKE CronJob 需要 roles/parallelstore.admin
和 roles/storage.admin
角色,才能在 Cloud Storage 和 Parallelstore 之間匯入及匯出資料。
建立 Google Cloud 服務帳戶
gcloud iam service-accounts create parallelstore-sa \
--project=PROJECT_ID
授予 Google Cloud 服務帳戶角色
將 Parallelstore 管理員和 Cloud Storage 管理員角色授予服務帳戶。
gcloud projects add-iam-policy-binding PROJECT_ID \
--member=serviceAccount:parallelstore-sa@PROJECT_ID.iam.gserviceaccount.com \
--role=roles/parallelstore.admin
gcloud projects add-iam-policy-binding PROJECT_ID \
--member serviceAccount:parallelstore-sa@PROJECT_ID.iam.gserviceaccount.com \
--role=roles/storage.admin
設定 GKE 服務帳戶
您需要設定 GKE 服務帳戶,並允許該帳戶模擬 Google Cloud 服務帳戶。 請按照下列步驟,允許 GKE 服務帳戶繫結至 Google Cloud 服務帳戶。
建立下列
parallelstore-sa.yaml
服務帳戶資訊清單:# GKE service account used by workload and will have access to Parallelstore and GCS apiVersion: v1 kind: ServiceAccount metadata: name: parallelstore-sa namespace: default
接著,使用下列指令將其部署至 GKE 叢集:
kubectl apply -f parallelstore-sa.yaml
允許 GKE 服務帳戶模擬 Google Cloud 服務帳戶。
# Bind the GCP SA and GKE SA gcloud iam service-accounts add-iam-policy-binding parallelstore-sa@PROJECT_ID.iam.gserviceaccount.com \ --role roles/iam.workloadIdentityUser \ --member "serviceAccount:PROJECT_ID.svc.id.goog[default/parallelstore-sa]" # Annotate the GKE SA with GCP SA kubectl annotate serviceaccount parallelstore-sa \ --namespace default \ iam.gke.io/gcp-service-account=parallelstore-sa@PROJECT_ID.iam.gserviceaccount.com
將權限授予 Parallelstore Agent 服務帳戶
gcloud storage buckets add-iam-policy-binding GCS_BUCKET \
--member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-parallelstore.iam.gserviceaccount.com \
--role=roles/storage.admin
更改下列內容:
- GCS_BUCKET:Cloud Storage 值區 URI,格式為
gs://<bucket_name>
。 - PROJECT_NUMBER: Google Cloud 專案編號。
啟動 CronJob
設定並啟動 GKE CronJob,定期將資料從 Parallelstore 匯出至 Cloud Storage。
為 CronJob 建立設定檔 ps-to-gcs-backup.yaml
:
apiVersion: batch/v1
kind: CronJob
metadata:
name: ps-to-gcs-backup
spec:
concurrencyPolicy: Forbid
failedJobsHistoryLimit: 1
schedule: "0 * * * *"
successfulJobsHistoryLimit: 3
suspend: false
jobTemplate:
spec:
template:
metadata:
annotations:
gke-parallelstore/cpu-limit: "0"
gke-parallelstore/ephemeral-storage-limit: "0"
gke-parallelstore/memory-limit: "0"
gke-parallelstore/volumes: "true"
spec:
serviceAccountName: parallelstore-sa
containers:
- name: pstore-backup
image: google/cloud-sdk:slim
imagePullPolicy: IfNotPresent
command:
- /bin/bash
- -c
- |
#!/bin/bash
set -ex
# Retrieve modification timestamp for the latest file up to the minute
latest_folder_timestamp=$(find $PSTORE_MOUNT_PATH/$SOURCE_PARALLELSTORE_PATH -type d -printf '%T@ %p\n'| sort -n | tail -1 | cut -d' ' -f2- | xargs -I{} stat -c %x {} | xargs -I {} date -d {} +"%Y-%m-%d %H:%M")
# Start exporting from PStore to GCS
operation=$(gcloud beta parallelstore instances export-data $PSTORE_NAME \
--location=$PSTORE_LOCATION \
--source-parallelstore-path=$SOURCE_PARALLELSTORE_PATH \
--destination-gcs-bucket-uri=$DESTINATION_GCS_URI \
--async \
--format="value(name)")
# Wait until operation complete
while true; do
status=$(gcloud beta parallelstore operations describe $operation \
--location=$PSTORE_LOCATION \
--format="value(done)")
if [ "$status" == "True" ]; then
break
fi
sleep 60
done
# Check if export succeeded
error=$(gcloud beta parallelstore operations describe $operation \
--location=$PSTORE_LOCATION \
--format="value(error)")
if [ "$error" != "" ]; then
echo "!!! ERROR while exporting data !!!"
fi
# Delete the old files from PStore if requested
# This will not delete the folder with the latest modification timestamp
if $DELETE_AFTER_BACKUP && [ "$error" == "" ]; then
find $PSTORE_MOUNT_PATH/$SOURCE_PARALLELSTORE_PATH -type d -mindepth 1 |
while read dir; do
# Only delete folders that is modified earlier than the latest modification timestamp
folder_timestamp=$(stat -c %y $dir)
if [ $(date -d "$folder_timestamp" +%s) -lt $(date -d "$latest_folder_timestamp" +%s) ]; then
echo "Deleting $dir"
rm -rf "$dir"
fi
done
fi
env:
- name: PSTORE_MOUNT_PATH # mount path of the Parallelstore instance, should match the volumeMount defined for this container
value: "PSTORE_MOUNT_PATH"
- name: PSTORE_NAME # name of the Parallelstore instance that need backup
value: "PSTORE_NAME"
- name: PSTORE_LOCATION # location/zone of the Parallelstore instance that need backup
value: "PSTORE_LOCATION"
- name: SOURCE_PARALLELSTORE_PATH # absolute path from the PStore instance, without volume mount path
value: "SOURCE_PARALLELSTORE_PATH"
- name: DESTINATION_GCS_URI # GCS bucket uri used for storing backups, starting with "gs://"
value: "DESTINATION_GCS_URI"
- name: DELETE_AFTER_BACKUP # will delete old data from Parallelstore if true
value: "DELETE_AFTER_BACKUP"
volumeMounts:
- mountPath: PSTORE_MOUNT_PATH # should match the value of env var PSTORE_MOUNT_PATH
name: PSTORE_PV_NAME
dnsPolicy: ClusterFirst
restartPolicy: OnFailure
terminationGracePeriodSeconds: 30
volumes:
- name: PSTORE_PV_NAME
persistentVolumeClaim:
claimName: PSTORE_PVC_NAME
請替換下列變數:
- PSTORE_MOUNT_PATH:Parallelstore 執行個體的掛接路徑,應與為這個容器定義的
volumeMount
相符。 - PSTORE_PV_NAME:指向 Parallelstore 執行個體的 GKE
PersistentVolume
名稱。您應該已在 GKE 叢集中設定這項服務,做為事前準備的一部分。 - PSTORE_PVC_NAME:要求使用 Parallelstore
PersistentVolume
的 GKEPersistentVolumeClaim
名稱。您應該已在 GKE 叢集中設定這項服務,做為事前準備工作的一部分。 - PSTORE_NAME:需要備份的 Parallelstore 執行個體名稱。
- PSTORE_LOCATION:需要備份的 Parallelstore 執行個體位置。
- SOURCE_PARALLELSTORE_PATH:Parallelstore 執行個體的絕對路徑,不含磁碟區掛接路徑,且必須以
/
開頭。 - DESTINATION_GCS_URI:Cloud Storage bucket URI,指向 Cloud Storage bucket 或 bucket 內的路徑,格式為
gs://<bucket_name>/<optional_path_inside_bucket>
。 - DELETE_AFTER_BACKUP:決定是否要在備份後從 Parallelstore 刪除舊資料並釋出空間的設定,支援的值為
true
或false
。
使用下列指令,將 CronJob 部署至 GKE 叢集:
kubectl apply -f ps-to-gcs-backup.yaml
如要進一步瞭解如何設定 CronJob,請參閱CronJob。
偵測資料遺失
如果 Parallelstore 執行個體的狀態為 FAILED
,您可能無法再存取執行個體上的資料。您可以使用下列 Google Cloud CLI 指令,檢查 Parallelstore 執行個體的狀態:
gcloud beta parallelstore instances describe PARALLELSTORE_NAME \
--location=PARALLELSTORE_LOCATION \
--format="value(state)"
資料復原
發生災害或 Parallelstore 執行個體因任何原因而失敗時,您可以使用 GKE VolumePopulator,從 Cloud Storage 自動將資料預先載入 GKE 管理的 Parallelstore 執行個體,也可以手動建立新的 Parallelstore 執行個體,並從 Cloud Storage 備份匯入資料。
如要從工作負載的檢查點還原,您需要提供 Cloud Storage bucket 內的路徑,決定要從哪個檢查點還原。
GKE Volume Populator
您可以使用 GKE Volume Populator,將資料從 Cloud Storage 值區路徑預先載入至新建立的 Parallelstore 執行個體。如需相關操作說明,請參閱「預先載入 Parallelstore」。
手動復原
您也可以手動建立 Parallelstore 執行個體,然後按照下列步驟從 Cloud Storage bucket 匯入資料。
建立新的 Parallelstore 執行個體:
gcloud beta parallelstore instances create PARALLELSTORE_NAME \ --capacity-gib=CAPACITY_GIB \ --location=PARALLELSTORE_LOCATION \ --network=NETWORK_NAME \ --project=PROJECT_ID
從 Cloud Storage 匯入資料:
gcloud beta parallelstore instances import-data PARALLELSTORE_NAME \ --location=PARALLELSTORE_LOCATION \ --source-gcs-bucket-uri=SOURCE_GCS_URI \ --destination-parallelstore-path=DESTINATION_PARALLELSTORE_PATH \ --async
更改下列內容:
- PARALLELSTORE_NAME:這個 Parallelstore 執行個體的名稱。
- CAPACITY_GIB:Parallelstore 執行個體的儲存空間容量 (GB),值介於
12000
到100000
之間,且為4000
的倍數。 - PARALLELSTORE_LOCATION:需要備份的 Parallelstore 執行個體位置,必須位於支援的區域。
- NETWORK_NAME:您在「設定虛擬私有雲網路」期間建立的虛擬私有雲網路名稱,必須與 GKE 叢集使用的網路相同,且已啟用
Private Services Access
。 - SOURCE_GCS_URI:Cloud Storage 值區的 Cloud Storage 值區 URI,或值區內的路徑 (您要從該處匯入資料),格式為
gs://<bucket_name>/<optional_path_inside_bucket>
。 - DESTINATION_PARALLELSTORE_PATH:要匯入資料的 Parallelstore 執行個體絕對路徑,必須以
/
開頭。
如要進一步瞭解如何將資料匯入 Parallelstore 執行個體,請參閱「將資料傳輸至 Cloud Storage 或從 Cloud Storage 傳輸資料」。