容器的资源限制

一、memory限制

我们在创建虚拟机时,会根据物理机的硬盘、CPU等资源对虚拟机的资源进行限制,同样的容器也可以对其进行资源限制,我们先看看docke runr中的命令:

[root@localhost vagrant]# docker run --help

其中,有关于对memory做限制的:

-m, --memory bytes                   Memory limit
      --memory-reservation bytes       Memory soft limit
      --memory-swap bytes              Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --memory-swappiness int          Tune container memory swappiness (0 to 100) (default -1)
[root@localhost vagrant]# docker run --memory=300M shenjianping0307/ubuntu-stress --vm 1 --verbose #容器限制memory的大小为300M

二、cpu限制

对CPU做限制的:

-c, --cpu-shares int                 CPU shares (relative weight)  #CPU权重
      --cpus decimal                   Number of CPUs   #指定CPU个数

开启三个终端,一个通过top用于查看cpu占用情况,另外两个开启测试:

第一个终端测试:

[vagrant@localhost ~]$ docker run --cpu-shares=10 --name=test1 shenjianping0307/ubuntu-stress --cpu 1
stress: info: [1] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd

可以看看cpu占用情况:

 可以看到占用一个cpu的占用率基本是100%,然后我们在另一个终端再开启一个测试,此时--cpu-shares=5:

[vagrant@localhost ~]$  docker run --cpu-shares=5 --name=tes2 shenjianping0307/ubuntu-stress --cpu 1

再看看cpu占用情况:

可以看到则两个测试根据--cpu-shares的值来分配这一个cpu的占用比率。

原文地址:https://www.cnblogs.com/shenjianping/p/12242461.html