Menjalankan tugas Spark di Google Kubernetes Engine

Dokumen ini menunjukkan cara membuat cluster virtual Managed Service untuk Apache Spark di GKE, lalu menjalankan tugas Spark di cluster.

Ringkasan opsi

Meskipun Managed Service untuk Apache Spark di GKE menawarkan kontrol yang canggih untuk lingkungan yang dikontainerkan, Google Cloud layanan ini juga menyediakan opsi terkelola sepenuhnya dan serverless yang dapat menyederhanakan operasi dan mempercepat pengembangan. Untuk perbandingan opsi deployment Spark Managed Service untuk Apache Spark, lihat Memutuskan layanan Spark terbaik.

Sebelum memulai

  1. Login ke akun Anda. Google Cloud Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  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. Instal Google Cloud CLI.

  7. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus terlebih dahulu login ke gcloud CLI dengan identitas gabungan Anda.

  8. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    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. Instal Google Cloud CLI.

  14. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus terlebih dahulu login ke gcloud CLI dengan identitas gabungan Anda.

  15. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  16. Anda harus membuat cluster Google Kubernetes Engine (GKE) standar (bukan autopilot) zonal atau regional yang mengaktifkan Workload Identity di cluster.

Peran yang diperlukan

Peran IAM tertentu diperlukan untuk menjalankan contoh di halaman ini. Bergantung pada kebijakan organisasi, peran ini mungkin sudah diberikan. Untuk memeriksa pemberian peran, lihat Apakah Anda perlu memberikan peran?.

Untuk mengetahui informasi selengkapnya tentang cara memberikan peran, lihat Mengelola akses ke project, folder, dan organisasi.

Peran pengguna

Untuk mendapatkan izin yang Anda perlukan untuk membuat cluster Managed Service untuk Apache Spark, minta administrator Anda untuk memberikan peran IAM berikut:

Peran akun layanan

Untuk memastikan bahwa akun layanan default Compute Engine memiliki izin yang diperlukan untuk membuat cluster Managed Service untuk Apache Spark, minta administrator Anda untuk memberikan peran IAM Dataproc Worker (roles/dataproc.worker) ke akun layanan default Compute Engine di project.

Membuat cluster virtual

Cluster virtual Managed Service untuk Apache Spark di GKE dibuat sebagai platform deployment untuk komponen Managed Service untuk Apache Spark. Cluster ini adalah resource virtual, dan tidak seperti cluster Managed Service untuk Apache Spark di Compute Engine, tidak menyertakan VM master dan worker Managed Service untuk Apache Spark yang terpisah.

  • Managed Service untuk Apache Spark di GKE membuat node pool dalam cluster GKE saat Anda membuat cluster virtual Managed Service untuk Apache Spark di GKE.

  • Tugas Managed Service untuk Apache Spark di GKE dijalankan sebagai pod di node pool ini. Node pool dan penjadwalan pod di node pool dikelola oleh GKE.

  • Membuat beberapa cluster virtual. Anda dapat membuat dan menjalankan beberapa cluster virtual di cluster GKE untuk mendapatkan pemanfaatan resource yang lebih baik dengan berbagi node pool di seluruh cluster virtual.

    • Setiap cluster virtual:
      • dibuat dengan properti terpisah, termasuk versi mesin Spark dan workload identity
      • diisolasi dalam namespace GKE terpisah di cluster GKE

Konsol

Pembuatan cluster Managed Service untuk Apache Spark di GKE tidak didukung di Google Cloud konsol.

gcloud

