

# 使用 Amazon CloudFormation 部署 OTel Container Insights
<a name="container-insights-eks-otel-cfn"></a>

可以使用 Amazon CloudFormation 模板将 OTel Container Insights 部署为基础设施即代码。此方法最适合通过 IaC 管道管理集群并需要可重复的、受版本控制的部署的团队。

模板安装启用了 OTel Container Insights 的 `amazon-cloudwatch-observability` EKS 附加组件。如果您更喜欢 Kubernetes 原生程序包管理，请参阅[使用 Helm OTel Container Insights](container-insights-eks-otel-helm.md)。

## 先决条件
<a name="container-insights-eks-otel-cfn-prereqs"></a>

在使用 Amazon CloudFormation 部署 OTel Container Insights 之前，请确认已满足以下要求。
+ 运行 Kubernetes 1.28 版本或更高版本的现有 Amazon EKS 集群
+ Amazon CLI 版本 2.15.0 或更高版本
+ 已配置 `kubectl` 以与目标集群通信
+ 创建 Amazon CloudFormation 堆栈所需的 IAM 权限
+ 从集群到 CloudWatch 端点的出站互联网访问

## 使用 Amazon CloudFormation 进行部署
<a name="container-insights-eks-otel-cfn-cloudformation"></a>

使用 Amazon CloudFormation 模板在单个堆栈中部署 CloudWatch 可观测性附加组件、所需的 IAM 角色和容器组身份关联。此模板使用 `AWS::EKS::Addon` 资源来安装启用了 OTel Container Insights 的附加组件。

### Amazon CloudFormation 模板
<a name="container-insights-eks-otel-cfn-template"></a>

以下模板创建了在 Amazon EKS 集群上启用 OTel Container Insights 所需的完整资源集。

```
AWSTemplateFormatVersion: '2010-09-09'
Description: Deploy OTel Container Insights on an EKS cluster

Parameters:
  ClusterName:
    Type: String
    Description: The name of your EKS cluster

Resources:
  CloudWatchAgentRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: EKS-CloudWatch-Observability-Role
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: pods.eks.amazonaws.com
            Action:
              - sts:AssumeRole
              - sts:TagSession
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy

  PodIdentityAssociation:
    Type: AWS::EKS::PodIdentityAssociation
    Properties:
      ClusterName: !Ref ClusterName
      Namespace: amazon-cloudwatch
      ServiceAccount: cloudwatch-agent
      RoleArn: !GetAtt CloudWatchAgentRole.Arn

  CloudWatchObservabilityAddon:
    Type: AWS::EKS::Addon
    DependsOn: PodIdentityAssociation
    Properties:
      ClusterName: !Ref ClusterName
      AddonName: amazon-cloudwatch-observability
      AddonVersion: v6.2.0-eksbuild.1
      ConfigurationValues: '{"otelContainerInsights":{"enabled":true}}'
      ServiceAccountRoleArn: !GetAtt CloudWatchAgentRole.Arn
      ResolveConflicts: OVERWRITE
```

此模板创建以下资源：
+ 附加了 `CloudWatchAgentServerPolicy` 托管策略的 IAM 角色
+ EKS 容器组身份关联，用于将角色映射到 CloudWatch 代理服务账户
+ 启用了 OTel Container Insights 的 `amazon-cloudwatch-observability` EKS 附加组件

### 部署 Amazon CloudFormation 堆栈
<a name="container-insights-eks-otel-cfn-deploy-stack"></a>

使用 Amazon CLI 根据模板创建 Amazon CloudFormation 堆栈。

**部署堆栈**

1. 将前述模板保存到一个名为 `otel-container-insights.yaml` 的文件中。

1. 运行如下命令。将 {{cluster-name}} 替换为 Amazon EKS 集群的名称。

   ```
   aws cloudformation create-stack \
     --stack-name otel-container-insights \
     --template-body file://otel-container-insights.yaml \
     --parameters ParameterKey=ClusterName,ParameterValue={{cluster-name}} \
     --capabilities CAPABILITY_NAMED_IAM
   ```

1. 等待堆栈创建完成。

   ```
   aws cloudformation wait stack-create-complete \
     --stack-name otel-container-insights
   ```

1. 确认堆栈状态是否为 `CREATE_COMPLETE`。

   ```
   aws cloudformation describe-stacks \
     --stack-name otel-container-insights \
     --query "Stacks[0].StackStatus" \
     --output text
   ```

## 验证部署
<a name="container-insights-eks-otel-cfn-verify"></a>

Amazon CloudFormation 堆栈创建完成后，确认附加组件是否正在运行并将数据发送到 CloudWatch。

**验证部署**

1. 确认 CloudWatch 代理容器组（pod）是否正在运行。

   ```
   kubectl get pods -n amazon-cloudwatch -l app.kubernetes.io/name=cloudwatch-agent
   ```

   所有容器组（pod）都必须显示 `Running` 状态。

1. 通过 [https://console.aws.amazon.com/cloudwatch/](https://console.amazonaws.cn/cloudwatch/) 打开 CloudWatch 控制台。

1. 在导航窗格中，选择 **Container Insights**。

1. 确认您的集群是否显示在集群列表中，并且指标是否已填充。

指标通常会在部署完成后的 3 到 5 分钟内显示在 CloudWatch 中。

## 移除部署
<a name="container-insights-eks-otel-cfn-cleanup"></a>

要移除 Amazon CloudFormation 模板创建的所有资源，请删除堆栈。

```
aws cloudformation delete-stack \
  --stack-name otel-container-insights
```