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


相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
3月前
|
弹性计算 人工智能 Serverless
阿里云ACK One:注册集群云上节点池(CPU/GPU)自动弹性伸缩,助力企业业务高效扩展
在当今数字化时代,企业业务的快速增长对IT基础设施提出了更高要求。然而,传统IDC数据中心却在业务存在扩容慢、缩容难等问题。为此,阿里云推出ACK One注册集群架构,通过云上节点池(CPU/GPU)自动弹性伸缩等特性,为企业带来全新突破。
|
16天前
|
Cloud Native Serverless 数据中心
阿里云ACK One:注册集群支持ACS算力——云原生时代的计算新引擎
ACK One注册集群已正式支持ACS(容器计算服务)算力,为企业的容器化工作负载提供更多选择和更强大的计算能力。
|
2月前
|
人工智能 缓存 并行计算
转载:【AI系统】CPU 计算本质
本文深入探讨了CPU计算性能,分析了算力敏感度及技术趋势对CPU性能的影响。文章通过具体数据和实例,讲解了CPU算力的计算方法、算力与数据加载之间的平衡,以及如何通过算力敏感度分析优化计算系统性能。同时,文章还考察了服务器、GPU和超级计算机等平台的性能发展,揭示了这些变化如何塑造我们对CPU性能的理解和期待。
转载:【AI系统】CPU 计算本质
|
6月前
|
C++
C++ 根据程序运行的时间和cpu频率来计算在另外的cpu上运行所花的时间
C++ 根据程序运行的时间和cpu频率来计算在另外的cpu上运行所花的时间
69 0
|
3月前
|
人工智能 缓存 并行计算
【AI系统】CPU 计算本质
本文深入探讨了CPU计算性能,分析了算力敏感度及技术趋势对CPU性能的影响。文章通过具体数据和实例,解释了算力计算方法、数据加载与计算的平衡点,以及如何通过算力敏感度分析优化性能瓶颈。同时,文章还讨论了服务器、GPU和超级计算机等不同计算平台的性能发展趋势,强调了优化数据传输速率和加载策略的重要性。
123 4
|
3月前
|
弹性计算 Kubernetes Perl
k8s 设置pod 的cpu 和内存
在 Kubernetes (k8s) 中,设置 Pod 的 CPU 和内存资源限制和请求是非常重要的,因为这有助于确保集群资源的合理分配和有效利用。你可以通过定义 Pod 的 `resources` 字段来设置这些限制。 以下是一个示例 YAML 文件,展示了如何为一个 Pod 设置 CPU 和内存资源请求(requests)和限制(limits): ```yaml apiVersion: v1 kind: Pod metadata: name: example-pod spec: containers: - name: example-container image:
556 1
|
3月前
|
缓存 人工智能 算法
【AI系统】CPU 计算时延
CPU(中央处理器)是计算机系统的核心,其计算时延(从指令发出到完成所需时间)对系统性能至关重要。本文探讨了CPU计算时延的组成,包括指令提取、解码、执行、存储器访问及写回时延,以及影响时延的因素,如时钟频率、流水线技术、并行处理、缓存命中率和内存带宽。通过优化这些方面,可以有效降低计算时延,提升系统性能。文中还通过具体示例解析了时延产生的原因,强调了内存时延对计算速度的关键影响。
68 0
|
5月前
|
KVM 虚拟化
计算虚拟化之CPU——qemu解析
【9月更文挑战10天】本文介绍了QEMU命令行参数的解析过程及其在KVM虚拟化中的应用。展示了QEMU通过多个`qemu_add_opts`函数调用处理不同类型设备和配置选项的方式,并附上了OpenStack生成的一个复杂KVM参数实例。
|
5月前
|
算法 C++
如何精确计算出一个算法的CPU运行时间?
如何精确计算出一个算法的CPU运行时间?
|
6月前
|
算法 Windows
CAE如何基于CPU最佳核数和token等计算成本
【8月更文挑战第26天】在使用CAE(计算机辅助工程)进行分析计算时,需综合考虑CPU核数和token对成本的影响。CPU核数越多,虽能加速计算,但过多核数会因通信开销和内存带宽限制导致性能提升放缓。成本计算需考虑硬件租赁或购买费用及云服务收费标准。Token作为软件许可,需分摊到每次计算中。通过测试优化找到性能与成本的平衡点,实现最低成本下的高效计算。
120 1

热门文章

最新文章