הפעלת משימת Spark ב-Google Kubernetes Engine

במאמר הזה מוסבר איך יוצרים אשכול וירטואלי של Managed Service for Apache Spark ב-GKE, ואז מריצים משימת Spark באשכול.

סקירה כללית של האפשרויות

למרות ש-Managed Service for Apache Spark ב-GKE מציע שליטה חזקה בסביבות בקונטיינרים,Google Cloud מספק גם אפשרויות מנוהלות לחלוטין וללא שרת (serverless) שיכולות לפשט את הפעולות ולהאיץ את הפיתוח. במאמר בחירת שירות Spark המתאים תוכלו לקרוא השוואה בין אפשרויות הפריסה של Managed Service for Apache Spark.

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

  1. נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloudליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that you have the permissions required to complete this guide.

  4. Verify that billing is enabled for your Google Cloud project.

  5. Enable the Dataproc API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  6. Install the Google Cloud CLI.

  7. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  8. To initialize the gcloud CLI, run the following command:

    gcloud init
  9. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  10. Verify that you have the permissions required to complete this guide.

  11. Verify that billing is enabled for your Google Cloud project.

  12. Enable the Dataproc API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  13. Install the Google Cloud CLI.

  14. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  15. To initialize the gcloud CLI, run the following command:

    gcloud init
  16. צריך ליצור אשכול אזורי או של תחום מוגדר ב-Google Kubernetes Engine (GKE) במצב Standard (לא Autopilot), שמופעל בו Workload Identity.

התפקידים הנדרשים

כדי להריץ את הדוגמאות שבדף הזה, צריך תפקידים מסוימים ב-IAM. יכול להיות שהתפקידים האלה כבר הוקצו בהתאם למדיניות הארגון. כדי לבדוק את התפקידים שהוקצו, קראו את המאמר האם צריך להקצות תפקידים?.

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

תפקידי משתמשים

כדי לקבל את ההרשאות שדרושות ליצירת אשכול Managed Service for Apache Spark, בקשו מהאדמין להקצות לכם את התפקידים הבאים ב-IAM:

תפקיד של חשבון שירות

כדי לוודא שלחשבון השירות שמשמש כברירת מחדל ב-Compute Engine יש את ההרשאות שנדרשות ליצירת אשכול Managed Service for Apache Spark, צריך לבקש מהאדמין לתת לחשבון השירות שמשמש כברירת מחדל ב-Compute Engine את התפקיד Dataproc Worker ‏(roles/dataproc.worker) ב-IAM בפרויקט.

יצירת אשכול וירטואלי

אשכול וירטואלי של Managed Service for Apache Spark ב-GKE נוצר כפלטפורמת הפריסה של רכיבי Managed Service for Apache Spark. זהו משאב וירטואלי, ובניגוד לאשכול של Managed Service for Apache Spark ב-Compute Engine, הוא לא כולל מכונות וירטואליות נפרדות של מאסטר ו-worker של Managed Service for Apache Spark.

  • כשיוצרים אשכול וירטואלי של Managed Service for Apache Spark ב-GKE, נוצרים מאגרי צמתים בתוך אשכול GKE.

  • המשימות של Managed Service for Apache Spark ב-GKE פועלות כ-pods במאגרי הצמתים האלה. מאגרי הצמתים ותזמון הפודים במאגרי הצמתים מנוהלים על ידי GKE.

  • ליצור כמה אשכולות וירטואליים. אתם יכולים ליצור ולהריץ כמה אשכולות וירטואליים באשכול GKE כדי לשפר את ניצול המשאבים, על ידי שיתוף של מאגרי צמתים בין האשכולות הווירטואליים.

    • כל אשכול וירטואלי:
      • נוצר עם נכסים נפרדים, כולל גרסת מנוע Spark ו-Workload Identity
      • מבודד במרחב שמות נפרד של GKE באשכול GKE

המסוף

אי אפשר ליצור אשכול של Managed Service for Apache Spark ב-GKE במסוף Google Cloud .

gcloud