Tetapkan variabel lingkungan, lalu jalankan perintah gcloud dataproc clusters gke create secara lokal atau di Cloud Shell untuk membuat cluster Managed Service untuk Apache Spark di GKE.

  1. Menetapkan variabel lingkungan:

    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
    
    Catatan:

    • DP_CLUSTER: Tetapkan nama cluster virtual Managed Service untuk Apache Spark, yang harus dimulai dengan huruf kecil, diikuti hingga 54 huruf kecil, angka, atau tanda hubung. Nama ini tidak boleh diakhiri dengan tanda hubung.
    • REGION: region harus sama dengan region tempat cluster GKE berada.
    • GKE_CLUSTER: Nama cluster GKE Anda yang ada.
    • BUCKET: (Opsional) Anda dapat menentukan nama bucket Cloud Storage, yang akan digunakan Managed Service untuk Apache Spark untuk menampung artefak. Jika Anda tidak menentukan bucket, Managed Service untuk Apache Spark di GKE akan membuat bucket penampung.
    • DP_POOLNAME: Nama node pool yang akan dibuat di cluster GKE.
    • PHS_CLUSTER: (Opsional) Managed Service untuk Apache Spark PHS Server yang akan digunakan untuk melihat histori tugas Spark di cluster Managed Service untuk Apache Spark di GKE yang aktif dan dihapus. Cluster PHS harus berada di region yang sama dengan cluster virtual Managed Service untuk Apache Spark di GKE.
  2. Jalankan perintah:

    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}
    
    Catatan:

    • --spark-engine-version: Versi image Spark yang digunakan di cluster Managed Service untuk Apache Spark. Anda dapat menggunakan ID, seperti 3, 3.1, atau latest, atau Anda dapat menentukan versi subminor lengkap, seperti 3.1-dataproc-5.
    • --staging-bucket: Hapus flag ini agar Managed Service untuk Apache Spark di GKE membuat bucket penampung.
    • --pools: Flag ini digunakan untuk menentukan node pool baru atau yang sudah ada yang akan dibuat atau digunakan Managed Service untuk Apache Spark untuk menjalankan workload. Cantumkan setelan node pool Managed Service untuk Apache Spark di GKE, yang dipisahkan dengan koma, misalnya:
      --pools=name=dp-default,roles=default,machineType=e2-standard-4,min=0,max=10
      
      Anda harus menentukan name dan role node pool. Setelan node pool lainnya bersifat opsional. Anda dapat menggunakan beberapa flag --pools untuk menentukan beberapa node pool. Setidaknya satu node pool harus memiliki peran default. Semua node pool harus memiliki lokasi yang sama.
    • --setup-workload-identity: Flag ini mengaktifkan binding Workload Identity. Binding ini memungkinkan akun layanan Kubernetes (KSA) bertindak sebagai Akun Layanan VM Managed Service untuk Apache Spark (identitas Data Plane) default dari cluster virtual.

REST

Lengkapi virtualClusterConfig sebagai bagian dari permintaan API cluster.create.

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT: Project ID Google Cloud
  • REGION: Region cluster virtual Dataproc (region yang sama dengan region cluster GKE yang ada)
  • DP_CLUSTER: Nama cluster Dataproc
  • GKE_CLUSTER: Nama cluster GKE
  • NODE_POOL: Nama node pool
  • PHS_CLUSTER: Nama cluster Persistent History Server (PHS)
  • BUCKET: (Opsional) Nama bucket penampung. Biarkan kolom ini kosong agar Managed Service untuk Apache Spark di GKE membuat bucket penampung.

Metode HTTP dan URL:

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

Meminta isi 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"
  }
}

Untuk mengirim permintaan Anda, perluas salah satu opsi berikut:

Anda akan melihat respons JSON seperti berikut:

{
  "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"
      }
    }
  }

Mengirimkan tugas Spark

Setelah cluster virtual Managed Service untuk Apache Spark di GKE Anda berjalan, kirimkan tugas Spark menggunakan Google Cloud konsol, gcloud CLI, atau Managed Service untuk Apache Spark jobs.submit API (dengan menggunakan permintaan HTTP langsung atau Cloud Client Libraries).

**Contoh tugas Spark gcloud CLI:**

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

**Contoh tugas PySpark gcloud CLI:**

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

**Contoh tugas SparkR gcloud CLI:**

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

Pembersihan