限制Docker容器的磁盘大小和带宽 [英] Limit disk size and bandwidth of a Docker container

查看:3427
本文介绍了限制Docker容器的磁盘大小和带宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台运行Ubuntu 14.04的物理主机。它具有100G磁盘和100M网络带宽。我安装了Docker并推出了10个容器。我想限制每个容器最多为10G磁盘和10M网络带宽。



在通过官方文档和在互联网上搜索后,我仍然不能找到一种方式来分配指定大小的磁盘和网络带宽到一个容器。



我认为这可能是不可能在Docker直接,也许我们需要绕过Docker。这是否意味着我们应该使用一些底层,如LXC或Cgroup?任何人都可以提出一些建议?






编辑:



Mbarthelemy,您的建议似乎有效,但我仍然对磁盘有一些疑问:



1)是否可以分配其他大小(如20G,30G等)容器?你说在Docker中是硬编码的,所以看起来是不可能的。



2)我使用下面的命令启动Docker守护程序和容器:

  docker -d -s devicemapper 
docker run -i -t training / webapp / bin / bash

然后我使用 df -h 来查看磁盘使用情况,它给出以下输出: / p>

 文件系统大小已使用可用性使用%安装在
/ dev / mapper / docker-longid 9.8G 276M 9.0G 3% /
/ dev / mapper / Chris - vg-root 27G 5.5G 20G 22%/ etc / hosts

从上面我认为容器可以使用的最大磁盘还是大于10G,你觉得

解决方案

我不认为现在可以使用Docker默认设置。关于磁盘使用情况:您可以告诉Docker使用DeviceMapper存储后端而不是AuFS。




  • 这样每个容器将运行在限制为10GB的块设备(Devicemapper dm-thin target)上(这是一个Docker默认值,幸运的是它符合您的要求!)。



    根据此链接,它看起来像Docker的最新版本现在可以接受高级存储后端选项。使用 devicemapper 后端,现在可以使用更改默认容器rootfs大小选项 - storage-opt dm.basesize = 20G (将应用于任何新创建的容器)



    要更改存储后端:使用 --storage-driver = devicemapper Docker选项。请注意,更改之后,Docker将不会再看到您以前的容器。


  • 关于网络带宽:您可以告诉Docker在引擎盖下使用LXC:使用 -e lxc 选项。



    然后,使用自定义LXC指令创建容器,使其成为流量类



    docker run --lxc-conf =lxc.cgroup.net_cls.classid = 0x00100001your / image / bin / stuff



    查看官方文档,了解如何为此类应用带宽限制。
    我从来没有尝试过这个(我的设置使用一个自定义的OpenVswitch桥接器和VLAN进行网络连接,因此带宽限制是不同的,而且容易一些),但是我想你必须创建和配置一个不同的类。 / p>




注意: - storage-driver = devicemapper -e lxc 选项适用于Docker 守护程序,而不是运行 docker运行时使用的Docker客户端。 ......


I have a physical host machine with Ubuntu 14.04 running on it. It has 100G disk and 100M network bandwidth. I installed Docker and launched 10 containers. I would like to limit each container to a maximum of 10G disk and 10M network bandwidth.

After going though the official documents and searching on the Internet, I still can't find a way to allocate specified size disk and network bandwidth to a container.

I think this may not be possible in Docker directly, maybe we need to bypass Docker. Does this means we should use something "underlying", such as LXC or Cgroup? Can anyone give some suggestions?


Edit:

@Mbarthelemy, your suggestion seems to work but I still have some questions about disk:

1) Is it possible to allocate other size (such as 20G, 30G etc) to each container? You said it is hardcoded in Docker so it seems impossible.

2) I use the command below to start the Docker daemon and container:

docker -d -s devicemapper
docker run -i -t training/webapp /bin/bash

then I use df -h to view the disk usage, it gives the following output:

Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/docker-longid   9.8G  276M  9.0G   3% /
/dev/mapper/Chris--vg-root   27G  5.5G   20G  22% /etc/hosts

from the above I think the maximum disk a container can use is still larger than 10G, what do you think ?

解决方案

I don't think this is possible right now using Docker default settings. Here's what I would try.

  • About disk usage: You could tell Docker to use the DeviceMapper storage backend instead of AuFS. This way each container would run on a block device (Devicemapper dm-thin target) limited to 10GB (this is a Docker default, luckily enough it matches your requirement!).

    According to this link, it looks like latest versions of Docker now accept advanced storage backend options. Using the devicemapperbackend, you can now change the default container rootfs size option using --storage-opt dm.basesize=20G (that would be applied to any newly created container).

    To change the storage backend: use the --storage-driver=devicemapper Docker option. Note that your previous containers won't be seen by Docker anymore after the change.

  • About network bandwidth : you could tell Docker to use LXC under the hoods : use the -e lxcoption.

    Then, create your containers with a custom LXC directive to put them into a traffic class :

    docker run --lxc-conf="lxc.cgroup.net_cls.classid = 0x00100001" your/image /bin/stuff

    Check the official documentation about how to apply bandwidth limits to this class. I've never tried this myself (my setup uses a custom OpenVswitch bridge and VLANs for networking, so bandwidth limitation is different and somewhat easier), but I think you'll have to create and configure a different class.

Note : the --storage-driver=devicemapperand -e lxcoptions are for the Docker daemon, not for the Docker client you're using when running docker run ........

这篇关于限制Docker容器的磁盘大小和带宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