גיבוי ושחזור נתונים של Parallelstore ב-Google Kubernetes Engine

הגישה ל-Parallelstore היא בהזמנה בלבד. אם אתם רוצים לבקש גישה ל-Parallelstore בפרויקט שלכם, אתם יכולים לפנות לנציג המכירות שלכם. Google Cloud

במדריך הזה מוסבר איך אפשר לגבות את הנתונים במופע Parallelstore שמחובר ל-GKE לקטגוריה של Cloud Storage, ולמנוע אובדן נתונים פוטנציאלי על ידי הגדרת CronJob ב-GKE לגיבוי אוטומטי של הנתונים לפי לוח זמנים. במדריך הזה מוסבר גם איך לשחזר נתונים של מופע Parallelstore.

לפני שמתחילים

פועלים לפי ההוראות במאמר יצירה של מכונת Parallelstore וחיבור אליה מ-GKE כדי להגדיר את אשכול GKE ואת מכונת Parallelstore.

גיבוי נתונים

בקטע הבא מוסבר איך להגדיר GKE CronJob כדי לגבות באופן רציף את הנתונים ממופע Parallelstore באשכול GKE, וכך למנוע אובדן נתונים.

התחברות לאשכול GKE

מקבלים את פרטי הכניסה לאשכול GKE:

    gcloud container clusters get-credentials CLUSTER_NAME \
      --project=PROJECT_ID \
      --location=CLUSTER_LOCATION

מחליפים את מה שכתוב בשדות הבאים:

  • CLUSTER_NAME: שם אשכול GKE.
  • PROJECT_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 Admin ו-Cloud Storage Admin.

    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 חשבון השירות, מבצעים את השלבים הבאים.

  1. יוצרים את מניפסט חשבון השירות 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
    
  2. מתן הרשאה לחשבון השירות של 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: ה-URI של קטגוריית Cloud Storage בפורמט gs://<bucket_name>.
  • PROJECT_NUMBER: מספר הפרויקט ב- Google Cloud .

הפעלת משימת Cron

מגדירים ומפעילים GKE CronJob לייצוא נתונים מ-Parallelstore ל-Cloud Storage באופן תקופתי.

יוצרים את קובץ ההגדרות ps-to-gcs-backup.yaml בשביל CronJob:

  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: השם של GKEPersistentVolume שמפנה למופע Parallelstore. ההגדרה הזו צריכה להתבצע באשכול GKE כחלק מהדרישות המוקדמות.
  • PSTORE_PVC_NAME: השם של GKEPersistentVolumeClaim שמבקש להשתמש ב-ParallelstorePersistentVolume. ההגדרה הזו אמורה להתבצע באשכול GKE כחלק מהדרישות המוקדמות.
  • PSTORE_NAME: השם של מופע Parallelstore שצריך לגבות.
  • PSTORE_LOCATION: המיקום של מופע Parallelstore שצריך לגבות.
  • SOURCE_PARALLELSTORE_PATH: הנתיב המוחלט ממופע Parallelstore ללא נתיב הטמעת הנפח, והוא חייב להתחיל ב-/.
  • DESTINATION_GCS_URI: ה-URI של קטגוריית Cloud Storage, או נתיב בתוך קטגוריה, בפורמט 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 נכשל מסיבה כלשהי, אפשר להשתמש ב-VolumePopulator של GKE כדי לטעון מראש נתונים מ-Cloud Storage באופן אוטומטי למופע מנוהל של Parallelstore ב-GKE, או ליצור באופן ידני מופע חדש של Parallelstore ולייבא נתונים מגיבוי ב-Cloud Storage.

אם אתם משחזרים מנקודת ביקורת של עומס העבודה, אתם צריכים להחליט מאיזו נקודת ביקורת לשחזר על ידי ציון הנתיב בתוך קטגוריה של Cloud Storage.

אם מופע Parallelstore נכשל באמצע פעולת הייצוא, יכול להיות שייצוא Parallelstore ב-Cloud Storage יכלול נתונים חלקיים. לפני שמייבאים את הנתונים ל-Parallelstore וממשיכים את עומס העבודה, צריך לבדוק שהנתונים מלאים במיקום היעד ב-Cloud Storage.

GKE Volume Populator

אפשר להשתמש ב-GKE Volume Populator כדי לטעון מראש נתונים מנתיב של קטגוריה של Cloud Storage למופע Parallelstore חדש שנוצר. הוראות לכך מופיעות במאמר טעינה מראש של Parallelstore.

שחזור ידני

אפשר גם ליצור מכונה של Parallelstore באופן ידני ולייבא נתונים מקטגוריה של Cloud Storage באמצעות השלבים הבאים.

  1. יוצרים מופע חדש של Parallelstore:

      gcloud beta parallelstore instances create PARALLELSTORE_NAME \
        --capacity-gib=CAPACITY_GIB \
        --location=PARALLELSTORE_LOCATION \
        --network=NETWORK_NAME \
        --project=PROJECT_ID
    
  2. ייבוא נתונים מ-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: ה-URI של קטגוריית Cloud Storage, או נתיב בתוך קטגוריה שבה נמצאים הנתונים שרוצים לייבא, בפורמט gs://<bucket_name>/<optional_path_inside_bucket>.
  • DESTINATION_PARALLELSTORE_PATH: הנתיב המוחלט ממופע Parallelstore שאליו רוצים לייבא את הנתונים, חייב להתחיל ב-/.

פרטים נוספים על ייבוא נתונים למופע Parallelstore זמינים במאמר העברת נתונים אל Cloud Storage וממנו.