מגדירים משתני סביבה ואז מריצים את הפקודה gcloud dataproc clusters gke create באופן מקומי או ב-Cloud Shell כדי ליצור אשכול של Managed Service for Apache Spark ב-GKE.

  1. מגדירים את משתני הסביבה:

    DP_CLUSTER=Managed Service for Apache Spark on GKE  cluster-name \
      REGION=region \
      GKE_CLUSTER=GKE cluster-name \
      BUCKET=Cloud Storage bucket-name \
      DP_POOLNAME=node pool-name
      PHS_CLUSTER=Managed Service for Apache Spark PHS server name
    
    הערות:

    • ‫DP_CLUSTER: מגדירים את שם האשכול הווירטואלי של Managed Service for Apache Spark. השם חייב להתחיל באות קטנה, ואחריה עד 54 אותיות קטנות, מספרים או מקפים. הוא לא יכול להסתיים במקף.
    • ‫REGION: הערך של region צריך להיות זהה לאזור שבו נמצא אשכול GKE.
    • ‫GKE_CLUSTER: השם של אשכול GKE הקיים.
    • ‫BUCKET: (אופציונלי) אתם יכולים לציין את השם של קטגוריה של Cloud Storage, שבה Managed Service for Apache Spark ישתמש כדי להעביר פריטי מידע שנוצרו בתהליך הפיתוח (Artifact) לשלב ביניים. אם לא תציינו קטגוריה, Managed Service for Apache Spark ב-GKE תיצור קטגוריית Staging.
    • ‫DP_POOLNAME: השם של מאגר הצמתים שרוצים ליצור באשכול GKE.
    • ‫PHS_CLUSTER: (אופציונלי) שרת PHS של Managed Service for Apache Spark, כדי להציג את היסטוריית המשימות של Spark באשכולות פעילים ונמחקים של Managed Service for Apache Spark ב-GKE. אשכול ה-PHS צריך להיות באותו אזור שבו נמצא האשכול הווירטואלי של Managed Service for Apache Spark ב-GKE.
  2. מריצים את הפקודה:

    gcloud dataproc clusters gke create ${DP_CLUSTER} \
        --region=${REGION} \
        --gke-cluster=${GKE_CLUSTER} \
        --spark-engine-version=latest \
        --staging-bucket=${BUCKET} \
        --pools="name=${DP_POOLNAME},roles=default" \
        --setup-workload-identity \
        --history-server-cluster=${PHS_CLUSTER}
    
    הערות:

    • ‫--spark-engine-version: גרסת האימג' של Spark שמשמשת באשכול של Managed Service for Apache Spark. אפשר להשתמש במזהה, כמו 3,‏ 3.1 או latest, או לציין את הגרסה המשנית המלאה, כמו 3.1-dataproc-5.
    • ‫--staging-bucket: מחקו את הדגל הזה כדי ש-Managed Service for Apache Spark ב-GKE ייצור קטגוריית Staging.
    • ‫--pools: הדגל הזה משמש כדי לציין מאגר צמתים חדש או קיים ש-Managed Service for Apache Spark ייצור או ישתמש בו כדי לבצע את עומס העבודה. רשימה של הגדרות למאגר הצמתים של Managed Service for Apache Spark ב-GKE, מופרדות בפסיקים, לדוגמה:
      --pools=name=dp-default,roles=default,machineType=e2-standard-4,min=0,max=10
      
      צריך לציין את מאגר הצמתים name ו-role. הגדרות אחרות של מאגר הצמתים הן אופציונליות. אפשר להשתמש בכמה דגלים של --pools כדי לציין כמה מאגרי צמתים. לפחות למאגר צמתים אחד צריך להיות התפקיד default. כל מאגרי הצמתים צריכים להיות באותו מיקום.
    • ‫--setup-workload-identity: הדגל הזה מפעיל קישורי Workload Identity. הקישורים האלה מאפשרים לחשבונות השירות של Kubernetes‏ (KSA) לפעול בתור חשבון השירות של Managed Service for Apache Spark VM (זהות מישור הנתונים) שמוגדר כברירת המחדל של האשכול הווירטואלי.

REST

ממלאים את השדה virtualClusterConfig כחלק מבקשת API של cluster.create.

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • ‫PROJECT: מזהה הפרויקט ב-Google Cloud
  • ‫REGION: האזור של האשכול הווירטואלי של Dataproc (אותו אזור של אשכול GKE הקיים)
  • ‫DP_CLUSTER: שם האשכול ב-Dataproc
  • ‫GKE_CLUSTER: שם אשכול GKE
  • ‫NODE_POOL: שם מאגר הצמתים
  • PHS_CLUSTER: שם האשכול של Persistent History Server (PHS)‎
  • ‫BUCKET: (אופציונלי) שם הקטגוריה של ה-Staging. אם משאירים את השדה הזה ריק, Managed Service for Apache Spark ב-GKE יוצר קטגוריית Staging.

