Kubernetes 计算 CPU 使用率

简介: Kubernetes 计算 CPU 使用率



网络异常,图片无法展示
|

使用 Prometheus 配置 kubernetes 环境中 Container 的 CPU 使用率时,会经常遇到 CPU 使用超出 100%,下面就来解释一下:

  1. container_spec_cpu_period
    当对容器进行 CPU 限制时,CFS 调度的时间窗口,又称容器 CPU 的时钟周期通常是 100,000 微秒
  2. container_spec_cpu_quota
    是指容器的使用 CPU 时间周期总量,如果 quota 设置的是 700,000,就代表该容器可用的 CPU 时间是 7*100,000 微秒,通常对应 kubernetes 的 resource.cpu.limits 的值
  3. container_spec_cpu_share
    是指 container 使用分配主机 CPU 相对值,比如 share 设置的是 500m,代表窗口启动时向主机节点申请 0.5 个 CPU,也就是 50,000 微秒,通常对应 kubernetes 的 resource.cpu.requests 的值
  4. container_cpu_usage_seconds_total
    统计容器的 CPU 在一秒内消耗使用率,应注意的是该 container 所有的 CORE
  5. container_cpu_system_seconds_total
    统计容器内核态在一秒时间内消耗的 CPU
  6. container_cpu_user_seconds_total
    统计容器用户态在一秒时间内消耗的 CPU
    参考官方地址 https://docs.signalfx.com/en/latest/integrations/agent/monitors/cadvisor.htmlhttps://github.com/google/cadvisor/blob/master/docs/storage/prometheus.md

具体公式

  1. 默认如果直接使用 container_cpu_usage_seconds_total 的话,如下
    sum(irate(container_cpu_usage_seconds_total{container="$Container",instance="$Node",pod="$Pod"}[5m])*100)by(pod)
    默认统计的数据是该容器所有的 CORE 的平均使用率

网络异常,图片无法展示
|


  1. 如果要精确计算每个容器的 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

网络异常,图片无法展示
|


  1. 参考官方 git issue
    https://github.com/google/cadvisor/issues/2026#issuecomment-415819667

docker stats

docker stats 输出的指标列是如何计算的,如下:

首先 docker stats 是通过 Docker API /containers/(id)/stats 接口来获得 live data stream,再通过 docker stats 进行整合。

在 Linux 中使用 docker stats 输出的内存使用率(MEM USAGE),实则该列的计算是不包含 Cache 的内存。

cache usage 在 ≤ docker 19.03 版本的 API 接口输出对应的字段是 memory_stats.total_inactive_file,而 > docker 19.03 的版本对应的字段是 memory_stats.cache。

docker stats 输出的 PIDS 一列代表的是该容器创建的进程或线程的数量,threads 是 Linux kernel 中的一个术语,又称 lightweight process & kernel task。


  1. 如何通过 Docker API 查看容器资源使用率,如下


