במאמר הזה נראה איך ליצור אשכול וירטואלי של Managed Service for Apache Spark ב-GKE, ולאחר מכן להריץ משימת Spark באשכול.
סקירה כללית של האפשרויות
Managed Service for Apache Spark on GKE מאפשר שליטה רבה בסביבות מבוססות קונטיינרים, אבלGoogle Cloud מספק גם אפשרויות מנוהלות במלואן וללא שרת (serverless) שיכולות לפשט את הפעולות ולהאיץ את הפיתוח. כדי להשוות בין אפשרויות הפריסה של Spark Managed Service for Apache Spark, אפשר לעיין במאמר החלטה על שירות Spark הכי טוב.
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloud ליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that you have the permissions required to complete this guide.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Dataproc API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that you have the permissions required to complete this guide.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Dataproc API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init צריך ליצור אשכול אזורי או אזורי רגיל (לא במצב Autopilot) של Google Kubernetes Engine (GKE) שבו מופעלת התכונה Workload Identity.
התפקידים הנדרשים
כדי להריץ את הדוגמאות שבדף הזה, צריך תפקידים מסוימים ב-IAM. יכול להיות שהתפקידים האלה כבר הוקצו לכם, בהתאם למדיניות הארגון. כדי לבדוק את התפקידים שהוקצו, ראו האם צריך להקצות תפקידים?.
כדי לקרוא הסבר על מתן תפקידים, קראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.
תפקידי משתמשים
כדי לקבל את ההרשאות שדרושות ליצירת אשכול של Managed Service for Apache Spark, צריך לבקש מהאדמין להקצות לכם את תפקידי ה-IAM הבאים:
- Dataproc Editor (
roles/dataproc.editor) on the project - משתמש בחשבון שירות (
roles/iam.serviceAccountUser) בחשבון השירות של Compute Engine שמוגדר כברירת מחדל
תפקיד בחשבון שירות
כדי לוודא שלחשבון השירות שמוגדר כברירת מחדל ב-Compute Engine יש את ההרשאות שנדרשות ליצירת אשכול של Managed Service for Apache Spark, צריך לבקש מהאדמין להקצות לחשבון השירות שמוגדר כברירת מחדל ב-Compute Engine את תפקיד ה-IAM Dataproc Worker (roles/dataproc.worker) בפרויקט.
יצירת אשכול וירטואלי
נוצר אשכול וירטואלי של Managed Service for Apache Spark ב-GKE כפלטפורמת הפריסה של רכיבי Managed Service for Apache Spark. זהו משאב וירטואלי, ובשונה מאשכול של Managed Service for Apache Spark ב-Compute Engine, הוא לא כולל מכונות וירטואליות נפרדות של Managed Service for Apache Spark master ו-worker.
Managed Service for Apache Spark on GKE יוצר מאגרי צמתים באשכול GKE כשיוצרים אשכול וירטואלי של Managed Service for Apache Spark on GKE.
משימות של Managed Service for Apache Spark on GKE מורצות כ-pods במאגרי הצמתים האלה. מאגרי הצמתים ותזמון הפודים במאגרי הצמתים מנוהלים על ידי GKE.
יצירת כמה אשכולות וירטואליים. אתם יכולים ליצור ולהפעיל כמה אשכולות וירטואליים באשכול GKE כדי לשפר את ניצול המשאבים על ידי שיתוף מאגרי צמתים בין האשכולות הווירטואליים.
- כל אשכול וירטואלי:
- נוצר עם נכסים נפרדים, כולל גרסת מנוע Spark וזהות עומס עבודה
- מבודד במרחב שמות נפרד של GKE באשכול GKE
- כל אשכול וירטואלי:
המסוף
אי אפשר ליצור Managed Service for Apache Spark באשכול GKE במסוף Google Cloud .
gcloud
מגדירים משתני סביבה, ואז מריצים את הפקודה gcloud dataproc clusters gke create באופן מקומי או ב-Cloud Shell כדי ליצור אשכול Managed Service for Apache Spark ב-GKE.
הגדרת משתני סביבה:
הערות: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 ישתמש בה כדי לאחסן נתונים במחסן ביניים (Stage) את הארטיפקטים. אם לא מציינים קטגוריה, Managed Service for Apache Spark on GKE ייצור קטגוריית אחסון זמנית. -
DP_POOLNAME: השם של מאגר הצמתים שייווצר באשכול GKE. -
PHS_CLUSTER: (אופציונלי) Managed Service for Apache Spark PHS Server לשימוש בצפייה בהיסטוריית משימות Spark באשכולות פעילים ומחוקים של Managed Service for Apache Spark ב-GKE. אשכול ה-PHS צריך להיות באותו אזור כמו האשכול הווירטואלי של Managed Service for Apache Spark ב-GKE.
-
מריצים את הפקודה:
הערות: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 on GKE ייצור באקט זמני. -
--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 (זהות מישור הנתונים) שמוגדר כברירת מחדל באשכול הווירטואלי.
-
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) cluster name
- BUCKET: (אופציונלי) שם קטגוריית ה-staging. אם משאירים את השדה הזה ריק, Managed Service for Apache Spark ב-GKE יוצר קטגוריית אחסון זמנית.
ה-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 on GKE פועל, שולחים עבודת Spark באמצעות Google Cloud המסוף, ה-CLI של gcloud או jobs.submit API של Managed Service for Apache Spark (באמצעות בקשות 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
הסרת המשאבים
מומלץ למחוק את המשאבים הבאים שבהם נעשה שימוש במדריך למתחילים הזה, אם לא רוצים להמשיך להשתמש בהם.
מחיקת מאגרי צמתים שמשמשים את Managed Service for Apache Spark באשכול GKE.