创建并运行使用存储卷的作业

本文档介绍了如何创建和运行使用 一个或多个外部存储卷 外部存储选项包括新的或现有的 永久性磁盘、新的本地 SSD、现有的 Cloud Storage 存储分区 和现有的网络文件系统 (NFS),例如 Filestore 文件共享。

无论您是添加外部存储卷, 用于某个作业的 Compute Engine 虚拟机有一个启动磁盘,该磁盘提供存储空间 获取作业的操作系统 (OS) 映像和说明。 如需了解如何为作业配置启动磁盘,请参阅 请改为参阅虚拟机操作系统环境概览

准备工作

创建使用存储卷的作业

作业可以使用以下一种或多种类型的 存储卷更多信息 所有类型的存储卷和 差异和限制,请参阅 Compute Engine 虚拟机存储方案

您可以通过添加存储卷来允许作业使用每个存储卷 并指定其 装载路径 (mountPath) 运行容器如需了解如何创建使用存储卷的作业,请参阅 以下一个或多个部分:

使用永久性磁盘

使用永久性磁盘的作业存在以下限制:

  • 所有永久性磁盘:查看 适用于所有永久性磁盘的限制

  • 新的永久性磁盘与现有的永久性磁盘:作业中的每个永久性磁盘都可以 新建(在作业中定义和创建)或现有(已经 在项目中创建并在作业中指定)。要使用永久性磁盘 它需要 格式化并装载到 这些虚拟机必须与永久性磁盘位于同一位置。 批量装载作业中包含的任何永久性磁盘, 格式化任何新的永久性磁盘,但您必须格式化并卸载任何 您希望作业使用的现有永久性磁盘

    受支持的地理位置选项 格式选项装载选项 新永久性磁盘和现有永久性磁盘之间会有所不同,具体说明如下 表:

    新的永久性磁盘 现有永久性磁盘
    格式选项

    永久性磁盘会自动格式化 ext4 文件系统

    您必须将永久性磁盘格式化 使用 ext4 文件系统 然后再将其用于工作

    装载选项

    支持所有选项

    支持除写入以外的所有选项。原因如下: 限制 多写入者模式

    您必须 分离永久性磁盘 从其挂接的任何虚拟机获取,然后再将其用于某项作业。

    位置选项

    您只能创建地区永久性磁盘

    您可以为作业选择任何位置。 磁盘是在项目运行所在的可用区中创建的。

    您可以选择地区和区域永久性磁盘


    您必须设置任务的位置(或者,如果指定,则仅设置任务的 允许的位置) 设置为仅包含作业所有 磁盘。例如,对于地区永久性磁盘,作业的 location 必须是磁盘的可用区;永久性磁盘 作业的位置必须是磁盘所在的区域,或者,如果 指定可用区时, 区域永久性磁盘所在的位置。

  • 实例模板:如果您想要 在创建此作业时使用虚拟机实例模板, 您必须在实例中为此作业挂接所有永久性磁盘 模板。否则,如果您不想使用实例模板,则必须 直接在作业定义中挂接任何永久性磁盘。

您可以使用 Google Cloud 控制台、gcloud CLI、Batch API Go、Java、Node.js、Python 或 C++。

控制台

以下示例使用 Google Cloud 控制台创建了一个作业 运行一个脚本,从 位于 us-central1-a 可用区中。示例脚本假定作业 有一个现有的地区永久性磁盘,该磁盘包含一个名为 example.txt(位于根目录下)。

可选:创建示例地区永久性磁盘

如果要创建可用区永久性磁盘 运行示例脚本,请在创建作业之前执行以下操作:

  1. 将名为 example-disk 的新空白永久性磁盘挂接到以下位置的 Linux 虚拟机: us-central1-a 可用区,然后在虚拟机上运行命令以格式化和装载 磁盘。有关说明,请参阅 向虚拟机添加永久性磁盘

    暂时不要断开与虚拟机的连接。

  2. 如需在永久性磁盘上创建 example.txt,请运行以下命令 命令:

    1. 要将当前工作目录更改为 永久性磁盘,请输入以下命令:

      cd VM_MOUNT_PATH
      

      VM_MOUNT_PATH 替换为 将永久性磁盘装载到此虚拟机的目录 例如 /mnt/disks/example-disk

    2. Enter 键。

    3. 如需创建并定义名为 example.txt 的文件,请执行以下操作: 输入以下命令:

      cat > example.txt
      
    4. Enter 键。

    5. 输入文件的内容。例如,输入 Hello world!

    6. 如需保存文件,请按 Ctrl+D(在 macOS 上,按 Command+D)。

    完成操作后,您可以断开与虚拟机的连接。

  3. 从虚拟机分离永久性磁盘。

    • 如果您不再需要该虚拟机,可以 删除虚拟机 会自动分离永久性磁盘

    • 否则,请分离永久性磁盘。有关说明,请参阅 分离和重新挂接启动磁盘 然后分离 example-disk 永久性磁盘,而不是 虚拟机的启动磁盘

创建一个使用现有地区永久性磁盘的作业

