您现在的位置是:首页 >学无止境 >Docker: 修改docker的storage的BackingFilesystem网站首页学无止境

Docker: 修改docker的storage的BackingFilesystem

最快乐的寂寞是独处 2024-08-25 00:01:03
简介Docker: 修改docker的storage的BackingFilesystem

Docker的数据目录 (/var/lib/docker)用来存放几乎所有与docker相关的数据.

比如:

[root@k8snode1 ~]# ls /var/lib/docker -l

total 20

drwx--x--x  4 root root   120 Apr 11 07:08 buildkit

drwx-----x 19 root root  4096 Apr 18 02:34 containers

drwx------  3 root root    22 Apr 11 07:08 image

drwxr-x---  3 root root    19 Apr 11 07:08 network

drwx-----x 77 root root 12288 Apr 18 02:34 overlay2

drwx------  4 root root    32 Apr 11 07:08 plugins

drwx------  2 root root     6 Apr 17 04:19 runtimes

drwx------  2 root root     6 Apr 11 07:08 swarm

drwx------  2 root root     6 Apr 18 01:33 tmp

drwx------  2 root root     6 Apr 11 07:08 trust

drwx-----x  2 root root    50 Apr 17 04:19 volumes

[root@k8snode1 ~]#

Docker supports several storage drivers, using a pluggable architecture. The storage driver controls how images and containers are stored and managed on your Docker host. After you have read the storage driver overview, the next step is to choose the best storage driver for your workloads.

当前的docker一般是用overlay2这种类型的StorageDriver

但overlay2的Backing Filesystem则可以有多种:

With regard to Docker, the backing filesystem is the filesystem where /var/lib/docker/ is located. Some storage drivers only work with specific backing filesystems.

Storage driver

Supported backing filesystems

overlay2overlay

xfs with ftype=1, ext4

查看当前docker的storage driver:

# docker info

...

Storage Driver: overlay2

  Backing Filesystem: xfs

  Supports d_type: true

  Native Overlay Diff: true

  userxattr: false

...

其中的Backing Filesystem就是 docker root dir (一般是/var/lib/docker)所在分区的文件系统格式

如果要修改这个, 那么只能新挂一块磁盘, 格式化成所需的格式(比如ext4, 或者xfs), 然后将此新盘作为docker的 root dir

以下是在虚拟机上尝试的.

原先的Backing Filesystem是ext4, 想改成xfs

因为改成了xfs后, 才可使用docker的  --storage-opt选项

======================================================================

docker info查看, 看到信息如下:

Storage Driver: overlay2

  Backing Filesystem: extfs

  Supports d_type: true

  Native Overlay Diff: true

  userxattr: false

因此这样是不支持设置容器 --storage-opt.

需要将Backing Filesystem切换成xfs

  1. 挂载一个新的磁盘, 挂载后可以看到在/dev下有个新的vdb出现.
  2. 格式化vdbxfs格式

# mkfs.xfs /dev/vdb

Use xfs_info to verify that the ftype option is set to 1. To format an xfs filesystem correctly, use the flag -n ftype=1.

  1. xfs_info工具来校验vdb, 看它的ftype是否为1.(一般默认是1,如果不是1, 则再次用mkfs.xfs格式化, –n ftype=1选项 # mkfs.xfs  -n ftype=1  /dev/vdb)

[root@zzfxc1 lib]# xfs_info /dev/vdb

meta-data=/dev/vdb               isize=512    agcount=4, agsize=1966080 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=1        finobt=1, sparse=1, rmapbt=0

         =                       reflink=1    bigtime=1 inobtcount=0

data     =                       bsize=4096   blocks=7864320, imaxpct=25

         =                       sunit=0      swidth=0 blks

naming   =version 2              bsize=4096   ascii-ci=0, ftype=1

log      =internal log           bsize=4096   blocks=3840, version=2

         =                       sectsz=512   sunit=0 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

  1. 创建一个新的目录, 比如 /root/zzf/mnt/var/lib/docker

# mkdir –p /root/zzf/mnt/var/lib/docker.

  1. /dev/vdb挂载到 /root/zzf/mnt/var/lib/docker, 要带pquota选项

# mount -o pquota /dev/vdb /root/zzf/mnt/var/lib/docker

  1. docker现在的数据目录(Docker data directory (/var/lib/docker))拷贝到 /root/zzf/mnt/var/lib/docker

这样做是为了防止等下将/var/lib/docker改变后, 数据没了, 比如image找不到了.

    # cp -a /var/lib/docker/*  /root/zzf/mnt/var/lib/docker/

拷贝完后,  ls /root/zzf/mnt/var/lib/docker  ls /var/lib/docker 显示的内容及目录结构应该一样.

  1. 停止容器服务

# systemctl stop docker

  1. 删除原容器数据目录(Docker data directory)  /var/lib/docker, 然后新建软连接

# rm –rf /var/lib/docker

# ln –snf /root/zzf/mnt/var/lib/docker /var/lib/docker

[root@zzfxc1 ~]# ls -l /var/lib | grep docker

lrwxrwxrwx.  1 root    root      28 Mar 22 03:08 docker -> /root/zzf/mnt/var/lib/docker

[root@zzfxc1 ~]#

  1. 启动容器服务

# systemc start docker

  1. 再次查看容器的Storage driver信息

[root@zzfxc1 lib]# docker info | grep Storage -A 5 -B 1

 Server Version: 20.10.7

 Storage Driver: overlay2

  Backing Filesystem: xfs

  Supports d_type: true

  Native Overlay Diff: true

  userxattr: false

 Logging Driver: json-file

[root@zzfxc1 lib]#

因此, 现在满足了设置 --storage-opt的要求 (For the overlay2 storage driver, the size option is only available if the backing fs is xfs and mounted with the pquota mount option.)

(tips: the backing filesystem is the filesystem where /var/lib/docker/ is located.)

参考资料:

Docker storage drivers | Docker Documentation

About storage drivers | Docker Documentation

Use the OverlayFS storage driver | Docker Documentation

dockerd | Docker Documentation

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。