4. 核心资源指标:CPU使用率、内存使用率、磁盘I/O、网络I/O的Prometheus指标名称与含义
在容器运行时环境中,CPU、内存、磁盘I/O和网络I/O是决定工作负载健康状态的四大核心资源维度。Prometheus通过cAdvisor(集成于kubelet)和Node Exporter暴露一系列标准指标,用于精确量化每个容器的资源消耗。本节将逐一解析这四类指标的名称、类型、标签结构及其业务含义,为后续设计预警规则奠定数据基础。
4.1 CPU使用率指标
CPU指标反映容器在单位时间内占用CPU核心的时间比例。Prometheus中与容器CPU相关的核心指标如下:
| 指标名称 | 类型 | 单位 | 含义 |
|---|---|---|---|
container_cpu_usage_seconds_total |
Counter | 秒 | 容器累计使用的CPU时间(包含所有核心) |
container_cpu_system_seconds_total |
Counter | 秒 | 容器在内核态累计消耗的CPU时间 |
container_cpu_user_seconds_total |
Counter | 秒 | 容器在用户态累计消耗的CPU时间 |
container_cpu_cfs_periods_total |
Counter | 次 | CFS调度周期总次数(用于计算限流) |
container_cpu_cfs_throttled_periods_total |
Counter | 次 | CFS调度周期中被限流的次数 |
预警设计要点: 计算容器CPU使用率时,应使用
rate(container_cpu_usage_seconds_total[1m]) 获取每秒CPU时间增量,再除以容器可用的CPU核心数(通常由 container_spec_cpu_quota 或Kubernetes resource limits决定),得到百分比形式的实际使用率。当 container_cpu_cfs_throttled_periods_total 的速率持续升高时,表明容器已触发CPU限流,是资源瓶颈的直接信号。
4.2 内存使用率指标
内存指标关注容器当前占用的物理内存量以及是否存在内存压力。核心指标如下:
| 指标名称 | 类型 | 单位 | 含义 |
|---|---|---|---|
container_memory_usage_bytes |
Gauge | 字节 | 容器当前使用的总内存(包含缓存和缓冲区) |
container_memory_rss |
Gauge | 字节 | 容器常驻内存集大小(不含缓存/缓冲区) |
container_memory_working_set_bytes |
Gauge | 字节 | 容器当前活跃工作集内存(OOM判定依据) |
container_memory_cache |
Gauge | 字节 | 容器页缓存和slab占用的内存 |
container_memory_swap |
Gauge | 字节 | 容器已使用的swap空间 |
关键区别: Kubernetes的OOM Killer基于
container_memory_working_set_bytes 判定是否超出内存limit,而非 container_memory_usage_bytes。因此,设计内存使用率预警时,应优先使用 container_memory_working_set_bytes 作为分子,除以 container_spec_memory_limit_bytes 计算真实压力水平。
4.3 磁盘I/O指标
磁盘I/O指标衡量容器对存储设备的读写吞吐量和操作频率。这些指标由cAdvisor按容器文件系统维度暴露:
| 指标名称 | 类型 | 单位 | 含义 |
|---|---|---|---|
container_fs_reads_bytes_total |
Counter | 字节 | 容器累计读取的字节数 |
container_fs_writes_bytes_total |
Counter | 字节 | 容器累计写入的字节数 |
container_fs_reads_total |
Counter | 次 | 容器累计读取操作的次数 |
container_fs_writes_total |
Counter | 次 | 容器累计写入操作的次数 |
container_fs_io_current |
Gauge | 次 | 容器当前正在进行的I/O操作数 |
I/O瓶颈判定: 使用
rate(container_fs_reads_bytes_total[1m]) 和 rate(container_fs_writes_bytes_total[1m]) 计算吞吐量速率。当吞吐量持续接近底层存储设备的IOPS或带宽上限,且 container_fs_io_current 长期处于高位时,表明磁盘I/O已成为性能瓶颈。对于有状态服务(如数据库),建议同时监控I/O等待时间(需通过Node Exporter的 node_disk_io_time_seconds_total 辅助判断)。
4.4 网络I/O指标
网络I/O指标反映容器通过网络接口收发数据的流量和包量。cAdvisor按容器网络接口维度提供以下指标:
| 指标名称 | 类型 | 单位 | 含义 |
|---|---|---|---|
container_network_receive_bytes_total |
Counter | 字节 | 容器累计接收的字节数 |
container_network_transmit_bytes_total |
Counter | 字节 | 容器累计发送的字节数 |
container_network_receive_packets_total |
Counter | 个 | 容器累计接收的数据包数量 |
container_network_transmit_packets_total |
Counter | 个 | 容器累计发送的数据包数量 |
container_network_receive_errors_total |
Counter | 个 | 容器累计接收错误的数据包数 |
container_network_transmit_errors_total |
Counter | 个 | 容器累计发送错误的数据包数 |
网络瓶颈预警: 当
rate(container_network_receive_bytes_total[1m]) 或 rate(container_network_transmit_bytes_total[1m]) 持续接近容器网络带宽上限(通常由CNI插件或节点带宽决定)时,应触发预警。同时,container_network_receive_errors_total 和 container_network_transmit_errors_total 的速率突增往往指示物理链路或驱动层面的问题,需立即关注。
4.5 四类指标关联关系图
下图展示了四类核心资源指标在容器运行时中的采集来源、Prometheus指标名称以及它们之间的关联关系,帮助理解各指标在资源瓶颈预警中的角色:
4.6 指标采集与预警设计对照
为便于在PromQL中快速引用,下表汇总了四类资源的核心预警计算表达式及其对应的Prometheus指标组合:
| 资源类型 | 预警计算表达式(PromQL) | 依赖指标 |
|---|---|---|
| CPU使用率 | rate(container_cpu_usage_seconds_total[1m]) / container_spec_cpu_quota * 100 |
container_cpu_usage_seconds_total, container_spec_cpu_quota |
| 内存使用率 | container_memory_working_set_bytes / container_spec_memory_limit_bytes * 100 |
container_memory_working_set_bytes, container_spec_memory_limit_bytes |
| 磁盘I/O吞吐 | rate(container_fs_reads_bytes_total[1m]) + rate(container_fs_writes_bytes_total[1m]) |
container_fs_reads_bytes_total, container_fs_writes_bytes_total |
| 网络I/O吞吐 | rate(container_network_receive_bytes_total[1m]) + rate(container_network_transmit_bytes_total[1m]) |
container_network_receive_bytes_total, container_network_transmit_bytes_total |
| CPU限流比例 | rate(container_cpu_cfs_throttled_periods_total[1m]) / rate(container_cpu_cfs_periods_total[1m]) * 100 |
container_cpu_cfs_throttled_periods_total, container_cpu_cfs_periods_total |
最佳实践: 在编写预警规则时,建议为每个资源维度设置至少两个阈值等级:Warning(例如CPU使用率 > 80%)和Critical(例如CPU使用率 > 95% 且限流比例 > 10%)。同时,将磁盘I/O和网络I/O的预警与容器应用类型关联——数据库类容器应设置更低的I/O等待阈值,而Web类容器则更关注网络吞吐量波动。