cAdvisor 对 Node 节点上的资源及容器进行实时监控和性能数据采集,包括 CPU 、内存、网络吞吐量及文件系统等。
Cadvisor exporter | Grafana Labs
容器监控的内存相关指标:
分类 | 指标名称 | 类型 | 含义 |
CPU | container_cpu_load_average_10s | gauge | 过去 10 秒容器 CPU 的平均负载 |
container_cpu_usage_seconds_total | counter | 容器 CPU 累计使用量 (单位:秒) | |
container_cpu_system_seconds_total | counter | System CPU 累计占用时间(单位:秒) | |
container_cpu_user_seconds_total | counter | User CPU 累计占用时间(单位:秒) | |
内存 | container_memory_max_usage_bytes | gauge | 容器的最大内存使用量(单位:字节) |
container_memory_usage_bytes | gauge | 容器当前的内存使用量(单位:字节),包括缓存等可释放的内存 | |
container_memory_working_set_bytes | gauge | 容器当前的内存使用量(单位:字节) | |
container_spec_memory_limit_bytes | gauge | 容器的内存使用量限制 | |
machine_memory_bytes | gauge | 当前主机的内存总量 | |
网络 | container_network_receive_bytes_total | counter | 容器网络累积接收数据总量(单位:字节) |
container_network_receive_packets_total | counter | 容器网络累积接收数据总量(单位:包) | |
container_network_transmit_bytes_total | counter | 容器网络累积发送数据总量(单位:字节) | |
container_network_transmit_packets_total | counter | 容器网络累积发送数据总量(单位:包) | |
container_network_receive_errors_total | counter | 容器网络累计接收错误总量 | |
container_network_transmit_errors_total | counter | 容器网络累计发送错误总量 |
CPU 指标:
- container_cpu_load_average_10s:过去 10 秒容器 CPU 的平均负载 。
- container_cpu_usage_seconds_total:容器在每个 CPU 内核上的累积占用时间(单位:秒)。
- container_cpu_system_seconds_total:系统 CPU 累积占用时间(单位:秒)。
- container_cpu_user_seconds_total:用户 CPU 累积占用时间(单位:秒)。
内存指标:
- container_memory_max_usage_bytes:容器的最大内存使用量(单位:字节)。
- container_memory_usage_bytes:容器当前的内存使用量(单位:字节)。
- container_spec_memory_limit_bytes:容器的内存使用量限制。
文件系统指标:
- container_fs_usage_bytes:容器中文件系统的使用量(单位:字节)。
- container_fs_limit_bytes:容器可以使用的文件系统总量(单位:字节)。
- container_fs_reads_bytes_total:容器累积读取数据的总量(单位:字节)。
- container_fs_writes_bytes_total:容器累积写入数据的总量(单位:字节)。
网络指标:
- container_network_receive_bytes_total:容器网络累积接收数据总量(单位:字节)。
- container_network_transmit_bytes_total:容器网络累积传输数据总量(单位:字节)。
CPU常用监控语句
sum(rate(container_cpu_usage_seconds_total{name=~"xxxxxxxxxxxxxxx.*"}[5m]))
k8s通过request(下限)和limit(上限)限制容器的CPU和内存的使用范围,在容器运行的过程中需要实时监控容器对cpu的使用情况
1、 容器用户态占用CPU的时间总和
container_cpu_user_seconds_total
2、 容器内核态占用CPU的时间总和
container_cpu_system_seconds_total
3、 container_cpu_user_seconds_total与container_cpu_system_seconds_total的总和,代表容器占用CPU的总和
container_cpu_usage_seconds_total
4、 由于这些指标都是计数器类型的,所以可以通过rate函数获取样本变化率,获取5min的样本变化率的表达式如下:
sum (rate(container_cpu_usage_seconds_total[5m])) by (container_name)
5、容器的每次CPU消耗超过设置的上限后,都会以累加的方式得到记在 container_cpu_cfs_throttled_seconds_total 指标中可以通过rate函数获取变化率
sum (rate(container_cpu_cfs_throttled_seconds_total[5m])) by (container_name)
如果容器的这个指标过高,则需要调整CPU的上限或者查看程序是有死循环等问题
6、 查询容器相关的 数据:查询所有POD的1min内CPU使用情况,用到的数据指标是<container_cpu_usage_seconds_total>
由于查询到的数据都是容器相关的,所以最好按照Pod聚合,对应的promQL语句如下
sum by (pod) ( rate(container_cpu_usage_seconds_total{image!="",pod!=""}[1m]))
-------------------------------------------------------------------------------------------------------------------
当能够正常采集到cAdvisor的样本数据后,可以通过以下表达式计算容器的CPU使用率:
(1)sum(irate(container_cpu_usage_seconds_total{image!=""}[1m])) without (cpu)
容器CPU使用率
(2)container_memory_usage_bytes{image!=""}
查询容器内存使用量(单位:字节):
(3)sum(rate(container_network_receive_bytes_total{image!=""}[1m])) without (interface)
查询容器网络接收量(速率)(单位:字节/秒):
(4)sum(rate(container_network_transmit_bytes_total{image!=""}[1m])) without (interface)
容器网络传输量 字节/秒
(5)sum(rate(container_fs_reads_bytes_total{image!=""}[1m])) without (device)
容器文件系统读取速率 字节/秒
(6)sum(rate(container_fs_writes_bytes_total{image!=""}[1m])) without (device)
容器文件系统写入速率 字节/秒
cadvisor 常用容器监控指标
网络流量
sum(rate(container_network_receive_bytes_total{name=~".+"}[1m])) by (name)
##容器网络接收的字节数(1分钟内),根据名称查询 name=~".+"
sum(rate(container_network_transmit_bytes_total{name=~".+"}[1m])) by (name)
##容器网络传输的字节数(1分钟内),根据名称查询 name=~".+"
容器 CPU相关
sum(rate(container_cpu_system_seconds_total[1m]))
###所用容器system cpu的累计使用时间(1min钟内)
sum(irate(container_cpu_system_seconds_total{image!=""}[1m])) without (cpu)
###每个容器system cpu的使用时间(1min钟内)
sum(rate(container_cpu_usage_seconds_total{name=~".+"}[1m])) by (name) * 100
#每个容器的cpu使用率
sum(sum(rate(container_cpu_usage_seconds_total{name=~".+"}[1m])) by (name) * 100)
#总容器的cpu使用率
参数解释
使用Prometheus配置kubernetes环境中Container的CPU使用率时,会经常遇到CPU使用超出100%,下面就来解释一下
- container_spec_cpu_period
当对容器进行CPU限制时,CFS调度的时间窗口,又称容器CPU的时钟周期通常是100,000微秒 - container_spec_cpu_quota
是指容器的使用CPU时间周期总量,如果quota设置的是700,000,就代表该容器可用的CPU时间是7*100,000微秒,通常对应kubernetes的resource.cpu.limits的值 - container_spec_cpu_share
是指container使用分配主机CPU相对值,比如share设置的是500m,代表窗口启动时向主机节点申请0.5个CPU,也就是50,000微秒,通常对应kubernetes的resource.cpu.requests的值 - container_cpu_usage_seconds_total
统计容器的CPU在一秒内消耗使用率,应注意的是该container所有的CORE - container_cpu_system_seconds_total
统计容器内核态在一秒时间内消耗的CPU - container_cpu_user_seconds_total 统计容器用户态在一秒时间内消耗的CPU
参考官方地址
https://docs.signalfx.com/en/latest/integrations/agent/monitors/cadvisor.html https://github.com/google/cadvisor/blob/master/docs/storage/prometheus.md
具体公式
- 默认如果直接使用container_cpu_usage_seconds_total的话,如下
sum(irate(container_cpu_usage_seconds_total{container="$Container",instance="$Node",pod="$Pod"}[5m])*100)by(pod)
默认统计的数据是该容器所有的CORE的平均使用率 - 如果要精确计算每个容器的CPU使用率,使用%呈现的形式,如下:
sum(irate(container_cpu_usage_seconds_total{container="$Container",instance="$Node",pod="$Pod"}[5m])*100)by(pod)
/
sum(container_spec_cpu_quota{container="$Container",instance="$Node",pod="$Pod"}
/container_spec_cpu_period{container="$Container",instance="$Node",pod="$Pod"})by(pod)
其中container_spec_cpu_quota/container_spec_cpu_period,就代表该容器有多少个CORE
- 参考官方git issue
https://github.com/google/cadvisor/issues/2026#issuecomment-415819667