docker 的 storage-driver 是 overlay2 时,限制单个容器可占用的磁盘空间 overlay2.size 设置
1. overlay2.size
overlay2.size 是在 17.07.0-ce 中引入的: Add overlay2.size daemon storage-opt。可以用来限制每个容器可以占用的磁盘空间。
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】。
否则可能出现下面的【错误提示】:
Mar 18 16:08:08 k8s-master dockerd[1260]: failed to start daemon: error initializing graphdriver: Storage option overlay2.size not supported. Filesystem does not support Project Quota: Filesystem does not support, or has not enabled quotas
2. 开启 xfs 的 quota 特性
How to Enable Disk Quotas on an XFS File System 中介绍了如何开启xfs的quota功能。
xfs 支持三种类型的 quota:uquota、gquota 和 pquota ( prjquota ),在 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.
情景1: 非 / 开启
# 查看你的设备 [root@kube-master ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/vdb 99G 2.0G 97G 2% / /dev/vda1 1014M 197M 818M 20% /boot # 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
情景2: / 开启
我认为您需要在 /etc/grub2.cfg 其中添加内核选项 rootflags=pquota
例如:
linux16 /vmlinuz-4.13.4-200.fc26.x86_64 root=/dev/mapper/fedora-root ro rd.lvm.lv=fedora/root rd.lvm.lv=fedora/swap rhgb quiet.UTF-8 rootflags=pquota
3. 配置docker daemon
/etc/docker/daemon.json配置文件如下,这里将每个容器可以使用的磁盘空间设置为1G:
{ "data-root": "/data/docker", "storage-driver": "overlay2", "storage-opts": [ "overlay2.override_kernel_check=true", "overlay2.size=1G" ] }
4. 写入文件测试
重启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
参考:
How to Enable Disk Quotas on an XFS File System
Adding pquota to /etc/fstab for rootfs requires adding dracut `rootflags` argument
docker的storage-driver是overlay2时,限制单个容器可占用的磁盘空间
共 0 条评论