

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

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

*Akamahadevi hiremath，Amazon Web Services*

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

此模式提供程式碼範例，以及在 Amazon EKS 叢集建立期間，在 Amazon Web Services (AWS) 雲端的 Amazon Elastic Kubernetes Service (Amazon EKS) 工作者節點上安裝 AWS Systems Manager Agent (SSM Agent) 和 Amazon CloudWatch 代理程式的步驟。 Amazon CloudWatch 您可以使用`eksctl`[組態檔案結構描述 ](https://eksctl.io/usage/schema/)(Weaveworks 文件） 中的 `preBootstrapCommands` 屬性來安裝 SSM Agent 和 CloudWatch 代理程式。然後，您可以使用 SSM Agent 連線到工作者節點，而無需使用 Amazon Elastic Compute Cloud (Amazon EC2) 金鑰對。此外，您可以使用 CloudWatch 代理程式來監控 Amazon EKS 工作者節點上的記憶體和磁碟使用率。

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

**先決條件**
+ 作用中的 AWS 帳戶
+ 在 macOS、Linux 或 Windows 上安裝和設定的 [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)

**限制**
+ 建議您避免將長時間執行的指令碼新增至 `preBootstrapCommands`****屬性，因為這會延遲節點在擴展活動期間加入 Amazon EKS 叢集。我們建議您改為建立[自訂 Amazon Machine Image (AMI)](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.customenv.html)。
+ 此模式僅適用於 Amazon EC2 Linux 執行個體。

## Architecture
<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 參數存放區

**目標架構**

下圖顯示使用者使用 SSM Agent 連線至 Amazon EKS 工作者節點的範例，該 SSM Agent 是使用 安裝。 `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_tw/prescriptive-guidance/latest/patterns/images/pattern-img/b37a3cdb-204f-4014-8317-3600a793dac7/images/9a5760af-23bb-4616-97b0-b401a9d080cf.png)


該圖顯示以下工作流程：

1. 使用者使用 `eksctl`組態檔案搭配 `preBootstrapCommands` 屬性來建立 Amazon EKS 叢集，這會安裝 SSM 代理程式和 CloudWatch 代理程式。

1. 任何稍後因擴展活動而加入叢集的新執行個體都會使用預先安裝的 SSM Agent 和 CloudWatch 代理程式建立。

1. 使用者使用 SSM 代理程式連線至 Amazon EC2，然後使用 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 參數存放區](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html)為組態資料管理和秘密管理提供安全的階層式儲存。
+ [AWS Systems Manager Session Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager.html) 可協助您透過互動式、一鍵式瀏覽器型 shell 或透過 AWS Command Line Interface (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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 存放 CloudWatch 代理程式組態檔案。 | 將 CloudWatch 代理程式組態檔案存放在您要建立 Amazon EKS 叢集的 AWS 區域中的 AWS [AWS Systems Manager 參數存放](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/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_tw/prescriptive-guidance/latest/patterns/install-the-ssm-agent-and-cloudwatch-agent-on-amazon-eks-worker-nodes-using-prebootstrapcommands.html) | AWS DevOps | 

### 驗證 SSM Agent 和 CloudWatch 代理程式是否正常運作
<a name="verify-that-the-ssm-agent-and-cloudwatch-agent-work"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 測試 SSM 代理程式。 | 透過使用 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_tw/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 文件）
+ [啟動工作階段 (Amazon 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 System Manager 參數存放區中建立的參數名稱。您必須在建立 Amazon EKS 叢集的相同 AWS 區域中的參數存放區`AmazonCloudwatch-linux`中包含 。您也可以指定檔案路徑，但建議您使用 Systems Manager，以便於自動化和重複使用。
+ 如果您在`eksctl`組態檔案中使用 ，您會`preBootstrapCommands`在 AWS 管理主控台中看到兩個啟動範本。第一個啟動範本包含 中指定的命令`preBootstrapCommands`。第二個範本包含 中指定的命令`preBootstrapCommands`和預設 Amazon EKS 使用者資料。需要此資料才能讓節點加入叢集。節點群組的 Auto Scaling 群組使用此使用者資料來啟動新的執行個體。
+ 如果您在`eksctl`組態檔案中使用 `iam` 屬性，則必須列出預設 Amazon EKS 政策，以及您連接的 AWS Identity and Access Management (IAM) 政策中所需的任何其他政策。在*建立 eksctl 組態檔案和叢集*步驟的程式碼片段中，新增`CloudWatchAgentServerPolicy``AmazonSSMMangedInstanceCore`了額外的政策，以確保 CloudWatch 代理程式和 SSM 代理程式如預期般運作。`AmazonEKSWorkerNodePolicy`、`AmazonEKS_CNI_Policy`、 `AmazonEC2ContainerRegistryReadOnly`政策是 Amazon EKS 叢集正常運作所需的必要政策。