Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting absolute limits on CPU for Docker containers

I'm trying to set absolute limits on Docker container CPU usage. The CPU shares concept (docker run -c <shares>) is relative, but I would like to say something like "let this container use at most 20ms of CPU time every 100ms. The closest answer I can find is a hint from the mailing list on using cpu.cfs_quota_us and cpu.cfs_period_us. How does one use these settings when using docker run?

I don't have a strict requirement for either LXC-backed Docker (e.g. pre0.9) or later versions, just need to see an example of these settings being used--any links to relevant documentation or helpful blogs are very welcome too. I am currently using Ubuntu 12.04, and under /sys/fs/cgroup/cpu/docker I see these options:

$ 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
like image 464
Hamy Avatar asked Oct 13 '14 21:10

Hamy


People also ask

What is the default CPU limit set for a container?

However, the same container's CPU limit is set to 1 cpu , which is the default CPU limit for that namespace.

What is CPU quota docker?

0 of Docker onward. The --cpu-quota option specifies the number of microseconds that a container has access to CPU resources during a period specified by --cpu-period. As the default value of --cpu-period is 100000, setting the value of --cpu-quota to 25000 limits a container to 25% of the CPU resources.

Can docker container use all CPU?

By default, all containers running on the same host can use the host CPU resources equally and without any restrictions. However, Docker can pass -c or --cpu-shares to set the weight of the CPU used by the container. If not specified, the default value is 1024.


1 Answers

I believe I've gotten this working. I had to restart my Docker daemon with --exec-driver=lxc as I could not find a way to pass cgroup arguments to libcontainer. This approach worked for me:

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

The necessary CFS docs on bandwidth limiting are here.

I briefly confirmed with sysbench that this does seem to introduce an absolute limit, as shown below:

$ 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
like image 159
Hamy Avatar answered Sep 21 '22 03:09

Hamy