You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Grafana中隐藏HPA终止Pod的Gauge指标序列?

解决已终止Pod的Gauge指标序列保留问题

利用应用心跳指标过滤活跃Pod的Gauge序列

你的场景可以通过心跳指标的上报状态识别活跃Pod,具体实现分两步:

  1. 先筛选最近N秒内有心跳上报的Pod(假设心跳指标为kubernetes_counter_my_app{namespace="my_ns", cluster="my_cluster", key="heartbeat"}):
count_over_time(kubernetes_counter_my_app{namespace="my_ns", cluster="my_cluster", key="heartbeat", host=~"my_app.*"}[10s]) > 0

用count_over_time统计10秒内的心跳上报次数,大于0说明Pod仍在活跃上报。

  1. 将上述结果与目标Gauge指标关联,仅保留活跃Pod的序列:
kubernetes_gauge_my_app{namespace="my_ns", cluster="my_cluster", key="concurrent_requests", host=~"my_app.*"}
  and on(host)
count_over_time(kubernetes_counter_my_app{namespace="my_ns", cluster="my_cluster", key="heartbeat", host=~"my_app.*"}[10s]) > 0

on(host)指定按Pod名称(host标签)做关联匹配,自动过滤无心跳的已终止Pod的Gauge序列。

可选:结合Kubernetes原生Pod状态指标过滤

如果你的监控系统采集了Kubernetes原生指标(比如kube-state-metrics输出的指标),也可直接用Pod就绪状态过滤:

kubernetes_gauge_my_app{namespace="my_ns", cluster="my_cluster", key="concurrent_requests", host=~"my_app.*"}
  and on(host)
kube_pod_status_ready{namespace="my_ns", cluster="my_cluster", pod=~"my_app.*", condition="true"} == 1

需确保host标签和kube_pod_status_ready的pod标签对应一致。

注意事项

  • 心跳时间窗口(示例中的10s)可根据实际上报频率调整,比如心跳每秒上报时,设为15s可避免网络波动导致的误判。
  • on(host)是关联核心,能精准匹配同一Pod的两类指标,保证过滤逻辑准确。

内容的提问来源于stack exchange,提问作者Maxim Slavutsky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 19:57:45