目录标题
- Prometheus 监控指标与 PromQL 语法指南
- 目录
- 一、环境组件概览
- 1.1 当前部署组件
- 1.2 组件职责
- 二、指标分类与验证
- 2.1 容器资源指标 (cAdvisor/Kubelet)
- 网络指标
- CPU 指标
- 内存指标
- 存储 I/O 指标
- 2.2 Kubernetes 对象指标 (kube-state-metrics)
- 2.3 节点系统指标 (node_exporter)
- 2.4 ETCD 指标
- 2.5 自定义/其他指标
- 三、PromQL 核心语法
- 3.1 选择器与匹配运算符
- 3.1.1 标签选择器
- 3.1.2 常用正则表达式示例
- 3.1.3 比较运算符
- 3.1.4 逻辑运算符
- 3.1.5 集合运算符
- 3.2 基础概念
- 指标类型
- 3.2 核心函数详解
- `rate()` - 计算速率
- `irate()` - 计算瞬时速率
- `sum()` - 求和
- `topk()` - Top K 值
- `avg()` - 平均值
- `count()` - 计数
- `max()` / `min()` - 最大/最小值
- 3.3 时间操作符
- `offset` - 时间偏移
- 3.4 聚合修饰符
- 四、实战查询示例
- 4.1 网络监控
- 4.2 存储 I/O 监控
- 4.3 CPU 监控
- 4.4 内存监控
- 4.5 Pod 状态监控
- 4.6 节点健康监控
- 4.7 ETCD 监控
- 4.8 数据库监控
- 五、官方参考文档
- 5.1 Prometheus 官方文档
- 5.2 组件官方文档
- 5.3 社区资源
- 附录:快速参考
- PromQL 函数速查表
- 时间单位
- 常用标签
Prometheus 监控指标与 PromQL 语法指南
目录
- 一、环境组件概览
- 二、指标分类与验证
- 三、PromQL 核心语法
- 四、实战查询示例
- 五、官方参考文档
一、环境组件概览
1.1 当前部署组件
| 组件 | 类型 | 副本数 | 版本 | 端口 | 状态 |
|---|---|---|---|---|---|
| Prometheus | StatefulSet | 2 | v2.43.1 | 9090 | ✅ Running |
| kube-state-metrics | Deployment | 1 | v1.5.0 | 8081/8082 | ✅ Running |
| node_exporter | DaemonSet | 4 | v0.17.2 | 9100 | ✅ Running |
| cAdvisor | DaemonSet | 4 | v0.47.0.qf0 | 8080 | ✅ Running |
| Alertmanager | Sidecar | - | v0.23.0 | 9093 | ✅ Running |
1.2 组件职责
┌─────────────────────────────────────────────────────────────┐ │ 监控架构图 │ ├─────────────────────────────────────────────────────────────┤ │ │ │ ┌──────────────┐ ┌──────────────┐ │ │ │ kubelet/cAdvisor│ │node_exporter│ │ │ │ 容器指标 │ │ 节点指标 │ │ │ └──────┬───────┘ └──────┬───────┘ │ │ │ │ │ │ ▼ ▼ │ │ ┌──────────────────────────────────────┐ │ │ │ Prometheus Server │ │ │ │ (数据采集与存储) │ │ │ └──────────────┬───────────────────────┘ │ │ │ │ │ ┌──────────────▼───────────────────────┐ │ │ │ kube-state-metrics │ │ │ │ (K8s 对象状态: Pod/Node等) │ │ │ └───────────────────────────────────────┘ │ │ │ │ │ ▼ │ │ ┌──────────────────────────────────────┐ │ │ │ Grafana/Alertmanager │ │ │ │ (可视化与告警) │ │ │ └──────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────────┘二、指标分类与验证
2.1 容器资源指标 (cAdvisor/Kubelet)
网络指标
| 指标 | 类型 | 说明 | 验证 |
|---|---|---|---|
container_network_receive_bytes_total | Counter | 容器网络接收字节数 | ✅ |
container_network_transmit_bytes_total | Counter | 容器网络发送字节数 | ✅ |
CPU 指标
| 指标 | 类型 | 说明 | 验证 |
|---|---|---|---|
container_cpu_usage_seconds_total | Counter | 容器 CPU 使用时间(秒) | ✅ |
内存指标
| 指标 | 类型 | 说明 | 验证 |
|---|---|---|---|
container_memory_rss | Gauge | 容器实际物理内存使用 | ✅ |
container_memory_working_set_bytes | Gauge | 工作集内存(包含匿名页) | ✅ |
存储 I/O 指标
| 指标 | 类型 | 说明 | 验证 |
|---|---|---|---|
container_fs_reads_total | Counter | 磁盘读取次数 | ✅ |
container_fs_writes_total | Counter | 磁盘写入次数 | ✅ |
container_fs_reads_bytes_total | Counter | 磁盘读取字节数 | ✅ |
container_fs_writes_bytes_total | Counter | 磁盘写入字节数 | ✅ |
2.2 Kubernetes 对象指标 (kube-state-metrics)
| 指标 | 类型 | 说明 | 验证 |
|---|---|---|---|
kube_pod_labels | Gauge | Pod 的标签信息 | ✅ |
kube_pod_info | Gauge | Pod 基本信息 | ✅ |
kube_pod_status_phase | Gauge | Pod 状态 (Running/Pending等) | ✅ |
kube_node_status_condition | Gauge | 节点状态条件 | ✅ |
kube_pod_container_status_restarts_total | Counter | Pod 重启次数 | ✅ |
2.3 节点系统指标 (node_exporter)
| 指标 | 类型 | 说明 | 验证 |
|---|---|---|---|
node_filefd_allocated | Gauge | 已分配文件描述符数量 | ✅ |
node_filesystem_free_bytes | Gauge | 文件系统剩余字节数 | ✅ |
node_filesystem_size_bytes | Gauge | 文件系统总字节数 | ✅ |
node_cpu_seconds_total | Counter | CPU 时间(秒) | ✅ |
node_memory_MemAvailable_bytes | Gauge | 可用内存字节数 | ✅ |
node_time_seconds | Gauge | 系统时间戳 | ✅ |
node_boot_time_seconds | Gauge | 系统启动时间戳 | ✅ |
2.4 ETCD 指标
| 指标 | 类型 | 说明 | 验证 |
|---|---|---|---|
etcd_server_is_leader | Gauge | 是否为 leader (1/0) | ✅ |
etcd_network_peer_round_trip_time_seconds | Histogram | 对端网络往返时间 | ✅ |
etcd_disk_wal_fsync_duration_seconds | Histogram | WAL fsync 延迟 | ✅ |
etcd_disk_backend_commit_duration_seconds | Histogram | DB commit 延迟 | ✅ |
2.5 自定义/其他指标
| 指标 | 类型 | 说明 | 验证 |
|---|---|---|---|
kube_system_time_diff | Gauge | 节点时间差 | ✅ |
pg_stat_replication_pg_location_diff | Gauge | PG 复制延迟 | ✅ |
三、PromQL 核心语法
3.1 选择器与匹配运算符
3.1.1 标签选择器
PromQL 使用标签选择器来过滤时间序列,支持以下匹配运算符:
| 运算符 | 说明 | 示例 | 匹配结果 |
|---|---|---|---|
= | ���确匹配 | pod="prometheus-0" | pod 标签等于 “prometheus-0” |
!= | 不等于 | container!="POD" | container 标签不等于 “POD” |
=~ | 正则匹配 | pod=~"prometheus.*" | pod 标签匹配正则表达式 |
!~ | 正则不匹配 | node!~"qfusion.*" | node 标签不匹配正则表达式 |
完整语法结构:
metric_name{label1="value1",label2!="value2",label3=~"regex",label4!~"regex"} 示例分解: ├── metric_name # 指标名称(必需) ├── {...} # 标签过滤器(可选) ├── label1="value1" # 精确匹配 ├── label2!="value2" # 排除特定值 ├── label3=~"regex" # 正则匹配 └── label4!~"regex" # 正则排除实战示例:
# 1. 精确匹配 (=) container_cpu_usage_seconds_total{pod="prometheus-0"} # 只返回 pod 标签为 "prometheus-0" 的指标 # 2. 不等于 (!=) container_memory_rss{container!="POD"} # 排除 container 为 "POD" 的数据(Pod sandbox 不计入实际使用) # 3. 正则匹配 (=~) container_cpu_usage_seconds_total{pod=~"prometheus.*"} # 匹配所有以 "prometheus" 开头的 pod # 如: prometheus-0, prometheus-1, prometheus-operator-xxx # 4. 正则匹配或 (使用 | ) kube_pod_info{namespace=~"default|kube-system|qfusion"} # 匹配多个命名空间 # 5. 正则排除 (!~) container_fs_reads_total{device!~"/dev/mapper/.*"} # 排除逻辑卷设备,只统计物理设备 # 6. 组合使用 rate(container_cpu_usage_seconds_total{ container!="", container!="POD", node="qfusion2", pod!~".*operator.*" }[5m])3.1.2 常用正则表达式示例
# 匹配任意字符 (.* ) pod=~"prometheus.*" # prometheus 开头的所有 pod pod=~".*-0" # 以 -0 结尾的 pod # 匹配多个选项 (|) namespace=~"default|kube-system" # default 或 kube-system node=~"qfusion[123]" # qfusion1, qfusion2, qfusion3 # 否定匹配 (!~) pod!~".*operator.*" # 排除包含 operator 的 pod device!~"/dev/(mapper|loop).*" # 排除 mapper 和 loop 设备 # 特殊字符转义 interface=~"eth[0-9]" # eth0, eth1, eth2... pod=~"test-.+-(debug|prod)" # test-xxx-debug 或 test-xxx-prod3.1.3 比较运算符
用于对指标值进行过滤,通常结合bool关键字使用。
| 运算符 | 说明 | 示例 |
|---|---|---|
== | 等于 | metric == 100 |
!= | 不等于 | metric != 0 |
> | 大于 | metric > 80 |
>= | 大于等于 | metric >= 100 |
< | 小于 | metric < 10 |
<= | 小于等于 | metric <= 50 |
示例:
# 1. 简单比较(返回过滤后的时间序列) container_memory_usage_bytes > 1024 * 1024 * 1024 # 只返回内存使用大于 1GB 的容器 # 2. 布尔模式(返回 1 或 0) container_memory_usage_bytes > bool 1024 * 1024 * 1024 # 返回 1(条件满足)或 0(不满足),用于告警表达式 # 3. 告警表达式示例 alert: HighMemoryUsage expr: container_memory_usage_bytes{container!="", container!="POD"} > 1024 * 1024 * 1024 * 4 for: 5m # 内存持续超过 4GB 达 5 分钟触发告警 # 4. 范围判断 node_filesystem_free_bytes < 1024 * 1024 * 1024 * 10 # 磁盘剩余空间小于 10GB # 5. 百分比比较 (1 - node_filesystem_free_bytes / node_filesystem_size_bytes) * 100 > 85 # 磁盘使用率超过 85%3.1.4 逻辑运算符
| 运算符 | 说明 | 示例 |
|---|---|---|
and | 逻辑与 | metric1 and metric2 |
or | 逻辑或 | metric1 or metric2 |
unless | 逻辑排除 | metric1 unless metric2 |
示例:
# AND: 两个指标都存在 rate(http_requests_total[5m]) > 100 and rate(http_errors_total[5m]) < 5 # OR: 任一指标存在 metric{label="a"} or metric{label="b"} # UNLESS: 排除 up{job="prometheus"} unless up{job="prometheus"} == 03.1.5 集合运算符
用于匹配两个指标集的标签。
| 运算符 | 说明 | 匹配规则 |
|---|---|---|
on | 指定匹配标签 | x on (pod) / y |
ignoring | 忽略指定标签 | x ignoring (instance) / y |
group_left | 多对一匹配 | x group_left pod / y |
group_right | 一对多匹配 | x group_right pod / y |
示例:
# 1. ON: 指定匹配标签 rate(http_requests_total[5m]) / rate(http_requests_total{code="200"}[5m]) on (job, instance) # 2. GROUP_LEFT: 多对一(用于比率计算) # 每个状态的 Pod 数 / 总 Pod 数 count(kube_pod_status_phase{phase="Running"}) by (namespace) / count(kube_pod_status_phase) by (namespace) group_left namespace # 3. 实际案例:HTTP 错误率 # 错误请求数 / 总请求数 rate(http_requests_total{status=~"5.."}[5m]) / rate(http_requests_total[5m]) on (job, instance) group_left # 4. CPU 使用率百分比 # 容器 CPU 时间 / 总 CPU 时间 sum(rate(container_cpu_usage_seconds_total{container!=""}[5m])) by (pod) / sum(rate(container_cpu_usage_seconds_total{container!=""}[5m])) by (node) on() group_left pod3.2 基础概念
指标类型
┌─────────────────────────────────────────────────────────┐ │ Prometheus 指标类型 │ ├─────────────────────────────────────────────────────────┤ │ │ │ Counter (计数器) │ │ - 只增不减的数值 │ │ - 用于请求数、错误数、字节数等 │ │ - 示例: http_requests_total, node_cpu_seconds_total │ │ │ │ Gauge (仪表盘) │ │ - 可增可减的数值 │ │ - 用于当前状态:内存、温度、队列长度 │ │ - 示例: memory_usage_bytes, temperature │ │ │ │ Histogram (直方图) │ │ - 记录数据分布情况 │ │ - 包含 _count, _sum, _bucket │ │ - 示例: http_request_duration_seconds │ │ │ │ Summary (摘要) │ │ - 类似 Histogram,客户端计算分位数 │ │ - 包含 _count, _sum, 分位数值 │ │ │ └─────────────────────────────────────────────────────────┘3.2 核心函数详解
rate()- 计算速率
语法:rate(指标[时间窗口])
作用:计算 Counter 类型指标在时间窗口内的平均每秒增长率
适用场景:
- 计算 QPS(每秒请求数)
- 计算网络带宽(每秒字节数)
- 计算错误率
语法结构: rate(metric[5m]) 计算公式: (最新值 - 最早值) / 时间窗口秒数 注意事项: - 仅适用于 Counter 类型 - 自动处理计数器重置(重启) - 返回值单位:每秒示例:
# 计算 CPU 使用率(每秒 CPU 时间) rate(container_cpu_usage_seconds_total{pod="prometheus-0"}[5m]) # 计算网络接收速率(字节/秒) rate(container_network_receive_bytes_total{node="qfusion1"}[3m]) # 计算磁盘读取速率(次/秒) rate(container_fs_reads_total{container!=""}[5m])irate()- 计算瞬时速率
语法:irate(指标[时间窗口])
作用:计算基于最后两个数据点的瞬时速率
与 rate 的区别:
| 函数 | 计算方式 | 适用场景 | 稳定性 |
|---|---|---|---|
rate() | 整个窗口平均值 | 长期趋势分析 | 平滑 |
irate() | 最后两点斜率 | 短期快速变化 | 敏感 |
# irate 对变化更敏感 irate(container_cpu_usage_seconds_total[5m]) # 适合告警场景(快速捕获异常) irate(http_requests_total{job="api"}[5m]) > 100sum()- 求和
语法:sum(指标)或sum(指标) by (标签)
作用:将所有时间序列的值相加
语法结构: sum(metric) # 全局求和 sum(metric) by (label) # 按标签分组求和 sum(metric) without (label) # 排除标签求和示例:
# 计算集群所有 Pod 的 CPU 使用总和 sum(rate(container_cpu_usage_seconds_total{container!=""}[5m])) # 按节点分组计算 CPU 使用 sum(rate(container_cpu_usage_seconds_total{container!=""}[5m])) by (node) # 按命名空间和 Pod 分组计算内存 sum(container_memory_rss{container!="", pod!="POD"}) by (namespace, pod) # 按节点计算网络接收总速率 sum(rate(container_network_receive_bytes_total{interface="eth0"}[3m])) by (node)topk()- Top K 值
语法:topk(k, 指标)或topk(k, 指标) by (标签)
作用:返回排序后前 k 个时间序列
语法结构: topk(3, metric) # 返回值最大的 3 个序列 topk(5, metric) by (node) # 按节点分组,每组取 Top 5示例:
# CPU 使用率最高的 20 个容器 topk(20, rate(container_cpu_usage_seconds_total{container!="", node="qfusion2"}[3m])) # 磁盘写入最活跃的 20 个容器 topk(20, rate(container_fs_writes_total{container!="", node="qfusion2"}[3m])) # 内存使用最高的 10 个 Pod topk(10, container_memory_rss{pod=~"prometheus.*", container="prometheus"})avg()- 平均值
语法:avg(指标)或avg(指标) by (标签)
示例:
# 计算平均 CPU 使用率 avg(rate(node_cpu_seconds_total{mode!="idle"}[5m])) by (instance) # 计算节点平均内存使用率 avg(node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) by (node)count()- 计数
语法:count(指标)或count(指标) by (标签)
示例:
# 计算节点上的 Pod 数量 count(kube_pod_info{node="qfusion1"}) # 统计 Running 状态的 Pod count(kube_pod_status_phase{phase="Running"}) by (namespace) # 统计处于 Ready 状态的节点数 count(kube_node_status_condition{condition="Ready", status="true"})max()/min()- 最大/最小值
示例:
# 获取节点间最大时间差 max(kube_system_time_diff) # 获取每个节点的最小可用内存 min(node_memory_MemAvailable_bytes) by (node)3.3 时间操作符
offset- 时间偏移
语法:指标 offset 时间偏移
作用:查询历史时间点的值
# 查询 1 天前的内存使用情况 container_memory_rss offset 1d # 对比当前和 1 小时前的 CPU 使用率 rate(container_cpu_usage_seconds_total[5m]) - rate(container_cpu_usage_seconds_total[5m]) offset 1h # 查询 1 周前的磁盘使用率 (1 - node_filesystem_free_bytes / node_filesystem_size_bytes) offset 7d3.4 聚合修饰符
| 修饰符 | 说明 | 示例 |
|---|---|---|
by (label) | 按标签分组 | sum(x) by (pod) |
without (label) | 排除标签分组 | sum(x) without (instance) |
on (label) | 匹配指定标签 | x on (pod) / y |
group_left | 多对一关联 | x group_left pod |
group_right | 一对多关联 | x group_right pod |
四、实战查询示例
4.1 网络监控
# 节点网络接收速率 (Top 20) topk(20, rate(container_network_receive_bytes_total{container!="", node="qfusion2", interface="eth0"}[3m])) # 节点网络发送速率 rate(container_network_transmit_bytes_total{pod!="", interface="eth0", node="qfusion1"}[3m]) # 按 Pod 统计网络总流量 (字节/秒) sum(rate(container_network_receive_bytes_total[5m])) by (pod) + sum(rate(container_network_transmit_bytes_total[5m])) by (pod)4.2 存储 I/O 监控
# 磁盘写入 IOPS (Top 20) topk(20, rate(container_fs_writes_total{container!="", device!~"/dev/mapper/.*", node="qfusion2"}[3m])) # 磁盘读取 IOPS topk(20, rate(container_fs_reads_total{container!="", device!~"/dev/mapper/.*", node="qfusion2"}[3m])) # 磁盘写入速率 (字节/秒) rate(container_fs_writes_bytes_total[3m]) # 磁盘读取速率 (字节/秒) rate(container_fs_reads_bytes_total[3m])4.3 CPU 监控
# 容器 CPU 使用率 (Top 20) topk(20, rate(container_cpu_usage_seconds_total{container!="", node="qfusion2"}[3m])) # 特定 Pod 的 CPU 使用 container_cpu_usage_seconds_total{pod=~"prometheus.*", container="prometheus"} # 节点 CPU 使用率 sum(rate(node_cpu_seconds_total{mode!="idle"}[5m])) by (instance) / sum(node_cpu_seconds_total{mode="idle"}) by (instance)4.4 内存监控
# 容器内存使用 (Top 20) - RSS topk(20, rate(container_memory_rss{container!="", node="qfusion2"}[3m])) # 特定 Pod 内存使用 container_memory_rss{pod=~"prometheus.*", container="prometheus"} # 工作集内存 (Kubernetes OOM 判断依据) container_memory_working_set_bytes{pod=~"prometheus.*"} # 节点内存使用率 (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 1004.5 Pod 状态监控
# 获取 Pod 标签 kube_pod_labels # 计算每个节点上的 Pod 数量 count(kube_pod_info) by (node) # 统计各状态 Pod 数量 count(kube_pod_status_phase) by (namespace, phase) # Pod 重启次数 rate(kube_pod_container_status_restarts_total{pod="your-pod"}[5m]) # 非 Ready 状态的 Pod count(kube_pod_status_ready{condition="false"}) by (namespace, pod)4.6 节点健康监控
# 节点 Ready 状态 kube_node_status_condition{condition="Ready", node="qfusion01", status="true"} # 节点时间同步检查 kube_system_time_diff # 系统运行时间(秒) node_time_seconds - node_boot_time_seconds # 节点目录使用率 (%) (1 - node_filesystem_free_bytes{ mountpoint!~"/var/lib/kubelet/.+|/boot|/opt/oracle/.+|/opt/qfusion/mnt/qfusion_backup/.+|/opt/qfusion/mnt/.+|/mnt/qfusion_backup/.+|.+runtime-runc.+", name="node_exporter", fstype!~"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs|nfs4|nfs3|fuse.juicefs" } / node_filesystem_size_bytes) * 100 # 文件句柄使用率 node_filefd_allocated / node_filefd_maximum * 1004.7 ETCD 监控
# ETCD 对端响应时间 (毫秒) rate(etcd_network_peer_round_trip_time_seconds_count{job="etcd-exporter"}[3m]) * 1000 # DB fsync P99 延迟 (毫秒) histogram_quantile(0.99, sum(rate(etcd_disk_backend_commit_duration_seconds_bucket{}[5m])) by (instance, le) ) * 1000 # WAL fsync P99 延迟 (毫秒) histogram_quantile(0.99, sum(rate(etcd_disk_wal_fsync_duration_seconds_bucket{}[5m])) by (instance, le) ) * 1000 # 检查 leader etcd_server_is_leader4.8 数据库监控
# PostgreSQL 复制延迟(字节) pg_stat_replication_pg_location_diff # 连接数 pg_stat_database_numbackends # 事务提交速率 rate(pg_stat_database_xact_commit{}[5m]) # 事务回滚速率 rate(pg_stat_database_xact_rollback{}[5m])五、官方参考文档
5.1 Prometheus 官方文档
- Prometheus 官方网站: https://prometheus.io
- PromQL 语法: https://prometheus.io/docs/prometheus/latest/querying/basics/
- 函数参考: https://prometheus.io/docs/prometheus/latest/querying/functions/
5.2 组件官方文档
| 组件 | 官方文档 | 说明 |
|---|---|---|
| cAdvisor | Monitoring Docker containers with cAdvisor | 容器资源监控 |
| kube-state-metrics | Metrics for Kubernetes Object States | K8s 对象状态 |
| node_exporter | Monitoring Linux host metrics | 节点系统指标 |
| etcd | etcd Metrics | etcd 监控指标 |
| Kubernetes Metrics | Kubernetes System Metrics | K8s 系统组件 |
5.3 社区资源
- Grafana Dashboards: https://grafana.com/grafana/dashboards/
- Prometheus Best Practices: https://prometheus.io/docs/practices/naming/
- Alerting Rules: https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/
附录:快速参考
PromQL 函数速查表
| 函数 | 作用 | 示例 |
|---|---|---|
rate() | 计算平均速率 | rate(metric[5m]) |
irate() | 计算瞬时速率 | irate(metric[5m]) |
sum() | 求和 | sum(metric) by (label) |
avg() | 平均值 | avg(metric) by (label) |
max() | 最大值 | max(metric) by (label) |
min() | 最小值 | min(metric) by (label) |
count() | 计数 | count(metric) by (label) |
topk() | Top K | topk(5, metric) |
bottomk() | Bottom K | bottomk(5, metric) |
histogram_quantile() | 分位数 | histogram_quantile(0.95, metric) |
时间单位
| 单位 | 含义 |
|---|---|
s | 秒 |
m | 分钟 |
h | 小时 |
d | 天 |
w | 周 |
y | 年 |
常用标签
| 标签 | 来源 | 示例值 |
|---|---|---|
namespace | K8s | default,kube-system |
pod | K8s | prometheus-0 |
container | cAdvisor | prometheus,POD |
node | K8s | qfusion1 |
instance | 采集目标 | localhost:9090 |
job | 采集任务 | prometheus,kubelet |
endpoint | Service | https-main |