Prometheus SQL统计容器cpu、内存使用率

1 概述

通过prometheus SQL统计容器cpu、内存使用率,指导容器requests、limits值的设置,提高资源利用率,降低云上资源成本。

2 方法

2.1 容器limits、requests值

  • CPU
# limits
avg by (container) (kube_pod_container_resource_limits{resource="cpu"})

# requests
avg by (container, resource) (kube_pod_container_resource_requests{resource="cpu"})
  • 内存(单位:Mi)
# limits
avg by (container) (kube_pod_container_resource_limits{resource="memory"})/1024/1024

# requests
avg by (container) (kube_pod_container_resource_requests{resource="memory"})/1024/1024

2.2 全天最高Limits值

用于判断pod的hpa伸缩区间,需注意的是,滚动更新时实际pod数量大于hpa限制,因此总资源并不恒等于hpa*limits

# CPU
max_over_time(
  sum by (container) (kube_pod_container_resource_limits{resource="cpu"}
  )[1d:]
)

# 内存
max_over_time(
  sum by (container)(kube_pod_container_resource_limits{resource="memory"}
  )[1d:]
)/1024/1024

2.3 全天最高使用量

  1. 以下PromQL统计的资源使用量进包含业务容器,若pod含有其它容器时(如sidecar),数值将略低于kubectl top pod命令
  2. irate更准确,rate统计误差较大
  3. avg更准确,sum在pod数量变化时存在成倍差异
# CPU
round(100*max_over_time(
  sum by(container) (irate(container_cpu_usage_seconds_total{}[5m])
  )[1d:]
))/100

# 内存
round(100*max_over_time(
  sum by(container) (container_memory_working_set_bytes{}
  )[1d:])/1024/1024/1024
)/100

2.4 全天最高使用率

# CPU
round(10000*max_over_time((
  avg by (container) (irate(container_cpu_usage_seconds_total {namespace="csdn",container!="consul"}[5m])) / 
  avg by (container) (kube_pod_container_resource_limits{resource="cpu"})
  ) [1d:])
)/100

# 内存
round(10000*max_over_time((
  avg by (container) (container_memory_working_set_bytes{}) / 
  avg by (container) (kube_pod_container_resource_limits{resource="memory"})
  ) [1d:])
)/100

2.5 全天平均使用率

# CPU 
round(10000*avg_over_time((
  avg by (container) (irate(container_cpu_usage_seconds_total {namespace="csdn",container!="consul"}[5m])) / 
  avg by (container) (kube_pod_container_resource_limits{resource="cpu"})
  ) [1d:])
)/100 

# 内存 
round(10000*avg_over_time((  
  avg by (container) (container_memory_working_set_bytes{}) / 
  avg by (container) (kube_pod_container_resource_limits{resource="memory"})
  ) [1d:])
)/100
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值