如何正確計(jì)算 Kubernetes 容器 CPU 使用率

參數(shù)解釋
使用 Prometheus 配置 kubernetes 環(huán)境中 Container 的 CPU 使用率時(shí),會(huì)經(jīng)常遇到 CPU 使用超出 100%,下面就來解釋一下:
container_spec_cpu_period
當(dāng)對(duì)容器進(jìn)行 CPU 限制時(shí),CFS 調(diào)度的時(shí)間窗口,又稱容器 CPU 的時(shí)鐘周期通常是 100,000 微秒
container_spec_cpu_quota
是指容器的使用 CPU 時(shí)間周期總量,如果 quota 設(shè)置的是 700,000,就代表該容器可用的 CPU 時(shí)間是 7*100,000 微秒,通常對(duì)應(yīng) kubernetes 的 resource.cpu.limits 的值
container_spec_cpu_share
是指 container 使用分配主機(jī) CPU 相對(duì)值,比如 share 設(shè)置的是 500m,代表窗口啟動(dòng)時(shí)向主機(jī)節(jié)點(diǎn)申請(qǐng) 0.5 個(gè) CPU,也就是 50,000 微秒,通常對(duì)應(yīng) kubernetes 的 resource.cpu.requests 的值
container_cpu_usage_seconds_total
統(tǒng)計(jì)容器的 CPU 在一秒內(nèi)消耗使用率,應(yīng)注意的是該 container 所有的 CORE
container_cpu_system_seconds_total
統(tǒng)計(jì)容器內(nèi)核態(tài)在一秒時(shí)間內(nèi)消耗的 CPU
container_cpu_user_seconds_total
統(tǒng)計(jì)容器用戶態(tài)在一秒時(shí)間內(nèi)消耗的 CPU
參考官方地址 https://docs.signalfx.com/en/latest/integrations/agent/monitors/cadvisor.html https://github.com/google/cadvisor/blob/master/docs/storage/prometheus.md
具體公式
默認(rèn)如果直接使用 container_cpu_usage_seconds_total 的話,如下
sum(irate(container_cpu_usage_seconds_total{container="$Container",instance="$Node",pod="$Pod"}[5m])*100)by(pod)默認(rèn)統(tǒng)計(jì)的數(shù)據(jù)是該容器所有的 CORE 的平均使用率

如果要精確計(jì)算每個(gè)容器的 CPU 使用率,使用 % 呈現(xiàn)的形式,如下
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,就代表該容器有多少個(gè) CORE

參考官方 git issue
https://github.com/google/cadvisor/issues/2026#issuecomment-415819667
docker stats
docker stats 輸出的指標(biāo)列是如何計(jì)算的,如下:
首先 docker stats 是通過 Docker API /containers/(id)/stats 接口來獲得 live data stream,再通過 docker stats 進(jìn)行整合。
在 Linux 中使用 docker stats 輸出的內(nèi)存使用率(MEM USAGE),實(shí)則該列的計(jì)算是不包含 Cache 的內(nèi)存。
cache usage 在 ≤ docker 19.03 版本的 API 接口輸出對(duì)應(yīng)的字段是 memory_stats.total_inactive_file,而 > docker 19.03 的版本對(duì)應(yīng)的字段是 memory_stats.cache。
docker stats 輸出的 PIDS 一列代表的是該容器創(chuàng)建的進(jìn)程或線程的數(shù)量,threads 是 Linux kernel 中的一個(gè)術(shù)語,又稱 lightweight process & kernel task。
如何通過 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"如何計(jì)算
官方地址
https://docs.docker.com/engine/api/v1.40/#operation/ContainerStats
The
precpu_statsis the CPU statistic of the previous read, and is used to calculate the CPU usage percentage. It is not an exact copy of thecpu_statsfield.If either
precpu_stats.online_cpusorcpu_stats.online_cpusis nil then for compatibility with older daemons the length of the correspondingcpu_usage.percpu_usagearray should be used.To calculate the values shown by the
statscommand of the docker cli tool the following formulas can be used:used_memory = memory_stats.usage - memory_stats.stats.cacheavailable_memory = memory_stats.limitMemory usage % = (used_memory / available_memory) * 100.0cpu_delta = cpu_stats.cpu_usage.total_usage - precpu_stats.cpu_usage.total_usagesystem_cpu_delta = cpu_stats.system_cpu_usage - precpu_stats.system_cpu_usagenumber_cpus = lenght(cpu_stats.cpu_usage.percpu_usage)orcpu_stats.online_cpusCPU usage % = (cpu_delta / system_cpu_delta) * number_cpus * 100.0
鏈接:https://www.cnblogs.com/apink/p/15767687.html
(版權(quán)歸原作者所有,侵刪)
10T 技術(shù)資源大放送!包括但不限于:Linux、虛擬化、容器、云計(jì)算、網(wǎng)絡(luò)、Python、Go 等。在開源Linux公眾號(hào)內(nèi)回復(fù)「10T」,即可免費(fèi)獲??!
shell編程100例(附PDF下載)
IPv6技術(shù)白皮書(附PDF下載)
Linux主流發(fā)行版本配置IP總結(jié)(Ubuntu、CentOS、Redhat、Suse)
批量安裝Windows系統(tǒng)
無人值守批量安裝服務(wù)器
運(yùn)維必備的《網(wǎng)絡(luò)端口大全》,看這一份就夠了。
收藏:服務(wù)器和存儲(chǔ)知識(shí)入門
什么叫SSH?原理詳解,看這一篇就夠了!
Nginx面試40問(收藏吃灰)
20 個(gè) Linux 服務(wù)器性能調(diào)優(yōu)技巧
超詳細(xì)!一文帶你了解LVS四層負(fù)載均衡企業(yè)級(jí)實(shí)踐!
收藏 | Linux系統(tǒng)日志位置及包含的日志內(nèi)容介紹
100 道 Linux 常見面試題,建議收藏,慢慢讀~
服務(wù)器12種基本故障+排查方法
IT運(yùn)維管理常用工具大全,讓你成為真正的高手
什么是QoS?
有收獲,點(diǎn)個(gè)在看 
shell編程100例(附PDF下載)
IPv6技術(shù)白皮書(附PDF下載)
Linux主流發(fā)行版本配置IP總結(jié)(Ubuntu、CentOS、Redhat、Suse)
批量安裝Windows系統(tǒng)
無人值守批量安裝服務(wù)器
運(yùn)維必備的《網(wǎng)絡(luò)端口大全》,看這一份就夠了。
收藏:服務(wù)器和存儲(chǔ)知識(shí)入門
什么叫SSH?原理詳解,看這一篇就夠了!
Nginx面試40問(收藏吃灰)
20 個(gè) Linux 服務(wù)器性能調(diào)優(yōu)技巧
超詳細(xì)!一文帶你了解LVS四層負(fù)載均衡企業(yè)級(jí)實(shí)踐!
收藏 | Linux系統(tǒng)日志位置及包含的日志內(nèi)容介紹
100 道 Linux 常見面試題,建議收藏,慢慢讀~
服務(wù)器12種基本故障+排查方法
IT運(yùn)維管理常用工具大全,讓你成為真正的高手
什么是QoS?
有收獲,點(diǎn)個(gè)在看
