likes
comments
collection
share

分布式数据库使用逻辑卷管理存储之扩容

作者站长头像
站长
· 阅读数 24

浪潮云溪数据库作为一款浪潮自主研发的国产分布式数据库,一大特性便是可支持 PB 级的数据服务。从存储层面来看,云溪数据库不仅可以使用物理磁盘,也可以使用逻辑卷的方式来管理存储。使用逻辑卷,主要有以下两大场景:

(1)当业务上使用大容量的磁盘,单个物理磁盘不能满足需求时。例如:我们需要在 /data 下挂载 30TB 的存储,而单个磁盘没有这么大的容量。如果使用逻辑卷,将多个小容量的磁盘组合成一个卷组,聚合为一个大的逻辑磁盘,即能满足需求。

(2)当数据量不确定,后期需要扩展磁盘时。在业务初期规划磁盘时,不能完全知道需要分配多少磁盘空间。若使用物理卷,后期则无法扩展。如果使用逻辑卷,可以根据后期的需求量,手动扩展和收缩。

- 基本概念 -

逻辑卷是使用逻辑卷组管理 (Logic Volume Manager) 创建出来的设备,如果要了解逻辑卷,那么首先需要了解逻辑卷管理中的一些概念。

  1. 逻辑卷管理器(Logical Volume Manager,LVM):LVM 将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,相对于普通的磁盘分区有很大的灵活性。LVM 使系统管理员可以更方便的为应用与用户分配存储空间。
  2. 物理卷(Physical Volume,PV):指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如 RAID),是 LVM 的基本存储逻辑块。
  3. 卷组(Volume Group,VG):PV 的集合。是由一个或多个物理卷所组成的存储池,在卷组上能创建一个或多个逻辑卷。
  4. 逻辑卷(Logic Volume,LV):VG 中画出来的一块逻辑磁盘。类似于非 LVM 系统中的硬盘分区,它建立在卷组之上,是一个标准的块设备,在逻辑卷之上可以建立文件系统。

物理磁盘或者磁盘分区转换为物理卷,一个或多个物理卷聚集形成一个或多个卷组,而逻辑卷就是从某个卷组里面抽象出来的一块磁盘空间。具体架构如下:

分布式数据库使用逻辑卷管理存储之扩容

- 创建 LVM -

1. 首先通过 fdisk -l 或 lsblk 查看磁盘的属性,找到要添加的磁盘名称。

vdc 是手动挂载到虚机上的一块磁盘,执行命令如下:

root@newsqltest:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    252:0    0   40G  0 disk 
|-vda1 252:1    0  571M  0 part /boot
`-vda2 252:2    0 39.5G  0 part /
vdb    252:16   0   64M  0 disk 
root@newsqltest:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    252:0    0   40G  0 disk 
|-vda1 252:1    0  571M  0 part /boot
`-vda2 252:2    0 39.5G  0 part /
vdb    252:16   0   64M  0 disk 
vdc    252:32   0   10G  0 disk 
root@newsqltest:~# fdisk -l
Disk /dev/vda: 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
Disklabel type: dos
Disk identifier: 0x9b600047

Device     Boot   Start      End  Sectors  Size Id Type
/dev/vda1  *       2048  1171455  1169408  571M 83 Linux
/dev/vda2       1171456 83886046 82714591 39.5G 83 Linux


Disk /dev/vdb: 64 MiB, 67108864 bytes, 131072 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
Disklabel type: dos
Disk identifier: 0x00000000


Disk /dev/vdc: 10 GiB, 10737418240 bytes, 20971520 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. 使用 fdisk 将磁盘进行逻辑分区

fdisk /dev/vdc

m 来查看命令帮助;

n 添加一张新的 partition,划分分区,以及分区的大小,这里都使用默认值,创建一个分区;

w 命令保存并退出。

root@newsqltest:~# fdisk /dev/vdc

Welcome to fdisk (util-linux 2.31.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 0xbb7719f5.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): 

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

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
root@newsqltest:~# fdisk -l
......
Device     Boot Start      End  Sectors Size Id Type
/dev/vdc1        2048 20971519 20969472  10G 83 Linux

3. 使用 lvm 来管理这个分区

1). 首先需要安装 lvm

apt-get install lvm2

2). 创建 Physical Volume(PV)

pvcreate /dev/vdc1

成功之后可以通过 pvdisplay 查看信息

root@newsqltest:~# pvcreate /dev/vdc1
  Physical volume "/dev/vdc1" successfully created.
root@newsqltest:~# pvdisplay
  "/dev/vdc1" is a new physical volume of "<10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdc1
  VG Name               
  PV Size               <10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               5FxevL-aneV-Xhe6-pFEv-b3pH-l4v4-Gb6TeA

3). 创建 Volume Group (VG).

vgcreate vg-newsql /dev/vdc1

root@newsqltest:~# vgcreate vg-newsql /dev/vdc1
  Volume group "vg-newsql" successfully created
root@newsqltest:~# vgscan
  Reading volume groups from cache.
  Found volume group "vg-newsql" using metadata type lvm2

4). 创建 Logical Volumes (LV).

lvcreate -n lv-newsql -l 100% VG vg-newsql (将所有的 vg-newsql 的内存都给这个 lv-newsql 逻辑卷)

root@newsqltest:~# lvcreate -n lv-newsql -l 100%VG vg-newsql
  Logical volume "lv-newsql" created.
