为Docker容器设置CPU的绝对限制

时间:2014-10-13 21:02:38

标签: docker cpu-usage scheduler lxc cgroups

我试图设置Docker容器CPU使用率的绝对限制。 CPU共享概念(docker run -c <shares>)是相对的,但我想说的是&#34;让这个容器每100毫秒使用最多20毫秒的CPU时间。我能找到的最接近的答案是hint from the mailing list使用cpu.cfs_quota_uscpu.cfs_period_us。使用docker run时如何使用这些设置?

我对LXC支持的Docker(例如pre0.9)或更高版本没有严格的要求,只需要查看正在使用的这些设置的示例 - 任何指向相关文档或有用博客的链接也非常欢迎。我目前正在使用Ubuntu 12.04,在/sys/fs/cgroup/cpu/docker下我看到了以下选项:

$ ls /sys/fs/cgroup/cpu/docker
cgroup.clone_children  cpu.cfs_quota_us   cpu.stat
cgroup.event_control   cpu.rt_period_us   notify_on_release
cgroup.procs           cpu.rt_runtime_us  tasks
cpu.cfs_period_us      cpu.shares

1 个答案:

答案 0 :(得分:5)

我相信我已经做到了这一点。我必须用--exec-driver=lxc重启我的Docker守护进程 找不到将cgroup参数传递给libcontainer的方法。这种方法对我有用:

# Run with absolute limit
sudo docker run --lxc-conf="lxc.cgroup.cpu.cfs_quota_us=50000" -it ubuntu bash

有关带宽限制的必要CFS文档为here

我简单地用sysbench确认这似乎引入了一个绝对限制,如下所示:

$ sudo docker run --lxc-conf="lxc.cgroup.cpu.cfs_quota_us=10000" --lxc-conf="lxc.cgroup.cpu.cfs_period_us=50000" -it ubuntu bash
root@302e651c0686:/# sysbench --test=cpu --num-threads=1 run
   <snip> 
   total time:                          90.5450s
$ sudo docker run --lxc-conf="lxc.cgroup.cpu.cfs_quota_us=20000" --lxc-conf="lxc.cgroup.cpu.cfs_period_us=50000" -it ubuntu bash
root@302e651c0686:/# sysbench --test=cpu --num-threads=1 run
   <snip> 
    total time:                          45.0423s
相关问题