

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

# 亚马逊 EKS 优化了 Windows AMI 管理
<a name="windows-ami"></a>

经过优化 AMIs 的 Windows 亚马逊 EKS 建立在 Windows Server 2019 和 Windows Server 2022 之上。它们被配置作为 Amazon EKS 节点的基本映像。默认情况下， AMIs 包括以下组件：
+  [kubelet](https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet/) 
+  [kube-proxy](https://kubernetes.io/docs/reference/command-line-tools-reference/kube-proxy/) 
+  [适用于 Kubernetes 的 AWS IAM Authenticator](https://github.com/kubernetes-sigs/aws-iam-authenticator) 
+  [csi-proxy](https://github.com/kubernetes-csi/csi-proxy) 
+  [containerd](https://containerd.io/) 

通过查询 AWS Systems Manager Parameter Store API，您可以 AMIs 通过编程方式检索亚马逊 EKS 的亚马逊系统映像 (AMI) ID。此参数使您无需手动查找 Amazon EKS 优化的 AMI IDs。有关 Systems Manager 参数存储 API 的更多信息，请参阅[GetParameter](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_GetParameter.html)。您的用户账户必须具有 ssm: GetParameter IAM 权限才能检索 Amazon EKS 优化的 AMI 元数据。

以下示例检索适用于 Windows Server 2019 LTSC Core 的最新亚马逊 EKS 优化版 AMI 的 AMI 的 AMI ID。AMI 名称中列出的版本号与其准备的相应的 Kubernetes 版本有关。

```
aws ssm get-parameter --name /aws/service/ami-windows-latest/Windows_Server-2019-English-Core-EKS_Optimized-1.21/image_id --region us-east-1 --query "Parameter.Value" --output text
```

输出示例：

```
ami-09770b3eec4552d4e
```

## 管理你自己的亚马逊 EKS 优化的 Windows AMI
<a name="_managing_your_own_amazon_eks_optimized_windows_ami"></a>

迈向生产环境的重要步骤是在整个亚马逊 EKS 集群中保持相同的亚马逊 EKS 优化版 Windows AMI 和 kubelet 版本。

在 Amazon EKS 集群中使用相同的版本可以缩短故障排除时间，并提高集群的一致性。[Amazon EC2 Image Builder](https://aws.amazon.com/image-builder/) 可帮助创建和维护定制 Amazon EKS 优化的 Windows， AMIs 以便在亚马逊 EKS 集群中使用。

使用 Amazon EC2 Image Builder 在 Windows 服务器版本、AWS Windows Server AMI 发布日期、 and/or 操作系统版本之间进行选择。构建组件步骤允许你在现有的 EKS 优化的 Windows Artifacts 和 kubelet 版本之间进行选择。欲了解更多信息： https://docs.aws.amazon.com/eks/latest/userguide/eks-custom-ami-windows .html

![\[构建组件\]](http://docs.aws.amazon.com/zh_cn/eks/latest/best-practices/images/windows/build-components.png)


 **注意：**在选择基础映像之前，请查阅 [Windows Server 版本和许可证](windows-licensing.md)部分，了解有关发布渠道更新的重要详细信息。

## 为经过优化的自定义 EKS 配置更快的启动 AMIs
<a name="_configuring_faster_launching_for_custom_eks_optimized_amis"></a>

使用经过优化 Windows Amazon EKS 的自定义 AMI 时，启用快速启动功能可以将 Windows 工作节点的启动速度提高多达 65%。此功能保留了一组预先配置的快照，这些快照具有 *Sysprep 专*用、*Windows 开箱即用体验 (OOBE) 步骤和已完成所需的*重启。这些快照随后用于后续启动，从而缩短了向外扩展或替换节点的时间。只能通过 EC2 控制台或 AWS CLI 为 AMIs *自己*启用快速启动，并且可以配置所维护的快照数量。

 **注意：**Fast Launch 与亚马逊提供的默认 EKS 优化 AMI 不兼容，请在尝试启用之前按上述方式创建自定义 AMI。

欲了解更多信息：[AWS Windows AMIs -配置您的 AMI 以加快启动速度](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/windows-ami-version-history.html#win-ami-config-fast-launch) 

## 在自定义模式下缓存 Windows 基础图层 AMIs
<a name="_caching_windows_base_layers_on_custom_amis"></a>

Windows 容器镜像比 Linux 容器镜像大。如果您正在运行任何基于容器化的.NET Framework 应用程序，则平均图像大小约为 8.24GB。在 Pod 调度期间，必须在 Pod 处于 Running 状态之前将容器镜像完全拉取并提取到磁盘中。

在此过程中，容器运行时 (containerd) 将整个容器镜像提取并提取到磁盘中。拉取操作是一个并行进程，这意味着容器运行时会并行拉取容器镜像层。相比之下，提取操作是按顺序进行的，而且是 I/O 密集型的。因此，容器镜像可能需要 8 分钟以上的时间才能完全提取并可供容器运行时 (containerd) 使用，因此，pod 的启动时间可能需要几分钟。

如**修补 Windows 服务器和容器**主题中所述，可以选择使用 EKS 构建自定义 AMI。在 AMI 准备过程中，您可以添加额外的 EC2 映像生成器组件，以便在本地提取所有必要的 Windows 容器映像，然后生成 AMI。这种策略将大大缩短 pod 变为 “**正在运行**” 状态的时间。

在 Amazon EC2 Image Builder 上，创建一个[组件](https://docs.aws.amazon.com/imagebuilder/latest/userguide/manage-components.html)来下载必要的图像并将其附加到图像配方中。以下示例从 ECR 存储库中提取特定映像。

```
name: ContainerdPull
description: This component pulls the necessary containers images for a cache strategy.
schemaVersion: 1.0

phases:
  - name: build
    steps:
      - name: containerdpull
        action: ExecutePowerShell
        inputs:
          commands:
            - Set-ExecutionPolicy Unrestricted -Force
            - (Get-ECRLoginCommand).Password | docker login --username AWS --password-stdin 111000111000.dkr.ecr.us-east-1.amazonaws.com
            - ctr image pull mcr.microsoft.com/dotnet/framework/aspnet:latest
            - ctr image pull 111000111000.dkr.ecr.us-east-1.amazonaws.com/myappcontainerimage:latest
```

为确保以下组件按预期运行，请检查 EC2 Image builder (EC2InstanceProfileForImageBuilder) 使用的 IAM 角色是否附加了策略：

![\[权限策略\]](http://docs.aws.amazon.com/zh_cn/eks/latest/best-practices/images/windows/permissions-policies.png)


## 博客文章
<a name="_blog_post"></a>

在以下博客文章中，您将逐步了解如何为自定义 Amazon EKS Windows 实施缓存策略 AMIs：

 [使用 EC2 映像生成器和图像缓存策略加快 Windows 容器的启动时间](https://aws.amazon.com/blogs/containers/speeding-up-windows-container-launch-times-with-ec2-image-builder-and-image-cache-strategy/) 