Docker存储驱动devicemapper配置

devicemapper驱动将每一个 Docker镜像 和容器存储在它自身的具有精简置备(thin-provisioned)、写时拷贝(copy-on-write)和快照功能(snapshotting)的虚拟设备上。由于Device Mapper技术是在块(block)层面而非文件层面,所以Docker Engine的devicemapper存储驱动使用的是块设备来存储数据而非文件系统。(摘自网络)

一、Devicemapper的模式

devicemapper是RHEL下 Docker Engine 的默认存储驱动,它有两种配置模式:loop-lvm和direct-lvm。

loop-lvm是默认的模式,它使用OS层面离散的文件来构建精简池(thin pool)。该模式主要是设计出来让Docker能够简单的被”开箱即用(out-of-the-box)”而无需额外的配置。但如果是在生产环境的部署Docker,官方明文不推荐使用该模式。我们使用docker info命令可以看到以下警告:

WARNING: Usage of loopback devices is strongly discouraged for production use. Either use –storage-opt dm.thinpooldev or use –storage-opt dm.no_warn_on_loop_devices=true to suppress this warning.

direct-lvm是Docker推荐的生产环境的推荐模式,他使用块设备来构建精简池来存放镜像和容器的数据。

二、配置Devicemapper direct-lvm模式

1. 停止docker

[root@hathor70 ~]# systemctl stop docker

2. 查看本机磁盘,使用空余的磁盘做lvm

[root@hathor70 ~]# fdisk -l

Disk /dev/sda: 146.0 GB, 145999527936 bytes, 285155328 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000e2af0

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    25167871    12582912   83  Linux
/dev/sda2        25167872    41945087     8388608   82  Linux swap / Solaris
/dev/sda3   *    41945088    50333695     4194304   83  Linux
/dev/sda4        50333696   285155327   117410816    5  Extended
/dev/sda5        50337792    67115007     8388608   83  Linux
/dev/sda6        67117056    83894271     8388608   83  Linux
/dev/sda7        83896320   285155327   100629504   83  Linux
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/sdb: 146.0 GB, 145999527936 bytes, 285155328 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt

#         Start          End    Size  Type            Name
 1           34    285155294    136G  Microsoft basic primary

3. 创建PV

[root@hathor70 ~]# pvcreate /dev/sda7 /dev/sdb1
  Physical volume "/dev/sdb" successfully created.

4. 创建VG

[root@hathor70 ~]# vgcreate docker /dev/sda7 /dev/sdb1
  Volume group "docker" successfully created

5. 查看VG信息

[root@hathor70 ~]# vgdisplay docker
  --- Volume group ---
  VG Name               docker
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               231.93 GiB
  PE Size               4.00 MiB
  Total PE              59375
  Alloc PE / Size       57592 / 224.97 GiB
  Free  PE / Size       1783 / 6.96 GiB
  VG UUID               ZOTofh-XYtf-bCbk-cttv-ARjX-lhDi-ahl5Zb

6. 创建thinpool

1)创建pool

[root@hathor70 ~]#  lvcreate --wipesignatures y -n thinpool docker -l 95%VG
    Logical volume "thinpool" created.
[root@hathor70 ~]#  lvcreate --wipesignatures y -n thinpoolmeta docker -l 1%VG
    Logical volume "thinpoolmeta" created.

说明: 数据LV大小为VG的95%,元数据LV大小为VG的1%,剩余的空间用来自动扩展。

2)将pool转换为thinpool

 [root@hathor70 ~]#  lvconvert -y --zero n -c 512K --thinpool docker/thinpool --poolmetadata docker/thinpoolmeta
      WARNING: Converting logical volume docker/thinpool and docker/thinpoolmeta to thin pool's data and metadata volumes with metadata wiping.
      THIS WILL DESTROY CONTENT OF LOGICAL VOLUME (filesystem etc.)
      Converted docker/thinpool to thin pool.

7. 配置thinpool

1)配置池的自动扩展

创建 docker-thinpool.profile,添加池的自动扩展

 [root@hathor70 ~]#  vim /etc/lvm/profile/docker-thinpool.profile
    activation {
        thin_pool_autoextend_threshold=80
        thin_pool_autoextend_percent=20
    }

2)应用配置变更

应用docker-thinpool.profile配置

[root@hathor70 ~]# lvchange --metadataprofile docker-thinpool docker/thinpool
      Logical volume docker/thinpool changed.
3)状态监控检查
[root@hathor70 ~]# lvs -o+seg_monitor
  LV       VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert Monitor
  thinpool docker twi-aot--- 220.34g             0.69   0.03                             monitored

8. 配置Docker服务并启动

1) 添加参数

[root@hathor70 ~]# vim /etc/sysconfig/docker
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false --storage-opt=dm.thinpooldev=/dev/mapper/docker-thinpool --storage-opt dm.use_deferred_removal=true --storage-opt dm.use_deferred_deletion=true --storage-opt dm.basesize=30G'
if [ -z "${DOCKER_CERT_PATH}" ]; then
    DOCKER_CERT_PATH=/etc/docker
fi
参数作用:
--storage-opt=dm.thinpooldev=/dev/mapper/docker-thinpool  存储到哪里
--storage-opt dm.use_deferred_removal=true --storage-opt dm dm.use_deferred_deletion=true  官方说:unintentionally leaking mount points 不明白具体意思
--storage-opt dm.basesize=30G 设置每个容器最大使用空间

[root@hathor70 ~]# vim /etc/sysconfig/docker-storage
DOCKER_STORAGE_OPTIONS="--storage-driver=devicemapper"
--storage-driver=devicemapper --storage-opt=dm.thinpooldev=/dev/mapper/docker-thinpool --storage-opt dm.use_deferred_removal=true --storage-opt dm.use_deferred_deletion=true --storage-opt dm.basesize=30G
参数作用 :
--storage-driver=devicemapper  使用什么存储驱动

