我做了以下测试,发现我无法使用psutil
来检索有意义的数据。
基本上我正在尝试运行此命令以使用python来报告容器内的可用内存。
我已经运行了三次测试,每次都使用不同的内存参数:
以下是测试:
docker run -m 10000000 -ti python:2.7 bash
docker run -m 100000000 -ti python:2.7 bash
docker run -m 800000000 -ti python:2.7 bash
这是结果
> docker run -m 10000000 -ti python:2.7 bash
root@310de7b416cc:/# pip install psutil && python -c "import psutil; print psutil.virtual_memory().available"
Killed
> docker run -m 100000000 -ti python:2.7 bash
root@e7aade23c143:/# pip install psutil && python -c "import psutil; print psutil.virtual_memory().available"
Collecting psutil
Downloading psutil-5.3.1.tar.gz (397kB)
100% |████████████████████████████████| 399kB 2.8MB/s
Building wheels for collected packages: psutil
Running setup.py bdist_wheel for psutil ... done
Stored in directory: /root/.cache/pip/wheels/bc/00/68/b4cbf1017e55880ef2afd1a248a1c88311f38a574c8929d687
Successfully built psutil
Installing collected packages: psutil
Successfully installed psutil-5.3.1
1741164544
> docker run -m 800000000 -ti python:2.7 bash
root@b8a28ad93432:/# pip install psutil && python -c "import psutil; print psutil.virtual_memory().available"
Collecting psutil
Downloading psutil-5.3.1.tar.gz (397kB)
100% |████████████████████████████████| 399kB 2.8MB/s
Building wheels for collected packages: psutil
Running setup.py bdist_wheel for psutil ... done
Stored in directory: /root/.cache/pip/wheels/bc/00/68/b4cbf1017e55880ef2afd1a248a1c88311f38a574c8929d687
Successfully built psutil
Installing collected packages: psutil
Successfully installed psutil-5.3.1
1739603968
在第一次测试中,工作立即被杀死。由于RAM分配较低,预计会出现这种情况。
但是在第二次和第三次测试中,即使-m
参数是8倍的差异,python代码也会返回或多或少相同的结果:1741164544和1739603968(即1,741,164,544和1,739,603,968)
为什么会这样?
我的主机PC是一台Mac,它有16G内存。
答案 0 :(得分:2)
Docker使用 CGroups 来控制容器的资源使用情况,因此您应该检查CGroup文件:
$ docker run -m 800000000 -ti centos bash
[root@c7406a25bc4b /]# cat /sys/fs/cgroup/memory/memory.limit_in_bytes
799997952
psutil
可能会使用显示系统而非容器功能的/proc/meminfo
文件。
另请参阅:https://serverfault.com/questions/680963/lxc-container-shows-hosts-full-ram-amount-and-cpu-count