

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

# 自定义 Amazon EKS 启动模板
<a name="eks-launch-templates"></a>

Amazon Batch 在 Amazon 上，EKS 支持启动模板。启动模板的功能受到限制。

**重要**  
对于 EKS 来说 AL2 AMIs， Amazon Batch 跑步`/etc/eks/bootstrap.sh`。请勿在启动模板或cloud-inituser-data脚本中运行`/etc/eks/bootstrap.sh`。除了 [bootstrap.sh](https://github.com/awslabs/amazon-eks-ami/blob/main/templates/al2/runtime/bootstrap.sh) 的`--kubelet-extra-args`参数外，还可以添加其他参数。为此，请在`AWS_BATCH_KUBELET_EXTRA_ARGS`文件中设置`/etc/aws-batch/batch.config`变量。详情请参阅以下示例。
对于 EKS AL2023， Amazon Batch 利用来[NodeConfigSpec](https://awslabs.github.io/amazon-eks-ami/nodeadm/doc/api/#nodeconfigspec)自 EKS 的使实例加入 EKS 集群。 Amazon Batch 将[NodeConfigSpec](https://awslabs.github.io/amazon-eks-ami/nodeadm/doc/api/#nodeconfigspec)为 EKS 集群填充 [ClusterDetails](https://awslabs.github.io/amazon-eks-ami/nodeadm/doc/api/#clusterdetails)，您无需指定它们。

**注意**  
我们建议您不要在启动模板中[https://awslabs.github.io/amazon-eks-ami/nodeadm/doc/api/#nodeconfigspec](https://awslabs.github.io/amazon-eks-ami/nodeadm/doc/api/#nodeconfigspec)设置以下任何设置，因为 Amazon Batch 这将覆盖您的值。有关更多信息，请参阅 [Kubernetes节点的共同责任](eks-ce-shared-responsibility.md)。  
`Taints`
`Cluster Name`
`apiServerEndpoint`
`certificatAuthority`
`CIDR`
不要创建使用前缀 `batch.amazonaws.com/` 的标签

**注意**  
如果在调用后[CreateComputeEnvironment](https://docs.amazonaws.cn/batch/latest/APIReference/API_CreateComputeEnvironment.html)更改了启动模板，则[https://docs.amazonaws.cn/batch/latest/APIReference/API_UpdateComputeEnvironment.html](https://docs.amazonaws.cn/batch/latest/APIReference/API_UpdateComputeEnvironment.html)必须调用该启动模板来评估要替换的启动模板的版本。

**Topics**
+ [添加 `kubelet` 额外参数](#kubelet-extra-args)
+ [配置容器运行时系统](#change-container-runtime)
+ [挂载 Amazon EFS 卷](#mounting-efs-volume)
+ [IPv6 支持](#eks-ipv6-support)

## 添加 `kubelet` 额外参数
<a name="kubelet-extra-args"></a>

Amazon Batch 支持在`kubelet`命令中添加额外的参数。有关支持的参数列表，请参阅*Kubernetes文档*中的[https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet/](https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet/)。在以下 EKS 示例中 AL2 AMIs`--node-labels mylabel=helloworld`，已添加到`kubelet`命令行中。

```
MIME-Version: 1.0
      Content-Type: multipart/mixed; boundary="==MYBOUNDARY=="

      --==MYBOUNDARY==
      Content-Type: text/x-shellscript; charset="us-ascii"

      #!/bin/bash
      mkdir -p /etc/aws-batch

      echo AWS_BATCH_KUBELET_EXTRA_ARGS=\"--node-labels mylabel=helloworld\" >> /etc/aws-batch/batch.config

      --==MYBOUNDARY==--
```

对于 EKS AL2023 AMIs ，文件格式为 YAML。有关支持的参数列表，请参阅*Kubernetes文档*中的[https://awslabs.github.io/amazon-eks-ami/nodeadm/doc/api/#nodeconfigspec](https://awslabs.github.io/amazon-eks-ami/nodeadm/doc/api/#nodeconfigspec)。在以下 EKS 示例中 AL2023 AMIs`--node-labels mylabel=helloworld`，已添加到`kubelet`命令行中。

```
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="==MYBOUNDARY=="

--==MYBOUNDARY==
Content-Type: application/node.eks.aws

apiVersion: node.eks.aws/v1alpha1
kind: NodeConfig
spec:
  kubelet:
    flags:
    - --node-labels=mylabel=helloworld

--==MYBOUNDARY==--
```

## 配置容器运行时系统
<a name="change-container-runtime"></a>

您可以使用 Amazon Batch `CONTAINER_RUNTIME`环境变量在托管节点上配置容器运行时。以下示例将容器运行时系统设置为“`bootstrap.sh`运行时`containerd`”。有关更多信息，请参阅*Kubernetes文档*中的[https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd](https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd)。

如果您使用的是优化型 `EKS_AL2023` 或 `EKS_AL2023_NVIDIA` AMI，则无需指定容器运行时，因为仅支持 **containerd**。

**注意**  
`CONTAINER_RUNTIME`环境变量等同于`bootstrap.sh`的`--container-runtime`选项。有关更多信息，请参阅*Kubernetes文档*中的[https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet/#options](https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet/#options)。

```
MIME-Version: 1.0
      Content-Type: multipart/mixed; boundary="==MYBOUNDARY=="

      --==MYBOUNDARY==
      Content-Type: text/x-shellscript; charset="us-ascii"

      #!/bin/bash
      mkdir -p /etc/aws-batch

      echo CONTAINER_RUNTIME=containerd >> /etc/aws-batch/batch.config

      --==MYBOUNDARY==--
```

## 挂载 Amazon EFS 卷
<a name="mounting-efs-volume"></a>

可以使用启动模板将卷装载到节点上。在以下示例中，使用了`cloud-config``packages`和`runcmd`设置。有关更多信息，请参阅*cloud-init文档*中的[云配置示例](https://cloudinit.readthedocs.io/en/latest/topics/examples.html)。

```
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="==MYBOUNDARY=="

--==MYBOUNDARY==
Content-Type: text/cloud-config; charset="us-ascii"

packages:
- amazon-efs-utils

runcmd:
- file_system_id_01=fs-abcdef123
- efs_directory=/mnt/efs

- mkdir -p ${efs_directory}
- echo "${file_system_id_01}:/ ${efs_directory} efs _netdev,noresvport,tls,iam 0 0" >> /etc/fstab
- mount -t efs -o tls ${file_system_id_01}:/ ${efs_directory}

--==MYBOUNDARY==--
```

要在作业中使用此卷，必须将其添加到 [eksPro](https://docs.amazonaws.cn/batch/latest/APIReference/API_EksProperties.html) perties 参数中。[RegisterJobDefinition](https://docs.amazonaws.cn/batch/latest/APIReference/API_RegisterJobDefinition.html)以下示例是作业定义的一大部分。

```
{
    "jobDefinitionName": "MyJobOnEks_EFS",
    "type": "container",
    "eksProperties": {
        "podProperties": {
            "containers": [
                {
                    "image": "public.ecr.aws/amazonlinux/amazonlinux:2",
                    "command": ["ls", "-la", "/efs"],
                    "resources": {
                        "limits": {
                            "cpu": "1",
                            "memory": "1024Mi"
                        }
                    },
                    "volumeMounts": [
                        {
                            "name": "efs-volume",
                            "mountPath": "/efs"
                        }
                    ]
                }
            ],
            "volumes": [
                {
                    "name": "efs-volume",
                    "hostPath": {
                        "path": "/mnt/efs"
                    }
                }
            ]
        }
    }
}
```

在节点中，Amazon EFS 卷装载在`/mnt/efs`目录中。在 Amazon EKS 作业的容器中，卷装载在 `/efs` 目录中。

## IPv6 支持
<a name="eks-ipv6-support"></a>

Amazon Batch 支持具有 IPv6 地址的 Amazon EKS 集群。无需自定义即可获得 Amazon Batch 支持。但是，在开始之前，我们建议您查看 *Amazon EKS 用户指南*中[为容器和服务分配 IPv6 地址](https://docs.amazonaws.cn/eks/latest/userguide/cni-ipv6.html)中概述的注意事项和条件。