

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

# 在 Amazon EKS 和 Kubernetes 叢集上安裝具有 Prometheus 指標集合的 CloudWatch 代理程式。
<a name="ContainerInsights-Prometheus-Setup"></a>

本節說明如何在執行 Amazon EKS 或 Kubernetes 的叢集中設定具有 Prometheus 監控的 CloudWatch 代理程式。執行這項操作之後，代理程式會自動湊集和匯入該叢集中執行的下列工作負載的指標。
+ AWS App Mesh
+ NGINX
+ Memcached
+ Java/JMX
+ HAProxy
+ Fluent Bit

您也可以設定代理程式，以湊集和匯入其他 Prometheus 工作負載和來源。

在依照下列步驟安裝 CloudWatch 代理程式進行 Prometheus 指標收集之前，您必須擁有在 Amazon EKS 上執行的叢集，或是在 Amazon EC2 執行個體上執行的 Kubernetes 叢集。

**VPC 安全群組要求**

Prometheus 工作負載安全群組的輸入規則必須開啟到 CloudWatch 代理程式的 Prometheus 連接埠，以便透過私有 IP 擷取 Prometheus 指標。

CloudWatch 代理程式安全群組的輸出規則必須允許 CloudWatch 代理程式透過私有 IP 連接至 Prometheus 工作負載的連接埠。

