加载中

As I was working on my upcoming presentation at ApacheCon, I was playing a little bit with cgroups inside Docker.

What I found was that there aren’t a whole lot of documentation on this, so I figured I put together a quick blog post about it.

Enable the LXC driver

Assuming you already have Docker installed on Ubuntu 14.04, you will still need to enable the LXC driver.

To do this, you will need to do the following

$ apt-get install -y lxc
$ echo 'DOCKER_OPTS="--exec-driver=lxc"' \    | tee -a /etc/default/docker
$ service docker restart`

Spin up two containers without cgroup policy

Let’s start by launching two containers that each will max out the CPU (by running md5sum /dev/urandom).

$ docker run -d busybox md5sum /dev/urandom
$ docker run -d busybox md5sum /dev/urandom

Docker containers running without cgroup policy

As expected, we can see that these containers fully utilize one core each.

作为我在ApacheCon工作的一份呈现,我正在Docker内部使用一些cgroups 。

我发现这方面的文档很少,所以我想把它们合到一块发一篇关于它的博客。

启用LXC驱动

假设你已经在Ubuntu 14.04上安装了Docker,你仍旧需要启用LXC驱动。

要开启该项,你需要这样做

$ apt-get install -y lxc
$ echo 'DOCKER_OPTS="--exec-driver=lxc"' \    | tee -a /etc/default/docker
$ service docker restart`

启用两个不没有cgroup策略的容器

让我们先加载两个容器,两个的CPU都要最大化(通过运行md5sum /dev/urandom)

$ docker run -d busybox md5sum /dev/urandom
$ docker run -d busybox md5sum /dev/urandom

Docker containers running without cgroup policy

正如预期的,我们可以看到这些容器完全使用了每一个CPU内核。

Spin up two containers without cgroup policy

Now let’s put the new LXC options to use by adding two cgroup policies. What we’re looking to do is to the same workload as before, but run them on the same core. We would then expect them to occupy 50% of the core each. However, we want to give one container 80% of the CPU share, and the other only 20%. To accomplish this, we use ‘cpu.shares’ to divvy up the CPU and ‘cpuset.cpus’ to lock the containers to the same core.

Start container with low priority:

$ docker run -d --name='low_prio' \    --lxc-conf="lxc.cgroup.cpu.shares=250" \ 
    --lxc-conf="lxc.cgroup.cpuset.cpus=0" \
    busybox md5sum /dev/urandom

Start container with high priority:

$ docker run -d --name='high_prio' \    --lxc-conf="lxc.cgroup.cpu.shares=750" \ 
    --lxc-conf="lxc.cgroup.cpuset.cpus=0" \
    busybox md5sum /dev/urandom

Docker containers running with cgroup policy

As you can see, it worked! Happy hacking!

启用两个没有cgroup策略的容器

现在让我们通过添加两条cgroup策略启用新的LXC选项。我们想要做的是运行相同的工作负荷且运行在相同CPU内核。我们期望它会占有CPU50%。而且,我们想给其中一个容器80%的CPU共享,其他一个共享20%。要做到这一点,我们使用‘cpu.shares’来分配CPU并使用‘cpuset.cpus’锁定容器到同一个CPU内核。

用低优先级启动容器:

$ docker run -d --name='low_prio' \    --lxc-conf="lxc.cgroup.cpu.shares=250" \ 
--lxc-conf="lxc.cgroup.cpuset.cpus=0" \
busybox md5sum /dev/urandom

用高优先级启动容器:

$ docker run -d --name='high_prio' \    --lxc-conf="lxc.cgroup.cpu.shares=750" \ 
--lxc-conf="lxc.cgroup.cpuset.cpus=0" \
busybox md5sum /dev/urandom

Docker containers running with cgroup policy

正如你所看到的,它奏效了!折腾快乐!

返回顶部
顶部