root@newsqltest:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg-newsql/lv-newsql
  LV Name                lv-newsql
  VG Name                vg-newsql
  LV UUID                bMsyAX-xatH-cdTY-JbzE-4ao5-QR6h-j4Z7ON
  LV Write Access        read/write
  LV Creation host, time newsqltest, 2021-01-27 16:09:54 +0800
  LV Status              available
  # open                 0
  LV Size                <10.00 GiB
  Current LE             2559
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

4. 格式化并挂载逻辑卷至 /data

1). 第一步格式化逻辑卷

mkfs.ext4 /dev/vg-newsql/lv-newsql

2). 将逻辑卷挂载到 home 文件夹下面

mount /dev/vg-newsql/lv-newsql /data

root@newsqltest:~# mkfs.ext4 /dev/vg-newsql/lv-newsql
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 2620416 4k blocks and 655360 inodes
Filesystem UUID: a667f950-9b91-45ce-9931-8fb147173054
Superblock backups stored on blocks: 
  32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done 

root@newsqltest:~# mount /dev/vg-newsql/lv-newsql /data
mount: /data: mount point does not exist.
root@newsqltest:~# mkdir /data
root@newsqltest:~# mount /dev/vg-newsql/lv-newsql /data
root@newsqltest:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               3.9G     0  3.9G   0% /dev
tmpfs                              798M  3.2M  795M   1% /run
/dev/vda2                           39G  2.8G   35G   8% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda1                          547M   77M  430M  16% /boot
tmpfs                              798M     0  798M   0% /run/user/0
/dev/mapper/vg--newsql-lv--newsql  9.8G   37M  9.3G   1% /data

至此,10G 的盘已卷组的方式挂载至 /data 目录。

- 扩容 -

1. 添加磁盘:

root@newsqltest:~# lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda                       252:0    0   40G  0 disk 
|-vda1                    252:1    0  571M  0 part /boot
`-vda2                    252:2    0 39.5G  0 part /
vdb                       252:16   0   64M  0 disk 
vdc                       252:32   0   10G  0 disk 
`-vdc1                    252:33   0   10G  0 part 
`-vg--newsql-lv--newsql 253:0    0   10G  0 lvm  /data
vdd                       252:48   0   10G  0 disk

2. 格式化磁盘

1). 当磁盘大小小于 2TB 的时候,与创建 LVM 的第 2 步骤相同:

fdisk /dev/vdd

m 来查看命令帮助

n 添加一张新的 partition,划分分区,以及分区的大小,这里我都使用默认值,创建一个分区

w 命令保存并退出

2)当大于 2TB 的时候,使用如下命令:

a. 编辑 /dev/sda 磁盘

parted /dev/vde

b. 设立磁盘分区类型

mklabel gpt

c. 设置默认单位为 TB、GB

unit TB

d. 创建分区大小

mkpart primary 0 0 or mkpart primary 0.00TB 2.10TB

e. quit

f. 格式化磁盘

mkfs.ext4 /dev/vde

root@newsqltest:~# parted /dev/vde
GNU Parted 3.2
Using /dev/vde
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help                                                             
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
(parted) mklabel gpt                                                      
(parted) unit TB
(parted) mkpart primary 0 0 or mkpart primary 0.00TB 2.10TB               
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
Warning: You requested a partition from 0.00TB to 2.10TB (sectors 0..4101562500).
The closest location we can manage is 0.00TB to 0.00TB (sectors 34..2047).
Is this still acceptable to you?
Yes/No? yes                                                               
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? ignore                                                     
(parted) quit                                                             
Information: You may need to update /etc/fstab.
root@newsqltest:~# mkfs.ext4 /dev/vde                                     
mke2fs 1.44.1 (24-Mar-2018)
Found a gpt partition table in /dev/vde
Proceed anyway? (y,N) y
Creating filesystem with 550502400 4k blocks and 137625600 inodes
Filesystem UUID: a08b03f2-05b7-4029-b317-0e07f93fb6d2
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
102400000, 214990848, 512000000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done 

3. 创建 pv 卷, 与使用 lvm 来管理这个分区中创建 Physical Volume 的步骤相同:

pvcreate /dev/vdd1

4. 扩展物理硬盘到同一个卷组里面,每个组管理不同物理磁盘:

vgextend vg-newsql /dev/vdd1

root@newsqltest:~# vgextend vg-newsql /dev/vdd1
  Volume group "vg-newsql" successfully extended

5. 从卷组里面向逻辑卷里面分配空间:

lvextend -l +100% FREE /dev/mapper/vg--newsql-lv--newsql(向逻辑卷里面分配空间)

root@newsqltest:~# lvextend -l +100%FREE /dev/mapper/vg--newsql-lv--newsql
  Size of logical volume vg-newsql/lv-newsql changed from <10.00 GiB (2559 extents) to 19.99 GiB (5118 extents).
  Logical volume vg-newsql/lv-newsql successfully resized.

6. 重启逻辑卷使其生效:

resize2fs /dev/mapper/vg--newsql-lv--newsql

root@newsqltest:~# resize2fs /dev/mapper/vg--newsql-lv--newsql
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/mapper/vg--newsql-lv--newsql is mounted on /data; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/mapper/vg--newsql-lv--newsql is now 5240832 (4k) blocks long.

7. 查看:

df -h 当发现该逻辑卷(resize2fs /dev/mapper/vg--newsql-lv--newsql)的空间增大时,表示添加成功。

root@newsqltest:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               3.9G     0  3.9G   0% /dev
tmpfs                              798M  3.2M  795M   1% /run
/dev/vda2                           39G  2.8G   35G   8% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda1                          547M   77M  430M  16% /boot
/dev/mapper/vg--newsql-lv--newsql   20G   44M   19G   1% /data