

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

# 使用在 Amazon EKS 工作节点上安装 SSM CloudWatch 代理和代理 preBootstrapCommands
<a name="install-the-ssm-agent-and-cloudwatch-agent-on-amazon-eks-worker-nodes-using-prebootstrapcommands"></a>

*Akkamahadevi Hiremath，Amazon Web Services*

## Summary
<a name="install-the-ssm-agent-and-cloudwatch-agent-on-amazon-eks-worker-nodes-using-prebootstrapcommands-summary"></a>

此模式提供了在亚马逊 EKS 集群创建期间在亚马逊网络服务 (AWS) 云中的亚马逊 Elastic Kubernetes Service（Amazon EKS）工作节点上安装 AWS Systems Manager 代理（SSM 代理）和亚马逊 CloudWatch 代理的代码示例和步骤。您可以使用`eksctl`[配置文件架构](https://eksctl.io/usage/schema/)中的`preBootstrapCommands`属性安装 SSM CloudWatch 代理和代理（Weaveworks 文档）。然后，您可以使用 SSM 代理连接到您的工作节点，而无需使用亚马逊弹性计算云 (Amazon EC2) 密钥对。此外，您还可以使用 CloudWatch 代理来监控 Amazon EKS 工作节点上的内存和磁盘利用率。

## 先决条件和限制
<a name="install-the-ssm-agent-and-cloudwatch-agent-on-amazon-eks-worker-nodes-using-prebootstrapcommands-prereqs"></a>

**先决条件**
+ 一个有效的 Amazon Web Services account
+ [eksctl 命令行实用程序](https://docs.aws.amazon.com/eks/latest/userguide/eksctl.html)，已在 macOS、Linux 或 Windows 上安装与配置
+ [kubectl 命令行实用程序](https://docs.aws.amazon.com/eks/latest/userguide/install-kubectl.html)，已在 macOS、Linux 或 Windows 上安装与配置

**限制**
+ 我们建议您避免向 `preBootstrapCommands` **** 属性中添加长时间运行的脚本，因为这会延迟节点在扩展活动期间加入 Amazon EKS 集群的时间。我们建议您改为创建[自定义亚马逊机器映像（AMI）](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.customenv.html)。
+ 此模式仅适用于亚马逊 EC2 Linux 实例。

## 架构
<a name="install-the-ssm-agent-and-cloudwatch-agent-on-amazon-eks-worker-nodes-using-prebootstrapcommands-architecture"></a>

**技术堆栈**
+ Amazon CloudWatch
+ Amazon Elastic Kubernetes Service(Amazon EKS)
+ AWS Systems Manager Parameter Store

**目标架构**

下图显示了一个用户使用 SSM Agent 连接到 Amazon EKS Worker 节点的示例，该代理是使用 `preBootstrapCommands` 安装的。

![\[User connecting to Amazon EKS worker nodes via Systems Manager, with SSM Agent and CloudWatch agent on each node.\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/b37a3cdb-204f-4014-8317-3600a793dac7/images/9a5760af-23bb-4616-97b0-b401a9d080cf.png)


下图显示了如下工作流：

1. 用户使用带有`preBootstrapCommands`属性的`eksctl`配置文件创建 Amazon EKS 集群，该文件将安装 SSM CloudWatch 代理和代理。

1. 之后由于扩展活动而加入集群的任何新实例都将使用预安装的 SSM 代理和 CloudWatch 代理创建。

1. 用户使用 SSM 代理连接 EC2 到 Amazon，然后使用代理监控内存和磁盘利用率。 CloudWatch 

## 工具
<a name="install-the-ssm-agent-and-cloudwatch-agent-on-amazon-eks-worker-nodes-using-prebootstrapcommands-tools"></a>
+ [Amazon CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html) 可帮助您实时监控您的 AWS 资源和在 AWS 上运行的应用程序的指标。
+ [Amazon Elastic Kubernetes Service (Amazon EKS)](https://docs.aws.amazon.com/eks/latest/userguide/getting-started.html) 可帮助您在 AWS 上运行 Kubernetes，而无需安装或维护您自己的 Kubernetes 控制面板或节点。
+ [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html) 提供安全的分层存储，用于配置数据管理和密钥管理。
+ [AWS Systems Manager 会话管理器](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager.html)可帮助您通过交互式、一键式、基于浏览器的外壳或 AWS 命令行界面 (AWS CLI) 来管理您的 EC2 实例、本地实例和虚拟机。
+ [eksctl](https://eksctl.io/usage/schema/) 是一种用于在 Amazon EKS 上创建和管理 Kubernetes 集群的命令行实用程序。
+ [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) 是命令行实用程序，用于与集群 API 服务器通信。

## 操作说明
<a name="install-the-ssm-agent-and-cloudwatch-agent-on-amazon-eks-worker-nodes-using-prebootstrapcommands-epics"></a>

### 创建 Amazon EKS 集群。
<a name="create-an-amazon-eks-cluster"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 存储 CloudWatch 代理配置文件。 | 将 CloudWatch 代理配置文件存储在您要创建亚马逊 EK [S 集群的 AWS 区域的 AWS Systems Manager Parameter St](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html) ore 中。为此，请在 AWS Systems Manager Parameter Store 中[创建参数](https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-create-console.html)，并备注参数名称（例如`AmazonCloudwatch-linux`）。有关更多信息，请参阅此模式的 “[其他信息](#install-the-ssm-agent-and-cloudwatch-agent-on-amazon-eks-worker-nodes-using-prebootstrapcommands-additional)” 部分中的* CloudWatch 代理配置文件代码示例*。 | DevOps 工程师 | 
| 创建 eksctl 配置文件与集群。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/install-the-ssm-agent-and-cloudwatch-agent-on-amazon-eks-worker-nodes-using-prebootstrapcommands.html) | AWS DevOps | 

### 验证 SSM 代理和 CloudWatch 代理是否正常工作
<a name="verify-that-the-ssm-agent-and-cloudwatch-agent-work"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 测试 SSM Agent。 | 使用 AWS Systems Manager 文档中的[启动会话](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-sessions-start.html#start-ec2-console%20%20or%20https:%2F%2Fdocs.aws.amazon.com%2Fsystems-manager%2Flatest%2Fuserguide%2Fsession-manager-working-with-sessions-start.html%23sessions-start-cli)所涵盖的任何方法，利用 SSH 连接至您的 Amazon EKS 集群节点。 | AWS DevOps | 
| 测试代 CloudWatch 理。 | 使用 CloudWatch 控制台验证代 CloudWatch 理：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/install-the-ssm-agent-and-cloudwatch-agent-on-amazon-eks-worker-nodes-using-prebootstrapcommands.html) | AWS DevOps | 

## 相关资源
<a name="install-the-ssm-agent-and-cloudwatch-agent-on-amazon-eks-worker-nodes-using-prebootstrapcommands-resources"></a>
+ 在@@ [您的服务器上安装和运行 CloudWatch 代理](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-commandline-fleet.html)（Amazon CloudWatch 文档）
+ [创建 Systems Manager 参数（控制台）](https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-create-console.html)(AWS Systems Manager 文档)
+ [创建 CloudWatch 代理配置文件](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-cloudwatch-agent-configuration-file.html)（Amazon CloudWatch 文档）
+ [启动会话 (AWS CLI)](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-sessions-start.html#sessions-start-cli)（AWS Systems Manager 文档）
+ [启动会话（亚马逊 EC2 控制台）（](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-sessions-start.html#start-ec2-console)AWS Systems Manager 文档）

## 附加信息
<a name="install-the-ssm-agent-and-cloudwatch-agent-on-amazon-eks-worker-nodes-using-prebootstrapcommands-additional"></a>

** CloudWatch 代理配置文件示例**

在以下示例中， CloudWatch 代理配置为监控 Amazon Linux 实例上的磁盘和内存利用率：

```
{
    "agent": {
        "metrics_collection_interval": 60,
        "run_as_user": "cwagent"
    },
    "metrics": {
        "append_dimensions": {
            "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
            "ImageId": "${aws:ImageId}",
            "InstanceId": "${aws:InstanceId}",
            "InstanceType": "${aws:InstanceType}"
        },
        "metrics_collected": {
            "disk": {
                "measurement": [
                    "used_percent"
                ],
                "metrics_collection_interval": 60,
                "resources": [
                    "*"
                ]
            },
            "mem": {
                "measurement": [
                    "mem_used_percent"
                ],
                "metrics_collection_interval": 60
            }
        }
    }
}
```

**eksctl 配置文件示例**

```
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
  name: test
  region: us-east-2
  version: "1.24"
managedNodeGroups:
  - name: test
    minSize: 2
    maxSize: 4
    desiredCapacity: 2
    volumeSize: 20
    instanceType: t3.medium
    preBootstrapCommands:
    - sudo yum install amazon-ssm-agent -y
    - sudo systemctl enable amazon-ssm-agent
    - sudo systemctl start amazon-ssm-agent
    - sudo yum install amazon-cloudwatch-agent -y
    - sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c ssm:AmazonCloudwatch-linux
    iam:
      attachPolicyARNs:
        - arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy
        - arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy
        - arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly
        - arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy
        - arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
```

**其他代码详细信息**
+ `preBootstrapCommands` 属性的最后一行，`AmazonCloudwatch-linux` 是在 AWS Systems Manager Parameter Store 中创建的参数名称。您必须在创建 Amazon EKS 集群的相同 AWS 区域的 Parameter Store 中纳入 `AmazonCloudwatch-linux`。您也可指定文件路径，但我们建议使用 Systems Manager，以便更轻松地实现自动化和重复使用。
+ 如果您在 `eksctl` 配置文件中使用 `preBootstrapCommands`，则会在 AWS 管理控制台中看到两个启动模板。第一个启动模板包含 `preBootstrapCommands` 中指定的命令。第二个模板包括 `preBootstrapCommands` 中指定的命令和默认 Amazon EKS 用户数据。此数据用于将节点加入集群。节点组的自动扩缩组使用此用户数据启动新实例。
+ 如果您在 `eksctl` 配置文件中使用 `iam` 属性，则必须列出默认 Amazon EKS 策略，以及随附 AWS Identity and Access Management (IAM) 策略中所需任何其他策略。在 “*创建 eksctl 配置文件和集群*” 步骤的代码片段中，`CloudWatchAgentServerPolicy`添加了其他策略以确保 CloudWatch 代理和 SSM 代理按预期运行。`AmazonSSMMangedInstanceCore``AmazonEKSWorkerNodePolicy`、`AmazonEKS_CNI_Policy`、`AmazonEC2ContainerRegistryReadOnly` 策略是 Amazon EKS 集群正常运行所需强制策略。