🚀 一、Prometheus 查看“哪个容器 CPU 高”的核心指标

Kubernetes 默认安装 metrics-server 或使用 cAdvisor 时,会暴露这几个指标:

在这里插入图片描述


1. container_cpu_usage_seconds_total(最经典、最准确)

查询容器 CPU 使用核心指标:

按容器 CPU 使用率(核数)排序:

topk(10, rate(container_cpu_usage_seconds_total[2m]))

你会看到类似结果:

containerpodnamespacevalue (CPU cores)
mysqlddb-pod-001prod3.82
kubeletnode-xkube-system1.42
envoysvc-gw-0istio0.89

value 的单位不是“百分比”,它表示:

  • 3.82 = 同时使用 3.82 个 CPU 核

2. 按 Pod 汇总 CPU:

topk(10, sum by (pod, namespace) (
      rate(container_cpu_usage_seconds_total{container!="POD"}[2m])
))

用途:

  • 直接看 哪个 Pod 拖垮 CPU

3. 按节点汇总 CPU(判断是否热点 node)

sum by (node) (rate(container_cpu_usage_seconds_total[2m]))

🚀 二、Prometheus 查看“哪个进程 CPU 高”的方案

Prometheus 本身 + node-exporter 不能看到“具体进程 CPU”。
因为 node-exporter 不采集每个 PID 的 CPU 数据(那样成本极高)。

要查看“进程 CPU”必须安装以下之一:


🧩 方案 1:process-exporter(推荐)

官方项目:
https://github.com/ncabatoff/process-exporter

采集以下进程级指标:

  • process_cpu_seconds_total
  • process_resident_memory_bytes
  • process_open_fds
  • process_errors_total

部署后,你可以查询:

✔ 按进程名列 CPU 高的进程:

topk(10, rate(process_cpu_seconds_total[2m]))

效果:

processpidcpu_cores
mysqld51523.8
etcd22781.2
java55000.9

🧩 方案 2:使用 node_exporter + pid_exporter

如果你只想监控“某些关键进程”,可以用 PID exporter:

https://github.com/treydock/pid-exporter


🚀 三、如何在 Grafana 中可视化“容器 CPU TopN”

Prometheus 推荐的仪表盘:

topk(10,
    sum by (pod, container) (
        rate(container_cpu_usage_seconds_total{container!="POD"}[2m])
    )
)

🚀 四、给你最常用的“容器 CPU 调查模板”(可复制)


🔎 (1) 查看 CPU 使用前 10 的容器

topk(10,
     rate(container_cpu_usage_seconds_total{container!="POD"}[1m])
)

🔎 (2) 按 Pod 汇总 CPU(最实用)

topk(10,
     sum by (pod, namespace) (
        rate(container_cpu_usage_seconds_total{container!="POD"}[1m])
     )
)

🔎 (3) 查看某个节点上的 CPU Top 容器

topk(10,
    rate(container_cpu_usage_seconds_total{node="qdgx-db03"}[1m])
)

🔎 (4) 查看某个命名空间的 Top 容器

topk(10,
    sum by (pod, container) (
        rate(container_cpu_usage_seconds_total{namespace="prod"}[1m])
    )
)

🔎 (5) 查看某个容器历史 CPU 趋势

rate(container_cpu_usage_seconds_total{pod="mysql-0", container="mysqld"}[5m])

🔥 五、总结:如何定位 CPU 高的是 “容器” 还是 “进程”

想查看什么Prometheus 能否直接看到?用哪个指标 / 工具
哪个节点 CPU 高node_exporter / sum(rate(container…))
哪个容器 CPU 高✔(推荐)rate(container_cpu_usage_seconds_total)
哪个 Pod CPU 高sum by(pod)(rate(container…))
哪个进程 CPU 高❌ node-exporter 无法提供✔ process-exporter

更多推荐