获取Docker容器的内存使用情况

一开始以为使用 free -h 得到的就是docker 容器本身的内存使用情况

#容器
root@feb0423bbe52:/sys/fs/cgroup/memory# free -h
              total        used        free      shared  buff/cache   available
Mem:            62G         21G        267M        423M         41G         40G
Swap:          5.0G        834M        4.2G

看到这个结果产生了疑问,因为和宿主机一模一样啊!

#宿主机
[root@mh-server-22 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:            62G         19G        1.2G        337M         41G         42G
Swap:          5.0G        835M        4.2G


网上查到说,可以使用 docker stats --no-stream 查看容器内存、CPU使用情况,可以是可以,但是还是不够详细(可能大多数人在这一步就OK了,也没必要往下看了)

我需要知道docker容器自己使用了多少cache memory以及更多信息。

经过各种Google,找到这篇文章 Getting memory usage in Linux and Docker,他也提到在容器里使用 free -h 和在宿主机使用 free -h 是一样的,free -h 本质上是读取 /proc/meminfo 文件内容。

文章中简单的介绍了下Docker背后隔离原理:

According to Docker Overview: The Underlying Technology, processes in a Docker container directly run in their host OS without any virtualization, but they are isolated from the host OS and other containers in effect thanks to these Linux kernel features:

namespaces: Isolate PIDs, hostnames, user IDs, network accesses, IPC, etc.
cgroups: Limit resource usage
UnionFS: Isolate file system
Because of the namespaces, ps command lists processes of Docker containers in addition to other processes in the host OS, while it cannot list processes of host OS or other containers in a docker container.

By default, Docker containers have no resource constraints. So, if you run one container in a host and don’t limit resource usage of the container, and this is my case, the container’s “free memory” is same as the host OS’s “free memory”.

核心意思是 资源不隔离。所以 the container’s “free memory” is same as the host OS’s “free memory”.

然而要怎么样才能获取到容器本身的资源信息呢?

进入容器的 /sys/fs/cgroup/memory 这个目录

里面有关内存的一些信息。如下:

root@feb0423bbe52:/sys/fs/cgroup/memory# ls
cgroup.clone_children  memory.force_empty              memory.kmem.slabinfo                memory.kmem.tcp.usage_in_bytes  memory.memsw.failcnt             memory.move_charge_at_immigrate  memory.soft_limit_in_bytes  memory.use_hierarchy
cgroup.event_control   memory.kmem.failcnt             memory.kmem.tcp.failcnt             memory.kmem.usage_in_bytes      memory.memsw.limit_in_bytes      memory.numa_stat                 memory.stat                 notify_on_release
cgroup.procs           memory.kmem.limit_in_bytes      memory.kmem.tcp.limit_in_bytes      memory.limit_in_bytes           memory.memsw.max_usage_in_bytes  memory.oom_control               memory.swappiness           tasks
memory.failcnt         memory.kmem.max_usage_in_bytes  memory.kmem.tcp.max_usage_in_bytes  memory.max_usage_in_bytes       memory.memsw.usage_in_bytes      memory.pressure_level            memory.usage_in_bytes
root@feb0423bbe52:/sys/fs/cgroup/memory#

查看 memory.stat 文件内容,可以得到很多信息,包括我需要的cache memory:

  • total_cache 1579098112 : 缓存内存
  • total_rss 497885184:已使用内存

附注:memory.stat 每项含义请看这篇文章https://xuxinkun.github.io/2016/05/16/memory-monitor-with-cgroup/

其实portainer.io对容器的统计图表也是读取的memory.stat 文件:

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以通过使用Docker的Java API来获取Docker容器的CPU、内存和网络信息。 1. 获取Docker容器的CPU信息: Java可以使用Docker的Java API的`stats()`方法获取Docker容器的统计信息。该方法会返回一个包含了Docker容器的CPU使用率的数据对象。具体代码如下: ```java DockerClient dockerClient = DefaultDockerClient.fromEnv().build(); Stats stats = dockerClient.stats(containerId); double cpuUsage = stats.getCpuStats().getCpuUsage().getTotalUsage(); ``` 2. 获取Docker容器内存信息: Java可以使用Docker的Java API的`stats()`方法获取Docker容器的统计信息。该方法会返回一个包含了Docker容器内存使用率的数据对象。具体代码如下: ```java DockerClient dockerClient = DefaultDockerClient.fromEnv().build(); Stats stats = dockerClient.stats(containerId); long memoryUsage = stats.getMemoryStats().getUsage(); ``` 3. 获取Docker容器的网络信息: Java可以使用Docker的Java API的`inspectContainer()`方法获取Docker容器的详细信息,包括网络信息。具体代码如下: ```java DockerClient dockerClient = DefaultDockerClient.fromEnv().build(); ContainerInfo containerInfo = dockerClient.inspectContainer(containerId); Map<String, Object> networkSettings = containerInfo.getNetworkSettings(); ``` 上述代码的`networkSettings`对象包含了Docker容器的网络信息,可以通过该对象获取容器的IP地址、网关等网络相关信息。 需要注意的是,上述代码的`containerId`是Docker容器的唯一标识符,需要根据实际情况进行替换。另外,还需要提前安装并配置好Docker环境,并添加相应的Java Docker API依赖。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值