このガイドでは、接続された(GKE)Parallelstore インスタンスのデータを Cloud Storage バケットにバックアップし、GKE CronJob を構成してスケジュールに従ってデータを自動的にバックアップすることで、データ損失の可能性を防ぐ方法について説明します。このガイドでは、Parallelstore インスタンスのデータを復元する方法についても説明します。
始める前に
GKE から Parallelstore インスタンスを作成して接続するに沿って、GKE クラスタと Parallelstore インスタンスを設定します。
データのバックアップ
次のセクションでは、GKE クラスタの Parallelstore インスタンスからデータを継続的にバックアップして、データ損失を防ぐように GKE CronJob を設定する方法について説明します。
GKE クラスタに接続する
GKE クラスタの認証情報を取得します。
gcloud container clusters get-credentials CLUSTER_NAME \
--project=PROJECT_ID \
--location=CLUSTER_LOCATION
次のように置き換えます。
- CLUSTER_NAME: GKE クラスタ名。
- PROJECT_ID: Google Cloud プロジェクト ID。
- CLUSTER_LOCATION: クラスタを含む Compute Engine ゾーン。クラスタは、Parallelstore CSI ドライバのサポートされているゾーンにある必要があります。
必要な権限をプロビジョニングする
GKE CronJob には、Cloud Storage と Parallelstore の間でデータをインポートおよびエクスポートするための roles/parallelstore.admin
ロールと roles/storage.admin
ロールが必要です。
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 エージェント サービス アカウントに権限を付与する
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:
gs://<bucket_name>
形式の Cloud Storage バケット URI。 - PROJECT_NUMBER: Google Cloud プロジェクト番号。
CronJob を開始する
Parallelstore から Cloud Storage にデータを定期的にエクスポートするように GKE CronJob を構成して開始します。
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 バケットの URI、またはバケット内のパス(
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 バケット内のパスを指定して、復元するチェックポイントを決定する必要があります。
GKE Volume Populator
GKE Volume Populator を使用して、Cloud Storage バケットパスから新しく作成された Parallelstore インスタンスにデータをプリロードできます。手順については、Preload Parallelstore をご覧ください。
手動での復元
次の手順で、Parallelstore インスタンスを手動で作成し、Cloud Storage バケットからデータをインポートすることもできます。
新しい 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: VPC ネットワークを構成するで作成した VPC ネットワークの名前。GKE クラスタが使用するネットワークと同じで、
Private Services Access
が有効になっている必要があります。 - SOURCE_GCS_URI: Cloud Storage バケットの URI、またはインポートするデータがあるバケット内のパス。
gs://<bucket_name>/<optional_path_inside_bucket>
形式を使用します。 - DESTINATION_PARALLELSTORE_PATH: データのインポート先となる Parallelstore インスタンスからの絶対パス。
/
で始まる必要があります。
Parallelstore インスタンスへのデータのインポートの詳細については、Cloud Storage との間でデータを転送するをご覧ください。