zoukankan      html  css  js  c++  java
  • 容器(四)限制容器对CPU的使用【23】

    (七)限制容器对CPU的使用

    ​ 默认设置下,所有容器可以平等地使用 host CPU 资源并且没有限制。Docker 可以通过 -c--cpu-shares 设置容器使用 CPU 的权重。如果不指定,默认值为 1024。

    ​ 与内存限额不同,通过 -c 设置的 cpu share 并不是 CPU 资源的绝对数量,而是一个相对的权重值。某个容器最终能分配到的 CPU 资源取决于它的 cpu share 占所有容器 cpu share 总和的比例。换句话说:通过 cpu share 可以设置容器使用 CPU 的优先级。

    ​ 比如在 host 中启动了两个容器:

    docker run --name "container_A" -c 1024 ubuntu
    docker run --name "container_B" -c 512 ubuntu
    
    

    ​ container_A 的 cpu share 1024,是 container_B 的两倍。当两个容器都需要 CPU 资源时,container_A 可以得到的 CPU 是 container_B 的两倍。

    ​ 需要特别注意的是,这种按权重分配 CPU 只会发生在 CPU 资源紧张的情况下。如果 container_A 处于空闲状态,这时,为了充分利用 CPU 资源,container_B 也可以分配到全部可用的 CPU。

    下面我们继续用 progrium/stress 做实验。

    ①启动 container_b,cpu share 为 1024:

    root@cuiyongchao:~# docker run --name container_A -it -c 1024 progrium/stress:latest --cpu 1
    stress: info: [1] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
    stress: dbug: [1] using backoff sleep of 3000us
    stress: dbug: [1] --> hogcpu worker 1 [6] forked
    
    

    --cpu 用来设置工作线程的数量。因为当前 host 只有 1 颗 CPU,所以一个工作线程就能将 CPU 压满。如果 host 有多颗 CPU,则需要相应增加 --cpu 的数量。

    ②启动container_c,cpu share 为512:

    root@cuiyongchao:~# docker run --name container_B -it -c 512 progrium/stress:latest --cpu 1
    stress: info: [1] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
    stress: dbug: [1] using backoff sleep of 3000us
    stress: dbug: [1] --> hogcpu worker 1 [6] forked
    
    

    ③在 host 中执行 top,查看容器对 CPU 的使用情况

    root@cuiyongchao:~# top
    top - 12:21:37 up 8 days, 23:40,  8 users,  load average: 1.91, 0.98, 0.41
    Tasks: 217 total,   3 running, 125 sleeping,   0 stopped,   0 zombie
    %Cpu(s): 50.1 us,  0.1 sy,  0.0 ni, 49.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    KiB Mem :  4015852 total,  1015488 free,   462096 used,  2538268 buff/cache
    KiB Swap:  4015100 total,  4014024 free,     1076 used.  3278544 avail Mem 
    
       PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                     
    
    

    ④现在暂停 container_A:

    root@cuiyongchao:~# docker pause container_A 
    container_A
    
    

    top 显示 container_B 在 container_A 空闲的情况下能够用满整颗 CPU:

    root@cuiyongchao:~# top
    top - 12:28:47 up 8 days, 23:47,  8 users,  load average: 4.89, 3.49, 1.69
    Tasks: 221 total,   5 running, 123 sleeping,   0 stopped,   0 zombie
    %Cpu(s): 99.9 us,  0.1 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    KiB Mem :  4015852 total,  1022156 free,   456852 used,  2536844 buff/cache
    KiB Swap:  4015100 total,  4014024 free,     1076 used.  3286020 avail Mem 
    
       PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                       
    
    
  • 相关阅读:
    23种设计模式(12):策略模式
    23种设计模式(11):责任链模式
    23种设计模式(10):命令模式
    23种设计模式(9):访问者模式
    23种设计模式(8):观察者模式
    23种设计模式(7):中介者模式
    23种设计模式(6):模版方法模式
    创建型模式总结
    23种设计模式(5):原型模式
    leetcode6
  • 原文地址:https://www.cnblogs.com/cuiyongchao007/p/13970693.html
Copyright © 2011-2022 走看看