**Topics**
+ [在 Amazon EKS 和 Kubernetes 叢集上安裝具有 Prometheus 指標集合的 CloudWatch 代理程式。](#ContainerInsights-Prometheus-Setup-roles)
+ [湊集其他 Prometheus 來源並匯入這些指標](ContainerInsights-Prometheus-Setup-configure.md)
+ [(選用) 設定範例容器化 Amazon EKS 工作負載範例進行 Prometheus 指標測試](ContainerInsights-Prometheus-Sample-Workloads.md)

## 在 Amazon EKS 和 Kubernetes 叢集上安裝具有 Prometheus 指標集合的 CloudWatch 代理程式。
<a name="ContainerInsights-Prometheus-Setup-roles"></a>

本節說明如何在執行 Amazon EKS 或 Kubernetes 的叢集中設定具有 Prometheus 監控的 CloudWatch 代理程式。執行這項操作之後，代理程式會自動湊集和匯入該叢集中執行的下列工作負載的指標。
+ AWS App Mesh
+ NGINX
+ Memcached
+ Java/JMX
+ HAProxy
+ Fluent Bit

您也可以設定代理程式，以湊集和匯入其他 Prometheus 工作負載和來源。

在依照下列步驟安裝 CloudWatch 代理程式進行 Prometheus 指標收集之前，您必須擁有在 Amazon EKS 上執行的叢集，或是在 Amazon EC2 執行個體上執行的 Kubernetes 叢集。

**VPC 安全群組要求**

Prometheus 工作負載安全群組的輸入規則必須開啟到 CloudWatch 代理程式的 Prometheus 連接埠，以便透過私有 IP 擷取 Prometheus 指標。

CloudWatch 代理程式安全群組的輸出規則必須允許 CloudWatch 代理程式透過私有 IP 連接至 Prometheus 工作負載的連接埠。

**Topics**
+ [設定 IAM 角色](#ContainerInsights-Prometheus-Setup-roles)
+ [安裝 CloudWatch 代理程式以收集 Prometheus 指標](#ContainerInsights-Prometheus-Setup-install-agent)

### 設定 IAM 角色
<a name="ContainerInsights-Prometheus-Setup-roles"></a>

第一步是在叢集中設定必要的 IAM 角色。有兩種方法：
+ 設定服務帳戶的 IAM 角色， 亦稱為*服務角色*。此方法適用於 EC2 啟動類型和 Fargate 啟動類型。
+ 將 IAM 政策新增至用於叢集的 IAM 角色。這只適用於 EC2 啟動類型。

**設定服務角色 (EC2 啟動類型和 Fargate 啟動類型)**

若要設定服務角色，請輸入下列命令。以叢集的名稱取代 *MyCluster*。

```
eksctl create iamserviceaccount \
 --name cwagent-prometheus \
--namespace amazon-cloudwatch \
 --cluster MyCluster \
--attach-policy-arn arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy \
--approve \
--override-existing-serviceaccounts
```

**將政策新增至節點群組的 IAM 角色 (僅適用於 EC2 啟動類型)**

**若要在節點群組中設定 IAM 政策以獲得 Prometheus 支援**

1. 前往 [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/) 開啟 Amazon EC2 主控台。

1. 在導覽窗格中，選擇**執行個體**。

1. 您需要找出叢集的 IAM 角色名稱的字首。若要執行此操作，請選取叢集中執行個體名稱旁的核取方塊，然後選擇**動作**、**安全性**、**修改 IAM 角色**。然後複製 IAM 角色的字首，例如 `eksctl-dev303-workshop-nodegroup`。

1. 在以下網址開啟 IAM 主控台：[https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/)。

1. 在導覽窗格中，選擇**角色**。

1. 使用搜尋方塊尋找您先前在此程序中複製的前綴，然後選擇該角色。

1. 選擇**連接政策**。

1. 使用搜尋方塊尋找 **CloudWatchAgentServerPolicy**。選取 **CloudWatchAgentServerPolicy** 旁的核取方塊，然後選擇 **Attach policy (連接政策)**。

### 安裝 CloudWatch 代理程式以收集 Prometheus 指標
<a name="ContainerInsights-Prometheus-Setup-install-agent"></a>

您必須在叢集中安裝 CloudWatch 代理程式，才能收集指標。如何安裝代理程式會因 Amazon EKS 叢集和 Kubernetes 叢集而有所不同。

**刪除支援 Prometheus 的舊版 CloudWatch 代理程式**

如果您已在叢集中安裝具有 Prometheus 支援的 CloudWatch 代理程式版本，則必須輸入下列命令來刪除該版本。只有具有 Prometheus 支援的舊版代理程式才需要這麼做。您不需要刪除啟用 Container Insights 但不含 Prometheus 支援的 CloudWatch 代理程式。

```
kubectl delete deployment cwagent-prometheus -n amazon-cloudwatch
```

#### 在具有 EC2 啟動類型的 Amazon EKS 叢集上安裝 CloudWatch 代理程式
<a name="ContainerInsights-Prometheus-Setup-install-agent-EKS"></a>

若要在 Amazon EKS 叢集上安裝具有 Prometheus 支援的 CloudWatch 代理程式，請依照下列步驟執行。

**若要在 Amazon EKS 叢集上安裝具有 Prometheus 支援的 CloudWatch 代理程式**

1. 輸入下列命令以檢查是否已建立 `amazon-cloudwatch` 命名空間：

   ```
   kubectl get namespace
   ```

1. 如果結果中未顯示 `amazon-cloudwatch`，請輸入下列命令來建立它：

   ```
   kubectl create namespace amazon-cloudwatch
   ```

1. 若要部署具有預設組態的代理程式，並讓它將資料傳送到其安裝所在的 AWS 區域，請輸入下列命令：

   ```
   kubectl apply -f https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/service/cwagent-prometheus/prometheus-eks.yaml
   ```

   如果要讓代理程式將資料傳送到不同的區域，請依照下列步驟執行：

   1. 輸入下列命令，以下載代理程式的 YAML 檔案：

      ```
      curl -O https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/service/cwagent-prometheus/prometheus-eks.yaml
      ```

   1. 使用文字編輯器開啟檔案，然後搜尋檔案的 `cwagentconfig.json` 區塊。

   1. 新增反白的行，指定您想要的區域：

      ```
      cwagentconfig.json: |
          {
            "agent": {
              "region": "us-east-2"
            },
            "logs": { ...
      ```

   1. 儲存檔案並使用您更新的檔案來部署代理程式。

      ```
      kubectl apply -f prometheus-eks.yaml
      ```

#### 在具有 Fargate 啟動類型的 Amazon EKS 叢集上安裝 CloudWatch 代理程式
<a name="ContainerInsights-Prometheus-Setup-install-agent-EKS-fargate"></a>

若要在具有 Fargate 啟動類型的 Amazon EKS 叢集上安裝具有 Prometheus 支援的 CloudWatch 代理程式，請依照下列步驟執行。

**若要在具有 Fargate 啟動類型的 Amazon EKS 叢集上安裝具有 Prometheus 支援的 CloudWatch 代理程式**

1. 輸入以下命令，為 CloudWatch 代理程式建立 Fargate 描述檔，以便它可以在叢集內執行。以叢集的名稱取代 *MyCluster*。

   ```
   eksctl create fargateprofile --cluster MyCluster \
   --name amazon-cloudwatch \
   --namespace amazon-cloudwatch
   ```

1. 若要安裝 CloudWatch 代理程式，請輸入以下命令。以叢集的名稱取代 *MyCluster*。此名稱會用於存放代理程式所收集之日誌事件的日誌群組名稱中，也用作為代理程式所收集之指標的維度。

   將 *region* 取代為您想要傳送指標的區域名稱。例如 `us-west-1`。

   ```
   curl https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/service/cwagent-prometheus/prometheus-eks-fargate.yaml | 
   sed "s/{{cluster_name}}/MyCluster/;s/{{region_name}}/region/" | 
   kubectl apply -f -
   ```

#### 在 Kubernetes 叢集上安裝 CloudWatch 代理程式
<a name="ContainerInsights-Prometheus-Setup-install-agent-Kubernetes"></a>

若要在執行 Kubernetes 的叢集上安裝具有 Prometheus 支援的 CloudWatch 代理程式，請輸入下列命令：

```
curl https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/service/cwagent-prometheus/prometheus-k8s.yaml | 
sed "s/{{cluster_name}}/MyCluster/;s/{{region_name}}/region/" | 
kubectl apply -f -
```

以叢集的名稱取代 *MyCluster*。此名稱會用於存放代理程式所收集之日誌事件的日誌群組名稱中，也用作為代理程式所收集之指標的維度。

將*區域*取代為 AWS 您要傳送指標的區域名稱。例如 **us-west-1**。

#### 確認代理程式正在執行中
<a name="ContainerInsights-Prometheus-Setup-install-agent-verify"></a>

在 Amazon EKS 和 Kubernetes 叢集上，您可以輸入下列命令，以確認代理程式正在執行。

```
kubectl get pod -l "app=cwagent-prometheus" -n amazon-cloudwatch
```

如果結果包含處於 `Running` 狀態的單一 CloudWatch 代理程式 pod，則表示代理程式正在執行並且正在收集 Prometheus 指標。預設情況下，CloudWatch 代理程式每分鐘會收集 App Mesh、NGINX，Memcached、Java/JMX 和 HAProxy 的指標。如需這些指標的詳細資訊，請參閱 [CloudWatch 代理程式收集的 Prometheus 指標](ContainerInsights-Prometheus-metrics.md)。如需如何在 CloudWatch 中查看 Prometheus 指標的相關指示，請參閱 [檢視 Prometheus 指標](ContainerInsights-Prometheus-viewmetrics.md)

您也可以將 CloudWatch 代理程式設定為從其他 Prometheus 匯出工具收集指標。如需詳細資訊，請參閱[湊集其他 Prometheus 來源並匯入這些指標](ContainerInsights-Prometheus-Setup-configure.md)。