其他分享
首页 > 其他分享> > 【Docker】使用 OverlayFS 存储驱动

【Docker】使用 OverlayFS 存储驱动

作者:互联网

参考教程:https://docs.docker.com/storage/storagedriver/overlayfs-driver/
以下内容来自官方文档翻译

环境

  1. virtual box 6.1
  2. centos 7.8
  3. docker 19.03

OverlayFS is a modern union filesystem that is similar to AUFS, but faster and with a simpler implementation. Docker provides two storage drivers for OverlayFS: the original overlay, and the newer and more stable overlay2.

OverlayFS 是一种现代的联合文件系统,与 AUFS 类似,但是速度更快且实现更简单。Docker 为 OverlayFS 提供了两个存储驱动程序:原始的 overlay 和更新且更稳定的 overlay2

This topic refers to the Linux kernel driver as OverlayFS and to the Docker storage driver as overlay or overlay2.

本主题将 Linux 内核驱动程序称为 OverlayFS,并将 Docker 存储驱动程序称为 overlayoverlay2

Note: If you use OverlayFS, use the overlay2 driver rather than the overlay driver, because it is more efficient in terms of inode utilization. To use the new driver, you need version 4.0 or higher of the Linux kernel, or RHEL or CentOS using version 3.10.0-514 and above.

注意:如果使用 OverlayFS,请使用 overlay2 驱动程序而不是 overlay 驱动程序,因为它在 inode 利用率方面更为有效。要使用新的驱动程序,您需要 Linux 内核的版本 4.0 或更高版本,或者使用 3.10.0-514 及更高版本的 RHEL 或 CentOS。

前置条件

OverlayFS is the recommended storage driver, and supported if you meet the following prerequisites:

OverlayFS 是推荐的存储驱动程序,如果满足以下先决条件,则受支持:

Warning: Running on XFS without d_type support now causes Docker to skip the attempt to use the overlay or overlay2 driver. Existing installs will continue to run, but produce an error. This is to allow users to migrate their data. In a future version, this will be a fatal error, which will prevent Docker from starting.

警告:在不支持 d_type 的 XFS 上运行现在会导致 Docker 跳过使用 overlayoverlay2 驱动程序的尝试。现有安装将继续运行,但会提示错误,这是为了允许用户迁移其数据。在将来的版本中,这将是一个致命错误,它将阻止Docker启动。

overlay2 驱动怎样工作

If you are still using the overlay driver rather than overlay2, see How the overlay driver works instead.

如果您仍在使用 overlay 驱动程序而不是 overlay2,请参阅 overlay 驱动程序的工作原理。

OverlayFS layers two directories on a single Linux host and presents them as a single directory. These directories are called layers and the unification process is referred to as a union mount. OverlayFS refers to the lower directory as lowerdir and the upper directory a upperdir. The unified view is exposed through its own directory called merged.

OverlayFS 在单个 Linux 主机上有两个目录,并将它们合并显示为单个目录。这些目录称为 layers,合并的过程称为联合挂载。OverlayFS 将较低的目录称为 lowerdir,而将较高的目录称为 upperdir。 合并后的视图通过 merged目录暴露。

The overlay2 driver natively supports up to 128 lower OverlayFS layers. This capability provides better performance for layer-related Docker commands such as docker build and docker commit, and consumes fewer inodes on the backing filesystem.

overlay2 驱动程序原生支持多达 128 个较低的 OverlayFS 层。此功能可为与层相关的 Docker 命令(如docker build 和 docker commit)提供更好的性能,并在支持文件系统上消耗更少的 inode。

磁盘上的镜像和容器层

After downloading a five-layer image using docker pull ubuntu, you can see six directories under /var/lib/docker/overlay2.

使用 docker pull ubuntu 下载五层镜像后,可以在 /var/lib/docker/overlay2 下看到六个目录。

Warning: Do not directly manipulate any files or directories within /var/lib/docker/. These files and directories are managed by Docker.

警告:请勿直接在 /var/lib/docker/ 中操作任何文件或目录。这些文件和目录由 Docker 管理。

$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
6a5697faee43: Pull complete 
ba13d3bc422b: Pull complete 
a254829d9e55: Pull complete 
Digest: sha256:fff16eea1a8ae92867721d90c59a75652ea66d29c05294e6e2f898704bdb8cf1
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

$ ls -l
total 0
drwx------    4 root     root            55 Nov 24 10:51 57bb3984fedd79355d16d090824068eb25a262897bd03f4cc903458a270fb135
drwx------    4 root     root            72 Nov 24 10:51 63492365c5b50b0b87988f7d7b90c817f87d9a4a88be4efd604ece8ce987e932
drwx------    3 root     root            47 Nov 24 10:51 654a21568916b79f8ce2b0b9795710980ef429d3b4d136a1cd6fdefabe8a4bed
brw-------    1 root     root        8,  16 Nov 24 10:50 backingFsBlockDev
drwx------    2 root     root           108 Nov 24 10:51 l

The new l (lowercase L) directory contains shortened layer identifiers as symbolic links. These identifiers are used to avoid hitting the page size limitation on arguments to the mount command.

