微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

docker 限制cpu使用率

限制cpu使用率为50%: --cpu-period=1000000 --cpu-quota=500000

详细过程如下

准备:

-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 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 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。

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