

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 亚马逊 EKS 和 Kubernetes 的指标
<a name="kubernetes-eks-metrics"></a>

Kubernetes 提供了一个指标 API，允许您访问资源使用率指标（例如，节点和 Pod 的 CPU 和内存使用情况），但该 API 仅提供 point-in-time信息，不提供历史指标。[Kubernetes 指标服务器通常用于 Amazon EKS](https://github.com/kubernetes-sigs/metrics-server) [和 Kubernetes 部署，以汇总指标，提供有关指标的短期历史信息，并支持横向 Pod Autoscaler 等功能。](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/)

Amazon EKS 通过 Kubernetes API 服务器以 [Prometheus 格式公开控制平面指标，并且可以捕获和摄取这些](https://docs.aws.amazon.com//eks/latest/userguide/prometheus.html)指标。 CloudWatch CloudWatch 还可以将容器见解配置为为您的 Amazon EKS 节点和 pod 提供全面的指标捕获、分析和警报。

## Kubernetes 控制平面指标
<a name="kubernetes-control-plane-metrics"></a>

Kubernetes 使用 HTTP API 端点以 Prometheus 格式公开控制平面指标。`/metrics`[你应该在你的 Kubernetes 集群中安装 Prometheus，以便使用网络浏览器绘制和查看这些指标。](https://prometheus.io/)你也可以将 Kubernetes [API 服务器公开的指标提取](https://docs.aws.amazon.com//AmazonCloudWatch/latest/monitoring/ContainerInsights-Prometheus-Setup-configure.html#ContainerInsights-Prometheus-Setup-new-exporters)到。 CloudWatch

## Kubernetes 的节点和系统指标
<a name="kubernetes-node-system-metrics"></a>

Kubernetes 提供了 Prometheus 指标[服务器容器，你可以在 Kubernetes 集群](https://github.com/kubernetes-sigs/metrics-server)上[部署和运行这些容器，以获取集群、节点和](https://docs.aws.amazon.com//eks/latest/userguide/metrics-server.html) pod 级别的 CPU 和内存统计信息。这些指标用于[水平吊舱自动扩缩器和[垂直](https://docs.aws.amazon.com/eks/latest/userguide/vertical-pod-autoscaler.html)吊舱自动扩缩器](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/)。 CloudWatch 也可以提供这些指标。

如果您使用 Kubernetes [控制面板或水平和垂直容器自动扩缩器，则应安装 Kubernetes](https://github.com/kubernetes/dashboard) 指标服务器。Kubernetes 控制面板可帮助您浏览和配置 Kubernetes 集群、节点、容器和相关配置，并从 Kubernetes 指标服务器查看 CPU 和内存指标。

Kubernetes 指标服务器提供的指标不能用于非自动扩展目的（例如，监控）。这些指标用于 point-in-time分析，而不是历史分析。Kubernetes 控制面板部署用于在短时间内存储`dashboard-metrics-scraper`来自 Kubernetes 指标服务器的指标。

Container Insights 使用在 Kubernetes 中运行的 CloudWatch 代理的容器化版本 DaemonSet 来发现集群中所有正在运行的容器并提供节点级指标。它收集性能堆栈每一层的性能数据。您可以使用快速入门中的 AWS 快速入门或单独配置容器见解。Quick Start 使用 CloudWatch 代理设置指标监控和使用 Fluent Bit 进行日志记录，因此您只需要部署一次即可进行日志记录和监控。

由于 Amazon EKS 节点是 EC2 实例，因此除了容器洞察捕获的指标外，您还应使用您为 Amazon EC2 定义的标准来捕获系统级指标。您可以使用本指南[为 CloudWatch 代理部署和配置设置状态管理器和分发服务器](install-cloudwatch-systems-manager.md#set-up-systems-manager-distributor)部分中的相同方法为您的 Amazon EKS 集群安装和配置 CloudWatch 代理。您可以更新您的 Amazon EKS 特定 CloudWatch配置文件以包含指标以及您的 Amazon EKS 特定日志配置。

[支持 Prometheus 的 CloudWatch 代理可以自动从支持的容器化工作负载和系统中发现和抓取 Prometheus 指标。](https://docs.aws.amazon.com//AmazonCloudWatch/latest/monitoring/ContainerInsights-Prometheus.html)它会将它们作为嵌入式指标格式的 CloudWatch 日志摄取，以便使用 L CloudWatch ogs Insights 进行分析，并自动创建 CloudWatch指标。

**重要**  
您必须[部署 CloudWatch 代理的专用版本](https://docs.aws.amazon.com//AmazonCloudWatch/latest/monitoring/ContainerInsights-Prometheus-Setup.html#ContainerInsights-Prometheus-Setup-install-agent)才能收集 Prometheus 指标。这是与为 Container Insights 部署的 CloudWatch 代理不同的代理。您可以使用 [prometheus\$1jmx 示例 J](https://github.com/aws-samples/logging-monitoring-apg-guide-examples/tree/main/examples/eks/prometheus_jmx) ava 应用程序（其中包括代理 CloudWatch 和 Amazon EKS 容器部署的部署和配置文件）来演示 Prometheus 指标发现。有关更多信息，请参阅文档中的[在 Amazon EKS 和 Kubernetes 上设置 Java/JMX 示例工作负载](https://docs.aws.amazon.com//AmazonCloudWatch/latest/monitoring/ContainerInsights-Prometheus-Sample-Workloads-javajmx.html)。 CloudWatch您还可以将 CloudWatch 代理配置为从您的 Amazon EKS 集群中运行的其他 Prometheus 目标捕获指标。

## 应用程序指标
<a name="application-metrics-eks"></a>

您可以使用[CloudWatch嵌入式指标格式创建自己的自定义指标。](https://docs.aws.amazon.com//AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format.html)要提取嵌入式指标格式语句，您需要将嵌入式指标格式条目发送到嵌入式指标格式端点。可以将 CloudWatch 代理配置为您的 [Amazon EKS 容器中的边车容器](https://docs.aws.amazon.com//AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Generation_CloudWatch_Agent.html)。 CloudWatch 代理配置存储为 Kubernetes， ConfigMap 并由您的 CloudWatch 代理 sidecar 容器读取以启动嵌入式指标格式端点。

您还可以将应用程序设置为 Prometheus 目标，并在支持 Prometheus 的支持下配置 CloudWatch代理，以发现、抓取和提取您的指标。 CloudWatch例如，您可以将[开源 JMX 导出器与 J](https://github.com/prometheus/jmx_exporter) ava 应用程序一起使用，公开 JMX Beans 供代理使用 Prometheus。 CloudWatch 

如果您不想使用嵌入式指标格式，也可以使用 [AWS API](https://docs.aws.amazon.com//AmazonCloudWatch/latest/APIReference/Welcome.html) 或 [AWS SDK](https://aws.amazon.com/developer/tools/) 创建和更新 CloudWatch指标。但是，我们不建议使用这种方法，因为它将监控和应用程序逻辑混为一谈。

## Fargate 上亚马逊 EKS 的指标
<a name="metrics-fargate-eks-workloads"></a>

Fargate 会自动配置 Amazon EKS 节点来运行你的 Kubernetes pod，因此你无需监控和收集节点级指标。但是，您必须监控在 Fargate 上的 Amazon EKS 节点上运行的 Pod 的指标。Fargate 上的 Amazon EKS 目前不支持容器见解，因为它需要以下目前不支持的功能：
+ DaemonSets 目前不支持。Container Insights 是通过 DaemonSet 在每个集群节点上以身份运行 CloudWatch 代理来部署的。
+ HostPath 不支持永久卷。 CloudWatch 代理容器使用 HostPath 永久卷作为收集容器指标数据的先决条件。
+ Fargate 禁止特权容器和访问主机信息。

您可以使用 [Fargate 的内置日志路由器](https://docs.aws.amazon.com//eks/latest/userguide/fargate-logging.html)向发送嵌入式指标格式语句。 CloudWatch日志路由器使用 Fluent Bit，它有一个可以配置为支持嵌入式指标格式语句的 CloudWatch 插件。

您可以在 Amazon EKS 集群中部署 Prometheus 服务器，从 Fargate 节点收集指标，从而检索和捕获 Fargate 节点的吊舱级别指标。由于 Prometheus 需要永久存储，因此如果您使用亚马逊弹性文件系统 (Amazon EFS) 进行永久存储，则可以在 Fargate 上部署 Prometheus。您也可以在亚马逊 EC2 支持的节点上部署 Prometheus。有关更多信息，请参阅博[客上关于 AWS Fargate 使用 Prometheus 和 Grafana 的监控 Amazon EKS。](https://aws.amazon.com//blogs/containers/monitoring-amazon-eks-on-aws-fargate-using-prometheus-and-grafana/) AWS 