如需创建作业来使用现有可用区级永久性磁盘(通过 Google Cloud 控制台中,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往作业列表页面。

    转到作业列表

  2. 点击 创建。通过 此时会打开创建批量作业页面。在左侧窗格中, 作业详情页面处于选中状态。

  3. 配置作业详情页面:

    1. 可选:在作业名称字段中,自定义作业名称。

      例如,输入 example-disk-job

    2. 配置任务详情部分:

      1. 新建可运行对象窗口中,添加至少一个脚本或 容器。

        例如,要运行输出文件内容的 名为 example.txt 且位于根目录下的文件 永久性磁盘的快照,请执行以下操作:

        1. 选中脚本复选框。系统会显示一个文本框。

        2. 在文本框中,输入以下脚本:

          echo "Here is the content of the example.txt file in the persistent disk."
          cat MOUNT_PATH/example.txt
          

          MOUNT_PATH 替换为 您计划将永久性磁盘装载到 例如 /mnt/disks/example-disk

        3. 点击完成

      2. 任务计数字段中,为 此作业。

        例如,输入 1(默认值)。

      3. 并行数量字段中,输入 同时运行

        例如,输入 1(默认值)。

  4. 配置资源规范页面:

    1. 在左侧窗格中,点击资源规范。 系统随即会打开资源规范页面。

    2. 选择此作业的位置。 如需使用现有的地区永久性磁盘,作业的虚拟机必须 位于同一可用区

      1. 区域字段中,选择一个区域。

        例如,如需使用可用区级永久性磁盘, 选择 us-central1 (Iowa)(默认)。

      2. 可用区字段中,选择一个可用区。

        例如,选择 us-central1-a (Iowa)

  5. 配置其他配置页面:

    1. 在左侧窗格中,点击其他配置。 系统会打开其他配置页面。

    2. 对于您要装载到的每个现有地区永久性磁盘 执行以下操作:

      1. 存储卷部分中,点击添加新卷。 系统随即会显示新建卷窗口。

      2. 新建卷窗口中,执行以下操作:

        1. 卷类型部分中,选择 Persistent Disk(默认)。

        2. 磁盘列表中,选择一个 要装载到的现有可用区级永久性磁盘 此作业。磁盘必须位于 用作此作业的可用区

          例如,选择现有的可用区级永久性磁盘 该文件位于 us-central1-a 可用区,其中包含文件 example.txt

        3. 可选:如果要重命名此可用区级永久性磁盘, 执行以下操作:

          1. 选择自定义设备名称

          2. 设备名称字段中,为 您的磁盘。

        4. 装载路径字段中,输入装载路径 (MOUNT_PATH) 永久性磁盘:

          例如,输入以下内容:

          /mnt/disks/EXISTING_PERSISTENT_DISK_NAME
          

          替换 EXISTING_PERSISTENT_DISK_NAME 替换为磁盘的名称。如果您重命名了 地区永久性磁盘,请使用新名称。

          例如,将 EXISTING_PERSISTENT_DISK_NAMEexample-disk.

        5. 点击完成

  6. 可选:配置 此职位的其他字段

  7. 可选:如需查看作业配置,请在左侧窗格中 点击预览

  8. 点击创建

作业详情页面会显示您创建的作业。

gcloud

以下示例使用 gcloud CLI 创建了一个作业 挂接并装载现有的永久性磁盘和新的永久性磁盘。 该作业包含 3 个任务,每个任务都会运行一个脚本,以在新的 名为 output_task_TASK_INDEX.txt 的永久性磁盘 其中 TASK_INDEX 是各项任务的索引:0、1, 2.

要通过 gcloud CLI,请使用 gcloud batch jobs submit 命令。 在作业的 JSON 配置文件中,指定永久性磁盘的 instances 字段,并在 volumes 字段中装载永久性磁盘。

  1. 创建一个 JSON 文件。

    • 如果您没有为此作业使用实例模板, 创建一个包含以下内容的 JSON 文件:

      {
          "allocationPolicy": {
              "instances": [
                  {
                      "policy": {
                          "disks": [
                              {
                                  "deviceName": "EXISTING_PERSISTENT_DISK_NAME",
                                  "existingDisk": "projects/PROJECT_ID/EXISTING_PERSISTENT_DISK_LOCATION/disks/EXISTING_PERSISTENT_DISK_NAME"
                              },
                              {
                                  "newDisk": {
                                      "sizeGb": NEW_PERSISTENT_DISK_SIZE,
                                      "type": "NEW_PERSISTENT_DISK_TYPE"
                                  },
                                  "deviceName": "NEW_PERSISTENT_DISK_NAME"
                              }
                          ]
                      }
                  }
              ],
              "location": {
                  "allowedLocations": [
                      "EXISTING_PERSISTENT_DISK_LOCATION"
                  ]
              }
          },
          "taskGroups": [
              {
                  "taskSpec": {
                      "runnables": [
                          {
                              "script": {
                                  "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/NEW_PERSISTENT_DISK_NAME/output_task_${BATCH_TASK_INDEX}.txt"
                              }
                          }
                      ],
                      "volumes": [
                          {
                              "deviceName": "NEW_PERSISTENT_DISK_NAME",
                              "mountPath": "/mnt/disks/NEW_PERSISTENT_DISK_NAME",
                              "mountOptions": "rw,async"
                          },
                          {
      
                              "deviceName": "EXISTING_PERSISTENT_DISK_NAME",
                              "mountPath": "/mnt/disks/EXISTING_PERSISTENT_DISK_NAME"
                          }
                      ]
                  },
                  "taskCount":3
              }
          ],
          "logsPolicy": {
              "destination": "CLOUD_LOGGING"
          }
      }
      

      替换以下内容:

      • PROJECT_ID项目 ID 项目名称
      • EXISTING_PERSISTENT_DISK_NAME:名称 现有永久性磁盘的快照
      • EXISTING_PERSISTENT_DISK_LOCATION: 现有永久性磁盘的位置对于每个现有可用区 则作业的位置必须是该磁盘所在的可用区;用于 每个现有的区域永久性磁盘,那么作业的位置必须 可以是磁盘的区域;如果指定了可用区,则为 可用区永久性磁盘所在的两个特定可用区 。如果您没有指定任何现有的 您可以选择任何位置详细了解 allowedLocations 字段
      • NEW_PERSISTENT_DISK_SIZE: 新永久性磁盘(以 GB 为单位)。通过 允许的尺寸 取决于永久性磁盘的类型,但最小值通常为 10 GB (10),最大值通常为 64 TB (64000)。
      • NEW_PERSISTENT_DISK_TYPE磁盘类型 (即 pd-standardpd-balancedpd-ssdpd-extreme。非启动磁盘的默认磁盘类型 永久性磁盘为 pd-standard
      • NEW_PERSISTENT_DISK_NAME:名称 新的永久性磁盘的快照
    • 如果您 为此作业使用虚拟机实例模板 按照上文所述创建 JSON 文件,但需要将 instances 个字段 替换为以下内容:

      "instances": [
          {
              "instanceTemplate": "INSTANCE_TEMPLATE_NAME"
          }
      ],
      

      其中 INSTANCE_TEMPLATE_NAME 是名称 此作业的实例模板的映像对于使用 永久性磁盘,此实例模板必须定义并挂接 作业使用的永久性磁盘在本示例中 该模板必须定义并挂接名为 NEW_PERSISTENT_DISK_NAME和 并将名称为 EXISTING_PERSISTENT_DISK_NAME.

  2. 运行以下命令:

    gcloud batch jobs submit JOB_NAME \
      --location LOCATION \
      --config JSON_CONFIGURATION_FILE
    

    替换以下内容:

    • JOB_NAME:作业的名称。

    • LOCATION位置 作业的组成部分。

    • JSON_CONFIGURATION_FILE:JSON 路径 该文件包含作业的配置详情。

API

以下示例使用 Batch API 创建作业 用于挂接和装载现有的永久性磁盘和 磁盘。该作业包含 3 个任务,每个任务都会运行一个脚本,以在新的 名为“存储分区”的 output_task_TASK_INDEX.txt,其中 TASK_INDEX 是每个任务的索引:0、1 和 2。

要通过 批处理 API,请使用 jobs.create 方法。 在请求中指定永久性磁盘的 instances 字段,并在 volumes 字段中装载永久性磁盘。

  • 如果您未对此作业使用实例模板,请执行以下操作: 请求:

    POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME
    
    {
        "allocationPolicy": {
            "instances": [
                {
                    "policy": {
                        "disks": [
                            {
                                "deviceName": "EXISTING_PERSISTENT_DISK_NAME",
                                "existingDisk": "projects/PROJECT_ID/EXISTING_PERSISTENT_DISK_LOCATION/disks/EXISTING_PERSISTENT_DISK_NAME"
                            },
                            {
                                "newDisk": {
                                    "sizeGb": NEW_PERSISTENT_DISK_SIZE,
                                    "type": "NEW_PERSISTENT_DISK_TYPE"
                                },
                                "deviceName": "NEW_PERSISTENT_DISK_NAME"
                            }
                        ]
                    }
                }
            ],
            "location": {
                "allowedLocations": [
                    "EXISTING_PERSISTENT_DISK_LOCATION"
                ]
            }
        },
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/NEW_PERSISTENT_DISK_NAME/output_task_${BATCH_TASK_INDEX}.txt"
                            }
                        }
                    ],
                    "volumes": [
                        {
                            "deviceName": "NEW_PERSISTENT_DISK_NAME",
                            "mountPath": "/mnt/disks/NEW_PERSISTENT_DISK_NAME",
                            "mountOptions": "rw,async"
                        },
                        {
    
                            "deviceName": "EXISTING_PERSISTENT_DISK_NAME",
                            "mountPath": "/mnt/disks/EXISTING_PERSISTENT_DISK_NAME"
                        }
                    ]
                },
                "taskCount":3
            }
        ],
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    替换以下内容:

    • PROJECT_ID项目 ID 项目名称
    • LOCATION位置 作业的组成部分。
    • JOB_NAME:作业的名称。
    • EXISTING_PERSISTENT_DISK_NAME:名称 现有永久性磁盘的快照
    • EXISTING_PERSISTENT_DISK_LOCATION: 现有永久性磁盘的位置对于每个现有可用区 则作业的位置必须是该磁盘所在的可用区;用于 每个现有的区域永久性磁盘,那么作业的位置必须 可以是磁盘的区域;如果指定了可用区,则为 可用区永久性磁盘所在的两个特定可用区 。如果您没有指定任何现有的 您可以选择任何位置详细了解 allowedLocations 字段
    • NEW_PERSISTENT_DISK_SIZE: 新永久性磁盘(以 GB 为单位)。通过 允许的尺寸 取决于永久性磁盘的类型,但最小值通常为 10 GB (10),最大值通常为 64 TB (64000)。
    • NEW_PERSISTENT_DISK_TYPE磁盘类型 (即 pd-standardpd-balancedpd-ssdpd-extreme。非启动磁盘的默认磁盘类型 永久性磁盘为 pd-standard
    • NEW_PERSISTENT_DISK_NAME:名称 新的永久性磁盘的快照
  • 如果您 为此作业使用虚拟机实例模板 按照上文所述创建 JSON 文件,但需要将 instances 个字段 替换为以下内容:

    "instances": [
        {
            "instanceTemplate": "INSTANCE_TEMPLATE_NAME"
        }
    ],
    ...
    

    其中 INSTANCE_TEMPLATE_NAME 是名称 此作业的实例模板的映像对于使用 永久性磁盘,此实例模板必须定义并挂接 作业使用的永久性磁盘在本示例中 该模板必须定义并挂接名为 NEW_PERSISTENT_DISK_NAME和 并将名称为 EXISTING_PERSISTENT_DISK_NAME.

Go

创建使用新作业或现有作业的 Batch 作业 永久性磁盘 Go 版 Cloud 客户端库,请使用 CreateJob 函数 包括:

如需查看类似用例的代码示例,请参阅 使用 Cloud Storage 存储桶

Java

创建使用新作业或现有作业的 Batch 作业 永久性磁盘 Java 版 Cloud 客户端库,请使用 CreateJobRequest 包括:

如需查看类似用例的代码示例,请参阅 使用 Cloud Storage 存储桶

Node.js

创建使用新作业或现有作业的 Batch 作业 永久性磁盘 Node.js 版 Cloud 客户端库,请使用 createJob 方法 包括:

如需查看类似用例的代码示例,请参阅 使用 Cloud Storage 存储桶

Python

创建使用新作业或现有作业的 Batch 作业 永久性磁盘 Python 版 Cloud 客户端库,请使用 CreateJob 函数 包括:

  • 如需将永久性磁盘挂接到虚拟机,以便执行某项作业, 包含以下其中一项: <ph type="x-smartling-placeholder">
  • 要将永久性磁盘装载到作业中,请使用 Volume 具有 device_name 属性 和 mount_path 属性。对于新的永久性磁盘,还可以使用 mount_options 属性以启用写入。

如需查看类似用例的代码示例,请参阅 使用 Cloud Storage 存储桶

C++

创建使用新作业或现有作业的 Batch 作业 永久性磁盘 C++ 版 Cloud 客户端库,请使用 CreateJob 函数 包括:

  • 如需将永久性磁盘挂接到虚拟机,以便执行某项作业, 包含以下其中一项: <ph type="x-smartling-placeholder">
      </ph>
    • 如果您没有为此作业使用虚拟机实例模板, 请使用 set_remote_path 方法。
    • 如果要为此作业使用虚拟机实例模板,请使用 set_instance_template 方法结合使用。
  • 如需将永久性磁盘装载到作业,请使用 volumes 字段和 deviceNamemountPath 字段。对于新的永久性磁盘 使用 mountOptions 字段启用写入。

如需查看类似用例的代码示例,请参阅 使用 Cloud Storage 存储桶

使用本地 SSD

使用本地 SSD 的作业具有以下限制:

您可以使用 gcloud CLI 或 Batch API。 以下示例介绍了如何创建作业,该作业可创建、附加和 会装载一个本地 SSD此作业还包含 3 项任务 让每个虚拟机运行一个脚本,在本地固态硬盘 (SSD) 中创建一个名为 output_task_TASK_INDEX.txt,其中 TASK_INDEX 是每个任务的索引: 012

gcloud

如需创建使用本地 SSD 的作业,请使用 gcloud CLI,请使用 gcloud batch jobs submit 命令。 在作业的 JSON 配置文件中,创建并挂接本地 SSD instances 字段,并在 volumes 字段中装载本地 SSD。

  1. 创建一个 JSON 文件。

    • 如果您没有为此作业使用实例模板, 创建一个包含以下内容的 JSON 文件:

      {
          "allocationPolicy": {
              "instances": [
                  {
                      "policy": {
                          "machineType": MACHINE_TYPE,
                          "disks": [
                              {
                                  "newDisk": {
                                      "sizeGb": LOCAL_SSD_SIZE,
                                      "type": "local-ssd"
                                  },
                                  "deviceName": "LOCAL_SSD_NAME"
                              }
                          ]
                      }
                  }
              ]
          },
          "taskGroups": [
              {
                  "taskSpec": {
                      "runnables": [
                          {
                              "script": {
                                  "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/LOCAL_SSD_NAME/output_task_${BATCH_TASK_INDEX}.txt"
                              }
                          }
                      ],
                      "volumes": [
                          {
                              "deviceName": "LOCAL_SSD_NAME",
                              "mountPath": "/mnt/disks/LOCAL_SSD_NAME",
                              "mountOptions": "rw,async"
                          }
                      ]
                  },
                  "taskCount":3
              }
          ],
          "logsPolicy": {
              "destination": "CLOUD_LOGGING"
          }
      }
      

      替换以下内容:

      • MACHINE_TYPE: 机器类型可以是 预定义自定义、 虚拟机数量通过 允许的本地 SSD 数量 取决于作业虚拟机的机器类型。
      • LOCAL_SSD_NAME:名称 为此作业创建的本地 SSD 的位置。
      • LOCAL_SSD_SIZE: 所有本地 SSD 以 GB 为单位。每个本地 SSD 为 375 GB, 此值必须是 375 GB 的倍数。对于 例如,对于 2 个本地 SSD,请将此值设置为 750 GB。
    • 如果您 为此作业使用虚拟机实例模板 按照上文所述创建 JSON 文件,但需要将 instances 个字段 替换为以下内容:

      "instances": [
          {
              "instanceTemplate": "INSTANCE_TEMPLATE_NAME"
          }
      ],
      

      其中 INSTANCE_TEMPLATE_NAME 是名称 此作业的实例模板的映像对于使用 此实例模板必须定义并挂接 您希望作业使用的本地 SSD。在本示例中 该模板必须定义并挂接名为 LOCAL_SSD_NAME.

  2. 运行以下命令:

    gcloud batch jobs submit JOB_NAME \
      --location LOCATION \
      --config JSON_CONFIGURATION_FILE
    

    替换以下内容:

    • JOB_NAME:作业的名称。
    • LOCATION位置 作业的组成部分。
    • JSON_CONFIGURATION_FILE:JSON 路径 该文件包含作业的配置详情。

API

如需创建使用本地 SSD 的作业,请使用 批处理 API,请使用 jobs.create 方法。 在请求中,创建本地 SSD 并将其挂接到 instances 字段,并在 volumes 字段中装载本地 SSD。

  • 如果您未对此作业使用实例模板,请执行以下操作: 请求:

    POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME
    
    {
        "allocationPolicy": {
            "instances": [
                {
                    "policy": {
                        "machineType": MACHINE_TYPE,
                        "disks": [
                            {
                                "newDisk": {
                                    "sizeGb": LOCAL_SSD_SIZE,
                                    "type": "local-ssd"
                                },
                                "deviceName": "LOCAL_SSD_NAME"
                            }
                        ]
                    }
                }
            ]
        },
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/LOCAL_SSD_NAME/output_task_${BATCH_TASK_INDEX}.txt"
                            }
                        }
                    ],
                    "volumes": [
                        {
                            "deviceName": "LOCAL_SSD_NAME",
                            "mountPath": "/mnt/disks/LOCAL_SSD_NAME",
                            "mountOptions": "rw,async"
                        }
                    ]
                },
                "taskCount":3
            }
        ],
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    替换以下内容:

    • PROJECT_ID项目 ID 项目名称
    • LOCATION位置 作业的组成部分。
    • JOB_NAME:作业的名称。
    • MACHINE_TYPE: 机器类型可以是 预定义自定义、 虚拟机数量通过 允许的本地 SSD 数量 取决于作业虚拟机的机器类型。
    • LOCAL_SSD_NAME:名称 为此作业创建的本地 SSD 的位置。
    • LOCAL_SSD_SIZE: 所有本地 SSD 以 GB 为单位。每个本地 SSD 为 375 GB, 此值必须是 375 GB 的倍数。对于 例如,对于 2 个本地 SSD,请将此值设置为 750 GB。
  • 如果您 为此作业使用虚拟机实例模板 按照上文所述创建 JSON 文件,但需要将 instances 个字段 替换为以下内容:

    "instances": [
        {
            "instanceTemplate": "INSTANCE_TEMPLATE_NAME"
        }
    ],
    ...
    

    其中 INSTANCE_TEMPLATE_NAME 是名称 此作业的实例模板的映像对于使用 此实例模板必须定义并挂接 您希望作业使用的本地 SSD。在本示例中 该模板必须定义并挂接名为 LOCAL_SSD_NAME.

