主机磁盘满扩容实战案例

一、检查过程

安装有gitlab的主机, 提示磁盘满了

[root@gitlab ~]$ df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 3.9G     0  3.9G   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    3.9G  9.0M  3.9G   1% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   50G   50G  749M  99% /
/dev/sda1               1014M  194M  821M  20% /boot
/dev/mapper/centos-home   26G   33M   26G   1% /home
tmpfs                    783M     0  783M   0% /run/user/1000

考虑到gitlab是以docker形式运行的, Docker Root Dir是默认文件夹,经使用 du -sh 查看文件夹及其子文件夹的大小,发现磁盘满是因为docker文件夹过大导致的。

[root@gitlab ~]$ docker info | grep "Docker Root Dir"
Docker Root Dir: /var/lib/docker

[root@gitlab ~]$ du -sh /var/lib/docker
41G     /var/lib/docker

二、解决思路

  1. 停止容器及docker服务
  2. 暂时移动原有 /var/lib/docker 文件夹到新的路径 /var/lib/docker1
  3. 新增磁盘并做初始化格式化,挂载到 /var/lib/docker
  4. 将原有 /var/lib/docker1 的文件拷贝到 /var/lib/docker
  5. 重启系统,验证

三、具体步骤

  1. 停止容器及docker服务, 移动Docker Root Dir
docker ps -a
docker stop CONTAINER_ID
systemctl stop docker
mv /var/lib/docker /var/lib/docker1
  1. 添加磁盘后,查看磁盘列表,sda是原有的磁盘,sdb是新增的磁盘
[root@gitlab ~]$ fdisk -l
Disk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 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: 0x000977ec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   167772159    82836480   8e  Linux LVM

Disk /dev/sdb: 536.9 GB, 536870912000 bytes, 1048576000 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 /dev/mapper/centos-root: 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 /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 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 /dev/mapper/centos-home: 27.0 GB, 26969374720 bytes, 52674560 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
  1. 创建硬盘分区
    按n,创建新分区,而后p创建主分区,而后分区数选择默认的1,第一个和最后的扇区均使用默认值即可,最后输入w写入
[root@gitlab ~]$ fdisk /dev/sdb
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 0x91c761c0.

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-1048575999, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1048575999, default 1048575999): 
Using default value 1048575999
Partition 1 of type Linux and of size 500 GiB is set

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

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

分区完成后,再输入 fdisk -l ,已可以看到新的分区

[root@gitlab ~]$ fdisk -l

Disk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 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: 0x000977ec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   167772159    82836480   8e  Linux LVM

Disk /dev/sdb: 536.9 GB, 536870912000 bytes, 1048576000 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: 0x40642f41

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1048575999   524286976   83  Linux

Disk /dev/mapper/centos-root: 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 /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 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 /dev/mapper/centos-home: 27.0 GB, 26969374720 bytes, 52674560 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

  1. 格式化与挂载

格式化分区, 建立挂载目录, 挂载分区, 设置开机自动挂载,拷贝目录,重启docker服务

[root@gitlab ~]$ mkfs.ext4 /dev/sdb1
[root@gitlab ~]$ mkdir /var/lib/docker
[root@gitlab ~]$ mount /dev/sdb1 /data
[root@gitlab ~]$ echo /dev/sdb1 /var/lib/docker ext4 defaults 0 0 >> /etc/fstab
[root@gitlab ~]$ cp -pdr /var/lib/docker1/ /var/lib/docker/
[root@gitlab ~]$ systemctl restart docker
[root@gitlab ~]$ docker ps -a

此时应能看到原来的docker容器了。

  1. 重启,并做最终校验
    没有问题则可以删除 /var/lib/docker1/ 文件夹

参考

  1. CentOS7如何添加硬盘和挂载硬盘

  2. centos 7 新增硬盘挂载分区,及扩容

  3. Centos7挂载新磁盘到目录

  4. docker容器存放目录磁盘空间满了,转移数据修改Docker默认存储位置

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