新的 l(小写L)目录包含缩短的层标识符作为符号链接。这些标识符用于避免在 mount 命令的参数上达到页面大小限制。

$ ls -l l
total 0
lrwxrwxrwx    1 root     root            72 Nov 24 10:51 6ZP27KCNIOAMZOSGMQJXZPXB5J -> ../654a21568916b79f8ce2b0b9795710980ef429d3b4d136a1cd6fdefabe8a4bed/diff
lrwxrwxrwx    1 root     root            72 Nov 24 10:51 OOHUGSX2SX7GFNVJQ6TQL4ZWW7 -> ../63492365c5b50b0b87988f7d7b90c817f87d9a4a88be4efd604ece8ce987e932/diff
lrwxrwxrwx    1 root     root            72 Nov 24 10:51 TKENHGOKTAE4UDAMIQPWY65JBU -> ../57bb3984fedd79355d16d090824068eb25a262897bd03f4cc903458a270fb135/diff

The lowest layer contains a file called link, which contains the name of the shortened identifier, and a directory called diff which contains the layer’s contents.

最低层包含一个名为 link 的文件,其中包含缩短的标识符的名称;一个目录是 diff,其中包含该层的内容。

[node1] (local) root@192.168.0.23 /var/lib/docker/overlay2
$ cat 654a21568916b79f8ce2b0b9795710980ef429d3b4d136a1cd6fdefabe8a4bed/link 
6ZP27KCNIOAMZOSGMQJXZPXB5J[node1] (local) root@192.168.0.23 /var/lib/docker/overlay2
$ ls -l l
total 0
lrwxrwxrwx    1 root     root            72 Nov 24 10:51 6ZP27KCNIOAMZOSGMQJXZPXB5J -> ../654a21568916b79f8ce2b0b9795710980ef429d3b4d136a1cd6fdefabe8a4bed/diff
lrwxrwxrwx    1 root     root            72 Nov 24 10:51 OOHUGSX2SX7GFNVJQ6TQL4ZWW7 -> ../63492365c5b50b0b87988f7d7b90c817f87d9a4a88be4efd604ece8ce987e932/diff
lrwxrwxrwx    1 root     root            72 Nov 24 10:51 TKENHGOKTAE4UDAMIQPWY65JBU -> ../57bb3984fedd79355d16d090824068eb25a262897bd03f4cc903458a270fb135/diff
[node1] (local) root@192.168.0.23 /var/lib/docker/overlay2
$ cat 654a21568916b79f8ce2b0b9795710980ef429d3b4d136a1cd6fdefabe8a4bed/link 
6ZP27KCNIOAMZOSGMQJXZPXB5J[node1] (local) root@192.168.0.23 /var/lib/docker/overlay2
$ ls 57bb3984fedd79355d16d090824068eb25a262897bd03f4cc903458a270fb135/diff/
run
[node1] (local) root@192.168.0.23 /var/lib/docker/overlay2
$ ls 63492365c5b50b0b87988f7d7b90c817f87d9a4a88be4efd604ece8ce987e932/diff
etc  usr  var
[node1] (local) root@192.168.0.23 /var/lib/docker/overlay2
$ ls 654a21568916b79f8ce2b0b9795710980ef429d3b4d136a1cd6fdefabe8a4bed/diff
bin     dev     home    lib32   libx32  mnt     proc    run     srv     tmp     var
boot    etc     lib     lib64   media   opt     root    sbin    sys     usr
[node1] (local) root@192.168.0.23 /var/lib/docker/overlay2
$ ^C
[node1] (local) root@192.168.0.23 /var/lib/docker/overlay2
$ 

The second-lowest layer, and each higher layer, contain a file called lower, which denotes its parent, and a directory called diff which contains its contents. It also contains a merged directory, which contains the unified contents of its parent layer and itself, and a work directory which is used internally by OverlayFS.

第二最低的层,以及每个较高的层,包含一个名为 lower 的文件(表示其父文件)和一个名为 diff 的目录,该文件包含其内容。它还包含一个 merged 目录,该目录包含其父层及其本身的统一内容,以及一个 work 目录,供 OverlayFS 内部使用。

容器使用 overlay 读写

读取文件

Consider three scenarios where a container opens a file for read access with overlay.

考虑三种容器使用 overlay 打开文件来读取的场景。

修改文件或者目录

Consider some scenarios where files in a container are modified.

考虑在某些情况下修改了容器中的文件。

OverlayFS and Docker Performance

Both overlay2 and overlay drivers are more performant than aufs and devicemapper. In certain circumstances, overlay2 may perform better than btrfs as well. However, be aware of the following details.

overlay2overlay 驱动程序都比 aufsdevicemapper 性能更高。在某些情况下,overlay2 的效果也可能会优于 btrfs。但是,请注意以下详细信息。

Performance best practices

The following generic performance best practices also apply to OverlayFS.

以下通用性能最佳实践也适用于 OverlayFS。

总结

介绍了 overlay 的实现方式,以及在容器中的如何处理文件的增删改查等。

标签:存储,container,overlay,OverlayFS,file,overlay2,root,Docker
来源: https://www.cnblogs.com/jiangbo44/p/14056879.html