使用 Cloud Storage 存储桶

如需创建使用现有 Cloud Storage 存储桶的作业,请执行以下操作: 选择以下方法之一:

  • 推荐做法:指定 创建存储桶,如本部分所示。在作业运行时 该存储桶会使用 Cloud Storage FUSE
  • 使用可以直接访问 使用 gsutil 命令行工具创建 Cloud Storage 存储桶,或 适用于 Cloud Storage API 的客户端库。要了解如何访问 Cloud Storage 存储桶,请参阅 Compute Engine 文档,了解 对 Cloud Storage 存储分区执行数据读写操作

在创建使用存储桶的作业之前,请先创建一个存储桶,或指定 现有存储桶如需了解详情,请参阅 创建存储分区列出存储分区

您可以使用 Google Cloud 控制台、gcloud CLI、Batch API、Go、 Java、Node.js、Python 或 C++。

以下示例介绍了如何创建将 Cloud Storage 存储桶。该作业还包含 3 个任务,每个 创建一个脚本,用于在名为 output_task_TASK_INDEX.txt 其中,TASK_INDEX 是每个任务的索引: 012

控制台

如需创建使用 Cloud Storage 存储桶的作业,您需要 Google Cloud 控制台中,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往作业列表页面。

    转到作业列表

  2. 点击 创建。通过 此时会打开创建批量作业页面。在左侧窗格中, 作业详情页面处于选中状态。

  3. 配置作业详情页面:

    1. 可选:在作业名称字段中,自定义作业名称。

      例如,输入 example-bucket-job

    2. 配置任务详情部分:

      1. 新建可运行对象窗口中,添加至少一个脚本或 容器。

        例如,执行以下操作:

        1. 选中脚本复选框。系统会显示一个文本框。

        2. 在文本框中,输入以下脚本:

          echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt
          

          MOUNT_PATH 替换为 装载路径,此作业的可运行对象用来访问 现有的 Cloud Storage 存储桶路径必须以 “/mnt/disks/”,后跟一个目录或路径 。例如,如果您想使用 存储桶(目录为 my-bucket),请将 /mnt/disks/my-bucket 的装载路径。

        3. 点击完成

      2. 任务计数字段中,为 此作业。

        例如,输入 3

      3. 并行数量字段中,输入 同时运行

        例如,输入 1(默认值)。

  4. 配置其他配置页面:

    1. 在左侧窗格中,点击其他配置。 系统会打开其他配置页面。

    2. 对于您要装载到的每个 Cloud Storage 存储桶 执行以下操作:

      1. 存储卷部分中,点击添加新卷。 系统随即会显示新建卷窗口。

      2. 新建卷窗口中,执行以下操作:

        1. 卷类型部分,选择 Cloud Storage 存储桶

        2. 存储分区名称字段中,输入 现有存储桶

          例如,输入您在此 它们是可以运行的

        3. 装载路径字段中,输入 存储桶 (MOUNT_PATH) 。

        4. 点击完成

  5. 可选:配置 此职位的其他字段

  6. 可选:如需查看作业配置,请在左侧窗格中 点击预览

  7. 点击创建