2) 重启服务

[root@hathor70 ~]# systemctl restart docker

9. 查看信息

[root@hathor70 ~]# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 1.12.6
Storage Driver: devicemapper
 Pool Name: docker-thinpool
 Pool Blocksize: 524.3 kB
 Base Device Size: 32.21 GB
 Backing Filesystem: xfs
 Data file:
 Metadata file:
 Data Space Used: 1.611 GB
 Data Space Total: 236.6 GB
 Data Space Available: 235 GB
 Metadata Space Used: 651.3 kB
 Metadata Space Total: 2.487 GB
 Metadata Space Available: 2.487 GB
 Thin Pool Minimum Free Space: 23.66 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: true
 Deferred Deletion Enabled: true
 Deferred Deleted Device Count: 0
 Library Version: 1.02.135-RHEL7 (2016-11-16)
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
 Volume: local
 Network: null host bridge overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Security Options: seccomp
Kernel Version: 3.10.0-514.6.2.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 2
CPUs: 24
Total Memory: 11.56 GiB
Name: hathor70
ID: TP5U:HQCL:7TSU:KVVV:6QHK:V6AI:O76G:PYHL:ILA3:NSBN:OOVG:ZPFS
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Insecure Registries:
 127.0.0.0/8
Registries: docker.io (secure)

10. 其他相关:

1)lvs 查看thinpool 使用情况

[root@hathor70 ~]# lvs
  LV       VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  thinpool docker twi-aot--- 220.34g             0.68   0.03

2)测试

a.进入容器

手动在机器上启动一个容器测试,进入至容器中,可以看到根目录为 30G

[root@check-deploy-1858202602-9f6kn /]# df -Th
Filesystem                                                                                     Type   Size  Used Avail Use% Mounted on
/dev/mapper/docker-8:6-395678-48a8cb18f633a222739468414e8ccc12170e094728eea44c6b8296c803c8b514 xfs     30G  726M   30G   3% /
tmpfs                                                                                          tmpfs  5.8G     0  5.8G   0% /dev
tmpfs                                                                                          tmpfs  5.8G     0  5.8G   0% /sys/fs/cgroup
/dev/sda6                                                                                      ext4   7.8G  2.2G  5.3G  29% /etc/hosts
shm                                                                                            tmpfs   64M     0   64M   0% /dev/shm
b.测试

往容器中根目录下写文件,此处写了多个文件,写至testfile.3时报错无可用空间

[root@check-deploy-1858202602-9f6kn /]# dd if=/dev/zero of=/testfile.3 bs=1440k seek=8190
dd: error writing ‘/testfile.3’: No space left on device
2574+0 records in
2573+0 records out
3795283968 bytes (3.8 GB) copied, 35.1777 s, 108 MB/s

允许DOCKER配置DIRECT-LVM模式

使用Docker 17.06和更高版本,Docker可以为您管理块设备,简化direct-lvm模式配置。这仅适用于全新的Docker设置。您只能使用单个块设备。如果需要使用多个块设备,请手动配置direct-lvm模式。添加了以下新配置选项:

选项 描述 需要? 默认
dm.directlvm_device 要配置的块设备的路径direct-lvm dm.directlvm_device="/dev/xvdf"
dm.thinp_percent 传入块设备中用于存储的空间百分比。 没有 95 dm.thinp_percent=95
dm.thinp_metapercent 传入块设备中用于元数据存储的空间百分比。 没有 1 dm.thinp_metapercent=1
dm.thinp_autoextend_threshold lvm何时应自动将精简池扩展为总存储空间的百分比的阈值。 没有 80 dm.thinp_autoextend_threshold=80
dm.thinp_autoextend_percent 触发自动扩展时增加精简池的百分比。 没有 20 dm.thinp_autoextend_percent=20
dm.directlvm_device_force 是否格式化块设备,即使其上已存在文件系统。如果设置为false并且存在文件系统,则会记录错误并保持文件系统不变。 没有 dm.directlvm_device_force=true

编辑daemon.json文件并设置适当的选项,然后重新启动Docker以使更改生效。以下daemon.json配置设置上表中的所有选项。

{
  "storage-driver": "devicemapper",
  "storage-opts": [
    "dm.directlvm_device=/dev/sdb1",
    "dm.thinp_percent=95",
    "dm.thinp_metapercent=1",
    "dm.thinp_autoextend_threshold=80",
    "dm.thinp_autoextend_percent=20",
    "dm.directlvm_device_force=false"
  ]
}

如果当前的块存储已经有文件系统,就需要设置dm.directlvm_device_force=false

Error starting daemon: error initializing graphdriver: /dev/sdb1 has a filesystem already ,use dm.directlvm_device_force=true if you want to wipe the device

重新启动Docker以使更改生效。Docker调用命令为您配置块设备。

警告:不支持Docker为您准备块设备后更改这些值并导致错误。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • Docker这两年可谓大红大紫,仿佛一夜之间,街坊邻居茶余饭后都在说Docker,我这也掰扯掰扯Docker那点儿...
    一块积木阅读 3,741评论 8 68
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,099评论 18 139
  • 二白呆呆的坐在沙发上,已经8个小时了。 手机弹出大白的微信:上班 于是二白开心的笑了。连忙回复到:开心点儿!人活着...
    虚度老太婆阅读 546评论 0 0
  • 都快毕业了,才开始考虑找工作的问题,是不是太迟了一点? 如果早几年做打算,就该从大一开始好好学习,积极参加实习和实...
    安庆卢十四阅读 806评论 3 9