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

作者:李佶澳  更新时间:2018-12-26 10:56:40 +0800

  问题    docker    刷新

目录

说明

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

关注加微信,一般不闲聊(直接说事)

Copyright @2011-2019 All rights reserved. 转载请添加原文连接,合作请加微信lijiaocn或者发送邮件: [email protected],备注网站合作

友情链接:  微信公众号精选文章  发现知识星球