为作业配置沙盒

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 jobs create JOB --image IMAGE_URL --sandbox-launcher

    替换以下内容:

    • JOB:Cloud Run 作业的名称
    • IMAGE_URL:对容器映像的引用,例如 us-docker.pkg.dev/cloudrun/container/job:latest
  • 如需更新现有作业,请运行以下命令:

    gcloud beta run jobs update JOB --sandbox-launcher

YAML

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

    gcloud run jobs describe JOB_NAME --format export > job.yaml
  2. 更新 YAML 文件,以在容器配置中添加设置为 truesandboxLauncher 属性:

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

    替换以下内容:

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

    gcloud run jobs replace job.yaml

    gcloud run jobs replace 命令默认使用 job.yaml 文件(如果存在)。

沙盒会共享分配给宿主容器的 CPU 和内存。请确保主容器的 CPU 和内存限制能够同时满足应用和任何正在运行的有效沙盒的需求。

停用沙盒

如需禁止在作业中启动沙盒,请使用 Google Cloud CLI 或 YAML 配置:

gcloud

运行以下命令,使用 --no-sandbox-launcher 标志更新作业:

gcloud beta run jobs update JOB --no-sandbox-launcher

JOB 替换为作业的名称。

YAML

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

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

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

    替换以下内容:

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

    gcloud run jobs replace job.yaml

    gcloud run jobs replace 命令默认使用 job.yaml 文件(如果存在)。

查看沙盒设置

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

控制台

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

    转到 Cloud Run 作业

  2. 点击相应作业以打开作业详情页面。

  3. 历史记录标签页中,点击列表中的已执行作业 ID。

  4. 容器标签页中,找到沙盒启动器设置,查看沙盒容器是处于启用状态还是停用状态。

gcloud

  1. 使用以下命令:

    gcloud run jobs executions describe EXECUTION_NAME --format yaml

    EXECUTION_NAME 替换为作业执行的名称。

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

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