从dockerized应用程序中获取docker容器的CPU和内存使用情况 [英] Getting CPU and Memory usage of a docker container from within the dockerized application

查看:62
本文介绍了从dockerized应用程序中获取docker容器的CPU和内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Docker容器中运行一个node.js应用程序.我正在尝试检索运行node.js应用程序的容器的系统使用率指标.现在,我正在使用 https://www.npmjs.com/package/dockerstats ,但是始终不显示cpu或内存使用情况,运行docker stats则显示每个视图.

I'm running a node.js application from within a docker container. I'm trying to retrieve system usage metrics of the container the node.js application is running inside of. Right now I'm using https://www.npmjs.com/package/dockerstats but it consistently shows no cpu or memory usage, running docker stats shows usage in each.

我的代码类似于以下内容:

My code resembles the following:

let dockerId = setUp.getDockerId();
dockerId.then(dockerId => {
    if (dockerId !== null) {
            console.log(`dockerId: ${dockerId}`);
            dockerstats.dockerContainerStats(dockerId, data => {
                console.log(`cpu_percent: ${data.cpu_percent}`);
                console.log(`memPercent: ${data.memPercent}`);
                console.log(`memUsage: ${data.memUsage}`);
            });
        }
    });

setUp类类似于以下内容,并使用 https://www.npmjs.com/包/docker-container-id :

The setUp class resembles the following and uses https://www.npmjs.com/package/docker-container-id:

const getId = require('docker-container-id');
module.exports = class setUp {

    getDockerId () {
        return getId().then(id => {
            if (!id) {
                return null;
            }
            return id;
        });
    }
}

如您所说,

推荐答案

您正在使用 docker-container-id 包获取容器ID.该程序包通过检查/proc/self/cgroup 文件起作用,因此它仅应在容器内部工作(即,仅当从容器化过程执行 getContainerId()时)).也就是说,进一步,我将假设您正在尝试从运行应用程序的容器内部获取指标(您没有明确提到这一事实).

As you said, you are using the docker-container-id package to obtain the container ID. This package works by inspecting the /proc/self/cgroup file, thus it should work only from inside the container (i.e. only when getContainerId() is executed from the containerized process). That said, further I will assume that you are trying to obtain the metrics from inside the container where your application runs (you did not mentioned this fact explicitly).

这里的问题是,如 dockerstats 软件包说明中所述,此软件包使用Docker API,并且按照

The problem here is that, as stated in the dockerstats package description, this package uses Docker API and, as per package source, the client connects to the docker socket (/var/run/docker.sock), which is is not available inside the container by default. The easy (but dangerous) way to workaround this is to mount host's /var/run/docker.sock into the container by using the following option when starting the container:

-v /var/run/docker.sock:/var/run/docker.sock

例如

docker run -v /var/run/docker.sock:/var/run/docker.sock $MY_IMAGE_NAME

但是,强烈反对,因为它会带来严重的安全风险.切勿在生产中执行此操作.这样,您允许您的容器控制Docker,这与授予容器 root 访问主机系统的权限基本相同.

However, this is STRONGLY DISCOURAGED, as it creates a serious security risk. Never do this in production. By doing so, you are allowing your container to control Docker, which is essentially the same as giving the container root access to the host system.

但是您实际上不需要使用Docker API来访问资源消耗指标.关键是您可以直接从<读取有关进程的 cpuacct memory 控制组(分别负责跟踪和限制CPU和内存消耗)的信息.代码>/sys/fs/cgroup .例如,读取/sys/fs/cgroup/memory/memory.usage_in_bytes 文件将为您提供容器使用的内存量(以字节为单位):

But you actually don't need to use the Docker API to access resource consumption metrics. The point is that you may directly read the information about process' cpuacct and memory control groups (which are responsible for tracking and limiting the CPU and the memory consumption respectively) from /sys/fs/cgroup. For example, reading the /sys/fs/cgroup/memory/memory.usage_in_bytes file will give you the amount of memory used by your container (in bytes):

# cat /sys/fs/cgroup/memory/memory.usage_in_bytes 
164823040

读取/sys/fs/cgroup/cpuacct/cpuacct.usage 文件将为您提供容器的CPU使用总量(以纳秒为单位):

And reading the /sys/fs/cgroup/cpuacct/cpuacct.usage file will give you a total CPU usage of your container (in nanoseconds):

# cat /sys/fs/cgroup/cpuacct/cpuacct.usage
2166331144

因此,您可以从应用程序中读取这些指标并进行处理.您也可以使用 procfs 中的统计信息,请参考

So, you can read these metrics from your application and process them. Also you may use statistics from procfs, refer to this discussion for details.

这篇关于从dockerized应用程序中获取docker容器的CPU和内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