LVM逻辑卷扩容实践—解决zabbix根目录空间满的问题

服务器

浏览数:33

2019-10-13

最近zabbix出现了根目录空间满的问题,可用空间不足,导致zabbix server服务直接挂掉了。

由于最初zabbix的虚机采用了LVM方式划分文件系统,就让解决这个问题降低了很大的难度。

解决方案是直接在Esxi上关掉虚机,增加硬盘,最后打开电源开机,给根目录所在LVM逻辑卷扩容。

针对LVM扩容,大体上步骤是,通过新增一个磁盘,建立逻辑卷组,然后扩容根目录所在的逻辑卷空间。

先简单了解一下LVM

普通分区==>LVM 8e分区==>PV物理卷==>VG卷组==>逻辑卷

一个VG卷组包含若干PV物理卷

一个VG卷组划分为若干LV逻辑卷

1.查看新增的磁盘

#查看到新增了第二块40G磁盘
root@zabbix:~# fdisk -l

Disk /dev/sdb: 40 GiB, 42949672960 bytes, 83886080 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

2.建立磁盘分区

#格式化分区
root@zabbix:~# fdisk /dev/sdb


Welcome to fdisk (util-linux 2.27.1).
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.
Created a new DOS disklabel with disk identifier 0xa76ea849.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p  #选择主分区

Partition number (1-4, default 1): 1  #将整个磁盘作为一个分区

First sector (2048-83886079, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-83886079, default 83886079): 

Created a new partition 1 of type 'Linux' and of size 40 GiB.

Command (m for help): w  

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

3.查看新分区

root@zabbix:~# fdisk -l |grep 40G

/dev/sdb1        2048 83886079 83884032  40G 83 Linux

4.将分区类型更改为8e-逻辑卷分区

root@zabbix:~# fdisk /dev/sdb


Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): t
Selected partition 1
Partition type (type L to list all types): L  #列出分区所有类型


 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            
Partition type (type L to list all types): 8e #选择8e创建LVM

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

5.查看修改为逻辑卷的新分区

root@zabbix:~# fdisk -l |grep 40G
/dev/sdb1        2048 83886079 83884032  40G 8e Linux LVM

6.创建物理卷PV

root@zabbix:~# pvcreate /dev/sdb1 
  Physical volume "/dev/sdb1" successfully created

7.查看当前物理卷

#当前系统共存在两个PV,原有的PV为zabbix-vg,新PV是/dev/sdb1
root@zabbix:~# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               zabbix-vg
  PV Size               29.52 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              7557
  Free PE               0
  Allocated PE          7557
  PV UUID               GCeSY1-svqb-LB7e-WkKB-4lf3-BZbo-LgfgoK

  "/dev/sdb1" is a new physical volume of "40.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               40.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               fvkxeN-6673-aE67-YEPy-eeQ9-vgyF-JDf30c

8.将新建立的PV /dev/sdb1 扩展到原有的VG-zabbix-vg上

root@zabbix:~# vgextend zabbix-vg /dev/sdb1
  Volume group "zabbix-vg" successfully extended

root@zabbix:~# pvscan 
  PV /dev/sda5   VG zabbix-vg       lvm2 [29.52 GiB / 0    free]
  PV /dev/sdb1   VG zabbix-vg       lvm2 [40.00 GiB / 40.00 40.00 GiB free]
  #PV总大小得到了增加
  Total: 2 [69.52 GiB] / in use: 2 [69.52 GiB] / in no VG: 0 [0   ]

9.给根目录的LV增加39G的空间

root@zabbix:~# lvextend -L +39G -n /dev/zabbix-vg/root
  Size of logical volume zabbix-vg/root changed from 25.52 GiB (6533 extents) to 64.52 GiB (16517 extents).
  Logical volume root successfully resized.

10.重新计算一下LV的空间

root@zabbix:~# resize2fs /dev/zabbix-vg/root
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/zabbix-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 5
The filesystem on /dev/zabbix-vg/root is now 16913408 (4k) blocks long.

11.查看根目录已扩容

root@zabbix:~# df -h
Filesystem                   Size  Used Avail Use% Mounted on
udev                         2.0G     0  2.0G   0% /dev
tmpfs                        396M   11M  385M   3% /run
/dev/mapper/zabbix--vg-root   64G   24G   37G  40% /
tmpfs                        2.0G     0  2.0G   0% /dev/shm
tmpfs                        5.0M     0  5.0M   0% /run/lock
tmpfs                        2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1                    472M  212M  237M  48% /boot

12.查看LV空间

root@zabbix:~# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/zabbix-vg/root
  LV Name                root
  VG Name                zabbix-vg
  LV UUID                eNEjaU-Xe7K-7fo0-tG8n-Sdzf-36YP-RfOJ4W
  LV Write Access        read/write
  LV Creation host, time zabbix, 2018-04-13 11:49:04 +0800
  LV Status              available
  # open                 1
  LV Size                64.52 GiB
  Current LE             16517
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

  --- Logical volume ---
  LV Path                /dev/zabbix-vg/swap_1
  LV Name                swap_1
  VG Name                zabbix-vg
  LV UUID                IwM0yO-FrSe-fHsD-03RQ-KT10-kFtU-9vX5vB
  LV Write Access        read/write
  LV Creation host, time zabbix, 2018-04-13 11:49:04 +0800
  LV Status              available
  # open                 2
  LV Size                4.00 GiB
  Current LE             1024
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1

13.重启mysql和zabbix server服务成功

root@zabbix:~# /etc/init.d/mysql restart
[ ok ] Restarting mysql (via systemctl): mysql.service.
root@zabbix:~# /etc/init.d/zabbix-server restart
[ ok ] Restarting zabbix-server (via systemctl): zabbix-server.service.

14.通过grafana查看剩余空间已为40G

image

作者:Shark1985