查看容器内存、CPU使用情况,可以是可以,但是还是不够详细, 可能大多数人在这一步就OK了,也没必要往下看了)
docker stats --no-stream
相关命令
根据内存占用大小排序
docker stats --no-stream | sort -rk7
我需要知道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@call-1299-051551186918-880000000248 /]# cd /sys/fs/cgroup/memory/
[root@call-1299-051551186918-880000000248 memory]# ls
cgroup.clone_children memory.kmem.failcnt memory.kmem.tcp.limit_in_bytes memory.max_usage_in_bytes memory.move_charge_at_immigrate memory.stat tasks
cgroup.event_control memory.kmem.limit_in_bytes memory.kmem.tcp.max_usage_in_bytes memory.memsw.failcnt memory.numa_stat memory.swappiness
cgroup.procs memory.kmem.max_usage_in_bytes memory.kmem.tcp.usage_in_bytes memory.memsw.limit_in_bytes memory.oom_control memory.usage_in_bytes
memory.failcnt memory.kmem.slabinfo memory.kmem.usage_in_bytes memory.memsw.max_usage_in_bytes memory.pressure_level memory.use_hierarchy
memory.force_empty memory.kmem.tcp.failcnt memory.limit_in_bytes memory.memsw.usage_in_bytes memory.soft_limit_in_bytes notify_on_release
[root@call-1299-051551186918-880000000248 memory]#
查看 memory.stat 文件内容,可以得到很多信息,包括我需要的cache memory:
[root@call-1299-051551186918-880000000248 memory]# cat memory.stat
cache 4157440
rss 2686976
rss_huge 0
shmem 0
mapped_file 4595712
dirty 0
writeback 0
swap 0
pgpgin 356400
pgpgout 359942
pgfault 802032
pgmajfault 0
inactive_anon 0
active_anon 3289088
inactive_file 1146880
active_file 786432
unevictable 0
hierarchical_memory_limit 9223372036854771712
hierarchical_memsw_limit 9223372036854771712
total_cache 4157440
total_rss 2686976
total_rss_huge 0
total_shmem 0
total_mapped_file 4595712
total_dirty 0
total_writeback 0
total_swap 0
total_pgpgin 356400
total_pgpgout 359942
total_pgfault 802032
total_pgmajfault 0
total_inactive_anon 0
total_active_anon 3289088
total_inactive_file 1146880
total_active_file 786432
total_unevictable 0
[root@call-1299-051551186918-880000000248 memory]#
- total_cache 1579098112 : 缓存内存
- total_rss 497885184:已使用内存
附注:memory.stat 每项含义请看这篇文章https://xuxinkun.github.io/2016/05/16/memory-monitor-with-cgroup/