このドキュメントでは、Managed Service for Apache Spark on GKE 仮想クラスタを作成し、 クラスタで Spark ジョブを実行する方法について説明します。
オプションの概要
Managed Service for Apache Spark on GKE では、コンテナ化された環境の高度な制御が可能ですが、 Google Cloud には、運用の簡素化と開発の加速に役立つ、フルマネージドでサーバーレスのオプションも用意されています。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.
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.-
Google Cloud CLI をインストールします。
-
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
-
gcloud CLI を初期化するには、次のコマンドを実行します:
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.
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.-
Google Cloud CLI をインストールします。
-
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
-
gcloud CLI を初期化するには、次のコマンドを実行します:
gcloud init クラスタでWorkload Identity が有効になっている自動パイロットではない標準的な Google Kubernetes Engine(GKE) ゾーン クラスタまたはリージョン クラスタを作成済みである必要があります。
必要なロール
このページの例を実行するには、特定の IAM ロールが必要です。組織のポリシーによっては、これらのロールがすでに付与されている場合があります。ロールの付与を確認するには、 ロールを付与する必要がありますか?をご覧ください。
ロールの付与については、 プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。
ユーザーロール
Managed Service for Apache Spark クラスタの作成に必要な権限を取得するには、次の IAM ロールを付与するよう管理者に依頼してください。
- Dataproc 編集者 (
roles/dataproc.editor)プロジェクトに対する - Compute Engine のデフォルトのサービス アカウントに対する サービス アカウント ユーザー(
roles/iam.serviceAccountUser)
サービス アカウント ロール
Compute Engine のデフォルトのサービス アカウントに、Managed Service for Apache Spark クラスタの作成に必要な
権限を付与するには、
プロジェクトに対する
Dataproc ワーカー (roles/dataproc.worker)IAM ロールを Compute Engine のデフォルトのサービス アカウントに付与するよう管理者に依頼します。
仮想クラスタを作成する
Managed Service for Apache Spark on GKE 仮想クラスタは、 Managed Service for Apache Spark コンポーネントのデプロイ プラットフォームとして作成されます。これは仮想 リソースであり、 Managed Service for Apache Spark on Compute Engine クラスタとは異なり、個別の Managed Service for Apache Spark マスター VM とワーカー VM が含まれません。
Managed Service for Apache Spark on GKE では、Managed Service for Apache Spark on GKE 仮想クラスタを作成するときに、GKE クラスタ内にノードプールが作成されます。
Managed Service for Apache Spark on GKE ジョブは、こうしたノードプールで Pod として実行されます。ノードプールとノードプール上の Pod のスケジューリングは、GKE が管理します。
複数の仮想クラスタを作成します。GKE クラスタで複数の仮想クラスタを作成して実行し、仮想クラスタ間でノードプールを共有することで、リソース使用率を改善できます。
- 各仮想クラスタ:
- Spark エンジンのバージョンやワークロード ID などの個別のプロパティで作成されます。
- GKE クラスタの個別の GKE Namespace 内で分離されています。
- 各仮想クラスタ:
コンソール
コンソールでは、Managed Service for Apache Spark on GKE クラスタの作成はサポートされていません。 Google Cloud
gcloud
環境変数を設定し、ローカルまたは Cloud Shell で
gcloud dataproc clusters gke create
コマンドを実行して、Managed Service for Apache Spark on 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 は、この名前を使用してアーティファクトをステージングします。バケットを指定しない場合は、 Managed Service for Apache Spark on GKE がステージング バケットを作成します。DP_POOLNAME: GKE クラスタに作成するノードプール の名前。PHS_CLUSTER: (省略可)アクティブな Managed Service for Apache Spark on GKE クラスタと削除された Managed Service for Apache Spark on GKE クラスタの Spark ジョブ履歴の表示に使用する Managed Service for Apache Spark PHS サーバーPHS クラスタは、 Managed Service for Apache Spark on 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: Managed Service for Apache Spark クラスタで使用される 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 on GKE ノードプールの設定をカンマで区切って列挙します(例: )。 ノードプールの--pools=name=dp-default,roles=default,machineType=e2-standard-4,min=0,max=10
nameとroleを指定する必要があります。他のノードプールの設定は省略可能です。複数の--poolsフラグを使用して、複数のノードプールを指定できます。 少なくとも 1 つのノードプールにdefaultロールが必要です。すべてのノードプールには同じロケーションが必要です。--setup-workload-identity: このフラグは、 Workload Identity バインディングを有効にします。これらのバインディングにより、Kubernetes サービス アカウント(KSA) が仮想クラスタのデフォルトの Managed Service for Apache Spark VM サービス アカウント(データプレーン ID) として機能できるようになります。
REST
virtualClusterConfig
を
cluster.create
API リクエストの一部として作成します。
リクエスト データを使用する前に、 次のように置き換えます:
- PROJECT: Google Cloud プロジェクト ID
- REGION: Dataproc 仮想クラスタのリージョン(既存の GKE クラスタ リージョンと同じリージョン)
- DP_CLUSTER: Dataproc クラスタ名
- GKE_CLUSTER: GKE クラスタ名
- NODE_POOL: ノードプール名
- PHS_CLUSTER: 永続履歴サーバー(PHS)のクラスタ名
- BUCKET:(省略可)ステージング バケット名。Managed Service for Apache Spark on GKE でステージング バケットを作成する場合は、これを空のままにします。
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 仮想クラスタが実行されたら、
コンソール、
Google Cloud gcloud CLI、または
Managed Service for Apache Spark
jobs.submit
API を使用して(直接 HTTP リクエストまたは
Cloud クライアント ライブラリを使用)Spark ジョブを送信します。
**gcloud CLI Spark ジョブの例:**
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
**gcloud CLI PySpark ジョブの例:**
gcloud dataproc jobs submit pyspark \
--region=${REGION} \
--cluster=${DP_CLUSTER} \
local:///usr/lib/spark/examples/src/main/python/pi.py \
-- 10
**gcloud CLI SparkR ジョブの例:**
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 on GKE クラスタによって使用されます。