

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

# Amazon EKS 和 Kubernetes 叢集上的 Prometheus 指標故障診斷
<a name="ContainerInsights-Prometheus-troubleshooting-EKS"></a>

本節提供對 Amazon EKS 和 Kubernetes 叢集上的 Prometheus 指標設定進行故障診斷的說明。

## Amazon EKS 上的一般故障診斷步驟
<a name="ContainerInsights-Prometheus-troubleshooting-general"></a>

若要確認 CloudWatch 代理程式正在執行中，請輸入下列命令：

```
kubectl get pod -n amazon-cloudwatch
```

輸出應該在 `NAME` 欄中包含 `cwagent-prometheus-id` 一行和 `STATUS column.` 中包含 `Running` 一行

若要顯示有關執行中 pod 的詳細資訊，請輸入下列命令。將 *pod-name* 取代為 pod 的完整名稱，即以 `cw-agent-prometheus` 為開頭的名稱。

```
kubectl describe pod pod-name -n amazon-cloudwatch
```

如果您已安裝 CloudWatch Container Insights，則可以使用 CloudWatch Logs Insights 查詢來自收集 Prometheus 指標之 CloudWatch 代理程式的日誌。

**查詢應用程式日誌**

1. 透過 [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/) 開啟 CloudWatch 主控台。

1. 在導覽窗格中，選擇 **CloudWatch Logs Insights**。

1. 選取應用程式日誌的日誌群組，**/aws/containerinsights/*cluster-name*/application**

1. 以下列查詢取代搜尋查詢表達式，然後選擇 **Run query (執行查詢)**

   ```
   fields ispresent(kubernetes.pod_name) as haskubernetes_pod_name, stream, kubernetes.pod_name, log | 
   filter haskubernetes_pod_name and kubernetes.pod_name like /cwagent-prometheus
   ```

您也可以確認 Prometheus 指標和中繼資料正擷取為 CloudWatch Logs 事件。

**確認 Prometheus 資料正擷取中**

1. 透過 [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/) 開啟 CloudWatch 主控台。

1. 在導覽窗格中，選擇 **CloudWatch Logs Insights**。

1. 選擇 **/aws/containerinsights/*cluster-name*/prometheus**

1. 以下列查詢取代搜尋查詢表達式，然後選擇 **Run query (執行查詢)**

   ```
   fields @timestamp, @message | sort @timestamp desc | limit 20
   ```

## 記錄日誌已捨棄 Prometheus 指標
<a name="ContainerInsights-Prometheus-troubleshooting-droppedmetrics"></a>

此版本不會收集長條圖類型的 Prometheus 指標。您可以使用 CloudWatch 代理程式來檢查是否有任何 Prometheus 指標是因為屬於長條圖指標而遭到捨棄。您也可以記錄前 500 個因為是長條圖指標而遭到捨棄且未傳送到 CloudWatch 的 Prometheus 指標清單。

若要查看是否有任何指標遭到捨棄，請輸入下列命令：

```
kubectl logs -l "app=cwagent-prometheus" -n amazon-cloudwatch --tail=-1
```

如果有任何指標遭到捨棄，您會在 `/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log` 檔案中看到以下各行。

```
I! Drop Prometheus metrics with unsupported types. Only Gauge, Counter and Summary are supported.
I! Please enable CWAgent debug mode to view the first 500 dropped metrics
```

如果您看到這些行，而且想要知道哪些指標遭到捨棄，請使用下列步驟。

**記錄捨棄的 Prometheus 指標量清單**

1. 將下列粗體行新增至您的 `prometheus-eks.yaml` 或 `prometheus-k8s.yaml` 檔案，將 CloudWatch 代理程式變更為偵錯模式，然後儲存該檔案。

   ```
   {
         "agent": {
           "debug": true
         },
   ```

   檔案的這個區段看起來應該像這樣：

   ```
   cwagentconfig.json: |
       {
         "agent": {
           "debug": true
         },
         "logs": {
           "metrics_collected": {
   ```

