

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用创建作业定义 EcsProperties
<a name="multi-container-jobs"></a>

使用 AWS Batch 作业定义后 [https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html)，可以在单独的容器中对硬件、传感器、3D 环境和其他模拟进行建模。您可以使用此功能从逻辑上整理工作负载组件，并将它们与主应用程序分开。此功能可在亚马逊弹性容器服务 (Amaz AWS Batch on ECS)、亚马逊 Elastic Kubernetes Service (Amazon EKS) 和 AWS Fargate

## `ContainerProperties` 与 `EcsProperties` 作业定义对比
<a name="containerpropertions-vs-ecsproperties"></a>

您可以根据应用场景的规定选择使用 [https://docs.aws.amazon.com/batch/latest/APIReference/API_ContainerProperties.html](https://docs.aws.amazon.com/batch/latest/APIReference/API_ContainerProperties.html) 或 [https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html) 作业定义。简而言之，使用运行 AWS Batch 作业与使用`EcsProperties`运行作业类似`ContainerProperties`。

仍然支持使用 `ContainerProperties` 的旧版作业定义结构。如果您当前有使用此结构的工作流，则可以继续运行这些工作流。

主要区别在于，在作业定义中添加了一个新对象以适应基于 `EcsProperties` 的定义。

例如，在 Amazon ECS 和 Fargate 上使用 `ContainerProperties` 的作业定义具有以下结构：

```
{
   "containerProperties": {
     ...
     "image": "my_ecr_image1",
     ...
  },
...
}
```

在 Amazon ECS 和 Fargate 上使用 `EcsProperties` 的作业定义具有以下结构：

```
{
  "ecsProperties": {
    "taskProperties": [{
      "containers": [
        { 
          ...
          "image": "my_ecr_image1",
          ...
        },
        { 
          ...
          "image": "my_ecr_image2",
          ...
        },
```

## 的一般更改 AWS Batch APIs
<a name="multi-container-general"></a>

以下内容进一步概述了使用 `ContainerProperties` 和 `EcsProperties` API 数据类型时的一些主要区别：
+ `ContainerProperties` 内所使用的许多参数出现在 `TaskContainerProperties` 中。一些示例包括 `command`、`image`、`privileged`、`secrets` 和 `users`。它们都可以在里面找到[TaskContainerProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html)。
+ 有些 `TaskContainerProperties` 参数在旧版结构中没有功能等效项。一些示例包括 `dependsOn`、`essential`、`name`、`ipcMode` 和 `pidMode`。有关更多信息，请参阅[EcsTaskDetails](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskDetails.html)和[TaskContainerProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html)。

  此外，有些 `ContainerProperties` 参数在 `EcsProperties` 结构中没有等效项或应用。在 [https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html#Batch-Type-EcsProperties-taskProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html#Batch-Type-EcsProperties-taskProperties) 中，`container` 已替换为 `containers`，因此新对象最多可以接受十个元素。[https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#Batch-RegisterJobDefinition-request-containerProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#Batch-RegisterJobDefinition-request-containerProperties)
+ `taskRoleArn` 在功能上与 `jobRoleArn` 等效。有关更多信息，请参阅[EcsTaskProperties:taskRoleArn](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskProperties.html)和[ContainerProperties:jobRoleArn](https://docs.aws.amazon.com/batch/latest/APIReference/API_ContainerProperties.html)。
+ 您可以在 `EcsProperties` 结构中包含一（1）到十（10）个容器。[更多信息请参阅:容器EcsTaskProperties。](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskProperties.html)
+ `taskProperties` 和 instanceTypes 对象是数组，但目前只接受一个元素。[https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html)

## Amazon ECS 的多容器作业定义
<a name="multi-container-ecs-updates"></a>

为了适应 Amazon ECS 的多容器结构，其中某些 API 数据类型有所不同。例如，
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#Batch-RegisterJobDefinition-request-ecsProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#Batch-RegisterJobDefinition-request-ecsProperties) 与单容器定义中的 `containerProperties` 级别相同。有关更多信息，请参阅 [EcsProperties API 参考指南](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html) 中的 *AWS Batch *。
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html#Batch-Type-EcsProperties-taskProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html#Batch-Type-EcsProperties-taskProperties) 包含为 Amazon ECS 任务定义的属性。有关更多信息，请参阅 [EcsProperties API 参考指南](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html) 中的 *AWS Batch *。
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskProperties.html#Batch-Type-EcsTaskProperties-containers](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskProperties.html#Batch-Type-EcsTaskProperties-containers) 包含与单容器定义中的 `containerProperties` 类似的信息。主要区别在于，`containers` 使您可以最多定义十个容器。有关更多信息，请参阅 *AWS Batch API* 参考指南中的[ECSTask属性:容器](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskProperties.html)。
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html#Batch-Type-TaskContainerProperties-essential](https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html#Batch-Type-TaskContainerProperties-essential) 参数指示容器如何影响作业。所有关键容器都必须成功完成（以 0 退出），作业才能继续进行。如果标记为关键的容器失败（以非 0 退出），则作业将会失败。

  默认值为 `true`，并且必须至少将一个容器标记为 `essential`。有关更多信息，请参阅 [`essential` API 参考指南](https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html#Batch-Type-TaskContainerProperties-essential) 中的 *AWS Batch *。
+ 使用 [https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html#Batch-Type-TaskContainerProperties-dependsOn](https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html#Batch-Type-TaskContainerProperties-dependsOn) 参数，您可以定义容器依赖项列表。有关更多信息，请参阅 [`dependsOn` API 参考指南](https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html#Batch-Type-TaskContainerProperties-dependsOn) 中的 *AWS Batch *。
**注意**  
`dependsOn` 列表的复杂性以及相关的容器运行时可能会影响作业的开始时间。如果依赖项需要很长时间才能运行，则作业将保持 `STARTING` 状态，直到它们完成。

有关`ecsProperties`和结构的更多信息，请参阅 [ecs](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#Batch-RegisterJobDefinition-request-ecsProperties) Properties 的[RegisterJobDefinition](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#API_RegisterJobDefinition_RequestBody)请求语法。

## Amazon EKS 的多容器作业定义
<a name="multi-container-eks-updates"></a>

为了适应 Amazon EKS 的多容器结构，其中某些 API 数据类型有所不同。例如，
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_EksContainer.html#Batch-Type-EksContainer-name](https://docs.aws.amazon.com/batch/latest/APIReference/API_EksContainer.html#Batch-Type-EksContainer-name) 是容器的唯一标识符。此对象不是单个容器所必需的，但是，当在一个容器组（pod）中定义多个容器时，它是必需的。如果未为单个容器定义 `name`，则应用默认名称 `default`。
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_EksPodProperties.html#Batch-Type-EksPodProperties-initContainers](https://docs.aws.amazon.com/batch/latest/APIReference/API_EksPodProperties.html#Batch-Type-EksPodProperties-initContainers)是在[eksPodProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_EksPodProperties.html)数据类型中定义的。它们在应用程序容器之前运行，将始终运行到完成，并且必须在下一个容器启动之前成功完成。

  这些容器已在 Amazon EKS Connector 代理中注册，并将注册信息保存在 Amazon Elastic Kubernetes Service 后端数据存储中。`initContainers` 对象最多可以接受十（10）个元素。有关更多信息，请参阅 *Kubernetes 文档*中的 [Init 容器](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/)。
**注意**  
`initContainers` 对象可能会影响作业的开始时间。如果 `initContainers` 需要很长时间才能运行，则作业将保持 `STARTING` 状态，直到它们完成。
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_EksPodProperties.html#Batch-Type-EksPodProperties-shareProcessNamespace](https://docs.aws.amazon.com/batch/latest/APIReference/API_EksPodProperties.html#Batch-Type-EksPodProperties-shareProcessNamespace) 指示容器组（pod）中的容器是否可以共享相同的进程命名空间。默认值为 `false`。将其设置为 `true` 可以让容器看到位于同一容器组（pod）中的其他容器中的进程并发出信号。
+ 每个容器都很重要。所有容器必须成功完成（以 0 退出），作业才能成功。如果一个容器失败（以非 0 退出），则作业将失败。

有关`eksProperties`和结构的更多信息，请参阅 [eks](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#Batch-RegisterJobDefinition-request-eksProperties) Properties 的[RegisterJobDefinition](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#API_RegisterJobDefinition_RequestBody)请求语法。

# 参考：使用 EcsProperties 的 AWS Batch 作业场景
<a name="multi-container-jobs-scenarios"></a>

为了说明如何根据您的需求来构造使用 `EcsProperties` 的 AWS Batch 作业定义，本主题介绍了以下 [https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html) 有效载荷。您可以将这些示例复制到文件中，根据需要对其进行自定义，然后使用 AWS Command Line Interface（AWS CLI）调用 `RegisterJobDefinition`。

## Amazon EC2 上的 Amazon ECS 的 AWS Batch 作业
<a name="multi-container-scenario-on-ec2"></a>

以下是 Amazon Elastic Compute Cloud 上的 Amazon Elastic Container Service 的 AWS Batch 作业示例：

```
{
    "jobDefinitionName": "multicontainer-ecs-ec2",
    "type": "container",
    "ecsProperties": {
        "taskProperties": [
          {
            "containers": [
              {
                "name": "c1",
                "essential": false,
                "command": [
                  "echo",
                  "hello world"
                ],
                "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                "resourceRequirements": [
                  {
                    "type": "VCPU",
                    "value": "2"
                  },
                  {
                    "type": "MEMORY",
                    "value": "4096"
                  }
                ]
              },
              {
                "name": "c2",
                "essential": false,
                "command": [
                  "echo",
                  "hello world"
                ],
                "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                "resourceRequirements": [
                  {
                    "type": "VCPU",
                    "value": "2"
                  },
                  {
                    "type": "MEMORY",
                    "value": "4096"
                  }
                ]
              },
              {
                "name": "c3",
                "essential": true,
                "command": [
                  "echo",
                  "hello world"
                ],
                "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                "firelensConfiguration": {
                  "type": "fluentbit",
                  "options": {
                    "enable-ecs-log-metadata": "true"
                  }
                 },
                "resourceRequirements": [
                  {
                    "type": "VCPU",
                    "value": "6"
                  },
                  {
                    "type": "MEMORY",
                    "value": "12288"
                  }
                ]
              }
            ]
          }
        ]
  }
}
```

## Fargate 上的 Amazon ECS 的 AWS Batch 作业
<a name="multi-containers-ecs-on-fargate"></a>

以下是 AWS Fargate 上的 Amazon Elastic Container Service 的 AWS Batch 作业示例：

```
{
    "jobDefinitionName": "multicontainer-ecs-fargate",
    "type": "container",
    "platformCapabilities": [
        "FARGATE"
    ],
    "ecsProperties": {
        "taskProperties": [
          {
            "containers": [
              {
                "name": "c1",
                "command": [
                  "echo",
                  "hello world"
                ],
                "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                "resourceRequirements": [
                  {
                    "type": "VCPU",
                    "value": "2"
                  },
                  {
                    "type": "MEMORY",
                    "value": "4096"
                  }
                ]
              },
              {
                "name": "c2",
                "essential": true,
                "command": [
                  "echo",
                  "hello world"
                ],
                "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                "resourceRequirements": [
                  {
                    "type": "VCPU",
                    "value": "6"
                  },
                  {
                    "type": "MEMORY",
                    "value": "12288"
                  }
                ]
              }
            ],
            "executionRoleArn": "arn:aws:iam::1112223333:role/ecsTaskExecutionRole"
          }
        ]
  }
}
```

## 适用于 Amazon EKS 的 AWS Batch 作业
<a name="multi-containers-eks-example"></a>

以下是 Amazon Elastic Kubernetes Service 的 AWS Batch 作业示例：

```
{
  "jobDefinitionName": "multicontainer-eks",
  "type": "container",
  "eksProperties": {
    "podProperties": {
      "shareProcessNamespace": true,
      "initContainers": [
        {
          "name": "init-container",
          "image": "public.ecr.aws/amazonlinux/amazonlinux:2",
          "command": [
            "echo"
          ],
          "args": [
            "hello world"
          ],
          "resources": {
            "requests": {
              "cpu": "1",
              "memory": "512Mi"
            }
          }
        },
        {
          "name": "init-container-2",
          "image": "public.ecr.aws/amazonlinux/amazonlinux:2",
          "command": [
            "echo",
            "my second init container"
          ],
          "resources": {
            "requests": {
              "cpu": "1",
              "memory": "512Mi"
            }
          }
        }
      ],
      "containers": [
        {
          "name": "c1",
          "image": "public.ecr.aws/amazonlinux/amazonlinux:2",
          "command": [
            "echo world"
         ],
          "resources": {
            "requests": {
              "cpu": "1",
              "memory": "512Mi"
            }
          }
        },
        {
          "name": "sleep-container",
          "image": "public.ecr.aws/amazonlinux/amazonlinux:2",
          "command": [
            "sleep",
            "20"
          ],
          "resources": {
            "requests": {
              "cpu": "1",
              "memory": "512Mi"
            }
          }
        }
      ]
    }
  }
}
```

## 每个节点具有多个容器的 MNP AWS Batch 作业
<a name="multi-container-ecs-mnp"></a>

以下是每个节点具有多个容器的多节点并行（MNP）AWS Batch 作业的示例：

```
{
  "jobDefinitionName": "multicontainer-mnp",
  "type": "multinode",
  "nodeProperties": {
    "numNodes": 6,
    "mainNode": 0,
    "nodeRangeProperties": [
      {
        "targetNodes": "0:5",
        "ecsProperties": {
          "taskProperties": [
            {
              "containers": [
                {
                  "name": "range05-c1",
                  "command": [
                    "echo",
                    "hello world"
                  ],
                  "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                  "resourceRequirements": [
                    {
                      "type": "VCPU",
                      "value": "2"
                    },
                    {
                      "type": "MEMORY",
                      "value": "4096"
                    }
                  ]
                },
                {
                  "name": "range05-c2",
                  "command": [
                    "echo",
                    "hello world"
                  ],
                  "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                  "resourceRequirements": [
                    {
                      "type": "VCPU",
                      "value": "2"
                    },
                    {
                      "type": "MEMORY",
                      "value": "4096"
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}
```