Membuat dan menjalankan tugas di Cloud Run

Halaman ini menunjukkan Anda cara membuat tugas di Cloud Run menggunakan container contoh, menjalankan tugas, dan melihat log untuk tugas tersebut.

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. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

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

  4. Enable the Cloud Run Admin 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.

    Enable the API

  5. Make sure that you have the following role or roles on the project: Cloud Run Developer, Logs Viewer

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      Buka IAM
    2. Pilih project.
    3. Klik Grant access.
    4. Di kolom New principals, masukkan ID pengguna Anda. Biasanya berupa alamat email untuk Akun Google.

    5. Di daftar Select a role, pilih peran.
    6. Untuk memberikan peran tambahan, klik Tambahkan peran lain, lalu tambahkan setiap peran tambahan.
    7. Klik Simpan.
  6. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

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

  8. Enable the Cloud Run Admin 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.

    Enable the API

  9. Make sure that you have the following role or roles on the project: Cloud Run Developer, Logs Viewer

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      Buka IAM
    2. Pilih project.
    3. Klik Grant access.
    4. Di kolom New principals, masukkan ID pengguna Anda. Biasanya berupa alamat email untuk Akun Google.

    5. Di daftar Select a role, pilih peran.
    6. Untuk memberikan peran tambahan, klik Tambahkan peran lain, lalu tambahkan setiap peran tambahan.
    7. Klik Simpan.
  10. Pastikan Anda memiliki peran Service Account User yang diberikan pada identitas layanan. Secara default, identitas layanan adalah akun layanan default Compute Engine.

    Memberikan peran

    Untuk memberikan akses pada resource identitas layanan, gunakan perintah gcloud iam service-accounts add-iam-policy-binding, dengan mengganti variabel yang ditandai dengan nilai yang sesuai:

          gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_EMAIL \
              --member=user:PRINCIPAL \
              --role=roles/iam.serviceAccountUser
          

    Ganti kode berikut:

    • SERVICE_ACCOUNT_EMAIL: alamat email akun layanan yang Anda gunakan sebagai identitas layanan, seperti:
      • Akun layanan default Compute Engine: PROJECT_NUMBER-compute@
      • Akun layanan yang Anda buat: SERVICE_ACCOUNT_NAME@PROJECT_ID.
    • PRINCIPAL: ID pengguna. Biasanya berupa alamat email Akun Google yang men-deploy resource Cloud Run.
  11. Tinjau harga Cloud Run atau perkirakan biaya dengan kalkulator harga.
  12. Buat tugas

    Untuk membuat tugas:

    1. Di konsol Google Cloud , buka halaman Cloud Run:

      Buka Cloud Run

    2. Pilih Jobs dari menu, lalu klik Deploy container untuk membuka formulir Create job.

    3. Klik Uji dengan container contoh.

    4. Di menu pull-down Region, pilih region tempat Anda ingin menjalankan tugas.

    5. Klik Buat untuk membuat tugas.

    Tugas dibuat dan siap dijalankan.

    Laksanakan tugas

    Setelah Anda membuat tugas, Anda dapat menjalankannya:

    1. Di tab Tugas, klik tugas yang baru saja Anda buat untuk membuka halaman Detail tugas.

    2. Klik Jalankan.

    3. Log untuk tugas tersedia segera setelah eksekusi tugas dimulai. Klik tab Logs untuk melihat log untuk tugas tersebut.

    Lokasi Cloud Run

    Cloud Run bersifat regional, berarti infrastruktur yang menjalankan layanan Cloud Run Anda terletak di region tertentu dan dikelola oleh Google agar tersedia secara redundan di semua zona dalam region tersebut.

    Memenuhi persyaratan latensi, ketersediaan, atau ketahanan adalah faktor utama untuk memilih region tempat layanan Cloud Run dijalankan. Pada umumnya, Anda dapat memilih region yang paling dekat dengan pengguna Anda, tetapi Anda harus mempertimbangkan lokasi produk Google Cloud lainnya yang digunakan oleh layanan Cloud Run Anda. Menggunakan Google Cloud produk secara bersamaan di beberapa lokasi dapat memengaruhi latensi serta biaya layanan Anda.

    Cloud Run tersedia di region berikut:

    Tergantung harga Tingkat 1

    • asia-east1 (Taiwan)
    • asia-northeast1 (Tokyo)
    • asia-northeast2 (Osaka)
    • asia-south1 (Mumbai, India)
    • europe-north1 (Finlandia) ikon daun CO2 Rendah
    • europe-north2 (Stockholm) ikon daun CO2 Rendah
    • europe-southwest1 (Madrid) ikon daun CO2 Rendah
    • europe-west1 (Belgia) ikon daun CO2 Rendah
    • europe-west4 (Belanda) ikon daun CO2 Rendah
    • europe-west8 (Milan)
    • europe-west9 (Paris) ikon daun CO2 Rendah
    • me-west1 (Tel Aviv)
    • northamerica-south1 (Meksiko)
    • us-central1 (Iowa) ikon daun CO2 Rendah
    • us-east1 (South Carolina)
    • us-east4 (North Virginia)
    • us-east5 (Columbus)
    • us-south1 (Dallas) ikon daun CO2 Rendah
    • us-west1 (Oregon) ikon daun CO2 Rendah

    Tergantung harga Tingkat 2

    • africa-south1 (Johannesburg)
    • asia-east2 (Hong Kong)
    • asia-northeast3 (Seoul, Korea Selatan)
    • asia-southeast1 (Singapura)
    • asia-southeast2 (Jakarta)
    • asia-south2 (Delhi, India)
    • australia-southeast1 (Sydney)
    • australia-southeast2 (Melbourne)
    • europe-central2 (Warsawa, Polandia)
    • europe-west10 (Berlin) ikon daun CO2 Rendah
    • europe-west12 (Turin)
    • europe-west2 (London, Inggris Raya) ikon daun CO2 Rendah
    • europe-west3 (Frankfurt, Jerman)
    • europe-west6 (Zurich, Swiss) ikon daun CO2 Rendah
    • me-central1 (Doha)
    • me-central2 (Dammam)
    • northamerica-northeast1 (Montreal) ikon daun CO2 Rendah
    • northamerica-northeast2 (Toronto) ikon daun CO2 Rendah
    • southamerica-east1 (Sao Paulo, Brasil) ikon daun CO2 Rendah
    • southamerica-west1 (Santiago, Cile) ikon daun CO2 Rendah
    • us-west2 (Los Angeles)
    • us-west3 (Salt Lake City)
    • us-west4 (Las Vegas)

    Jika sudah membuat layanan Cloud Run, Anda dapat melihat region di dasbor Cloud Run di Google Cloud konsol.

    Pembersihan

    Untuk menghindari biaya tambahan pada akun Google Cloud Anda, hapus semua resource yang Anda deploy dengan panduan memulai ini.

    Menghapus repositori Anda

    Cloud Run hanya mengenakan biaya untuk waktu eksekusi tugas Anda. Namun, Anda mungkin tetap dikenai biaya atas penyimpanan image container di Artifact Registry. Untuk menghapus repositori Artifact Registry, ikuti langkah-langkah di Menghapus repositori dalam dokumentasi Artifact Registry.

    Menghapus tugas Anda

    Tugas Cloud Run hanya menimbulkan biaya saat tugas pekerjaan sedang dieksekusi. Untuk menghapus tugas Cloud Run, ikuti salah satu langkah berikut:

    Konsol

    Untuk menghapus tugas:

    1. Di konsol Google Cloud , buka Cloud Run:

      Buka Cloud Run

    2. Temukan tugas yang ingin Anda hapus dalam daftar tugas, lalu klik kotak centang tugas tersebut untuk memilihnya.

    3. Klik Hapus. Tindakan ini akan menghentikan semua eksekusi tugas yang sedang berlangsung dan semua instance container yang sedang berjalan.

    gcloud

    Untuk menghapus tugas, jalankan perintah berikut:

    gcloud run jobs delete JOB_NAME

    Ganti JOB_NAME dengan nama tugas.

    Menghapus project pengujian Anda

    Menghapus project Google Cloud akan menghentikan penagihan untuk semua resource dalam project tersebut. Untuk melepaskan semua Google Cloud resource di project Anda, ikuti langkah-langkah berikut:

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

    Langkah berikutnya