docker的storage-driver是overlay2时,限制单个容器可占用的磁盘空间

栏目: 编程工具 · 发布时间: 7年前

内容简介:overlay2.size是在这里使用的docker版本是18.03-ce:

说明

overlay2.size是在 17.07.0-ce 中引入的: Add overlay2.size daemon storage-opt

这里使用的 docker 版本是18.03-ce:

$ docker info
Containers: 2
 Running: 2
 Paused: 0
 Stopped: 0
Images: 2
Server Version: 18.03.1-ce
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
...

overlay2.size

docker daemon配置项 中介绍了 overlay2.size 配置项,可以用来限制每个容器可以占用的磁盘空间。

overlay2.size
Sets the default max size of the container. It is supported only when the backing fs is xfs and mounted with pquota mount option. Under these conditions the user can pass any size less then the backing fs size.
Example
$ sudo dockerd -s overlay2 –storage-opt overlay2.size=1G

如文档中所述,需要使用xfs文件系统,并且挂载时使用 pquota

开启xfs的quota特性

How to Enable Disk Quotas on an XFS File System 中介绍了如何开启xfs的quota功能。

xfs支持三种类型的quota: uquotagquotapquota ,在man xfs中可以看到:

uquota/usrquota/quota/uqnoenforce/qnoenforce
       User disk quota accounting enabled, and limits (optionally) enforced.  Refer to xfs_quota(8) for further details.

gquota/grpquota/gqnoenforce
       Group disk quota accounting enabled and limits (optionally) enforced.  Refer to xfs_quota(8) for further details.

pquota/prjquota/pqnoenforce
       Project disk quota accounting enabled and limits (optionally) enforced.  Refer to xfs_quota(8) for further details.

docker的overlay2需要的是 pquota ,在 /etc/fstab 中设置:

/dev/vdb /data xfs rw,pquota 0 0

/dev/vdb 卸载后重新挂载:

umount /dev/vdb
mount -a

可以在 /proc/mounts 中看到已经被挂载的目录和参数:

$ cat /proc/mounts  |grep vdb
/dev/vdb /data xfs rw,relatime,attr2,inode64,prjquota 0 0

配置docker daemon

/etc/docker/daemon.json 配置文件如下,这里将每个容器可以使用的磁盘空间设置为1G:

{
    "data-root": "/data/docker",
    "storage-driver": "overlay2",
    "storage-opts": [
      "overlay2.override_kernel_check=true",
      "overlay2.size=1G"
    ]
}

写入文件测试

重启docker后,启动一个容器,在容器中创建文件。

先创建一个1000M的文件:

/ # dd if=/dev/zero of=/a bs=100M count=10
10+0 records in
10+0 records out

然后创建第二个1000M的文件:

/ # dd if=/dev/zero of=/b bs=100M count=10
dd: writing '/b': No space left on device
2+0 records in
0+1 records out

可以看到第二个1000M文件因为空间不足创建失败,并且只写入了24M:

/ # ls -lh
total 1048572
-rw-r--r--    1 root     root     1000.0M Dec 26 03:38 a
-rw-r--r--    1 root     root       24.0M Dec 26 03:38 b

参考

  1. docker daemon配置项
  2. How to Enable Disk Quotas on an XFS File System

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C++编程风格

C++编程风格

卡吉尔 / 聂雪军 / 机械工业出版社发行室 / 2007-1 / 25.00元

本书描述C++语言中较深层次的程序设计思想和使用方法,包含大量软件工程概念和设计模式,重点介绍大规模编程相关的内容,例如增加代码的可读性、可维护性、可扩展性以及执行效率等的方法。本书的示例代码都是从实际程序中抽取出来的,融人了作者的实际开发经验。讲解如何正确地编写代码以及避开一些常见的误区和陷阱,并给出了许多实用的编程规则,可快速提升读者的C++编程功力。   本书描述平实,示例丰富,适合有......一起来看看 《C++编程风格》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

html转js在线工具
html转js在线工具

html转js在线工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具