在 Cloud Run 中建立及執行工作

本頁面說明如何使用範例容器在 Cloud Run 中建立工作、執行工作,以及查看工作記錄。

事前準備

  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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  5. Enable the Cloud Run Admin API, if it is not already enabled.

    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. 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

  7. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  9. Enable the Cloud Run Admin API, if it is not already enabled.

    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

  10. 查看 Cloud Run 定價,或使用 Pricing Calculator 估算費用。

必要的角色

如要取得完成本快速入門導覽課程所需的權限,請要求管理員授予您下列 IAM 角色:

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

您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

建立工作

如要建立工作,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Cloud Run」頁面:

    前往 Cloud Run。

  2. 從 Cloud Run 導覽選單中選取「Jobs」(工作),然後按一下「Deploy container」(部署容器),開啟「Create job」(建立工作) 表單。

  3. 在「Container image URL」(容器映像檔網址) 欄位中, 按一下「Test with a sample container」(使用範例容器進行測試)。

  4. 在「Region」(區域) 下拉式選單中, 使用預設值 europe-west1,或選取要執行工作的區域。

  5. 按一下「建立」,建立工作。

工作已建立,可以執行。

執行工作

建立工作後,您可以執行工作:

  1. 在「Jobs」(工作) 分頁中,按一下剛建立的工作,開啟「Job details」(工作詳細資料) 頁面。

  2. 點選「Execute」。

  3. 工作執行後,即可查看記錄。依序點選「Observability」(觀測能力) 分頁標籤和「Logs」(記錄),即可查看記錄。

Cloud Run 位置

Cloud Run 具有區域性,這表示執行 Cloud Run 服務的基礎架構位於特定區域,並由 Google 負責管理,實現該區域內所有可用區之間的備援可用性。

選擇執行 Cloud Run 服務的區域時,主要考量重點在於延遲時間、可用性或耐用性需求。原則上可以選擇距離使用者最近的區域,但也應考慮 Cloud Run 服務所用其他 Google Cloud產品的位置。如果跨多個位置使用 Google Cloud 產品,可能會影響服務的延遲時間和費用。

Cloud Run 可在下列地區使用:

採用級別 1 定價

採用級別 2 定價

  • africa-south1 (約翰尼斯堡)
  • asia-east2 (香港)
  • asia-northeast3 (韓國首爾)
  • asia-southeast1 (新加坡)
  • asia-southeast2 (雅加達)
  • asia-south2 (印度德里)
  • australia-southeast1 (雪梨)
  • australia-southeast2 (墨爾本)
  • europe-central2 (波蘭華沙) 節能綠葉圖示 低二氧化碳
  • europe-west10 (柏林)
  • europe-west12 (杜林) 節能綠葉圖示 低二氧化碳排放
  • europe-west2 (英國倫敦) 節能綠葉圖示 低二氧化碳排放
  • europe-west3 (德國法蘭克福)
  • europe-west6 (瑞士蘇黎世) 節能綠葉圖示 低二氧化碳
  • me-central1 (杜哈)
  • me-central2 (達曼)
  • northamerica-northeast1 (蒙特婁) 節能綠葉圖示 低二氧化碳排放
  • northamerica-northeast2 (多倫多) 節能綠葉圖示 低二氧化碳排放
  • southamerica-east1 (巴西聖保羅) 節能綠葉圖示 低二氧化碳排放
  • southamerica-west1 (智利聖地牙哥) 節能綠葉圖示 低二氧化碳排放
  • us-west2 (洛杉磯)
  • us-west3 (鹽湖城)
  • us-west4 (拉斯維加斯)

如果您已建立 Cloud Run 服務,可以在Google Cloud 控制台的 Cloud Run 資訊主頁中查看區域。

清除所用資源

為避免系統向您的 Google Cloud 帳戶收取額外費用,請刪除您透過本快速入門導覽課程部署的所有資源。

刪除存放區

Cloud Run 只會依據工作執行時間收費。不過,您可能仍須支付在 Artifact Registry 儲存容器映像檔的費用。如要刪除 Artifact Registry 存放區,請按照 Artifact Registry 說明文件中的「刪除存放區」步驟操作。

刪除工作

只有在執行工作任務時,Cloud Run 工作才會產生費用。如要刪除 Cloud Run 工作,請按照下列任一步驟操作:

控制台

如要刪除工作:

  1. 前往 Google Cloud 控制台的「Cloud Run」頁面:

    前往 Cloud Run。

  2. 在工作清單中找到要刪除的工作,然後勾選核取方塊加以選取。

  3. 按一下「Delete」(刪除)。這會終止所有執行中的工作和容器執行個體。

gcloud

如要刪除工作,請執行下列指令:

gcloud run jobs delete JOB_NAME

將 JOB_NAME 替換為工作名稱。

刪除測試專案

刪除 Google Cloud 專案後,系統會停止對該專案中的所有資源計費。如要釋出專案中的所有 Google Cloud 資源,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

後續步驟