Prometheus - 查询以检测过去 24 小时内存在的 pods 数量

Prometheus - query to detect the number of pods that existed in the last 24 hours

我还在努力学习 PromQL。我写这个查询是为了检测给定命名空间中过去 24 小时内存在的 kubernetes pods 的数量。

我这里的流程是:

count(increase(kube_pod_created{namespace=~".*-airflow"}[1d]))

有没有懂prometheus的朋友告诉我这个逻辑是不是这样?因为它不是正常的 database/etc,所以我无法弄清楚如何验证此查询。不过,当扩展到一天时,它“看起来”可能会做正确的事情。

我建议用 count_over_time() 替换 increase(),因为 increase 可能会错过 short-living pods 且生命周期小于 2 倍的抓取间隔。以下查询应该 return 过去 24 小时内看到的 pods 总数:

count(count_over_time(kube_pod_created{namespace=~".*airflow"}[24h]))

以下查询应该 return 过去 24 小时内存在的 pods 数量:

count(last_over_time(kube_pod_created[24h]))

过去 24 小时内 pods 的 last_over_time(kube_pod_created[24h]) returns 时间序列(参见 last_over_time() docs). The count() returns 此类时间的数量系列,等于过去 24 小时内存在的 pods 个数。