为工作器池配置沙盒

Cloud Run 沙盒提供快速、安全且隔离的环境,以便在工作器池中运行代理工作流、执行不受信任的有效负载,以及隔离处理事件。沙盒经过高度优化,可实现低延迟,并且与容器在同一实例中运行,共享其分配的 CPU 和内存。

本页介绍了如何在容器上配置沙盒。 如需详细了解如何编写代码以使用 CLI 与沙盒进行交互,请参阅 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. Verify that billing is enabled for your Google Cloud project.

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

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

  6. 安装并初始化 gcloud CLI。
  7. 部署 Cloud Run 工作器池

所需的角色

如需获得配置和部署 Cloud Run 工作器池所需的权限,请让您的管理员为您授予以下 IAM 角色:

如需查看与 Cloud Run 关联的 IAM 角色和权限的列表,请参阅 Cloud Run IAM 角色Cloud Run IAM 权限。 如果您的 Cloud Run 工作器池与Google Cloud API(例如 Cloud 客户端库)进行交互,请参阅服务身份配置指南。如需详细了解如何授予角色,请参阅 部署权限管理访问权限

启用沙盒

任何配置更改都会导致新修订版本的创建。后续修订版本也将自动采用此配置设置,除非您进行了明确更新。

如需在 Cloud Run 工作器池中启用沙盒,请使用 Google Cloud CLI 或 YAML 配置:

gcloud

如需部署或更新工作器池,请指定 --sandbox-launcher 标志:

  • 如需部署新的工作器池,请运行以下命令:

    gcloud beta run worker-pools deploy WORKER_POOL --image IMAGE_URL --sandbox-launcher

    替换以下内容:

    • WORKER_POOL:Cloud Run 工作器池的名称。
    • IMAGE_URL:对包含工作器池的容器映像的引用,例如 us-docker.pkg.dev/cloudrun/container/worker-pool:latest
  • 如需更新现有工作器池,请运行以下命令:

    gcloud beta run worker-pools update WORKER_POOL --sandbox-launcher

YAML

  1. 如果您要创建新的工作器池,请跳过此步骤。 如果您要更新现有工作器池,请下载其 YAML 配置

    gcloud run worker-pools describe WORKER_POOL --format export > worker-pool.yaml
  2. 更新 YAML 文件,以在容器配置中包含设置为 truesandboxLauncher 属性:

    apiVersion: run.googleapis.com/v1
    kind: WorkerPool
    metadata:
      name: WORKER_POOL
      annotations:
        run.googleapis.com/launch-stage: BETA
    spec:
      template:
        spec:
          containers:
          - name: CONTAINER
            image: IMAGE_URL
            sandboxLauncher: true
    

    替换以下内容:

    • WORKER_POOL:Cloud Run 工作器池的名称。
    • CONTAINER:容器的名称。
    • IMAGE_URL:对包含工作器池的容器映像的引用,例如 us-docker.pkg.dev/cloudrun/container/worker-pool:latest
  3. 使用以下命令创建或更新工作器池:

    gcloud run worker-pools replace worker-pool.yaml

    如果存在 worker-pool.yaml 文件,gcloud run worker-pools replace 命令默认使用该文件。

沙盒共享分配给宿主容器的 CPU 和内存。请确保主容器的 CPU 和内存限制能够同时容纳您的应用和您运行的任何活跃沙盒。

停用沙盒

如需停用在工作器池中启动沙盒的功能,请使用 Google Cloud CLI 或 YAML 配置:

gcloud

使用 --no-sandbox-launcher 标志更新工作器池,方法是运行以下命令:

gcloud beta run worker-pools update WORKER_POOL --no-sandbox-launcher

WORKER_POOL 替换为工作器池的名称。

YAML

  1. 如果您要创建新的工作器池,请跳过此步骤。 如果您要更新现有工作器池,请下载其 YAML 配置

    gcloud run worker-pools describe WORKER_POOL --format export > worker-pool.yaml
  2. 更新 YAML 文件,以移除容器配置中的 sandboxLauncher 属性:

    apiVersion: run.googleapis.com/v1
    kind: WorkerPool
    metadata:
      name: WORKER_POOL
    spec:
      template:
        spec:
          containers:
          - name: CONTAINER
            image: IMAGE_URL
    

    替换以下内容:

    • WORKER_POOL:Cloud Run 工作器池的名称。
    • CONTAINER:容器的名称。
    • IMAGE_URL:对包含工作器池的容器映像的引用,例如 us-docker.pkg.dev/cloudrun/container/worker-pool:latest
  3. 使用以下命令创建或更新工作器池:

    gcloud run worker-pools replace worker-pool.yaml

    如果存在 worker-pool.yaml 文件,gcloud run worker-pools replace 命令默认使用该文件。

查看沙盒设置

如需查看 Cloud Run 工作器池的当前沙盒设置,请执行以下操作:

控制台

  1. 在 Google Cloud 控制台中,前往 Cloud Run 工作器池 页面:

    前往 Cloud Run 工作器池

  2. 点击相应工作器池以打开其工作器池详情 页面。

  3. 点击修订版本 标签页。

  4. 容器 标签页中,找到 Sandbox 启动器 设置,以查看沙盒容器是已启用还是已停用。

gcloud

  1. 使用以下命令:

    gcloud run worker-pools describe WORKER_POOL

    WORKER_POOL 替换为工作器池的名称。

  2. 如需验证沙盒容器是否已启用,请在返回的配置中找到 sandboxLauncher: true 属性。

如需在工作器池的沙盒中运行不受信任的代码,请参阅 Cloud Run 中的代码执行