首页 新闻 会员 周边 捐助

docker swarm中如何限制每个容器最多只能使用1核CPU

0
悬赏园豆:30 [已解决问题] 解决于 2018-04-01 22:29

请问如何在docker swarm的stack compose yml 配置文件中限制每个容器最多只能使用1核CPU?

dudu的主页 dudu | 高人七级 | 园豆:30778
提问于:2018-04-01 14:29
< >
分享
最佳答案
0

使用 --cpus=<value> 命令,在 docker run的时候进行分配,
If you have 1 CPU, each of the following commands guarantees the container at most 50% of the CPU every second.

Docker 1.13 and higher:
docker run -it --cpus=".5" ubuntu /bin/bash
Docker 1.12 and lower:

$ docker run -it --cpu-period=100000 --cpu-quota=50000 ubuntu /bin/bash

更多的参数可以参考:
Limit a container's resources

收获园豆:30
BUTTERAPPLE | 老鸟四级 |园豆:3190 | 2018-04-01 20:21

这是命令行参数,yml中怎么写?

dudu | 园豆:30778 (高人七级) | 2018-04-01 21:23

园子里也有一篇相关博文:Docker: 限制容器可用的 CPU

dudu | 园豆:30778 (高人七级) | 2018-04-01 21:24

@dudu:
In this general example, the redis service is constrained to use no more than 50M of memory and 0.50 (50%) of available processing time (CPU), and has 20M of memory and 0.25 CPU time reserved (as always available to it).

version: '3'
services:
  redis:
    image: redis:alpine
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 50M
        reservations:
          cpus: '0.25'
          memory: 20M

Reference: Compose file version 3 reference

BUTTERAPPLE | 园豆:3190 (老鸟四级) | 2018-04-01 22:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册