
Prometheus 监控 Kube-state-metrics 终极实战Kubernetes 对象状态可观测性全解析在 Kubernetes 集群中cAdvisor 告诉我们容器用了多少 CPU/内存而kube-state-metrics则回答“当前有多少个 Pod 处于 Running多少 Deployment 不可用节点是否就绪Job 是否成功”这类面向 Kubernetes 对象状态的问题。它监听 API Server将 Deployment、Pod、Node、DaemonSet、StatefulSet、Job、CronJob、PVC 等资源的期望与当前状态转化为 Prometheus 指标是构建 K8s 集群可观测性不可或缺的一环。本文将带你从部署、指标解读到生产告警彻底掌握基于 kube-state-metrics 的 K8s 状态监控。1. 为什么是 kube-state-metrics它与 cAdvisor 有何不同工具关注点指标示例cAdvisor容器资源使用CPU、内存、磁盘、网络container_cpu_usage_seconds_totalkube-state-metricsK8s 对象的状态和元数据kube_deployment_status_replicas_available两者完全互补cAdvisor 负责“用多少”kube-state-metrics 负责“有没有、对不对”。2. 部署 kube-state-metrics推荐使用官方 Helm Chart 或直接 YAML 部署。2.1 使用 Helm 部署最简单helm repoaddprometheus-community https://prometheus-community.github.io/helm-charts helminstallkube-state-metrics prometheus-community/kube-state-metrics\--namespacekube-system2.2 使用 Kubernetes Deployment Service生产常见可直接从官方仓库获取最新 manifestkubectl apply-fhttps://github.com/kubernetes/kube-state-metrics/releases/latest/download/kube-state-metrics.yaml这会创建 Deployment、ServiceAccount、ClusterRole需要读取集群资源、Service暴露端口 8080。默认监听在:8080/metrics端点。验证kubectl port-forward svc/kube-state-metrics8080:8080-nkube-systemcurlhttp://localhost:8080/metrics应看到大量以kube_开头的指标。3. 配置 Prometheus 抓取如果使用 Prometheus Operator通常会通过ServiceMonitor或PodMonitor自动发现。确保 Service 带有正确的标签例如 Helm 安装时自动配置。手动添加静态抓取配置scrape_configs:-job_name:kube-state-metricsscrape_interval:30sstatic_configs:-targets:[kube-state-metrics.kube-system.svc.cluster.local:8080]labels:cluster:prod-cluster如果 Prometheus 在集群外需通过 NodePort 或 LoadBalancer 暴露并配置 TLS 与认证。4. 核心监控指标与 PromQLkube-state-metrics 指标以kube_为前缀按资源类型分组标签包含namespace、deployment、node、pod等。4.1 Deployment 与 StatefulSet指标含义kube_deployment_status_replicas_available可用的副本数kube_deployment_status_replicas_desired期望副本数kube_deployment_status_replicas_updated已更新的副本数kube_statefulset_status_replicas_readyStatefulSet 就绪副本PromQL 示例Deployment 不可用可用副本小于期望kube_deployment_status_replicas_available kube_deployment_spec_replicas滚动更新停滞已更新副本不等于期望且可用副本不等于期望kube_deployment_status_replicas_updated ! kube_deployment_spec_replicas and kube_deployment_status_replicas_available ! kube_deployment_spec_replicas4.2 Pod 状态指标含义kube_pod_status_phasePod 当前阶段Pending0, Running1, Succeeded2, Failed3, Unknown4kube_pod_status_readyPod 是否就绪1Ready0Not Readykube_pod_container_status_restarts_total容器重启次数PromQL处于 CrashLoopBackOff 的 Pod 数量通过重启次数和状态组合increase(kube_pod_container_status_restarts_total[5m]) 0非 Running 且非 Succeeded 的 Podkube_pod_status_phase{phase!~Running|Succeeded} 0长时间未就绪的 Podkube_pod_status_ready 0 and kube_pod_status_phase 14.3 Node 状态指标含义kube_node_status_condition节点条件conditionReadystatustrue/false/unknownkube_node_spec_unschedulable节点是否已被 cordon不可调度kube_node_status_allocatable可分配资源CPU、内存PromQL节点 NotReadykube_node_status_condition{conditionReady,statustrue} ! 1节点被 cordonkube_node_spec_unschedulable 14.4 Job 与 CronJob指标含义kube_job_status_failedJob 失败数1失败kube_job_status_succeededJob 成功数kube_cronjob_spec_suspendCronJob 是否被挂起告警Job 失败kube_job_status_failed{job_name~.*} 04.5 PVC 与存储指标含义kube_persistentvolumeclaim_status_phasePVC 状态Bound0, Pending1, Lost2kube_persistentvolume_status_phasePV 状态告警未绑定的 PVCkube_persistentvolumeclaim_status_phase{phasePending} 04.6 资源配额指标含义kube_resourcequotaResourceQuota 已用/硬限制可用于容量规划。5. Grafana 仪表盘推荐Kubernetes Cluster (Prometheus)Dashboard ID13332结合 kube-state-metrics 与 cAdvisor提供 Deployment、Pod、Node、资源配额的全面视图。K8s Object StatesID13824更侧重对象状态。Kubernetes kube-state-metricsID10856经典独立仪表板。导入后选择数据源并将cluster变量与集群标签绑定。6. 告警规则实战groups:-name:kubernetes_state_alertsrules:-alert:K8sNodeNotReadyexpr:kube_node_status_condition{conditionReady,statustrue}!1for:5mlabels:severity:criticalannotations:summary:节点 {{ $labels.node }} 处于 NotReady 状态超过 5 分钟-alert:K8sDeploymentUnavailableexpr:kube_deployment_status_replicas_available kube_deployment_spec_replicasfor:5mlabels:severity:criticalannotations:summary:Deployment {{ $labels.namespace }}/{{ $labels.deployment }} 可用副本不足-alert:K8sPodCrashLoopBackOffexpr:increase(kube_pod_container_status_restarts_total[10m])3labels:severity:warningannotations:summary:Pod {{ $labels.namespace }}/{{ $labels.pod }} 中容器频繁重启-alert:K8sPodNotReadyexpr:kube_pod_status_ready 0 and kube_pod_status_phase 1for:10mlabels:severity:warningannotations:summary:Pod {{ $labels.namespace }}/{{ $labels.pod }} 处于 Running 但未就绪-alert:K8sJobFailedexpr:kube_job_status_failed0labels:severity:criticalannotations:summary:Job {{ $labels.namespace }}/{{ $labels.job_name }} 执行失败-alert:K8sPersistentVolumeClaimPendingexpr:kube_persistentvolumeclaim_status_phase{phasePending}0for:30mlabels:severity:warningannotations:summary:PVC {{ $labels.namespace }}/{{ $labels.persistentvolumeclaim }} 长期处于 Pending-alert:K8sCronJobSuspendedexpr:kube_cronjob_spec_suspend 1labels:severity:infoannotations:summary:CronJob {{ $labels.namespace }}/{{ $labels.cronjob }} 已被挂起7. 进阶多集群、安全与性能7.1 多集群监控每个集群内部署 kube-state-metricsPrometheus 使用联邦模式或 Thanos/Cortex 进行聚合通过在抓取时注入cluster标签区分集群。7.2 指标基数控制kube-state-metrics 会为每个 Pod、每个容器暴露指标在大规模集群10k Pods中指标数量可能爆炸。可以通过以下方式缓解使用--metric-allowlist或--metric-denylist限制暴露的指标。在 Prometheus 抓取侧使用metric_relabel_configs丢弃不需要的标签或指标。部署多个 kube-state-metrics 实例每个负责特定资源如单独部署一个只收集节点指标的实例。7.3 RBAC 与安全kube-state-metrics 需要读取集群范围内的资源务必使用只读的 ClusterRole切勿授予写权限。在私有环境中Service 端口通过内网访问即可避免暴露到公网。8. 总结Kube-state-metrics 是 Kubernetes 可观测性三大基石之一另外两个是 cAdvisor 和 kube-event。它让集群中每一个 Deployment 的副本、每一个 Node 的健康、每一个 Job 的成败都成为可查询、可告警的时序指标。结合 Prometheus 和 Alertmanager你可以在集群状态偏离预期时第一时间收到通知真正做到 “GitOps 期望 vs 集群实际” 的持续校验。部署它让你的 K8s 集群不再是黑盒而是一张透明的资源地图。