

# 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* 替换为名称以 `cw-agent-prometheus` 开头的 pod 的完整名称。

```
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. 使用以下查询替换搜索查询表达式，然后选择**运行查询**

   ```
   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. 使用以下查询替换搜索查询表达式，然后选择**运行查询**

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

## 录入丢弃的 Prometheus 指标
<a name="ContainerInsights-Prometheus-troubleshooting-droppedmetrics"></a>

此版本不会收集直方图的 Prometheus 指标。您可以使用 CloudWatch 代理来检查是否因为任何 Prometheus 指标是直方图指标而丢弃了这些指标。您还可以记录由于是直方图指标，因此丢弃而未发送到 CloudWatch 的前 500 个 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 指标都作为 CloudWatch Logs 事件摄取到名为 **/aws/containerinsights/cluster-name/Prometheus** 的日志组下的 **kubernetes-pod-appmesh-envoy** 日志流中。

## 我在 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)。