Docker CPU percentage

Is there a way that I can get the CPU percentage in the docker container and not outside it ?! docker stats DOCKER_ID

shows the percentage which is exactly what I need, but I need it as a variable. I need to get the percentage of cpu inside the container itself and perform some operation on it. I've looked at different things like cgroup API and docker rest, but they don't provide CPU percentage. If there is a way to get the CPU percentage inside the container and not outside it, that would be ideal. I found one solution provided by someone from below link, which is still outside the container by the rest of the api, however I didn't really figure out how to calculate the percentage.

Use Docker Container Processor Percentage

+3


source to share


2 answers


You can install Google cAdvisor with Axibase Time-Series Database Storage Driver. It will collect and save the CPU usage measured in both base units and percentage.

Screenshots with examples of how the processor is reported are located at the bottom of the page: https://axibase.com/products/axibase-time-series-database/writing-data/docker-cadvisor/

In a centralized configuration, the ATSD container itself can ingest metrics from multiple cAdvisor instances installed on multiple docker nodes.



EDIT 1: One liner for calculating the total CPU usage of all processes running inside a container. Adjust the -d parameter to change the sample spacing to smooth out spikes:

top -b -d 5 -n 2 | awk '$1 == "PID" {block_num++; next} block_num == 2 {sum += $9;} END {print sum}'

      

0


source


I used ctop , which gives a more graphical way than docker_stats But I found that the CPU hit rate is higher than what Top showed for the system. This is mainly shown relative to the root process. Docker containers run as a child process.

To illustrate an example

First, find the root process where all containers run

docker-containerd-shim - The Docker architecture is broken down into four components: Docker engine, containerd, containerd-shm, and runC. The binaries are named docker, docker-containerd, docker-containerd-shim, and docker-runc respectively. - https://hackernoon.com/docker-containerd-standalone-runtimes-heres-what-you-should-know-b834ef155426

root 1843 1918 0 Aug31 ? 00:00:00 docker-containerd-shim 611bd9... /var/run/docker/libcontainerd/611bd92.... docker-runc

You can see all containers that are executed with the command

pstree -p 1918



Now tell us that we are interested in the CPU consumption of fluentdb.

An easy way to get the child pid of this

pstree -p 1918 |grep fluentd

What gives 21670

You can now run top -p 21670

to see the CPU share of this child process, and also top -p 1918

to see the total CPU of the parent process.

With the creation of the cadvisor in Promethus and the scan in Grafana, this was the closest and most accurate representation of the actual percentage of processor used by the container; in relation to the host machine. This diagram illustrates this. CTop and docker statistics gives 23% as a percentage of CPU. The actual CPU percentage of the parent docker process is about 2%, and the cAdvisor output from Grafana shows the most "accurate" percentage of the container CPU's percentage associated with the host.

graphana-cadvisor-top-ptree-docker

0


source







All Articles