【Docker 运维】Docker迁移至新磁盘

当服务器磁盘空间不足时,本文档提供了一种解决方案,即如何将Docker服务从满载的磁盘迁移到新的磁盘。步骤包括:检查现有磁盘、创建新分区、格式化新磁盘、挂载新分区、修改Docker配置以及重启Docker服务。通过这一系列操作,可以有效解决Docker因磁盘空间不足导致的问题。
摘要由CSDN通过智能技术生成

项目场景:

将Docker整体迁移至新磁盘


问题描述:

服务器运维小伙伴今天通知我说服务器磁盘快满了, 我上去df -h一下发现

Filesystem      Size  Used Avail Use% Mounted on
devtmpfs         16G     0   16G   0% /dev
tmpfs            16G     0   16G   0% /dev/shm
tmpfs            16G  2.9M   16G   1% /run
tmpfs            16G     0   16G   0% /sys/fs/cgroup
/dev/vda1        40G   37G  618M  99% /
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/874d6847419d1a2d219619b688581d9dff86d12a3846e7f8565ad736535e10c5/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/002aeb4ee79b35db4af7af2674bae1403a01978403ed4b1c911c7faf0be69c0c/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/625a48a7d031e224faf24168ec0a85db6a71d9ca88a967d8a9c1be4bf1058ee1/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/1f65639248c9296ced9651cccf4f6722db4d8c4e11dddaa6c90dbb417e1f2577/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/196d72245989acd46cf49b6f297f8b08a6e3a02bcd284abfff42daf5971ad419/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/81664287228bbb9c3bf62b9d4123226e57575f63f7354ee6466aaf48638eed64/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/3d761039a2370da18639e3fe756a4f4f335293c897a1a1096a0c8d840a171e87/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/134c87c2ed413b2ab200d9953a0e9fc7b89b4eb7482355beb905bd40edf405d8/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/892b9b187cfdb3a03288aeb1e88e2791604111b22e8d2ae7031720f3692fc77b/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/3d911badbb7a06ea4697cf974c7cb177e1e1203c354353300b9bf879ad9b1e88/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/c58232a29e6e807f294bcd5ee3cdb55157cdc657f587c1140e86a1f2ae17e6a1/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/b99bcf7bdd2961d036b2384b76038fca61dccb489248a4b8b20d737707f3905f/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/7a86b18097c6cb6ff5a48ac1653877af8604fb5c1ee3d26aa4cc8e3371a31a30/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/3f602c9294c2691164b37bb98ba765a6e2af911e28bc6609e69c13e8df472413/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/ab613c0f0f70c3b6838e1683004db33d8df826eb8a2c6f9583eb561e84ac1b06/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/f526b92fe3212385ea89d00c5a75ade50c8134aedd2c27c39e1261c437bea72f/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/fa32d3d9a6570792d25472d65670ea1ba56ce87307b69cd27a0436212c0bdb20/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/d6073cd627ab4b452e36824acec7544ad5d87fad1fca0fd36dd5a561f0bddc9a/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/59dec3519a2c47630c5a84bab2521d041001b404c66cdf40a3e0ca6262fdaacd/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/9f3614bd56c60dae18d5957230aaba85cbae11f133bc0f6988cb78bb554a50b6/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/1a2683fddf524a8cca26f1ef0aceedead8765a6d63fd408c3cd8730e2d94daef/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/e41dd9ee1f9d4bbbd7399afa028dfd77d98fb524396ba854692a30a11f0be16f/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/e80ba6f30f52913ddcb094676c615e276116fa9405ee2602194a972a9ba99e71/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/4cfeac7e4b74299cc0d4f3195f390516270a2a71c484ff6246ee96f9aea114a7/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/a05c836e9ca3525a4b6ce2220c593e11b096eeecf34428e7af3b78dfba144a54/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/53277a7e1b586a1aa26fc8530cd4a06aa498e9829bb7bb838021c6c4bcd3246a/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/2db7ee3c090f746de4d4ae22bbea4219c8e90d0bf608e1204bda8c7fe2d6c8e9/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/e22cc0b13f096688a42d05aa64792b301331147dfd14b7f0191773fc1867b526/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/0d69b1e589cb0f94aa2d527c55fc6f83ef66bfde0424f5fce8fad73df290e994/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/895dfbbf7556d98e4f9cfd40bc8909c41c13823049d91750bac43dbebe6f3046/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/237530eee48a542d64ee9cf14afea8061c4f152314f9fa1ae67c88cfaf1d0674/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/7a1554ffb2848259f202d016fac022259a0b76de64e69fa6064a539d997512a0/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/6a64c2b345ca36508c10806a703d4ca4b5017e58463a1df8991910881ac4c668/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/9674a2d533ae579a11fefabc024b34a4c1c22723476b68da96cbcbcd64d95e5d/merged
overlay          40G   37G  618M  99% /var/lib/docker/overlay2/064ed4015c70aff05563512c512b939ed222ed037819b7a0ce607c88c00070a4/merged
tmpfs           3.2G     0  3.2G   0% /run/user/0

好嘛, 直接干到99了, 我问了运维小伙伴能不能扩容后, 对方直接说本来就有第二个磁盘, 是我没挂载…
在这里插入图片描述
由于这台服务器只运行Docker服务, 所以把Docker迁移到新磁盘就能解决问题


原因分析:


解决方案:

运行环境

CentOS: 7.9.2009
Docker: 20.10.17

【Docker运维】磁盘满了如何迁移Docker到新磁盘

挂载磁盘

查看现有磁盘

fdisk -l
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000a57df

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   104857566    52427759+  83  Linux

Disk /dev/vdb: 322.1 GB, 322122547200 bytes, 629145600 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

磁盘分区

fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xa3561fb4.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-629145599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-629145599, default 629145599):
Using default value 629145599
Partition 1 of type Linux and of size 300 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

格式化为ext4

mkfs.ext4 /dev/vdb

将磁盘挂载到指定目录

mkdir /data
mount -t ext4 /dev/vdb /data

开机自动挂载目录

blkid |grep /dev/vdb

/dev/vdb: UUID="46a7455b-2344-43aa-bfa7-b3ef3c800600" TYPE="ext4"
vim /etc/fstab

# 追加
UUID=46a7455b-2344-43aa-bfa7-b3ef3c800600 /data                ext4    defaults        0 0

迁移Docker

暂停Docker

systemctl stop docker

迁移数据

mv /var/lib/docker /data/

修改配置文件中的目录地址

vim /etc/docker/daemon.json

# 添加
{
    "data-root": "/data/docker",
    ...
}

加载配置并重启Docker

systemctl daemon-reload
systemctl start docker

查看是否生效

docker info

参考:
https://www.chenshaowen.com/blog/how-to-migrate-docker-storage-to-new-disk.html
https://icode.best/i/36183140598208

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值