Docker容器监控告警怎么配置才生效?
我用 Prometheus + cAdvisor 监控 Docker 容器,但设置的告警规则一直不触发,明明内存使用都超了。
我试过在 alert.rules 里加了这条规则:
groups:
- name: docker-alerts
rules:
- alert: HighContainerMemoryUsage
expr: container_memory_usage_bytes / container_spec_memory_limit_bytes > 0.8
for: 1m
labels:
severity: warning
annotations:
summary: "High memory usage on {{ $labels.instance }}"
但 Prometheus 的 Alerts 页面显示“pending”,就是不变成 firing。cAdvisor 数据是有的,container_memory_usage_bytes 指标也能查到。是不是表达式写错了?还是单位没对上?
你的容器大概率没设置内存 limit,导致
container_spec_memory_limit_bytes返回 0,除法结果变成 Inf 或 NaN,Prometheus 直接忽略这条记录,所以一直 pending。代码放这了,加上过滤条件就行:
核心就是加那个
and container_spec_memory_limit_bytes > 0,把没设 limit 的容器过滤掉。如果你想监控所有容器,不管有没有设 limit,可以用机器总内存做基准:
改完记得重启 Prometheus 或者热加载一下配置:
还有个事,
container_memory_usage_bytes包含了 cache,如果你只想看实际使用内存,用container_memory_working_set_bytes更准。