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

Prometheus告警规则动态变量配置与内存限制展示咨询

容器内存告警规则优化问题解答

原始告警规则

- alert: high_memory_usage_resource_limits
  expr: >
    (
      sum by (container, pod, namespace) (container_memory_usage_bytes)
    ) 
    / 
    (
      sum by (container, pod, namespace) (kube_pod_container_resource_limits{resource="memory"})
    ) * 100 > 90
  for: 1m
  labels:
    severity: warning
  annotations:
    summary: "High memory usage compared to resource limits on {{$labels.pod}}"
    description: "Container {{$labels.container}} in pod {{$labels.pod}} is consuming more memory than its resource limit ({{$value}}% of the limit)"

问题1:能否将阈值配置为变量并在告警消息中复用?

可以实现,推荐两种实用方式:

方式1:自定义标签复用阈值

无需修改Prometheus全局配置,直接在告警规则的labels中定义阈值标签,后续在annotations中引用即可:

- alert: high_memory_usage_resource_limits
  expr: >
    (
      sum by (container, pod, namespace) (container_memory_usage_bytes)
    ) 
    / 
    (
      sum by (container, pod, namespace) (kube_pod_container_resource_limits{resource="memory"})
    ) * 100 > 90
  for: 1m
  labels:
    severity: warning
    memory_threshold: "90" # 定义阈值标签
  annotations:
    summary: "High memory usage compared to resource limits on {{$labels.pod}}"
    description: "Container {{$labels.container}} in pod {{$labels.pod}} is consuming {{$value}}% of its memory limit, exceeding the threshold of {{$labels.memory_threshold}}%"

方式2:全局变量复用阈值

如果需要在多个告警规则中共享同一阈值,可在Prometheus全局配置中定义变量:

  1. 在prometheus.yml添加全局变量:
global:
  external_labels:
    memory_threshold: 90
  1. 修改告警规则引用变量:
- alert: high_memory_usage_resource_limits
  expr: >
    (
      sum by (container, pod, namespace) (container_memory_usage_bytes)
    ) 
    / 
    (
      sum by (container, pod, namespace) (kube_pod_container_resource_limits{resource="memory"})
    ) * 100 > $memory_threshold
  for: 1m
  labels:
    severity: warning
  annotations:
    summary: "High memory usage compared to resource limits on {{$labels.pod}}"
    description: "Container {{$labels.container}} in pod {{$labels.pod}} is consuming {{$value}}% of its memory limit, exceeding the threshold of {{$external_labels.memory_threshold}}%"

问题2:能否在告警消息中展示内存限制值?

可以通过Prometheus模板函数humanize将原始字节数转换为可读格式(如1Gi、512Mi),直接在annotations中查询并展示内存限制:

- alert: high_memory_usage_resource_limits
  expr: >
    (
      sum by (container, pod, namespace) (container_memory_usage_bytes)
    ) 
    / 
    (
      sum by (container, pod, namespace) (kube_pod_container_resource_limits{resource="memory"})
    ) * 100 > 90
  for: 1m
  labels:
    severity: warning
  annotations:
    summary: "High memory usage compared to resource limits on {{$labels.pod}}"
    description: |
      Container {{$labels.container}} in pod {{$labels.pod}} is consuming {{$value}}% of its memory limit.
      Memory limit: {{ humanize (sum by (container, pod, namespace) (kube_pod_container_resource_limits{resource="memory"})) }}

humanize函数会自动将字节单位转换为最直观的可读格式,比如将1073741824字节转换为1Gi。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 09:53:13