ה-method של ה-HTTP וכתובת ה-URL:

POST https://dataproc.googleapis.com/v1/projects/project-id/regions/region/clusters

תוכן בקשת JSON:

{
  "clusterName":"DP_CLUSTER",
  "projectId":"PROJECT",
  "virtualClusterConfig":{
    "auxiliaryServicesConfig":{
      "sparkHistoryServerConfig":{
        "dataprocCluster":"projects/PROJECT/regions/REGION/clusters/PHS_CLUSTER"
      }
    },
    "kubernetesClusterConfig":{
      "gkeClusterConfig":{
        "gkeClusterTarget":"projects/PROJECT/locations/REGION/clusters/GKE_CLUSTER",
        "nodePoolTarget":[
          {
"nodePool":"projects/PROJECT/locations/REGION/clusters/GKE_CLUSTER/nodePools/NODE_POOL",
            "roles":[
              "DEFAULT"
            ]
          }
        ]
      },
      "kubernetesSoftwareConfig":{
        "componentVersion":{
          "SPARK":"latest"
        }
      }
    },
    "stagingBucket":"BUCKET"
  }
}

כדי לשלוח את הבקשה צריך להרחיב אחת מהאפשרויות הבאות:

אתם אמורים לקבל תגובת JSON שדומה לזו:

{
  "projectId":"PROJECT",
  "clusterName":"DP_CLUSTER",
  "status":{
    "state":"RUNNING",
    "stateStartTime":"2022-04-01T19:16:39.865716Z"
  },
  "clusterUuid":"98060b77-...",
  "statusHistory":[
    {
      "state":"CREATING",
      "stateStartTime":"2022-04-01T19:14:27.340544Z"
    }
  ],
  "labels":{
    "goog-dataproc-cluster-name":"DP_CLUSTER",
    "goog-dataproc-cluster-uuid":"98060b77-...",
    "goog-dataproc-location":"REGION",
    "goog-dataproc-environment":"prod"
  },
  "virtualClusterConfig":{
    "stagingBucket":"BUCKET",
    "kubernetesClusterConfig":{
      "kubernetesNamespace":"dp-cluster",
      "gkeClusterConfig":{
"gkeClusterTarget":"projects/PROJECT/locations/REGION/clusters/GKE_CLUSTER",
        "nodePoolTarget":[
          {
"nodePool":"projects/PROJECT/locations/REGION/clusters/GKE_CLUSTER/nodePools/NODE_POOL",
            "roles":[
              "DEFAULT"
            ]
          }
        ]
      },
      "kubernetesSoftwareConfig":{
        "componentVersion":{
          "SPARK":"3.1-..."
        },
        "properties":{
          "dpgke:dpgke.unstable.outputOnly.endpoints.sparkHistoryServer":"https://...",
          "spark:spark.eventLog.dir":"gs://BUCKET/.../spark-job-history",
          "spark:spark.eventLog.enabled":"true"
        }
      }
    },
    "auxiliaryServicesConfig":{
      "sparkHistoryServerConfig":{
        "dataprocCluster":"projects/PROJECT/regions/REGION/clusters/PHS_CLUSTER"
      }
    }
  }

שליחת משימת Spark

אחרי שהאשכול הווירטואלי של Managed Service for Apache Spark ב-GKE פועל, שולחים משימת Spark באמצעות מסוף Google Cloud , ה-CLI של gcloud או ה-API של Managed Service for Apache Spark jobs.submit (באמצעות בקשות HTTP ישירות או ספריות הלקוח של Cloud).

**דוגמה למשימת Spark ב-CLI של gcloud:**

gcloud dataproc jobs submit spark \
    --region=${REGION} \
    --cluster=${DP_CLUSTER} \
    --class=org.apache.spark.examples.SparkPi \
    --jars=local:///usr/lib/spark/examples/jars/spark-examples.jar \
    -- 1000

**דוגמה למשימת PySpark ב-CLI של gcloud:**

gcloud dataproc jobs submit pyspark \
    --region=${REGION} \
    --cluster=${DP_CLUSTER} \
    local:///usr/lib/spark/examples/src/main/python/pi.py \
    -- 10

**דוגמה למשימת SparkR ב-CLI של gcloud:**

gcloud dataproc jobs submit spark-r \
    --region=${REGION} \
    --cluster=${DP_CLUSTER} \
    local:///usr/lib/spark/examples/src/main/r/dataframe.R

הסרת המשאבים