作业详情页面会显示您创建的作业。

gcloud

如需创建使用 Cloud Storage 存储桶的作业,您需要 gcloud CLI,请使用 gcloud batch jobs submit 命令。 在作业的 JSON 配置文件中,将存储桶装载到 volumes 字段。

例如,如需创建一个将文件输出到 Cloud Storage 的作业,请执行以下操作:

  1. 使用以下内容创建 JSON 文件:

    {
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt"
                            }
                        }
                    ],
                    "volumes": [
                        {
                            "gcs": {
                                "remotePath": "BUCKET_PATH"
                            },
                            "mountPath": "MOUNT_PATH"
                        }
                    ]
                },
                "taskCount": 3
            }
        ],
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    替换以下内容:

    • BUCKET_PATH:存储桶目录的路径 该名称必须以 存储桶。例如,对于名为 BUCKET_NAME,路径 BUCKET_NAME 表示根目录 存储桶的名称和路径 BUCKET_NAME/subdirectory 表示 subdirectory 子目录。
    • MOUNT_PATH:作业的装载路径 Runnable 用于访问此存储桶。路径必须以 /mnt/disks/,后跟您选择的目录或路径。对于 例如,如果您想使用名称为 my-bucket 时,请将装载路径设置为 /mnt/disks/my-bucket
  2. 运行以下命令:

    gcloud batch jobs submit JOB_NAME \
      --location LOCATION \
      --config JSON_CONFIGURATION_FILE
    

    替换以下内容:

    • JOB_NAME:作业的名称。
    • LOCATION位置 作业的组成部分。
    • JSON_CONFIGURATION_FILE:JSON 路径 该文件包含作业的配置详情。

