在 Google Kubernetes Engine 上執行 Spark 工作

本文說明如何建立 Managed Service for Apache Spark on GKE 虛擬叢集,然後在叢集上執行 Spark 工作。

選項總覽

GKE 上的 Managed Service for Apache Spark 可有效控管容器化環境,Google Cloud 也提供全代管無伺服器選項,可簡化作業並加快開發速度。如要比較 Spark Managed Service for Apache Spark 部署選項,請參閱「決定最合適的 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.

    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. 安裝 Google Cloud CLI。

  7. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  8. 執行下列指令,初始化 gcloud CLI:

    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.

    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. 安裝 Google Cloud CLI。

  14. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  15. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  16. 您必須建立標準 (非 Autopilot) Google Kubernetes Engine (GKE) 區域地區叢集,並在叢集上啟用 Workload Identity

必要的角色

如要執行本頁的範例,您必須具備特定的 IAM 角色。根據機構政策,這些角色可能已獲授權。如要查看角色授予情況,請參閱「是否需要授予角色?」一文。

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

使用者角色

如要取得建立 Managed Service for Apache Spark 叢集所需的權限,請要求管理員授予您下列 IAM 角色:

服務帳戶角色

為確保 Compute Engine 預設服務帳戶具備建立 Managed Service for Apache Spark 叢集的必要權限,請要求管理員在專案中,將 Dataproc Worker (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。

  • 建立 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 引擎版本和工作負載身分
      • 在 GKE 叢集的獨立 GKE 命名空間中隔離

控制台

Google Cloud 控制台不支援在 GKE 叢集上建立 Managed Service for Apache Spark。

gcloud

設定環境變數,然後在本機或 Cloud Shell 中執行 gcloud dataproc clusters gke create 指令,建立 Managed Service for Apache Spark on 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 個小寫英文字母、數字或連字號。結尾不得為連字號。
    • REGIONregion必須與 GKE 叢集所在的區域相同。
    • GKE_CLUSTER:現有 GKE 叢集的名稱。
    • BUCKET:(選用) 您可以指定 Cloud Storage bucket 的名稱,Managed Service for Apache Spark 會使用這個 bucket 暫存構件。如未指定值區,GKE 上的 Managed Service for Apache Spark 會建立暫存值區。
    • DP_POOLNAME:要在 GKE 叢集上建立的節點集區名稱。
    • PHS_CLUSTER:(選用) Managed Service for Apache Spark PHS 伺服器,用於查看現有和已刪除的 Managed Service for Apache Spark on GKE 叢集中的 Spark 工作記錄。PHS 叢集必須與 Managed Service for Apache Spark on 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:Managed Service for Apache Spark 叢集使用的 Spark 映像檔版本。您可以使用 ID,例如 33.1latest,也可以指定完整次要版本,例如 3.1-dataproc-5
    • --staging-bucket:刪除這個標記,讓 GKE 上的 Managed Service for Apache Spark 建立暫存值區。
    • --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
      
      您必須指定節點集區 namerole。其他節點集區設定則為選填。您可以使用多個 --pools 標記指定多個節點集區。至少須有一個節點集區具備 default 角色。所有節點集區必須位於相同位置。
    • --setup-workload-identity:這個旗標會啟用 Workload Identity 繫結。這些繫結可讓 Kubernetes 服務帳戶 (KSA) 做為虛擬叢集的預設 Managed Service for Apache Spark VM 服務帳戶 (資料層身分)

REST

cluster.create API 要求中,完成 virtualClusterConfig

使用任何要求資料之前,請先修改下列項目的值:

  • 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 方法和網址:

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 Client Libraries),提交 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

清除所用資源