docker容器--cpu-shares选项

    xiaoxiao2022-07-08  206

    问题:

    创建两个容器为 centos1 和 centos2,若只有这两个容器,该怎么设置容器的权重,才能使得centos1和centos2的CPU资源占比为33.3%和66.7%。

    准备:

    -centos7系统 -docker -stress软件包(用来有效的消耗cpu资源,模拟高负载场景)

    安装stress:

    [root@node1 ~]# yum install -y epel-release [root@node1 ~]# yum -y install stress

    过程:

    -宿主机:cpu1 核心1 -使用--cpu-shares int参数 -权重341:682

    运行容器(同一时间内):

    centos1:

    [root@node1 ~]# docker run -dit --name centos1 --cpu-shares 341 centos/stress bash e2b4a2546c8ad6449db4197c69daaa37556ee12cee17e85dcc8e6419bf1a663a

    压力测试: 尽量过载

    [root@e2b4a2546c8a /]# stress -c 4 stress: info: [56] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd [root@node1 ~]# docker stats centos1 CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS centos1 32.35% 956 KiB / 1.782 GiB 0.05% 1.3 kB / 648 B 0 B / 0 B 0

    centos2:

    [root@node1 ~]# docker run -dit --name centos2 --cpu-shares 682 centos/stress bash 9b04b81e5dd438203bc7d6e7702b4d74fb7e18a4fd424f5813433f52bdc68061

    压力测试:

    [root@9b04b81e5dd4 /]# stress -c 4 stress: info: [31] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd [root@node1 ~]# docker stats centos2 CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS centos2 66.00% 952 KiB / 1.782 GiB 0.05% 648 B / 648 B 0 B / 0 B 0

    答案正确!

    如果宿主机有多个cpu或者多个核心:

    centos1:

    [root@server ~]# docker run -dit --name centos1 --cpu-shares 341 192.168.200.104:5000/centos/stress bash [root@server ~]# docker stats centos1 CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS centos1 133.20% 980 KiB / 3.86 GiB 0.02% 1.206 kB / 648 B 5.911 MB / 0 B 0

    centos2:

    [root@server ~]# docker run -dit --name centos2 --cpu-shares 682 192.168.200.104:5000/centos/stress bash [root@server ~]# docker stats centos2 CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS centos2 266.73% 968 KiB / 3.86 GiB 0.02% 648 B / 648 B 5.895 MB / 0 B 0

    总结:

    无论宿主机有多少个cpu或者内核,--cpu-shares选项都会按照比例分配cpu资源。 另外只有一个容器时--cpu-shares选项无意义。

    附cpu数量查询(一位老哥的文章):http://www.cnblogs.com/emanlee/p/3587571.html

    最新回复(0)