API

如需创建使用 Cloud Storage 存储桶的作业,您需要 批处理 API,请使用 jobs.create 方法 并在 volumes 字段中装载该存储桶。

POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME

{
    "taskGroups": [
        {
            "taskSpec": {
                "runnables": [
                    {
                        "script": {
                            "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt"
                        }
                    }
                ],
                "volumes": [
                    {
                        "gcs": {
                            "remotePath": "BUCKET_PATH"
                        },
                        "mountPath": "MOUNT_PATH"
                    }
                ]
            },
            "taskCount": 3
        }
    ],
    "logsPolicy": {
            "destination": "CLOUD_LOGGING"
    }
}

替换以下内容:

  • PROJECT_ID项目 ID 项目名称
  • LOCATION位置 作业的组成部分。
  • JOB_NAME:作业的名称。
  • BUCKET_PATH:存储桶目录的路径 该名称必须以 存储桶例如,对于名为 BUCKET_NAME,路径 BUCKET_NAME 表示根 存储桶的目录和路径 BUCKET_NAME/subdirectory 表示 subdirectory 子目录。
  • MOUNT_PATH:作业的装载路径 Runnable 用于访问此存储桶。路径必须以 /mnt/disks/,后跟您选择的目录或路径。 例如,如果您希望以 my-bucket 目录中,请将装载路径设置为 /mnt/disks/my-bucket.

Go

Go

有关详情,请参阅 批处理 Go API 参考文档

如需向 Batch 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

import (
	"context"
	"fmt"
	"io"

	batch "cloud.google.com/go/batch/apiv1"
	batchpb "google.golang.org/genproto/googleapis/cloud/batch/v1"
	durationpb "google.golang.org/protobuf/types/known/durationpb"
)

// Creates and runs a job that executes the specified script
func createScriptJobWithBucket(w io.Writer, projectID, region, jobName, bucketName string) error {
	// projectID := "your_project_id"
	// region := "us-central1"
	// jobName := "some-job"
	// jobName := "some-bucket"

	ctx := context.Background()
	batchClient, err := batch.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %w", err)
	}
	defer batchClient.Close()

	// Define what will be done as part of the job.
	command := &batchpb.Runnable_Script_Text{
		Text: "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt",
	}

	// Specify the Google Cloud Storage bucket to mount
	volume := &batchpb.Volume{
		Source: &batchpb.Volume_Gcs{
			Gcs: &batchpb.GCS{
				RemotePath: bucketName,
			},
		},
		MountPath:    "/mnt/share",
		MountOptions: []string{},
	}

	// We can specify what resources are requested by each task.
	resources := &batchpb.ComputeResource{
		// CpuMilli is milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
		CpuMilli:  500,
		MemoryMib: 16,
	}

	taskSpec := &batchpb.TaskSpec{
		Runnables: []*batchpb.Runnable{{
			Executable: &batchpb.Runnable_Script_{
				Script: &batchpb.Runnable_Script{Command: command},
			},
		}},
		ComputeResource: resources,
		MaxRunDuration: &durationpb.Duration{
			Seconds: 3600,
		},
		MaxRetryCount: 2,
		Volumes:       []*batchpb.Volume{volume},
	}

	// Tasks are grouped inside a job using TaskGroups.
	taskGroups := []*batchpb.TaskGroup{
		{
			TaskCount: 4,
			TaskSpec:  taskSpec,
		},
	}

	// Policies are used to define on what kind of virtual machines the tasks will run on.
	// In this case, we tell the system to use "e2-standard-4" machine type.
	// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
	allocationPolicy := &batchpb.AllocationPolicy{
		Instances: []*batchpb.AllocationPolicy_InstancePolicyOrTemplate{{
			PolicyTemplate: &batchpb.AllocationPolicy_InstancePolicyOrTemplate_Policy{
				Policy: &batchpb.AllocationPolicy_InstancePolicy{
					MachineType: "e2-standard-4",
				},
			},
		}},
	}

	// We use Cloud Logging as it's an out of the box available option
	logsPolicy := &batchpb.LogsPolicy{
		Destination: batchpb.LogsPolicy_CLOUD_LOGGING,
	}

	jobLabels := map[string]string{"env": "testing", "type": "script"}

	// The job's parent is the region in which the job will run
	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, region)

	job := batchpb.Job{
		TaskGroups:       taskGroups,
		AllocationPolicy: allocationPolicy,
		Labels:           jobLabels,
		LogsPolicy:       logsPolicy,
	}

	req := &batchpb.CreateJobRequest{
		Parent: parent,
		JobId:  jobName,
		Job:    &job,
	}

	created_job, err := batchClient.CreateJob(ctx, req)
	if err != nil {
		return fmt.Errorf("unable to create job: %w", err)
	}

	fmt.Fprintf(w, "Job created: %v\n", created_job)

	return nil
}

