docker学习笔记2 -- 资源限制

Posted SailorXiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker学习笔记2 -- 资源限制相关的知识,希望对你有一定的参考价值。

压测工具stress:

测试CPU

宿主机:CPU 3CORE

场景一:不做限制

  1. 宿主机启动镜像,不指定绑定到哪个核

    docker run --rm -ti -v /tmp/stress/:/tmp centos7.2 /bin/bash
    

      

  2. 进入宿主机执行stress:

    /tmp/stress -c 3
    

      

3.观察宿主机CPU使用情况:

top:
    top - 09:43:48 up 34 days, 28 min,  2 users,  load average: 2.74, 1.89, 1.02
    Tasks: 208 total,   5 running, 203 sleeping,   0 stopped,   0 zombie
    %Cpu0  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    %Cpu1  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    %Cpu2  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    %Cpu3  :  2.0 us,  1.0 sy,  0.0 ni, 96.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.3 st 

  

观察到4个core中的3个core已经被占满了

 

场景二:测试--cpuset-cpus

  1. 宿主机启动镜像,通过--cpuset-cpus指定绑定到CPU1

    docker run --rm -ti -v /tmp/stress/:/tmp --cpuset-cpus=1 centos7.2 /bin/bash
    

      

  2. 进入宿主机执行stress:

    ./tmp/stress -c 3
    

      

3.观察宿主机CPU使用情况:

top - 09:45:04 up 34 days, 29 min,  2 users,  load average: 2.45, 2.02, 1.14
Tasks: 207 total,   4 running, 203 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.3 us,  0.3 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.3 st
%Cpu1  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :  0.7 us,  0.3 sy,  0.0 ni, 98.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.3 st
%Cpu3  :  0.7 us,  0.3 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

  

观察到只有core1被占满,其余CPU都出空闲状态

 

场景三:测试--cpu-shares

  1. 宿主机启动容器test1,通过--cpuset-cpus指定绑定到CPU1,--cpu-shares设置权重为1024

    docker run --rm -ti -v /tmp/stress/:/tmp --cpuset-cpus=1 --cpu-shares 1024 --name test1 centos7.2 /bin/bash
    
    /tmp/stress -c 1  # 启动后执行
    

      

  2. 宿主机启动容器test2,通过--cpuset-cpus指定绑定到CPU1,--cpu-shares设置权重为512

    docker run --rm -ti -v /tmp/stress/:/tmp --cpuset-cpus=1 --cpu-shares 512 --name test2 centos7.2 /bin/bash
    
    /tmp/stress -c 1  # 启动后执行
    

      

  3. 宿主机top查看CPU使用率

    13745 root      20   0    7256     92      0 R  66.7  0.0   0:40.38 stress              
    14117 root      20   0    7256     92      0 R  33.3  0.0   0:04.01 stress 
    

      

发现其中一个进程cpu占了三分之一,另一个占用三分之二

 

测试内存

宿主机内存16G

 

场景一 测试-m --memory

  1. 宿主机启动test1,通过-m绑定使用内存为100MB
    
    docker run --rm -ti -v /tmp/stress/:/tmp --memory 100m --name test1 centos7.2 /bin/bash
    docker容器使用stress测试内存
    
    [[email protected] /]# /tmp/stress --vm 1 --vm-bytes 90M --vm-hang 0  
    stress: info: [18] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
    ^C
    [[email protected] /]# /tmp/stress --vm 1 --vm-bytes 99M --vm-hang 0  
    stress: info: [20] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
    ^C
    [[email protected] /]# /tmp/stress --vm 1 --vm-bytes 100M --vm-hang 0  
    stress: info: [22] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
    stress: FAIL: [22] (415) <-- worker 23 got signal 9
    stress: WARN: [22] (417) now reaping child worker processes
    stress: FAIL: [22] (451) failed run completed in 0s
    
    [[email protected] /]# /tmp/stress --vm 1 --vm-bytes 110M --vm-hang 0 
    stress: info: [24] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
    stress: FAIL: [24] (415) <-- worker 25 got signal 9
    stress: WARN: [24] (417) now reaping child worker processes
    stress: FAIL: [24] (451) failed run completed in 0s

发现当容器内使用内存超过-m内存限制时直接被kill

 

场景二:测试--momory 和 --memory-swap

  1. 宿主机启动test1,通过-m绑定使用内存为100MB

    docker run --rm -ti -v /tmp/stress/:/tmp --memory 100m --memory-swap 150m --name test1 centos7.2 /bin/bash
    

      

  2. docker容器使用stress测试内存

    [[email protected] /]# /tmp/stress --vm 1 --vm-bytes 90M --vm-hang 0  
    stress: info: [18] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
    ^C
    [[email protected] /]# /tmp/stress --vm 1 --vm-bytes 99M --vm-hang 0  
    stress: info: [20] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
    ^C
    [[email protected] /]# /tmp/stress --vm 1 --vm-bytes 100M --vm-hang 0  
    stress: info: [22] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
    stress: FAIL: [22] (415) <-- worker 23 got signal 9
    stress: WARN: [22] (417) now reaping child worker processes
    stress: FAIL: [22] (451) failed run completed in 0s
    
    [[email protected] /]# /tmp/stress --vm 1 --vm-bytes 110M --vm-hang 0 
    stress: info: [24] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
    stress: FAIL: [24] (415) <-- worker 25 got signal 9
    stress: WARN: [24] (417) now reaping child worker processes
    stress: FAIL: [24] (451) failed run completed in 0s
    

      

以上是关于docker学习笔记2 -- 资源限制的主要内容,如果未能解决你的问题,请参考以下文章

Docker学习笔记Docker容器相关技术

白话 Linux 容器资源的隔离限制原理

Docker学习笔记

OS学习笔记四:同步机制

企业运维实战--k8s学习笔记11.k8s容器资源限制

docker学习笔记4-dockerfile