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

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

查看所有标签

猜你喜欢:

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

互联网黑洞

互联网黑洞

仲昭川 / 电子工业出版社 / 2014-4 / 50.00

万物之灵,存乎一心;互联网时代,上兵伐谋。 纵横古今商业奥秘,无非兴趣与利益、诱惑与满足、成本与利润、价格与价值。 本书着眼于大互联网时代,旨在通过对时下互联网圈子的冷静分析、传奇披露、实战揭秘,进而传授互联网哲学,阐述互联网现状,揭示互联网价值,尝试为互联网的未来探寻狭窄的通道。一起来看看 《互联网黑洞》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

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

HEX HSV 互换工具