Prometheus监控K8S节点,容器 表达式计算
表达式
node exporter的一些计算语句 CPU使用率(单位为percent) 100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) 内存已使用(单位为bytes) node_memory_MemTotal_bytes - node_memory_MemFree_bytes - node_memory_Cached_bytes - node_memory_Buffers_bytes - node_memory_Slab_bytes 内存使用量(单位为bytes/sec) node_memory_MemTotal_bytes - node_memory_MemFree_bytes - node_memory_Cached_bytes - node_memory_Buffers_bytes - node_memory_Slab_bytes 内存使用率(单位为percent) ((node_memory_MemTotal_bytes - node_memory_MemFree_bytes - node_memory_Cached_bytes - node_memory_Buffers_bytes - node_memory_Slab_bytes)/node_memory_MemTotal_bytes) * 100 192.168.61.223:9100的内存使用率(单位为percent) ((node_memory_MemTotal_bytes{instance="192.168.61.223:9100"} - node_memory_MemAvailable_bytes{instance="192.168.61.223:9100"})/node_memory_MemTotal_bytes{instance="192.168.61.223:9100"}) * 100 192.168.61.223:9100的磁盘使用率(单位为percent) ((node_filesystem_size_bytes{fstype=~"xfs|ext4",instance="192.168.61.223:9100"} - node_filesystem_free_bytes{fstype=~"xfs|ext4",instance="192.168.61.223:9100"}) / node_filesystem_size_bytes{fstype=~"xfs|ext4",instance="192.168.61.223:9100"}) * 100 uptime时间(单位为seconds) time() - node_boot_time 192.168.61.223:9100的uptime时间(单位为seconds) time() - node_boot_time_seconds{instance="192.168.61.223:9100"} 网络流出量(单位为bytes/sec) irate(node_network_transmit_bytes_total{device!~"lo|bond[0-9]|cbr[0-9]|veth.*"}[5m]) > 0 192.168.61.223:9100的网络流出量(单位为bytes/sec) irate(node_network_transmit_bytes_total{instance="192.168.61.223:9100", device!~"lo|bond[0-9]|cbr[0-9]|veth.*"}[5m]) > 0 网络流入量(单位为bytes/sec) irate(node_network_receive_bytes_total{device!~"lo|bond[0-9]|cbr[0-9]|veth.*"}[5m]) > 0 192.168.61.223:9100的网络流入量(单位为bytes/sec) irate(node_network_receive_bytes_total{instance="192.168.61.223:9100", device!~"lo|bond[0-9]|cbr[0-9]|veth.*"}[5m]) > 0 磁盘读取速度(单位为bytes/sec) irate(node_disk_read_bytes_total{device=~"sd.*"}[5m]) 官网文档:irate适合快速变化的计数器(counter),而rate适合缓慢变化的计数器(counter) 参考: https://songjiayang.gitbooks.io/prometheus/content/exporter/nodeexporter_query.html
pod exporter的一些计算语句 容器CPU使用率: sum(irate(container_cpu_usage_seconds_total{image!=""}[1m])) without (cpu) 全部容器的CPU使用率总和: sum(sum(rate(container_cpu_usage_seconds_total{name=~".+"}[1m])) by (name) * 100) 查询容器内存使用量(单位:字节): container_memory_usage_bytes{image!=""} 所有容器总和: sum(container_memory_rss{name=~".+"}) 所有容器当前内存使用: container_memory_usage_bytes{name=~".+"} ## container_memory_usage_bytes : Current memory usage in bytes. 所有容器当前内存使用总和: sum(container_memory_usage_bytes{name=~".+"}) 查询容器网络接收量速率(单位:字节/秒): sum(rate(container_network_receive_bytes_total{image!=""}[1m])) without (interface) 查询容器网络传输量速率(单位:字节/秒): sum(rate(container_network_transmit_bytes_total{image!=""}[1m])) without (interface) 查询容器文件系统读取速率(单位:字节/秒): sum(rate(container_fs_reads_bytes_total{image!=""}[1m])) without (device) 查询容器文件系统写入速率(单位:字节/秒): sum(rate(container_fs_writes_bytes_total{image!=""}[1m])) without (device) 容器入带宽大于50M sum by (namespace,job,pod_name) (irate(container_network_receive_bytes_total{image!=""}[3m])) / 1024 /1024 > 50 容器出带宽大于50M sum by (namespace,job,pod_name) (irate(container_network_transmit_bytes_total{image!=""}[1m])) / 1024 /1024 > 50
Prometheus基础
1.时间序列是指将同一统计指标的数值按其发生的时间先后顺序排列而成的数列
=:选择正好相等的字符串标签
!=:选择不相等的字符串标签
=~:选择匹配正则表达式的标签(或子标签)
!~:选择不匹配正则表达式的标签(或子标签)
s:seconds
m:minutes
h:hours
d:days
w:weeks
y:years
注: [1m]指过去的1分钟内
4.操作符
bool
and
or
unless
on
without : without(label)在结果中移除括号内的标签和值
by : by(label)在结果中只保留括号内的标签和值
作者:一毛
本博客所有文章仅用于学习、研究和交流目的,欢迎非商业性质转载。
不管遇到了什么烦心事,都不要自己为难自己;无论今天发生多么糟糕的事,都不应该感到悲伤。记住一句话:越努力,越幸运。
分类:
Kubernetes
, Prometheus
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?