Java

Java

有关详情,请参阅 批处理 Java API 参考文档

如需向 Batch 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.ComputeResource;
import com.google.cloud.batch.v1.CreateJobRequest;
import com.google.cloud.batch.v1.GCS;
import com.google.cloud.batch.v1.Job;
import com.google.cloud.batch.v1.LogsPolicy;
import com.google.cloud.batch.v1.LogsPolicy.Destination;
import com.google.cloud.batch.v1.Runnable;
import com.google.cloud.batch.v1.Runnable.Script;
import com.google.cloud.batch.v1.TaskGroup;
import com.google.cloud.batch.v1.TaskSpec;
import com.google.cloud.batch.v1.Volume;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateWithMountedBucket {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";

    // Name of the region you want to use to run the job. Regions that are
    // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
    String region = "europe-central2";

    // The name of the job that will be created.
    // It needs to be unique for each project and region pair.
    String jobName = "JOB_NAME";

    // Name of the bucket to be mounted for your Job.
    String bucketName = "BUCKET_NAME";

    createScriptJobWithBucket(projectId, region, jobName, bucketName);
  }

  // This method shows how to create a sample Batch Job that will run
  // a simple command on Cloud Compute instances.
  public static void createScriptJobWithBucket(String projectId, String region, String jobName,
      String bucketName)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the `batchServiceClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

      // Define what will be done as part of the job.
      Runnable runnable =
          Runnable.newBuilder()
              .setScript(
                  Script.newBuilder()
                      .setText(
                          "echo Hello world from task ${BATCH_TASK_INDEX}. >> "
                              + "/mnt/share/output_task_${BATCH_TASK_INDEX}.txt")
                      // You can also run a script from a file. Just remember, that needs to be a
                      // script that's already on the VM that will be running the job.
                      // Using setText() and setPath() is mutually exclusive.
                      // .setPath("/tmp/test.sh")
                      .build())
              .build();

      Volume volume = Volume.newBuilder()
          .setGcs(GCS.newBuilder()
              .setRemotePath(bucketName)
              .build())
          .setMountPath("/mnt/share")
          .build();

      // We can specify what resources are requested by each task.
      ComputeResource computeResource =
          ComputeResource.newBuilder()
              // In milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
              .setCpuMilli(500)
              // In MiB.
              .setMemoryMib(16)
              .build();

      TaskSpec task =
          TaskSpec.newBuilder()
              // Jobs can be divided into tasks. In this case, we have only one task.
              .addRunnables(runnable)
              .addVolumes(volume)
              .setComputeResource(computeResource)
              .setMaxRetryCount(2)
              .setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
              .build();

      // Tasks are grouped inside a job using TaskGroups.
      // Currently, it's possible to have only one task group.
      TaskGroup taskGroup = TaskGroup.newBuilder().setTaskCount(4).setTaskSpec(task).build();

      // Policies are used to define on what kind of virtual machines the tasks will run on.
      // In this case, we tell the system to use "e2-standard-4" machine type.
      // Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
      InstancePolicy instancePolicy =
          InstancePolicy.newBuilder().setMachineType("e2-standard-4").build();

      AllocationPolicy allocationPolicy =
          AllocationPolicy.newBuilder()
              .addInstances(InstancePolicyOrTemplate.newBuilder().setPolicy(instancePolicy).build())
              .build();

      Job job =
          Job.newBuilder()
              .addTaskGroups(taskGroup)
              .setAllocationPolicy(allocationPolicy)
              .putLabels("env", "testing")
              .putLabels("type", "script")
              .putLabels("mount", "bucket")
              // We use Cloud Logging as it's an out of the box available option.
              .setLogsPolicy(
                  LogsPolicy.newBuilder().setDestination(Destination.CLOUD_LOGGING).build())
              .build();

      CreateJobRequest createJobRequest =
          CreateJobRequest.newBuilder()
              // The job's parent is the region in which the job will run.
              .setParent(String.format("projects/%s/locations/%s", projectId, region))
              .setJob(job)
              .setJobId(jobName)
              .build();

      Job result =
          batchServiceClient
              .createJobCallable()
              .futureCall(createJobRequest)
              .get(5, TimeUnit.MINUTES);

      System.out.printf("Successfully created the job: %s", result.getName());
    }
  }
}

Node.js

Node.js

有关详情,请参阅 批处理 Node.js API 参考文档

如需向 Batch 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

/**
 * TODO(developer): Uncomment and replace these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
/**
 * The region you want to the job to run in. The regions that support Batch are listed here:
 * https://cloud.google.com/batch/docs/get-started#locations
 */
// const region = 'us-central-1';
/**
 * The name of the job that will be created.
 * It needs to be unique for each project and region pair.
 */
// const jobName = 'YOUR_JOB_NAME';
/**
 * The name of the bucket to be mounted.
 */
// const bucketName = 'YOUR_BUCKET_NAME';

// Imports the Batch library
const batchLib = require('@google-cloud/batch');
const batch = batchLib.protos.google.cloud.batch.v1;

// Instantiates a client
const batchClient = new batchLib.v1.BatchServiceClient();

// Define what will be done as part of the job.
const task = new batch.TaskSpec();
const runnable = new batch.Runnable();
runnable.script = new batch.Runnable.Script();
runnable.script.text =
  'echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt';
// You can also run a script from a file. Just remember, that needs to be a script that's
// already on the VM that will be running the job. Using runnable.script.text and runnable.script.path is mutually
// exclusive.
// runnable.script.path = '/tmp/test.sh'
task.runnables = [runnable];

const gcsBucket = new batch.GCS();
gcsBucket.remotePath = bucketName;
const gcsVolume = new batch.Volume();
gcsVolume.gcs = gcsBucket;
gcsVolume.mountPath = '/mnt/share';
task.volumes = [gcsVolume];

// We can specify what resources are requested by each task.
const resources = new batch.ComputeResource();
resources.cpuMilli = 2000; // in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
resources.memoryMib = 16;
task.computeResource = resources;

task.maxRetryCount = 2;
task.maxRunDuration = {seconds: 3600};

// Tasks are grouped inside a job using TaskGroups.
const group = new batch.TaskGroup();
group.taskCount = 4;
group.taskSpec = task;

// Policies are used to define on what kind of virtual machines the tasks will run on.
// In this case, we tell the system to use "e2-standard-4" machine type.
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
const allocationPolicy = new batch.AllocationPolicy();
const policy = new batch.AllocationPolicy.InstancePolicy();
policy.machineType = 'e2-standard-4';
const instances = new batch.AllocationPolicy.InstancePolicyOrTemplate();
instances.policy = policy;
allocationPolicy.instances = [instances];

const job = new batch.Job();
job.name = jobName;
job.taskGroups = [group];
job.allocationPolicy = allocationPolicy;
job.labels = {env: 'testing', type: 'script'};
// We use Cloud Logging as it's an option available out of the box
job.logsPolicy = new batch.LogsPolicy();
job.logsPolicy.destination = batch.LogsPolicy.Destination.CLOUD_LOGGING;

// The job's parent is the project and region in which the job will run
const parent = `projects/${projectId}/locations/${region}`;

async function callCreateJob() {
  // Construct request
  const request = {
    parent,
    jobId: jobName,
    job,
  };

  // Run request
  const response = await batchClient.createJob(request);
  console.log(response);
}

callCreateJob();

Python

Python

有关详情,请参阅 批处理 Python API 参考文档

如需向 Batch 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

from google.cloud import batch_v1


def create_script_job_with_bucket(
    project_id: str, region: str, job_name: str, bucket_name: str
) -> batch_v1.Job:
    """
    This method shows how to create a sample Batch Job that will run
    a simple command on Cloud Compute instances.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        region: name of the region you want to use to run the job. Regions that are
            available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
        job_name: the name of the job that will be created.
            It needs to be unique for each project and region pair.
        bucket_name: name of the bucket to be mounted for your Job.

    Returns:
        A job object representing the job created.
    """
    client = batch_v1.BatchServiceClient()

    # Define what will be done as part of the job.
    task = batch_v1.TaskSpec()
    runnable = batch_v1.Runnable()
    runnable.script = batch_v1.Runnable.Script()
    runnable.script.text = "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt"
    task.runnables = [runnable]

    gcs_bucket = batch_v1.GCS()
    gcs_bucket.remote_path = bucket_name
    gcs_volume = batch_v1.Volume()
    gcs_volume.gcs = gcs_bucket
    gcs_volume.mount_path = "/mnt/share"
    task.volumes = [gcs_volume]

    # We can specify what resources are requested by each task.
    resources = batch_v1.ComputeResource()
    resources.cpu_milli = 500  # in milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
    resources.memory_mib = 16
    task.compute_resource = resources

    task.max_retry_count = 2
    task.max_run_duration = "3600s"

    # Tasks are grouped inside a job using TaskGroups.
    # Currently, it's possible to have only one task group.
    group = batch_v1.TaskGroup()
    group.task_count = 4
    group.task_spec = task

    # Policies are used to define on what kind of virtual machines the tasks will run on.
    # In this case, we tell the system to use "e2-standard-4" machine type.
    # Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
    allocation_policy = batch_v1.AllocationPolicy()
    policy = batch_v1.AllocationPolicy.InstancePolicy()
    policy.machine_type = "e2-standard-4"
    instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
    instances.policy = policy
    allocation_policy.instances = [instances]

    job = batch_v1.Job()
    job.task_groups = [group]
    job.allocation_policy = allocation_policy
    job.labels = {"env": "testing", "type": "script", "mount": "bucket"}
    # We use Cloud Logging as it's an out of the box available option
    job.logs_policy = batch_v1.LogsPolicy()
    job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING

    create_request = batch_v1.CreateJobRequest()
    create_request.job = job
    create_request.job_id = job_name
    # The job's parent is the region in which the job will run
    create_request.parent = f"projects/{project_id}/locations/{region}"

    return client.create_job(create_request)

C++

C++

有关详情,请参阅 批处理 C++ API 参考文档

如需向 Batch 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

#include "google/cloud/batch/v1/batch_client.h"

  [](std::string const& project_id, std::string const& location_id,
     std::string const& job_id, std::string const& bucket_name) {
    // Initialize the request; start with the fields that depend on the sample
    // input.
    google::cloud::batch::v1::CreateJobRequest request;
    request.set_parent("projects/" + project_id + "/locations/" + location_id);
    request.set_job_id(job_id);
    // Most of the job description is fixed in this example; use a string to
    // initialize it, and then override the GCS remote path.
    auto constexpr kText = R"pb(
      task_groups {
        task_count: 4
        task_spec {
          compute_resource { cpu_milli: 500 memory_mib: 16 }
          max_retry_count: 2
          max_run_duration { seconds: 3600 }
          runnables {
            script {
              text: "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt"
            }
          }
          volumes { mount_path: "/mnt/share" }
        }
      }
      allocation_policy {
        instances {
          policy { machine_type: "e2-standard-4" provisioning_model: STANDARD }
        }
      }
      labels { key: "env" value: "testing" }
      labels { key: "type" value: "script" }
      logs_policy { destination: CLOUD_LOGGING }
    )pb";
    auto* job = request.mutable_job();
    if (!google::protobuf::TextFormat::ParseFromString(kText, job)) {
      throw std::runtime_error("Error parsing Job description");
    }
    job->mutable_task_groups(0)
        ->mutable_task_spec()
        ->mutable_volumes(0)
        ->mutable_gcs()
        ->set_remote_path(bucket_name);
    // Create a client and issue the request.
    auto client = google::cloud::batch_v1::BatchServiceClient(
        google::cloud::batch_v1::MakeBatchServiceConnection());
    auto response = client.CreateJob(request);
    if (!response) throw std::move(response).status();
    std::cout << "Job : " << response->DebugString() << "\n";
  }

使用网络文件系统

您可以创建使用现有网络文件系统 (NFS) 的作业, 例如 Filestore 文件共享, 使用 Google Cloud 控制台、gcloud CLI 或 Batch API。

在创建使用 NFS 的作业之前,请确保您的 网络的防火墙已正确配置为 以及 NFS 和作业的虚拟机如需了解详情,请参阅 为 Filestore 配置防火墙规则

以下示例介绍了如何创建作业,该作业指定 会装载一个 NFS该职位还有 3 个 每个任务都运行一个脚本,在名为 output_task_TASK_INDEX.txt 其中,TASK_INDEX 是每个任务的索引: 012

控制台

如需使用 Google Cloud 控制台创建使用 NFS 的作业,请执行以下操作: 执行以下操作:

  1. 在 Google Cloud 控制台中,前往作业列表页面。

    转到作业列表

  2. 点击 创建。通过 此时会打开创建批量作业页面。在左侧窗格中, 作业详情页面处于选中状态。

  3. 配置作业详情页面:

    1. 可选:在作业名称字段中,自定义作业名称。

      例如,输入 example-nfs-job

    2. 配置任务详情部分:

      1. 新建可运行对象窗口中,添加至少一个脚本或 容器。

        例如,执行以下操作:

        1. 选中脚本复选框。系统会显示一个文本框。

        2. 在文本框中,输入以下脚本:

          echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt
          

          MOUNT_PATH 替换为 作业的可运行用于访问此 NFS 的装载路径。 路径必须以 /mnt/disks/ 开头,后跟 您选择的目录或路径。例如,如果您 并使用名为 my-nfs 时,请将装载路径设置为 /mnt/disks/my-nfs

        3. 点击完成

      2. 任务计数字段中,为 此作业。

        例如,输入 3

      3. 并行数量字段中,输入 同时运行

        例如,输入 1(默认值)。

  4. 配置其他配置页面:

    1. 在左侧窗格中,点击其他配置。 系统会打开其他配置页面。

    2. 对于您要装载到的每个 Cloud Storage 存储桶 执行以下操作:

      1. 存储卷部分中,点击添加新卷。 系统随即会显示新建卷窗口。

      2. 新建卷窗口中,执行以下操作:

        1. 卷类型部分中,执行以下操作: 选择网络文件系统

        2. 文件服务器字段中,输入 您在此作业中指定的 NFS 的 。

          例如,如果您的 NFS 是 Filestore 文件共享 然后指定 Filestore 的 IP 地址 您可以 描述 Filestore 实例

        3. 远程路径字段中,输入可访问的路径 您在上一步中指定的 NFS。

          NFS 目录的路径必须以 / 开头 然后是 NFS 的根目录。

        4. 装载路径字段中,输入 NFS 的装载路径 (MOUNT_PATH) 是在 上一步。

    3. 点击完成

  5. 可选:配置 此职位的其他字段

  6. 可选:如需查看作业配置,请在左侧窗格中 点击预览

  7. 点击创建

作业详情页面会显示您创建的作业。

gcloud

如需创建使用 gcloud CLI,请使用 gcloud batch jobs submit 命令。 在作业的 JSON 配置文件中,将 NFS 装载到 volumes 字段。

  1. 使用以下内容创建 JSON 文件:

    {
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt"
                            }
                        }
                    ],
                    "volumes": [
                        {
                            "nfs": {
                                "server": "NFS_IP_ADDRESS",
                                "remotePath": "NFS_PATH"
                            },
                            "mountPath": "MOUNT_PATH"
                        }
                    ]
                },
                "taskCount": 3
            }
        ],
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    替换以下内容:

    • NFS_IP_ADDRESS: NFS 的 IP 地址。例如,如果您的 NFS 是 Filestore 文件共享 指定 Filestore 的 IP 地址 您可以 描述 Filestore 实例
    • NFS_PATH:NFS 目录的路径 该名称,且必须以 / 开头 然后是 NFS 的根目录。例如,对于 Filestore 文件共享名为 FILE_SHARE_NAME,路径 /FILE_SHARE_NAME 表示根 文件共享的目录和路径 /FILE_SHARE_NAME/subdirectory 表示 subdirectory 子目录。
    • MOUNT_PATH:作业的装载路径 runnables 用于访问此 NFS路径必须以 /mnt/disks/,后跟您选择的目录或路径。 例如,如果您想用 目录中,请将装载路径设置为 /mnt/disks/my-nfsmy-nfs
  2. 运行以下命令:

    gcloud batch jobs submit JOB_NAME \
      --location LOCATION \
      --config JSON_CONFIGURATION_FILE
    

    替换以下内容:

    • JOB_NAME:作业的名称。
    • LOCATION位置 作业的组成部分。
    • JSON_CONFIGURATION_FILE:JSON 路径 该文件包含作业的配置详情。

API

如需创建使用 批处理 API,请使用 jobs.create 方法 并在 volumes 字段中装载 NFS。

POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME

   {
    "taskGroups": [
        {
            "taskSpec": {
                "runnables": [
                    {
                        "script": {
                            "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt"
                        }
                    }
                ],
                "volumes": [
                    {
                        "nfs": {
                            "server": "NFS_IP_ADDRESS",
                            "remotePath": "NFS_PATH"
                        },
                        "mountPath": "MOUNT_PATH"
                    }
                ]
            },
            "taskCount": 3
        }
    ],
    "logsPolicy": {
        "destination": "CLOUD_LOGGING"
    }
}

替换以下内容:

  • PROJECT_ID项目 ID 项目名称
  • LOCATION位置 作业的组成部分。
  • JOB_NAME:作业的名称。
  • NFS_IP_ADDRESS: 网络文件系统的 IP 地址。例如,如果您的 NFS 是 Filestore 文件共享 指定 Filestore 的 IP 地址 您可以 描述 Filestore 实例
  • NFS_PATH:NFS 目录的路径 该名称,且必须以 / 开头 然后是 NFS 的根目录。例如,对于 Filestore 文件共享名为 FILE_SHARE_NAME,路径 /FILE_SHARE_NAME 表示根 文件共享的目录和路径 /FILE_SHARE_NAME/subdirectory 代表一个子目录。
  • MOUNT_PATH:作业的装载路径 runnables 用于访问此 NFS路径必须以 /mnt/disks/,后跟您选择的目录或路径。 例如,如果您想用 目录中,请将装载路径设置为 /mnt/disks/my-nfsmy-nfs

后续步骤