$ curl -s --unix-socket /var/run/docker.sock "http://localhost/v1.40/containers/10f2db238edc/stats" | jq -r{  "read": "2022-01-05T06:14:47.705943252Z",  "preread": "0001-01-01T00:00:00Z",  "pids_stats": {    "current": 240  },  "blkio_stats": {    "io_service_bytes_recursive": [      {        "major": 253,        "minor": 0,        "op": "Read",        "value": 0      },      {        "major": 253,        "minor": 0,        "op": "Write",        "value": 917504      },      {        "major": 253,        "minor": 0,        "op": "Sync",        "value": 0      },      {        "major": 253,        "minor": 0,        "op": "Async",        "value": 917504      },      {        "major": 253,        "minor": 0,        "op": "Discard",        "value": 0      },      {        "major": 253,        "minor": 0,        "op": "Total",        "value": 917504      }    ],    "io_serviced_recursive": [      {        "major": 253,        "minor": 0,        "op": "Read",        "value": 0      },      {        "major": 253,        "minor": 0,        "op": "Write",        "value": 32      },      {        "major": 253,        "minor": 0,        "op": "Sync",        "value": 0      },      {        "major": 253,        "minor": 0,        "op": "Async",        "value": 32      },      {        "major": 253,        "minor": 0,        "op": "Discard",        "value": 0      },      {        "major": 253,        "minor": 0,        "op": "Total",        "value": 32      }    ],    "io_queue_recursive": [],    "io_service_time_recursive": [],    "io_wait_time_recursive": [],    "io_merged_recursive": [],    "io_time_recursive": [],    "sectors_recursive": []  },  "num_procs": 0,  "storage_stats": {},  "cpu_stats": {    "cpu_usage": {      "total_usage": 251563853433744,      "percpu_usage": [        22988555937059,        6049382848016,        22411490707722,        5362525449957,        25004835766513,        6165050456944,        27740046633494,        6245013152748,        29404953317631,        5960151933082,        29169053441816,        5894880727311,        25772990860310,        5398581194412,        22856145246881,        5140195759848      ],      "usage_in_kernelmode": 30692640000000,      "usage_in_usermode": 213996900000000    },    "system_cpu_usage": 22058735930000000,    "online_cpus": 16,    "throttling_data": {      "periods": 10673334,      "throttled_periods": 1437,      "throttled_time": 109134709435    }  },  "precpu_stats": {    "cpu_usage": {      "total_usage": 0,      "usage_in_kernelmode": 0,      "usage_in_usermode": 0    },    "throttling_data": {      "periods": 0,      "throttled_periods": 0,      "throttled_time": 0    }  },  "memory_stats": {    "usage": 8589447168,    "max_usage": 8589926400,    "stats": {      "active_anon": 0,      "active_file": 260198400,      "cache": 1561460736,      "dirty": 3514368,      "hierarchical_memory_limit": 8589934592,      "hierarchical_memsw_limit": 8589934592,      "inactive_anon": 6947250176,      "inactive_file": 1300377600,      "mapped_file": 0,      "pgfault": 3519153,      "pgmajfault": 0,      "pgpgin": 184508478,      "pgpgout": 184052901,      "rss": 6947373056,      "rss_huge": 6090129408,      "total_active_anon": 0,      "total_active_file": 260198400,      "total_cache": 1561460736,      "total_dirty": 3514368,      "total_inactive_anon": 6947250176,      "total_inactive_file": 1300377600,      "total_mapped_file": 0,      "total_pgfault": 3519153,      "total_pgmajfault": 0,      "total_pgpgin": 184508478,      "total_pgpgout": 184052901,      "total_rss": 6947373056,      "total_rss_huge": 6090129408,      "total_unevictable": 0,      "total_writeback": 0,      "unevictable": 0,      "writeback": 0    },    "limit": 8589934592  },  "name": "/k8s_prod-xc-fund_prod-xc-fund-646dfc657b-g4px4_prod_523dcf9d-6137-4abf-b4ad-bd3999abcf25_0",  "id": "10f2db238edc13f538716952764d6c9751e5519224bcce83b72ea7c876cc0475"


  1. 如何计算
    官方地址
    https://docs.docker.com/engine/api/v1.40/#operation/ContainerStats
    The precpu_stats is the CPU statistic of the previous read, and is used to calculate the CPU usage percentage. It is not an exact copy of the cpu_stats field.
    If either precpu_stats.online_cpus or cpu_stats.online_cpus is nil then for compatibility with older daemons the length of the corresponding cpu_usage.percpu_usage array should be used.
    To calculate the values shown by the stats command of the docker cli tool the following formulas can be used:
    used_memory = memory_stats.usage - memory_stats.stats.cache
    available_memory = memory_stats.limit
    Memory usage % = (used_memory / available_memory) * 100.0
    cpu_delta = cpu_stats.cpu_usage.total_usage - precpu_stats.cpu_usage.total_usage
    system_cpu_delta = cpu_stats.system_cpu_usage - precpu_stats.system_cpu_usage
    number_cpus = lenght(cpu_stats.cpu_usage.percpu_usage) or cpu_stats.online_cpus
    CPU usage % = (cpu_delta / system_cpu_delta) * number_cpus * 100.0
相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。     相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
Linux API C++
PCI-E配置MSI中断流程解析
<div id="article_content" class="article_content" style="margin: 20px 0px 0px; font-size: 14px; line-height: 26px; font-family: Arial;"> <p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0
8028 0
|
Prometheus Kubernetes 监控
云原生|kubernetes |使用Prometheus监控k8s cAdvisor篇(进阶篇--- 一)(centos操作系统)
云原生|kubernetes |使用Prometheus监控k8s cAdvisor篇(进阶篇--- 一)(centos操作系统)
4039 0
|
11月前
|
供应链 容器
什么是code128码?
Code 128码是一种高密度条形码,支持全ASCII字符,广泛用于物流、运输和供应链管理。它分为A、B、C三个子集,可编码字母、数字及控制符,具有高密度、小空间优势,适用于复杂数据编码需求。
1741 3
|
机器学习/深度学习 人工智能 运维
运维不只是“修电脑”:聊聊运维如何助力 AI 优化服务质量
运维不只是“修电脑”:聊聊运维如何助力 AI 优化服务质量
727 9
|
8月前
|
机器学习/深度学习 数据采集 算法
大模型应用:K-Means/LDA + 千问大模型:无监督文本自动打标完整方案.85
本文介绍“聚类算法+大模型”无监督自动打标方案:先用K-Means/LDA对海量无标签文本(如电商评论、客服工单)自动分组,再由大模型为每簇生成可理解的业务标签与语义解释,实现从“类1/类2”到“物流慢”“价格争议”等高价值洞察的跃迁,显著降本增效。
972 6
|
9月前
|
数据采集 自然语言处理 监控
拒绝“数据断层”:高质量舆情分析背后的隐形功臣——动态节点池
在AI与大数据时代,社交媒体数据是舆情监控、情感分析的核心资产。但再精妙的NLP模型也难逃“垃圾进、垃圾出”——数据断层导致的幸存者偏差,常源于爬虫被限流封禁。本文揭示动态代理IP池如何保障数据时序完整性、提升并发吞吐、规避风控,附可落地的Python实战代码,强调:稳定的数据管道,才是最高级的ROI。
872 4
|
9月前
|
消息中间件 关系型数据库 Java
击穿分布式事务痛点:BASE 理论全解,从最终一致性到柔性事务落地全指南
本文深入解析分布式事务核心理论BASE(基本可用、软状态、最终一致性),对比ACID差异,详解五大最终一致性模型,并结合本地消息表、TCC、SAGA等四种柔性事务方案,提供可落地的代码实例与避坑指南,助你平衡一致性与可用性。
1308 1
|
机器学习/深度学习 运维 数据挖掘
运维告警不是“玄学”:聊聊怎么用机器学习优化事件关联分析
运维告警不是“玄学”:聊聊怎么用机器学习优化事件关联分析
642 3
|
Linux 网络安全 数据安全/隐私保护
|
机器学习/深度学习 人工智能 Python

热门文章

最新文章