内存
默认内存是只要kernel允许docker就能无限制的使用,当内存被耗尽就会出现oome,docker的进程oom是调整过的所以一般不会被杀掉,但是容器实例并未做调整
oome:out of menmory exception–>杀死掉oom_score分高的进程(hogs)
oom_adj:降低oom_adj—>oom_score分数降低—>系统杀掉分数高的进程,从而进程得到了保留
-m, --memory bytes 最小限制为4M 物理内存ram
--memory-reservation bytes 交换内存必须先设置ram
-m 正数m --memory-swap bytes 正数S ram=m swap=S-m
-m 正数m --memory-swap bytes 0 ram=m swap=unset 为2*m
-m 正数m --memory-swap bytes unset ram=m swap=unset 为2*m
-m 正数m --memory-swap bytes -1 ram=m swap=无限制
容器free看见的内存不太准确
--memory-reservation bytes 内存软限制
--memory-swappiness int swap内存倾向性(0-100)是个百分比数值0是能不用就不用100能用就用 一般物理服务器是40-60
--oom-kill-disable 不让容器被oom掉(true 或者 flase)与-m一块用
--oom-score-adj int 调整容器oom优先级 (-1000 to 1000)
验证测试内存限制
下载docker-stress-ng
# docker pull lorel/docker-stress-ng
Using default tag: latest
latest: Pulling from lorel/docker-stress-ng
c52e3ed763ff: Pull complete
a3ed95caeb02: Pull complete
7f831269c70e: Pull complete
Digest: sha256:c8776b750869e274b340f8e8eb9a7d8fb2472edd5b25ff5b7d55728bca681322
Status: Downloaded newer image for lorel/docker-stress-ng:latest
不限制内存cpu、内存跑容器
# docker run -it --name stress --rm \
lorel/docker-stress-ng --cpu 8 --vm 2 --vm-bytes 512M
不限制内存cpu虚拟器资源情况
# docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM %
a123dba2e35f stress 278.63% 560.8MiB / 3.683GiB 14.87%
限制物理内存为256M
# docker run -it --name stress --rm --memory 256M --memory-swap 512M --oom-kill-disable=true \
lorel/docker-stress-ng --cpu 8 --vm 2 --vm-bytes 512M
内存被限制使用到了256M
# docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM %
a8a093effbcf stress 0.00% 253.8MiB / 256MiB 99.16%
不会被oom:
# docker inspect stress -f {{.State.OOMKilled}}
false
限制物理内存为256M
# docker inspect stress -f {{.HostConfig.Memory}}
268435456
限制ram+swap为512
# docker inspect stress -f {{.HostConfig.MemorySwap}}
536870912
内存倾向性:
# docker inspect stress -f {{.HostConfig.MemorySwappiness}}
<nil>
cpu限制
默认情况,每个容器都可以使用系统的所有CPU资源。
你能使用多种参数来限制容器访问物理机资源,大多数的时候我们可以通过使用CFS调度器来分配容器使用的CPU资源。
在docker1.13版本以后你也可以调整实时优先级
实时优先级: 0-99一般为系统kernel进程
非实时优先级:100-139 默认为120即nice值为0(-20,19)数值越小优先级越高
CFS(完全公平调度算法):一般用来调度非实时优先级,完全公平的调度每一个进程,内核会有一种算法随时计算,对一些已经占了很长时间的进程,动态调低优先级,对cpu饥饿的进行调高优先级
交互式的优先级比较高,使用cpu时间比较短,比如敲键盘、点鼠标,批处理比较耗cpu,优先级比较低。
cpu密集型:尽可能调低
io密集型
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in microseconds
--cpu-shares int 按比例切分cpu资源,cpu资源空闲的时候一个容器,可以使用到100%,当别人都要使用的时候,按比率分配cpu资源。
--cpus decimal 限制container使用几个核心的cpu --cpu="1.5"
--cpuset-cpus string 这个容器允许在那颗cpu核心上0-x
--cpu-period int 指定CPU CFS的调度周期,默认是100ms
--cpu-quota int 限制CPU CFS的配额。
四核cpu
# 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
Socket(s): 2
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 62
Model name: Intel(R) Xeon(R) CPU E5-2660 v2 @ 2.20GHz
Stepping: 4
CPU MHz: 2195.039
BogoMIPS: 4390.07
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 25600K
NUMA node0 CPU(s): 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 eagerfpu 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
限制cpu例子:
# docker run -it --name tinyweb -P -v /root/nginx/www/:/data/web/html \
--memory 100M --memory-swap 200M --memory-swappiness 0 --oom-kill-disable=true \
--cpus 2 --cpuset-cpus 0,3 \
jiazi:v1
不限制内存cpu、内存跑容器
# docker run -it --name stress --rm \
lorel/docker-stress-ng --cpu 8 --vm 2 --vm-bytes 512M
不限制内存cpu虚拟器资源情况
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM %
87c96007394e stress 279.02% 1.016GiB / 3.683GiB 27.58%
限制使用cpus0,3
# docker run -it --name stress --rm \
--cpuset-cpus 0,3 \
lorel/docker-stress-ng --cpu 8 --vm 2 --vm-bytes 512M
资源情况:
# docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM %
993309ace734 stress 183.75% 1.141GiB / 3.683GiB 30.97%
切分资源1:1024:2048
# docker run -it --name stress2048 --rm \
--cpuset-cpus 0,3 --cpu-shares 2048 \
lorel/docker-stress-ng --cpu 8 --vm 2 --vm-bytes 512M
# docker run -it --name stress2014 --rm \
--cpuset-cpus 0,3 --cpu-shares 1024 \
lorel/docker-stress-ng --cpu 8 --vm 2 --vm-bytes 512M
资源情况:
总共使用了2核心,两个容器按比率分配cpu资源
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM %
bfbf585c1d7d stress2014 59.05% 1.021GiB / 3.683GiB 27.72%
e7b428f491f2 stress2048 124.33% 905.9MiB / 3.683GiB 24.02%