Membuat cluster Dataproc menggunakan gcloud CLI

Halaman ini menunjukkan cara menggunakan alat command line Google Cloud CLI gcloud untuk membuat cluster Dataproc, menjalankan tugas Apache Spark di cluster, lalu mengubah jumlah pekerja di cluster.

Anda dapat mengetahui cara melakukan tugas yang sama atau serupa dengan Panduan memulai menggunakan API Explorer, konsol Google Cloud di Membuat cluster Dataproc menggunakan konsol Google Cloud , dan menggunakan library klien di Membuat cluster Dataproc menggunakan library klien.

Sebelum memulai

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

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

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

    gcloud init
  5. Create or select 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.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Pastikan Anda memiliki izin yang diperlukan untuk menyelesaikan panduan ini.

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

  8. Enable the Dataproc API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable dataproc.googleapis.com
  9. Install the Google Cloud CLI.

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

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

    gcloud init
  12. Create or select 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.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  13. Pastikan Anda memiliki izin yang diperlukan untuk menyelesaikan panduan ini.

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

  15. Enable the Dataproc API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable dataproc.googleapis.com
  16. 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 pemberian peran, lihat Mengelola akses ke project,folder, dan organisasi.

    Peran pengguna

    Untuk mendapatkan izin yang Anda perlukan untuk membuat cluster Dataproc, minta administrator untuk memberi Anda peran IAM berikut:

    Peran akun layanan

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

    Membuat cluster

    Untuk membuat cluster bernama example-cluster, jalankan perintah gcloud Dataproc clusters create berikut.

    gcloud dataproc clusters create example-cluster --region=REGION
    

    Ganti kode berikut:

    REGION: Tentukan region tempat cluster akan berada.

    Mengirim tugas

    Untuk mengirimkan contoh tugas Spark yang menghitung nilai kasar untuk pi, jalankan perintah gcloud Dataproc jobs submit spark berikut:

    gcloud dataproc jobs submit spark --cluster example-cluster \
        --region=REGION \
        --class org.apache.spark.examples.SparkPi \
        --jars file:///usr/lib/spark/examples/jars/spark-examples.jar -- 1000
    

    Catatan:

    Ganti kode berikut:

    REGION: Tentukan region cluster.

    • Tugas berjalan di example-cluster.
    • class berisi metode utama untuk SparkPi, yang menghitung nilai pi perkiraan. integritas aplikasi.
    • File jar berisi kode tugas.
    • 1000 adalah parameter tugas. Menentukan jumlah tugas (iterasi) yang dilakukan tugas untuk menghitung nilai pi.

    Tugas sedang berjalan dan output terakhir ditampilkan di jendela terminal:

    Waiting for job output...
    ...
    Pi is roughly 3.14118528
    ...
    Job finished successfully.
    

    Mengupdate cluster

    Untuk mengubah jumlah worker di cluster menjadi lima, jalankan perintah berikut:

    gcloud dataproc clusters update example-cluster \
        --region=REGION \
        --num-workers 5
    

    Output perintah menampilkan detail cluster:

    workerConfig:
    ...
      instanceNames:
      - example-cluster-w-0
      - example-cluster-w-1
      - example-cluster-w-2
      - example-cluster-w-3
      - example-cluster-w-4
      numInstances: 5
    statusHistory:
    ...
    - detail: Add 3 workers.
    

    Untuk mengurangi jumlah worker node ke nilai asli 2, jalankan perintah berikut:

    gcloud dataproc clusters update example-cluster \
        --region=REGION \
        --num-workers 2
    

    Pembersihan

    Agar akun Google Cloud Anda tidak dikenai biaya untuk resource yang digunakan di halaman ini, ikuti langkah-langkah berikut.

    1. Untuk menghapus example-cluster, jalankan perintah clusters delete:
      gcloud dataproc clusters delete example-cluster \
          --region=REGION
      

    Langkah berikutnya