dcoker存储引擎介绍官方网址:https://docs.docker.com/storage/storagedriver/select-storage-driver/
存储引擎类型
docker有6种存储驱动方案,且都是写时复制机制
- 1.aufs - unionfs类型(AUFS)
AUFS是一种UnionFS,是文件级的存储驱动。所谓的UnionFS就是把不同物理位置的目录合并mount到同一个目录中。在文件层次上工作内存利用高效,仅有ubuntu支持
简单来说就是支持将不同目录挂载到同一个虚拟文件系统下的文件系统。这种文件系统可以一层一层的叠加修改文件。无论底下有多少层都是只读的,只有最上层的文件系统是可写的。当需要修改一个文件时,AUFS创建该文件的一个副本,使用COW将文件从只读层复制到可写层进行修改,结果也保存在可写层。在docker中,底下的只读层时image(镜像),可写层是container(容器),是Docker18.06及更早版本的首选存储驱动程序,在内核3.13上运行Ubntu14.04时不支持overlay2。
- 2.overlay/overlay2 - unionfs类型
overlay:一种UnionFS文件系统,Linux内核3.18后支持
overlay2:overlay的升级版。到目前为止,所有Linux发行版推荐使用的存储类型。
overlay/overlay2 在文件层次上工作内存利用高效,但分层文件的写入会增长的比较大(需要较高Linux内核的支持)
overlayfs是基于文件
- 3.devicemapper
是Centos和RHEK的推荐存储驱动程序,因为之前的内核版本(7.2以及7.2之前)不支持overlay2,但目前较新版本(7.5之后)的Centos和RHEL现在已经支持overlay2。在block层次工作,分层文件写入效率较高。
devicemapper类型docker磁盘空间默认是100G,当数据到达100G之后,即使宿主机磁盘还有很多空闲,也不会在写入数据就会报错。
devicemapper是基于块
- 4.btrfs
不流行
- 5.zfs
不流行推荐在paas上工作
- 6.vfs
仅用于调试
对于Docker,支持文件系统是所在的文件系统 /var/lib/docker/。一些存储驱动程序仅适用于特定的后备文件系统。
各个存储引擎支持的文件系统
docker存储引擎升级
devicemapper升级为overlay
Centos7.2版升级docker存储引擎(亲测有效)
- 内核版本
[root@localhost ~] uname -a
Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
- 查看docker info信息
注意以下信息:
Storage Driver: devicemapper ###存储引擎
Data Space Total: 107.4GB ###默认总空间大小(所有docker镜像,容器以及信息存储大小,占满之后不会扩大,和宿主机磁盘大小无关)
[root@localhost ~] docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 18.09.9
Storage Driver: devicemapper
Pool Name: docker-253:0-101290349-pool
Pool Blocksize: 65.54kB
Base Device Size: 10.74GB
Backing Filesystem: xfs
Udev Sync Supported: true
Data file: /dev/loop0
Metadata file: /dev/loop1
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Data Space Used: 11.8MB
Data Space Total: 107.4GB
Data Space Available: 16.43GB
Metadata Space Used: 581.6kB
Metadata Space Total: 2.147GB
Metadata Space Available: 2.147GB
Thin Pool Minimum Free Space: 10.74GB
Deferred Removal Enabled: true
Deferred Deletion Enabled: true
Deferred Deleted Device Count: 0
Library Version: 1.02.170-RHEL7 (2020-03-24)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-327.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 977.9MiB
Name: localhost.localdomain
ID: WTR6:OZEX:2J7L:5JV5:J5Z7:ZV7Q:MIHH:CP47:URRA:5WFY:XW6N:V474
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine
WARNING: the devicemapper storage-driver is deprecated, and will be removed in a future release.
WARNING: devicemapper: usage of loopback devices is strongly discouraged for production use.
Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
- 查看xfs_info信息
ftype=0 参数等于0时,表示xfs文件系统不支持overlay类型的存储引擎,等于1反之
[root@localhost ~]# xfs_info /
meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=1144832 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=4579328, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
- 7.7系统xfs_info
[root@localhost system]# xfs_info /
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1769216 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=7076864, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 **ftype=1**
log =internal bsize=4096 blocks=3455, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
- 升级存储引擎
因为xfs文件系统格式化时不支持overlay,所以只能挂载一块新盘并格式化
另一种处理方法引荐文章:https://www.cnblogs.com/youruncloud/p/5736718.html
- 备份docker /var/lib/docker工作目录
systemctl stop docker
cp -r /var/lib/docker/* /docker-data/
-查看本机容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c9da4279290 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp distracted_taussig
-查看新挂载的硬盘
[root@localhost ~] fdisk -l
磁盘 /dev/sdb:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000710ef
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 41943039 20458496 8e Linux LVM
磁盘 /dev/mapper/centos-root:18.8 GB, 18756927488 字节,36634624 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
-创建新硬盘
[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0xdb21dc05 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
分区号 (1-4,默认 1):
起始 扇区 (2048-62914559,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-62914559,默认为 62914559):
将使用默认值 62914559
分区 1 已设置为 Linux 类型,大小设为 30 GiB
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xdb21dc05
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 62914559 31456256 83 Linux
命令(输入 m 获取帮助):t
已选择分区 1
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xdb21dc05
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 62914559 31456256 8e Linux LVM
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xdb21dc05
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 62914559 31456256 8e Linux LVM
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
-创建LVM卷
#创建PV
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
#创建VG
[root@localhost ~]# vgcreate vgdocker /dev/sdb1
Volume group "vgdocker" successfully created
#查看VG
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vgdocker
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size <30.00 GiB
PE Size 4.00 MiB
Total PE 7679
Alloc PE / Size 0 / 0
Free PE / Size 7679 / <30.00 GiB
VG UUID xIEwCJ-4q9s-VYMv-TBw8-5ezG-K0Nf-FotCLv
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <19.51 GiB
PE Size 4.00 MiB
Total PE 4994
Alloc PE / Size 4984 / <19.47 GiB
Free PE / Size 10 / 40.00 MiB
VG UUID FHOV4s-bBZZ-Tsrh-WOZk-AAOw-jrwe-w2WWmT
#创建LV
[root@localhost ~]# lvcreate -L 25G -n lvdocker vgdocker
Logical volume "lvdocker" created.
#查看LV
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vgdocker/lvdocker
LV Name lvdocker
VG Name vgdocker
LV UUID Rxlz1M-yton-V1WB-MwEa-elej-2bEv-4Ngb0a
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2021-01-20 22:45:08 +0800
LV Status available
# open 0
LV Size 25.00 GiB
Current LE 6400
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID mkwhba-h6sq-fzdy-dKAR-rmFU-DTbq-4Tj6K0
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2021-01-19 22:21:52 +0800
LV Status available
# open 1
LV Size <17.47 GiB
Current LE 4472
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID Csteou-IPef-9VPr-0mPP-vAuV-gEaU-ueNWPV
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2021-01-19 22:21:52 +0800
LV Status available
# open 2
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
-格式化新LVM
可以格式化成ext4,也可以格式成xfs,但是xfs需要ftype=1
格式化完成之后ftype=1
[root@localhost ~]# mkfs.xfs -n ftype=1 /dev/vgdocker/lvdocker
meta-data=/dev/vgdocker/lvdocker isize=256 agcount=4, agsize=1638400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=6553600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=3200, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
-修改docker存储引擎
在dockered后边加上-s=overlay2或者-s=overlay
ExecStart=/usr/bin/dockerd -s=overlay2 -H fd:// --containerd=/run/containerd/containerd.sock
-将新磁盘挂载到/var/lib/docker
mount /dev/mapper/vgdocker-lvdocker /var/lib/docker
-还原备份
cp -r /docker-data/* /var/lib/docker/
###删除devicemapper目录,因为这个目录是devicemapper存储引擎使用的
rm -rf /var/lib/docker/devicemapper
systemctl start docker
-查看docker info
确定存储引擎变为overlay2
[root@localhost ~]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 18.09.9
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-327.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 977.9MiB
Name: localhost.localdomain
ID: WTR6:OZEX:2J7L:5JV5:J5Z7:ZV7Q:MIHH:CP47:URRA:5WFY:XW6N:V474
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://uxk0ognt.mirror.aliyuncs.com/
Live Restore Enabled: false
Product License: Community Engine
-查看之前数据
工作目录下会出现devicemapper和overlay2两个目录
devicemapper存的是之前的数据
overlay2中没有数据,所以docker images看不到之前的数据
查看volumes目录下的数据存在的话,就可以重新下载镜像启动容器,将数据目录重新挂载。
[root@localhost ~]# ls /var/lib/docker/
builder buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost docker]# ls /var/lib/docker/volumes/
metadata.db web-html
-下载镜像重新启动容器
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 9 days ago 133MB
[root@localhost docker]# docker run -d -it -p 80:80 -v web-html:/usr/share/nginx/ --name nginx1 nginx:latest
be3f47cf50c5266cb9fbd010e6c1b8f70eab48c45328617b1f81f4108722987f
[root@localhost docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be3f47cf50c5 nginx:latest "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp nginx1
-查看web页面