設定服務的沙箱

Cloud Run 沙箱提供快速、安全且隔離的環境,可在第二代環境的現有服務中執行不受信任的程式碼或工具,例如 AI 代理。沙箱經過高度最佳化,可降低延遲,並與容器在同一個執行個體中執行,共用分配到的 CPU 和記憶體。

本頁面說明如何在容器中設定沙箱。如要瞭解如何編寫程式碼,透過 CLI 與沙箱互動,請參閱「在 Cloud Run 中執行程式碼」。

事前準備

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

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

  3. 安裝並初始化 gcloud CLI。
  4. 第二代執行環境中部署 Cloud Run 服務

必要的角色

如要取得設定及部署 Cloud Run 服務所需的權限,請要求管理員授予您下列 IAM 角色:

如果您要從原始碼部署服務函式,則必須在專案和 Cloud Build 服務帳戶中獲得額外角色。

如需與 Cloud Run 相關聯的 IAM 角色和權限清單,請參閱「Cloud Run IAM 角色」和「Cloud Run IAM 權限」。如果 Cloud Run 服務與Google Cloud API (例如 Cloud 用戶端程式庫) 介接,請參閱服務身分設定指南。 如要進一步瞭解如何授予角色,請參閱「部署權限」和「管理存取權」。

啟用沙箱

變更任何設定都會建立新的修訂版本。除非您明確做出更新,改變這項設定,否則後續的修訂版本也會自動取得這個設定。

啟用沙箱後,Cloud Run 服務會部署在第二代執行環境中。如要在 Cloud Run 服務中啟用沙箱,請使用 Google Cloud CLI 或 YAML 設定:

gcloud

如要部署或更新服務,請指定 --sandbox-launcher 標記:

  • 如要部署新服務,請執行下列指令:

    gcloud beta run deploy SERVICE --image IMAGE_URL --sandbox-launcher

    更改下列內容:

    • SERVICE:Cloud Run 服務的名稱。
    • IMAGE_URL:容器映像檔的參照,例如 us-docker.pkg.dev/cloudrun/container/hello:latest。如果您使用 Artifact Registry,則必須先建立存放區 REPO_NAME。網址格式為 LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
  • 如要更新現有服務,請執行下列指令:

    gcloud beta run services update SERVICE --sandbox-launcher

YAML

  1. 如要建立新服務,請略過這個步驟。 如要更新現有服務,請下載其 YAML 設定

    gcloud run services describe SERVICE --format export > service.yaml
  2. 更新 YAML 檔案,在容器設定中加入設為 truesandboxLauncher 屬性:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: SERVICE
      annotations:
        run.googleapis.com/launch-stage: BETA
    spec:
      template:
        spec:
          containers:
          - name: CONTAINER
            image: IMAGE_URL
            sandboxLauncher: true
            port: 8080
    

    更改下列內容:

    • SERVICE:Cloud Run 服務的名稱。
    • CONTAINER:容器名稱。
    • IMAGE_URL:容器映像檔的參照,例如 us-docker.pkg.dev/cloudrun/container/hello:latest。如果您使用 Artifact Registry,則必須先建立存放區 REPO_NAME。網址格式為 LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
  3. 使用下列指令建立或更新服務:

    gcloud run services replace service.yaml

    如果存在 service.yaml 檔案,gcloud run services replace 指令預設會使用該檔案。

沙箱會共用分配給主機容器的 CPU 和記憶體。請確認主要容器的 CPU 和記憶體上限,可同時容納應用程式和您執行的任何有效沙箱。

停用沙箱

如要禁止在服務中啟動沙箱,請使用 Google Cloud CLI 或 YAML 設定:

gcloud

執行下列指令,使用 --no-sandbox-launcher 旗標更新服務:

gcloud beta run services update SERVICE --no-sandbox-launcher

SERVICE 改為您的服務名稱。

YAML

  1. 如要建立新服務,請略過這個步驟。 如要更新現有服務,請下載其 YAML 設定

    gcloud run services describe SERVICE --format export > service.yaml
  2. 更新 YAML 檔案,移除容器設定中的 sandboxLauncher 屬性:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: SERVICE
    spec:
      template:
        spec:
          containers:
          - name: CONTAINER
            image: IMAGE_URL
            port: 8080
    

    更改下列內容:

    • SERVICE:Cloud Run 服務的名稱。
    • CONTAINER:容器名稱。
    • IMAGE_URL:容器映像檔的參照,例如 us-docker.pkg.dev/cloudrun/container/hello:latest。如果您使用 Artifact Registry,則必須先建立存放區 REPO_NAME。網址格式為 LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
  3. 使用下列指令建立或更新服務:

    gcloud run services replace service.yaml

    如果存在 service.yaml 檔案,gcloud run services replace 指令預設會使用該檔案。

查看沙箱設定

如要查看 Cloud Run 服務目前的沙箱設定:

控制台

  1. 前往 Google Cloud 控制台的 Cloud Run「Services」(服務) 頁面:

    前往 Cloud Run

  2. 按一下服務,開啟「服務詳細資料」頁面。

  3. 按一下「修訂版本記錄」分頁標籤。

  4. 在「Containers」(容器) 分頁中,找到「Sandbox launcher」(沙箱啟動器) 設定,查看沙箱容器是否已啟用或停用。

gcloud

  1. 使用下列指令:

    gcloud beta run services describe SERVICE

    SERVICE 改為您的服務名稱。

  2. 如要確認已啟用沙箱化容器,請在傳回的設定中找出 sandboxLauncher: true 屬性。

如要從服務在沙箱中執行不受信任的程式碼,請參閱「在 Cloud Run 中執行程式碼」。