docker容器的CPU资源限制

    xiaoxiao2022-07-08  196

    准备:

    -centos7 -docker平台 -stress命令(模拟CPU负载) -用到docker参数--cpu-period(周期),--cpu-quota(配额)单位是微秒;1秒=1000毫秒,1毫秒=1000微秒,1秒=1000000微秒

    过程:

    运行容器:(限制容器CPU占用比重为500 000/1 000 000=50%;1秒内最多使用0.5秒的CPU;最多不超过一个内核的50%)

    [root@server ~]# docker run -dit --name centos1 --cpu-period=1000000 --cpu-quota=500000 192.168.200.104:5000/centos/stress:latest bash 59c89af717d4606f4a9ec3843ad7fe4d72a81ab81bc299095eab07627aae3952

    压力测试(尽量超载):

    进入到容器中模拟负载,负载量为400%(1个内核=100%): [root@server ~]# docker exec -it centos1 bash [root@59c89af717d4 /]# stress -c 4 stress: info: [29] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd 同时查看该容器的CPU资源占用: [root@server ~]# docker stats centos1 CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS centos1 50.07% 968 KiB / 3.86 GiB 0.02% 648 B / 648 B 5.895 MB / 0 B 0

    CPU使用率为50.07%,说明限制生效了。

     

    测试:

    也可以设置上限为2个内核(200%)。

    [root@server ~]# docker run -dit --name centos1 --cpu-period=1000000 --cpu-quota=2000000 192.168.200.104:5000/centos/stress:latest bash 64428831a83319325d685c762e75306e5a45a75eeaa01979eeede758b5f68e65 root@server ~]# docexec -it centos1 bash [root@64428831a833 /]# stress -c 4 stress: info: [28] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd [root@server ~]# docker stats centos1 CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS centos1 201.48% 968 KiB / 3.86 GiB 0.02% 648 B / 648 B 5.886 MB / 0 B 0

    本机有四个内核,设置上限为2000%(20个内核):

    [root@server ~]# docker run -dit --name centos1 --cpu-period=1000000 --cpu-quota=20000000 192.168.200.104:5000/centos/stress:latest bash 4ac343f7b5e8a87118e10821b0a252013aeb08e9cbfed86cd2a691b699464d19 [root@server ~]# docker exec -it centos1 bash [root@4ac343f7b5e8 /]# stress -c 10 stress: info: [35] dispatching hogs: 10 cpu, 0 io, 0 vm, 0 hdd [root@server ~]# docker stats centos1 CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS centos1 417.47% 1.098 MiB / 3.86 GiB 0.03% 648 B / 648 B 5.878 MB / 0 B 0

    总结:

    容器的CPU配额是通过period和quota之间的大小比重来确定内核的数量,而且是以时间为单位。

    例如:1秒内可以使用0.5秒的CPU,等同于可以使用一个内核的50%;1秒内可以使用2秒的CPU,则是两个内核。

    限制内核数为quota/period=n。

    最新回复(0)