1. 輸入下列命令，重新安裝 CloudWatch 代理程式以啟動偵錯模式：

   ```
   kubectl delete deployment cwagent-prometheus -n amazon-cloudwatch
   kubectl apply -f prometheus.yaml
   ```

   捨棄的指標會記錄在 CloudWatch 代理程式 pod 中。

1. 若要從 CloudWatch 代理程式 pod 擷取日誌，請輸入下列命令：

   ```
   kubectl logs -l "app=cwagent-prometheus" -n amazon-cloudwatch --tail=-1
   ```

   或者，如果您已安裝 Container Insights Fluentd 日誌記錄，則日誌也會儲存在 CloudWatch Logs 日誌群組 **/aws/containerinsights/*cluster\$1name*/application** 中。

   若要查詢這些日誌，您可以遵循[Amazon EKS 上的一般故障診斷步驟](#ContainerInsights-Prometheus-troubleshooting-general)中的步驟查詢應用程式日誌。

## Prometheus 指標擷取為 CloudWatch Logs 日誌事件的位置在哪裡？
<a name="ContainerInsights-Prometheus-troubleshooting-metrics_ingested"></a>

CloudWatch 代理程式會為每個 Prometheus 湊集任務組態建立日誌串流。例如，在 `prometheus-eks.yaml` 和 `prometheus-k8s.yaml` 檔案中，`job_name: 'kubernetes-pod-appmesh-envoy'` 一行會湊集 App Mesh 指標。Prometheus 目標會定義為 `kubernetes-pod-appmesh-envoy`。因此，所有 App Mesh Prometheus 指標都會在名為 **/aws/containerinsights/cluster-name/Prometheus** 的日誌群組下的日誌串流 **kubernetes-pod-appmesh-envoy** 中擷取為 CloudWatch Logs 事件。

## 我在 CloudWatch 指標中看不到 Amazon EKS 或 Kubernetes Prometheus 指標
<a name="ContainerInsights-Prometheus-troubleshooting-no-metrics"></a>

首先，請確定 Prometheus 指標在日誌群組 **/aws/containerinsights/cluster-name/Prometheus** 中擷取為日誌事件。使用 [Prometheus 指標擷取為 CloudWatch Logs 日誌事件的位置在哪裡？](#ContainerInsights-Prometheus-troubleshooting-metrics_ingested) 中的資訊來協助您檢查目標日誌串流。如果未建立日誌串流，或是日誌串流中沒有新的日誌事件，請檢查下列項目：
+ 檢查 Prometheus 指標匯出工具端點是否已正確設定
+ 檢查 CloudWatch 代理程式 YAML 檔案 `config map: cwagent-prometheus` 區段中的 Prometheus 湊集組態是否正確。該組態應該與 Prometheus 組態檔中的組態相同。如需詳細資訊，請參閱 Prometheus 文件中的 [<scrape\$1config>](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config)。

如果 Prometheus 指標正確擷取為日誌事件，請檢查內嵌指標格式設定是否已新增至日誌事件，以產生 CloudWatch 指標。

```
"CloudWatchMetrics":[
   {
      "Metrics":[
         {
            "Name":"envoy_http_downstream_cx_destroy_remote_active_rq"
         }
      ],
      "Dimensions":[
         [
            "ClusterName",
            "Namespace"
         ]
      ],
      "Namespace":"ContainerInsights/Prometheus"
   }
],
```

如需內嵌指標格式的詳細資訊，請參閱[規格：內嵌指標格式](CloudWatch_Embedded_Metric_Format_Specification.md)。

如果日誌事件中沒有內嵌指標格式，請檢查 CloudWatch 代理程式安裝 YAML 檔案 `config map: prometheus-cwagentconfig` 區段中的 `metric_declaration` 區段設定是否正確。如需詳細資訊，請參閱[新增 Prometheus 湊集目標的教學課程：Prometheus API 伺服器指標](ContainerInsights-Prometheus-Setup-configure.md#ContainerInsights-Prometheus-Setup-new-exporters)。