(限制容器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。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有