• 第 4 章 容器


    限制容器对CPU的使用

    默认设置下,所有容器可以平等地使用 host CPU 资源并且没有限制。

    • Docker 可以通过 -c 或 --cpu-shares 设置容器使用 CPU 的权重。如果不指定,默认值为 1024。
    • 与内存限额不同,通过 -c 设置的 cpu share 并不是 CPU 资源的绝对数量,而是一个相对的权重值。
    • 某个容器最终能分配到的 CPU 资源取决于它的 cpu share 占所有容器 cpu share 总和的比例。

    通过 cpu share 可以设置容器使用 CPU 的优先级

    在 host 中启动了两个容器:

    1 docker run --name "container_A" -c 1024 ubuntu
    2 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_A,cpu share 为 1024: 

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

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

    启动 container_B,cpu share 为 512: 

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

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

     1 root@ubuntu:~# top
     2 top - 07:01:37 up 25 days, 20:05,  4 users,  load average: 3.76, 1.94, 0.81
     3 Tasks: 127 total,   5 running, 122 sleeping,   0 stopped,   0 zombie
     4 %Cpu(s):100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
     5 KiB Mem :  4046396 total,  2387620 free,   201112 used,  1457664 buff/cache
     6 KiB Swap:  1003516 total,  1003440 free,       76 used.  3542828 avail Mem 
     7 
     8   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                
     9 23316 root      20   0    7316    100      0 R  66.8  0.0   3:22.19 stress    #container_A10 23317 root      20   0    7316    100      0 R  66.4  0.0   3:22.23 stress                                                                                                 
    11 23423 root      20   0    7316     96      0 R  33.2  0.0   0:44.54 stress    #container_B                                                                                             
    12 23424 root      20   0    7316     96      0 R  32.9  0.0   0:44.49 stress                                                                                                 
    13     1 root      20   0   37904   5920   3972 S   0.0  0.1   0:13.31 systemd                                                                                                
    14     2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd                                                                                               
    15     3 root      20   0       0      0      0 S   0.0  0.0   0:00.16 ksoftirqd/0                                                                                            
    16     5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H

    container_A 消耗的 CPU 是 container_B 的两倍。

    现在暂停 container_A: 

    1 root@ubuntu:~# docker pause container_A
    2 container_A
    3 root@ubuntu:~# 

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

     1 root@ubuntu:~# top
     2 top - 07:06:06 up 25 days, 20:09,  4 users,  load average: 2.86, 2.86, 1.52
     3 Tasks: 127 total,   3 running, 124 sleeping,   0 stopped,   0 zombie
     4 %Cpu(s): 99.8 us,  0.2 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
     5 KiB Mem :  4046396 total,  2386864 free,   201812 used,  1457720 buff/cache
     6 KiB Swap:  1003516 total,  1003440 free,       76 used.  3542100 avail Mem 
     7 
     8   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                
     9 23423 root      20   0    7316     96      0 R 100.0  0.0   2:46.35 stress                                                                                                 
    10 23424 root      20   0    7316     96      0 R 100.0  0.0   2:46.33 stress                                                                                                 
    11     1 root      20   0   37904   5920   3972 S   0.0  0.1   0:13.31 systemd                                                                                                
    12     2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd    

    ------------------------引用来自---------------------------

    https://mp.weixin.qq.com/s?__biz=MzIwMTM5MjUwMg==&mid=2653587664&idx=1&sn=3f3185b9e5d55c8d0b9859340eecd860&chksm=8d3080c9ba4709df0b0bd72cb4fc8e9bf47bf2086823f44c0a6a758adc6cc7a0fafc399c911a&scene=21#wechat_redirect

  • 相关阅读:
    jQuery_03之事件、动画、类数组操作
    jQuery_02之元素操作及事件绑定
    jQuery_01之选择器
    DOM_06之定时器、事件、cookie
    DOM_05之DOM、BOM常用对象
    DOM_04之常用对象及BOM
    DOM_03之元素及常用对象
    DOM_02之查找及元素操作
    了解bootstrap导航条
    学习Angularjs-day1-总结
  • 原文地址:https://www.cnblogs.com/gsophy/p/10335614.html
Copyright © 2020-2023  润新知