Limit a container's resources

By default, a container has no resource contraints and can use as much of a given resource as the host's kernel scheduler allows.

Docker provides ways to control how much memory,CPU, or block IO a container can use,setting runtime configuration flags of the docker run command.

Many of these features require your kernel to support Linux capabilities.

  To check for support,you can use the docker info command.

Resource allowances

Eight--sided containers

Memory

OOME

  On Linux hosts,if the kernel detects that there is not enough memory to perform important system functions, it throws an OOME,or Out Of Memory Exception,and starts killing processes to free up memory

  一旦发生OOME,任何进程都有可能被杀死,包括docker daemon在内

  为此,Docker特地调整了docker daemon的OOM优选级,以免它被内核"正法",但容器的优选级并未被调整

Limit a container's access to memory

--memory-swap

Using swap allows the container to write excess memory requirements to disk when the container has exhausted all the RAM that is available to it.

--memory-swap is a modifier flag that only has meaning if --memory is also set.

CPU

By default,each container's access to the host machine's CPU cycles is unlimited.

You can set various constraints to limit a given container's access to the host machine's CPU cycles.

Most users use and configure the default CFS scheduler.

In Docker 1.13 and higher,you can also configure the realtime scheduler.

Configure the default CFS scheduler

 

容器的资源限制:

  CPU:

  RAM:

  Device:

    --device-read-bps value Limit read rate (bytes per second) from a device (default [])

    --device-read-iops value Limit read rate (IO per second) from a device (default [])

    --device-write-bps value Limit write rate (bytes per second) to a device (default [])

    --device-write-iops value Limit write rate (IO per second) to a device (default [])

 

node1:

[root@node1 ~]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    2
座:                 2
NUMA 节点:         1
厂商 ID:           GenuineIntel
CPU 系列:          6
型号:              58
型号名称:        Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
步进:              9
CPU MHz:             2494.370
BogoMIPS:            4988.74
超管理器厂商:  VMware
虚拟化类型:     完全
L1d 缓存:          32K
L1i 缓存:          32K
L2 缓存:           256K
L3 缓存:           3072K
NUMA 节点0 CPU:    0-3
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx
 rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc
_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm fsgsbase tsc_adjust smep arat

 在docker-hub:https://hub.docker.com搜索stress压力测试镜像,我的账号smoke584520,密码smoke@5201314

[root@node1 ~]# docker pull lorel/docker-stress-ng
[root@node1 ~]# docker run --name stress -it --rm lorel/docker-stress-ng stress --help
[root@node1 ~]# docker run --name stress -it --rm -m 256m lorel/docker-stress-ng stress --vm 2
[root@node1 ~]# docker top stress
UID      PID        PPID        C         STIME               TTY                 TIME                CMD
root     6441       6419        1         21:40               pts/0               00:00:00            /usr/bin/stress-ng stress --vm 2
root     6480       6441        0         21:40               pts/0               00:00:00            /usr/bin/stress-ng stress --vm 2
root     6481       6441        0         21:40               pts/0               00:00:00            /usr/bin/stress-ng stress --vm 2
root     6494       6480        88        21:40               pts/0               00:00:19            /usr/bin/stress-ng stress --vm 2
root     6521       6481        85        21:40               pts/0               00:00:02            /usr/bin/stress-ng stress --vm 2
[root@node1 ~]# docker stats
CONTAINER ID        NAME      CPU %        MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O               PIDS
a7f260e94297        stress    0.17%        255.9MiB / 256MiB   99.95%              1.3kB / 0B          1.66GB / 5.65GB         5
[root@node1 ~]# docker run --name stress -it --rm --cpus 2 lorel/docker-stress-ng stress --cpu 8
[root@node1 ~]# docker top stress
UID     PID        PPID         C         STIME               TTY                 TIME                CMD
root    6668       6650         0         21:45               pts/0               00:00:00            /usr/bin/stress-ng stress --cpu 8
root    6707       6668         24        21:45               pts/0               00:00:21            /usr/bin/stress-ng stress --cpu 8
root    6708       6668         24        21:45               pts/0               00:00:21            /usr/bin/stress-ng stress --cpu 8
root    6709       6668         26        21:45               pts/0               00:00:23            /usr/bin/stress-ng stress --cpu 8
root    6710       6668         24        21:45               pts/0               00:00:21            /usr/bin/stress-ng stress --cpu 8
root    6711       6668         25        21:45               pts/0               00:00:22            /usr/bin/stress-ng stress --cpu 8
root    6712       6668         24        21:45               pts/0               00:00:21            /usr/bin/stress-ng stress --cpu 8
root    6713       6668         23        21:45               pts/0               00:00:20            /usr/bin/stress-ng stress --cpu 8
root    6714       6668         24        21:45               pts/0               00:00:21            /usr/bin/stress-ng stress --cpu 8
[root@node1 ~]# docker stats
CONTAINER ID        NAME      CPU %        MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
af3a9cafa689        stress    226.86%      29.84MiB / 1.779GiB   1.64%               648B / 0B           0B / 0B             9
[root@node1 ~]# docker run --name stress -it --rm lorel/docker-stress-ng stress --cpu 8
[root@node1 ~]# docker stats                                     9
CONTAINER ID        NAME      CPU %        MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
01082ec0ca74        stress    393.39%      27.38MiB / 1.779GiB   1.50%               648B / 0B           0B / 0B             9
[root@node1 ~]# docker run --name stress -it --cpuset-cpus 0,2 --rm lorel/docker-stress-ng stress --cpu 8
[root@node1 ~]# docker stats
CONTAINER ID        NAME      CPU %        MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
a35607f59976        stress    185.47%      27.7MiB / 1.779GiB    1.52%               648B / 0B           0B / 0B             9
[root@node1 ~]# docker run --name stress -it --cpu-shares 1024 --rm lorel/docker-stress-ng stress --cpu 8
[root@node1 ~]# docker stats
CONTAINER ID        NAME      CPU %        MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
0e389ffa45e8        stress    429.66%      28.56MiB / 1.779GiB   1.57%               578B / 0B           0B / 0B             9
[root@node1 ~]# docker run --name stress2 -it --cpu-shares 512 --rm lorel/docker-stress-ng stress --cpu 8    #再启动一个容器
[root@node1 ~]# docker stats    #两个容器cpu占用率为2:1
CONTAINER ID        NAME      CPU %        MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
2abe695726a1        stress2   171.57%      29.19MiB / 1.779GiB   1.60%               648B / 0B           0B / 0B             9
0e389ffa45e8        stress    344.39%      28.56MiB / 1.779GiB   1.57%               648B / 0B           0B / 0B             9