您可以配置 Google Kubernetes Engine (GKE) 集群,以使用 Google Cloud Managed Service for Prometheus 将一组精选的 kube 状态指标(包括 Pod、Deployment 等指标)发送到 Cloud Monitoring。本文档介绍了这些指标在写入 Cloud Monitoring 时如何设置其格式,以及如何查询这些指标。本文档还提供了一些表,其中列出了每个集合中的指标,并提供了有关如何使用这些指标的信息。
您必须先启用收集功能,然后才能使用 kube 状态指标。
指标格式
写入 Cloud Monitoring 的所有 Kubernetes kube 状态指标都使用资源类型 prometheus_target
。每个指标名称都以 prometheus.googleapis.com/
为前缀,并带有表示 Prometheus 指标类型的后缀,例如 /gauge
、/histogram
或 /counter
。否则,每个指标名称都与开源 Kubernetes 公开的指标名称相同。
从 Cloud Monitoring 导出
您可以使用 Cloud Monitoring API 从 Cloud Monitoring 导出 kube 状态指标。由于所有 kube 状态指标均使用 Google Cloud Managed Service for Prometheus 注入,因此您可以使用 Prometheus Query Language (PromQL) 查询 kube 状态指标。您还可以使用 Monitoring Query Language (MQL) 进行查询。
查询指标
查询 kube 状态指标时,您使用的名称取决于您使用的是 PromQL 还是基于 Cloud Monitoring 的功能,例如 MQL 或 Metrics Explorer 菜单驱动的界面。
以下 kube 状态指标表展示了每个指标名称的两个版本:
- PromQL 指标名称:在 Google Cloud 控制台的 Cloud Monitoring 页面中或 Cloud Monitoring API 的 PromQL 字段中使用 PromQL 时,请使用 PromQL 字段名称。
- Cloud Monitoring 指标名称:使用其他 Cloud Monitoring 功能时,请使用下表中的 Cloud Monitoring 指标名称。此名称必须以
prometheus.googleapis.com/
为前缀,表中的条目省略了该前缀。
存储空间指标
此表中的 Cloud Monitoring 指标名称必须以 prometheus.googleapis.com/
为前缀。表中的条目已省略该前缀。
PromQL 指标名称 Cloud Monitoring 指标名称 |
|
---|---|
种类、类型、单位 受监控的资源 所需的 GKE 版本 |
说明 标签 |
kube_persistentvolume_capacity_bytes kube_persistentvolume_capacity_bytes/gauge
|
|
GAUGE 、DOUBLE 、By
prometheus_target 1.27.2-gke.1200 |
Persistentvolume 容量(以字节为单位)。每 30 秒采样一次。persistentvolume :persistentvolume。
|
kube_persistentvolume_claim_ref kube_persistentvolume_claim_ref/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
有关永久性卷声明引用的信息。每 30 秒采样一次。claim_name :claim_name。
name :name。
persistentvolume :persistentvolume。
|
kube_persistentvolume_info kube_persistentvolume_info/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
有关 persistentvolume 的信息。每 30 秒采样一次。csi_driver :csi_driver。
csi_volume_handle :csi_volume_handle。
local_fs :local_fs。
local_path :local_path。
persistentvolume :persistentvolume。
storageclass :storageclass。
|
kube_persistentvolume_status_phase kube_persistentvolume_status_phase/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
此阶段指示卷是可用、绑定到声明,还是由声明释放。每 30 秒采样一次。persistentvolume :persistentvolume。
phase :phase。
|
kube_persistentvolumeclaim_info kube_persistentvolumeclaim_info/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
有关永久性卷声明的信息。每 30 秒采样一次。persistentvolumeclaim :persistentvolumeclaim。
storageclass :storageclass。
volumename :volumename。
|
kube_persistentvolumeclaim_resource_requests_storage_bytes kube_persistentvolumeclaim_resource_requests_storage_bytes/gauge
|
|
GAUGE 、DOUBLE 、By
prometheus_target 1.27.2-gke.1200 |
永久性卷声明所请求的存储空间容量。每 30 秒采样一次。persistentvolumeclaim :persistentvolumeclaim。
|
kube_persistentvolumeclaim_status_phase kube_persistentvolumeclaim_status_phase/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
永久性卷声明当前所处的阶段。每 30 秒采样一次。persistentvolumeclaim :persistentvolumeclaim。
phase :phase。
|
如需了解详情,请参阅 PersistentVolume 指标和 PersistentVolumeClaim 指标。
Pod 指标
Pod 是由一个或多个容器组成的群组,其中包含如何运行共享容器和网络资源的规范。
Pod 指标表
借助 Pod 指标,您可以监控 Pod 的行为并发出提醒。下表显示了 kube 状态指标数据包中提供的指标:
此表中的 Cloud Monitoring 指标名称必须以 prometheus.googleapis.com/
为前缀。表中的条目已省略该前缀。
PromQL 指标名称 Cloud Monitoring 指标名称 |
|
---|---|
种类、类型、单位 受监控的资源 所需的 GKE 版本 |
说明 标签 |
kube_pod_container_status_ready kube_pod_container_status_ready/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
描述容器就绪性检查是否成功。每 30 秒采样一次。container :container。
pod :pod。
uid :uid。
|
kube_pod_container_status_waiting_reason kube_pod_status_container_status_waiting_reason/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
描述容器当前处于等待状态的原因。每 30 秒采样一次。container :container。
pod :pod。
reason :reason。
uid :uid。
|
kube_pod_status_phase kube_pod_status_phase/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
pod 当前阶段。每 30 秒采样一次。phase :phase。
pod :pod。
uid :uid。
|
kube_pod_status_unschedulable kube_pod_status_unschedulable/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
描述 pod 不可调度的状态。每 30 秒采样一次。pod :pod。
uid :uid。
|
如需了解详情,请参阅 Pod 指标。
Pod 指标的查询示例
如需确定您是否有无法安排的 Pod,请使用以下 PromQL 表达式:
sum(kube_pod_status_unschedulable{cluster="CLUSTER", namespace="NAMESPACE"})
如需对命名空间中一些无法安排的 Pod 发出提醒,您可以使用以下 PromQL 表达式:
sum(kube_pod_status_unschedulable{cluster="CLUSTER", namespace="NAMESPACE"}) > LIMIT
通过如下所示的 PromQL 表达式,您可以使用 kube_pod_container_status_waiting_reason
指标为卡在特定等待状态的容器创建提醒:
max_over_time(kube_pod_container_status_waiting_reason{reason="REASON", cluster="CLUSTER", namespace="NAMESPACE"}[5m]) >= 1
REASON 的值指定容器的等待状态,例如:
CrashLoopBackOff
ImagePullBackOff
ContainerCreating
如需为卡在任何等待状态的容器创建提醒,请使用以下 PromQL 表达式:
max_over_time(kube_pod_container_status_waiting_reason{cluster="CLUSTER", namespace="NAMESPACE"}[5m]) >= 1
如需确定未通过就绪性检查的容器数量,请使用以下 PromQL 表达式
sum(kube_pod_container_status_ready) by (pod, container) == 0
本部分中的某些查询和提醒表达式改编自基于 GitHub 上的 Prometheus 社区 Kubernetes Helm 图表代码库中的 kubernetes-apps.yaml
文件。
交互式 Playbook
Kube 状态指标也在 GKE 交互式 Playbook 中用于排查无法安排或崩溃的 Pod 的问题。如需详细了解这些故障模式,请参阅以下问题排查文档:
如果未启用 kube 状态指标数据包,则检测 Pod 调度问题的主要方法是查询“失败的调度”日志事件。启用 kube 状态指标数据包后,您可以使用 kube_pod_status_unschedulable
指标,它的用途相同,但更易于聚合和绘制图表。通过使用指标,您可以查看无法安排的 Pod 数量以及问题出现的时间。
同样,GKE 系统指标 kubernetes.io/container/restart_count
可以帮助您检测崩溃循环的 Pod。kube_pod_container_status_waiting_reason
指标还枚举了崩溃循环 Pod,并且您还可以确定 Pod 是否卡在 CrashLookBackOff
以外的等待状态,如 ImagePullBackOff
和 ContainerCreating
。
如需探索交互式 Playbook,请执行以下操作:
-
在 Google Cloud 控制台的导航面板中,选择 Monitoring,然后选择 信息中心:
- 点击 GCE 类别来过滤信息中心列表。
- 点击列表中的“GKE 互动式 Playbook”信息中心的名称。
部署指标
Deployment 是一个控制器,用于更新 Pod 等资源的状态,以管理发布和关停等事件。
Deployment 指标表
借助 Deployment 指标,您可以监控控制器的行为并发出提醒。下表显示了 kube 状态指标数据包中提供的指标:
此表中的 Cloud Monitoring 指标名称必须以 prometheus.googleapis.com/
为前缀。表中的条目已省略该前缀。
PromQL 指标名称 Cloud Monitoring 指标名称 |
|
---|---|
种类、类型、单位 受监控的资源 所需的 GKE 版本 |
说明 标签 |
kube_deployment_spec_replicas kube_deployment_spec_replicas/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
部署所需的 pod 数量。每 30 秒采样一次。deployment :deployment。
|
kube_deployment_status_replicas_available kube_deployment_status_replicas_available/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
每个部署的可用副本的数量。每 30 秒采样一次。deployment :deployment。
|
kube_deployment_status_replicas_updated kube_deployment_status_replicas_updated/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
每个部署的更新后副本的数量。每 30 秒采样一次。deployment :deployment。
|
如需了解详情,请参阅 Deployment 指标。
Deployment 指标的查询示例
您可以通过按集群、命名空间和 Deployment 的名称过滤 Deployment 指标,为单个 Deployment 创建图表和提醒政策。
例如,如需将可用副本的数量与单个 Deployment 中的预期副本数进行比较,您可以使用以下 PromQL 查询在单个图表中绘制两个指标:
kube_deployment_spec_replicas{cluster="CLUSTER", namespace="NAMESPACE", deployment=DEPLOYMENT"}和
kube_deployment_status_replicas_available{cluster="CLUSTER", namespace="NAMESPACE", deployment=DEPLOYMENT"}
如需对失败或停滞的 Deployment 发出提醒,您可以使用以下 PromQL 表达式:
( kube_deployment_spec_replicas{cluster="CLUSTER", namespace="NAMESPACE", deployment="DEPLOYMENT"} > kube_deployment_status_replicas_available{cluster="CLUSTER", namespace="NAMESPACE", deployment="DEPLOYMENT"} ) and ( changes(kube_deployment_status_replicas_updated{cluster="CLUSTER", namespace="NAMESPACE", deployment="DEPLOYMENT"}[10m]) == 0 )
本部分中的某些查询和提醒表达式改编自基于 GitHub 上的 Prometheus 社区 Kubernetes Helm 图表代码库中的 kubernetes-apps.yaml
文件。
StatefulSet 指标
StatefulSet 是一个控制器,用于管理有状态应用的一组 Pod 的部署和扩缩。此控制器管理 Pod 的排序和唯一性。
StatefulSet 指标表
借助 StatefulSet 指标,您可以监控控制器的行为并发出提醒。下表显示了 kube 状态指标数据包中提供的指标:
此表中的 Cloud Monitoring 指标名称必须以 prometheus.googleapis.com/
为前缀。表中的条目已省略该前缀。
PromQL 指标名称 Cloud Monitoring 指标名称 |
|
---|---|
种类、类型、单位 受监控的资源 所需的 GKE 版本 |
说明 标签 |
kube_statefulset_replicas kube_statefulset_replicas/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
StatefulSet 所需的 pod 数量。每 30 秒采样一次。statefulset :statefulset。
|
kube_statefulset_status_replicas_ready kube_statefulset_status_replicas_ready/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
每个 StatefulSet 的准备就绪副本的数量。每 30 秒采样一次。statefulset :statefulset。
|
kube_statefulset_status_replicas_updated kube_statefulset_status_replicas_updated/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
每个 StatefulSet 的更新后副本的数量。每 30 秒采样一次。statefulset :statefulset。
|
如需了解详情,请参阅 StatefulSet 指标。
StatefulSet 指标的查询示例
您可以按集群、命名空间和 StatefulSet 的名称过滤 StatefulSet 指标,从而为各个 StatefulSet 创建图表和提醒政策。
例如,如需将可用副本的数量与单个 StatefulSet 中的预期副本数进行比较,您可以使用以下 PromQL 查询在单个图表中绘制两个指标:
kube_statefulset_replicas{cluster="CLUSTER", namespace="NAMESPACE", statefulset="STATEFULSET"}和
kube_statefulset_status_replicas_ready{cluster="CLUSTER", namespace="NAMESPACE", statefulset="STATEFULSET"}
如需对失败或 StatefulSet 发布失败发出提醒,您可以使用以下 PromQL 表达式:
( kube_statefulset_replicas{cluster="CLUSTER", namespace="NAMESPACE", statefulset="STATEFULSET"} > kube_statefulset_status_replicas_ready{cluster="CLUSTER", namespace="NAMESPACE", statefulset="STATEFULSET"} ) and ( changes(kube_statefulset_status_replicas_updated{cluster="CLUSTER", namespace="NAMESPACE", statefulset="STATEFULSET"}[10m]) == 0 )
本部分中的某些查询和提醒表达式改编自基于 GitHub 上的 Prometheus 社区 Kubernetes Helm 图表代码库中的 kubernetes-apps.yaml
文件。
DaemonSet 指标
DaemonSet 是一种控制器,可确保一组节点运行一个 Pod 的副本。例如,当节点添加到集群中时,DaemonSet 会将 Pod 添加到节点。此控制器有助于确保某些进程在每个节点上运行。
DaemonSet 指标表
借助 DaemonSet 指标,您可以监控控制器的行为并发出提醒。下表显示了 kube 状态指标数据包中提供的指标:
此表中的 Cloud Monitoring 指标名称必须以 prometheus.googleapis.com/
为前缀。表中的条目已省略该前缀。
PromQL 指标名称 Cloud Monitoring 指标名称 |
|
---|---|
种类、类型、单位 受监控的资源 所需的 GKE 版本 |
说明 标签 |
kube_daemonset_status_desired_number_scheduled kube_daemonset_status_desired_number_scheduled/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
应运行守护程序 pod 的节点数量。每 30 秒采样一次。daemonset :daemonset。
|
kube_daemonset_status_number_misscheduled kube_daemonset_status_number_misscheduled/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
不应运行守护程序 pod 但这样做了的节点数量。每 30 秒采样一次。daemonset :daemonset。
|
kube_daemonset_status_number_ready kube_daemonset_status_number_ready/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
应运行守护程序 pod 并且一个或多个守护程序 pod 正在运行且准备就绪的节点数量。每 30 秒采样一次。daemonset :daemonset。
|
kube_daemonset_status_updated_number_scheduled kube_daemonset_status_updated_number_scheduled/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
运行更新后的守护程序 pod 的节点数量。每 30 秒采样一次。daemonset :daemonset。
|
如需了解详情,请参阅 DaemonSet 指标。
DaemonSet 指标的查询示例
您可以按集群、命名空间和 DaemonSet 的名称过滤守护进程集指标,从而为单个 DaemonSet 创建图表和提醒政策。
例如,如需将可用副本的数量与单个 DaemonSet 中的预期副本数进行比较,您可以使用以下 PromQL 查询在单个图表中绘制两个指标:
kube_daemonset_status_updated_number_scheduled{cluster="CLUSTER", namespace="NAMESPACE", daemonsetset=DAEMONSET"}和
kube_daemonset_status_desired_number_scheduled{cluster="CLUSTER", namespace="NAMESPACE", daemonset=DAEMONSET"}
如需对失败或 DaemonSet 发布失败发出提醒,您可以使用以下 PromQL 表达式:
( ( kube_daemonset_status_number_misscheduled{cluster="CLUSTER", namespace="NAMESPACE", daemonset="DAEMONSET"} != 0 ) or ( kube_daemonset_status_updated_number_scheduled{cluster="CLUSTER", namespace="NAMESPACE", daemonset="DAEMONSET"} != kube_daemonset_status_desired_number_scheduled{cluster="CLUSTER", namespace="NAMESPACE", daemonset="DAEMONSET"} ) ) and ( changes(kube_daemonset_status_updated_number_scheduled{cluster="CLUSTER", namespace="NAMESPACE", daemonset="DAEMONSET"}[5m]) == 0 )
本部分中的某些查询和提醒表达式改编自基于 GitHub 上的 Prometheus 社区 Kubernetes Helm 图表代码库中的 kubernetes-apps.yaml
文件。
HorizontalPodAutoscaler 指标
HorizontalPodAutoscaler (HPA) 是一个控制器,用于响应 CPU 或内存利用率等指标,定期更改工作负载(例如 Deployment 或 StatefulSet)中的 Pod 数量。更改工作负载可用的 Pod 数量可以使工作负载保持快速响应但高效。
如需详细了解 HPA,请参阅查看有关 Pod 横向自动扩缩器的详细信息。
HPA 指标表
HorizontalPodAutoscaler 指标可让您监控和控制器的行为。下表显示了 kube 状态指标数据包中提供的指标:
此表中的 Cloud Monitoring 指标名称必须以 prometheus.googleapis.com/
为前缀。表中的条目已省略该前缀。
PromQL 指标名称 Cloud Monitoring 指标名称 |
|
---|---|
种类、类型、单位 受监控的资源 所需的 GKE 版本 |
说明 标签 |
kube_horizontalpodautoscaler_spec_max_replicas kube_horizontalpodautoscaler_spec_max_replicas/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
自动扩缩器可以设置的 pod 数上限,不能小于 MinReplicas。每 30 秒采样一次。horizontalpodautoscaler :horizontalpodautoscaler。
|
kube_horizontalpodautoscaler_spec_min_replicas kube_horizontalpodautoscaler_spec_min_replicas/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
自动扩缩器可以设置的 pod 数下限,默认值为 1。每 30 秒采样一次。horizontalpodautoscaler :horizontalpodautoscaler。
|
kube_horizontalpodautoscaler_spec_target_metric kube_horizontalpodautoscaler_spec_target_metric/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
在计算所需副本数时此自动扩缩器使用的指标规范。每 30 秒采样一次。horizontalpodautoscaler :horizontalpodautoscaler。
metric_name :metric_name。
metric_target_type :metric_target_type。
|
kube_horizontalpodautoscaler_status_condition kube_horizontalpodautoscaler_status_condition/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
此自动扩缩器的条件。每 30 秒采样一次。condition :condition。
horizontalpodautoscaler :horizontalpodautoscaler。
namespace :namespace。
status :status。
|
kube_horizontalpodautoscaler_status_current_replicas kube_horizontalpodautoscaler_status_status_current_replicas/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
此自动扩缩器管理的 pod 副本的当前数量。每 30 秒采样一次。horizontalpodautoscaler :horizontalpodautoscaler。
|
kube_horizontalpodautoscaler_status_desired_replicas kube_horizontalpodautoscaler_status_desired_replicas/gauge
|
|
GAUGE 、DOUBLE 、1
prometheus_target 1.27.2-gke.1200 |
此自动扩缩器管理的 pod 副本的所需数量。每 30 秒采样一次。horizontalpodautoscaler :horizontalpodautoscaler。
|
如需了解详情,请参阅 Pod 横向自动扩缩器指标。
HPA 指标查询示例
例如,如需确定 HPA 是否接近副本数上限,您可以绘制以下比率:
kube_horizontalpodautoscaler_status_current_replicas{cluster="CLUSTER", namespace="NAMESPACE", horizontalpodautoscaler="HPA"} / kube_horizontalpodautoscaler_spec_max_replicas{cluster="CLUSTER", namespace="NAMESPACE", horizontalpodautoscaler="HPA"}
当 HPA 运行时,副本数量达到上限,您可能需要增加 Pod 数上限规范。您可以使用以下 PromQL 表达式创建提醒,以通知您这种情况:
kube_horizontalpodautoscaler_status_current_replicas{cluster="CLUSTER", namespace="NAMESPACE", horizontalpodautoscaler="HPA"} == kube_horizontalpodautoscaler_spec_max_replicas{cluster="CLUSTER", namespace="NAMESPACE", horizontalpodautoscaler="HPA"}
您还可以比较 kube_horizontalpodautoscaler_status_current_replicas
和 kube_horizontalpodautoscaler_status_desired_replicas
指标的值,以确定当前副本数量和所需副本数量之间是否存在差异。差异可能表示集群中的资源限制条件。以下 PromQL 表达式会查找当前副本数量与所需副本数量、最小和最大副本数量之间的差异,以及当前副本数量的变化:
(kube_horizontalpodautoscaler_status_desired_replicas{job="kube-state-metrics", namespace=~"NAMESPACE"} != kube_horizontalpodautoscaler_status_current_replicas{job="kube-state-metrics", namespace=~"NAMESPACE"}) and (kube_horizontalpodautoscaler_status_current_replicas{job="kube-state-metrics", namespace=~"NAMESPACE"} > kube_horizontalpodautoscaler_spec_min_replicas{job="kube-state-metrics", namespace=~"NAMESPACE"}) and (kube_horizontalpodautoscaler_status_current_replicas{job="kube-state-metrics", namespace=~"NAMESPACE"} < kube_horizontalpodautoscaler_spec_max_replicas{job="kube-state-metrics", namespace=~"NAMESPACE"}) and changes(kube_horizontalpodautoscaler_status_current_replicas{job="kube-state-metrics", namespace=~"NAMESPACE"}[15m]) == 0
kube_horizontalpodautoscaler_status_condition
指标上的 condition
和 status
标签也可以帮助您检测 HPA 何时进入各种故障模式。例如:
- 条件
ScalingLimited
和true
状态表示 HPA 受其最小或最大副本数绑定:kube_horizontalpodautoscaler_status_condition{status="true", condition="ScalingLimited"} == 1
条件
AbleToScale
和false
状态表示 HPA 在提取或更新扩缩时遇到问题:kube_horizontalpodautoscaler_status_condition{status="false", condition="AbleToScale"} == 1
条件
ScalingActive
和false
状态表示 HPA 已停用或无法计算新扩缩:kube_horizontalpodautoscaler_status_condition{status="false", condition="ScalingActive"} == 1
本部分中的某些查询和提醒表达式改编自基于 GitHub 上的 Prometheus 社区 Kubernetes Helm 图表代码库中的 kubernetes-apps.yaml
文件。