清洁 docker 环境:devicemapper [英] Clean docker environment: devicemapper
问题描述
我有一个带有 2 个容器的 docker 环境(Jenkins 和 Nexus,都有自己的命名卷).我有一个每日定时任务,它会删除未使用的容器和图像.这工作正常.但问题出在我的设备映射器中:
I have a docker environment with 2 containers (Jenkins and Nexus, both with their own named volume). I have a daily cron-job which deletes unused containers and images. This is working fine. But the problem is inside my devicemapper:
du -sh /var/lib/docker/
30G docker/
我可以访问我的 docker 文件夹中的每个文件夹:卷(很大,但在我的情况下这是正常的):
I can each folder in my docker folder: Volumes (big, but that's normal in my case):
/var/lib/docker# du -sh volumes/
14G volumes/
容器:
/var/lib/docker# du -sh containers/
3.2M containers/
图像:
/var/lib/docker# du -sh image/
5.8M image/
设备映射器:
/var/lib/docker# du -sh devicemapper/
16G devicemapper/
/var/lib/docker/devicemapper/mnt
是 7.3G/var/lib/docker/devicemapper/devicemapper
是 8.1G
/var/lib/docker/devicemapper/mnt
is 7.3G
/var/lib/docker/devicemapper/devicemapper
is 8.1G
码头工人信息:
Storage Driver: devicemapper
Pool Name: docker-202:1-xxx-pool
Pool Blocksize: 65.54 kB
Base Device Size: 10.74 GB
Backing Filesystem: ext4
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 5.377 GB
Data Space Total: 107.4 GB
Data Space Available: 28.8 GB
Metadata Space Used: 6.148 MB
Metadata Space Total: 2.147 GB
Metadata Space Available: 2.141 GB
Udev Sync Supported: true
这个空间是什么,我能在不破坏东西的情况下清理它吗?
What is this space and am I able to clean this without breaking stuff?
推荐答案
不要将 devicemapper 循环文件用于任何严重的事情!Docker 对此发出了重大警告.
Don't use a devicemapper loop file for anything serious! Docker has big warnings about this.
/var/lib/docker/devicemapper/devicemapper
目录包含稀疏循环文件,其中包含 docker 挂载的所有数据.所以你需要使用 lvm 工具来搜索它们并做一些事情.阅读删除设备映射器的问题,它们有点解决,但可能没有.
The /var/lib/docker/devicemapper/devicemapper
directory contains the sparse loop files that contain all the data that docker mounts. So you would need to use lvm tools to trawl around them and do things. Have a read though the remove issues with devicemapper, they are kinda sorta resolved but maybe not.
我会尽可能远离 devicemapper
,或者在任何基于 RHEL 的设备上使用 LVM 精简池.如果您无法更改存储驱动程序,则相同的过程至少会清除您无法回收的所有已分配稀疏空间.
I would move away from devicemapper
where possible or use LVM thin pools on anything RHEL based. If you can't change storage drivers, the same procedure will at least clear up any allocated sparse space you can't reclaim.
更改存储驱动程序将需要转储包含所有 docker 数据的 /var/lib/docker
目录.有一些方法可以保存其中的一部分,但这涉及弄乱 Docker 内部结构.最好提交和导出您想要保留的任何容器或卷,并在更改后导入它们.否则你将有一个全新的、空白的 Docker 安装!
Changing storage driver will require dumping your /var/lib/docker
directories which contains all your docker data. There are ways to save portions of it but that involves messing around with Docker internals. Better to commit and export any containers or volumes you want to keep and import them after the change. Otherwise you will have a fresh, blank Docker install!
导出数据
Export data
停止 Docker
删除/var/lib/docker
修改您的 docker 启动以使用新的存储驱动程序.在 /lib/systemd/system/docker.service
或 /etc/systemd/system/docker.service 中设置
--storage-driver=
或 /etc/default/docker
或 /etc/sysconfig/docker
Modify your docker startup to use the new storage driver.
Set --storage-driver=<name>
in /lib/systemd/system/docker.service
or /etc/systemd/system/docker.service
or /etc/default/docker
or /etc/sysconfig/docker
启动 Docker
导入数据
AUFS
AUFS 不在主线内核中(并且永远不会),这意味着发行版必须以某种方式主动包含它.对于 Ubuntu,它位于 linux-image-extra
包中.
apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual
然后将存储驱动程序选项更改为--storage-driver=aufs
Then change the storage driver option to --storage-driver=aufs
OverlayFS 在 Ubuntu 中已经可用,如果你还在使用,只需将存储驱动程序更改为 --storage-driver=overlay2
或 --storage-driver=overlay
一个 3.x 内核
OverlayFS is already available in Ubuntu, just change the storage driver to --storage-driver=overlay2
or --storage-driver=overlay
if you are still using a 3.x kernel
我不确定现在这个主意有多好.它不会比循环文件差多少,但是overlay2
驱动程序非常适合开发人员使用,但尚未被视为生产就绪(例如 Docker Enterprise 不提供支持),但由于 AUFS/内核,它正在被推动成为标准驱动程序问题.
I'm not sure how good an idea this is right now. It can't be much worse than the loop file but
The overlay2
driver is pretty solid for dev use but isn't considered production ready yet (e.g. Docker Enterprise don't provide support) but it is being pushed to become the standard driver due to the AUFS/Kernel issues.
您可以直接使用 LVM 精简池而不是 devicemapper 循环文件.RHEL 通过 docker-storage-setup
实用程序,随其 EPEL docker 包分发.Docker有详细步骤用于手动设置卷.
Instead of the devicemapper loop file you can use an LVM thin pool directly. RHEL makes this easy with a docker-storage-setup
utility that distributed with their EPEL docker package. Docker have detailed steps for setting up the volumes manually.
--storage-driver=devicemapper
--storage-opt=dm.thinpooldev=/dev/mapper/docker-thinpool
--storage-opt dm.use_deferred_removal=true
Docker 17.06+ 支持 为您管理简单的 direct-lvm
块设备设置.
Docker 17.06+ supports managing simple direct-lvm
block device setups for you.
永远不要用完 LVM 卷中的空间.您最终会得到一个无响应的 Docker 守护进程,需要将其杀死,然后是仍在使用的 LVM 资源难以清理.
Just don't run out of space in the LVM volume, ever. You end up with an unresponsive Docker daemon that needs to be killed and then LVM resources that are still in use that are hard to clean up.
这篇关于清洁 docker 环境:devicemapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!