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 全天最高使用量
- 以下PromQL统计的资源使用量进包含业务容器,若pod含有其它容器时(如sidecar),数值将略低于kubectl top pod命令
- irate更准确,rate统计误差较大
- 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
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/70878.html