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

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

查看所有标签

猜你喜欢:

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

ACM国际大学生程序设计竞赛题解

ACM国际大学生程序设计竞赛题解

赵端阳//袁鹤 / 电子工业 / 2010-6 / 38.00元

《ACM国际大学生程序设计竞赛题解(1)》可以作为高等院校有关专业的本科和大专学生参加国际大学生程序设计竞赛的辅导教材,或者作为高等院校数据结构、C/C++程序设计或算法设计与分析等相关课程的教学参考书。随着各大专院校参加ACM/ICPC热情的高涨,迫切需要有关介绍ACM国际大学生程序设计竞赛题解的书籍。《ACM国际大学生程序设计竞赛题解(1)》根据浙江大学在线题库的前80题,进行了解答(个别特别......一起来看看 《ACM国际大学生程序设计竞赛题解》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

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

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换