Erlo

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

收藏 2022-06-21 12:00:06   48   开源中国
页面报错/反馈
点赞

浪潮云溪数据库作为一款浪潮自主研发的国产分布式数据库,一大特性便是可支持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:~# lsblkNAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTvda    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:~# lsblkNAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTvda    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 -lDisk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk 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 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x00000000

Disk /dev/vdc: 10 GiB, 10737418240 bytes, 20971520 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/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): nPartition 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): wThe 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  1083 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 createdroot@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-newsqlmke2fs 1.44.1 (24-Mar-2018)Creating filesystem with 2620416 4k blocks and 655360 inodesFilesystem UUID: a667f950-9b91-45ce-9931-8fb147173054Superblock backups stored on blocks:   32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done                            Writing inode tables: done                            Creating journal (16384 blocks): doneWriting superblocks and filesystem accounting information: done 
root@newsqltest:~# mount /dev/vg-newsql/lv-newsql /datamount: /data: mount point does not exist.root@newsqltest:~# mkdir /dataroot@newsqltest:~# mount /dev/vg-newsql/lv-newsql /dataroot@newsqltest:~# df -hFilesystem                         Size  Used Avail Use% Mounted onudev                               3.9G     0  3.9G   0% /devtmpfs                              798M  3.2M  795M   1% /run/dev/vda2                           39G  2.8G   35G   8% /tmpfs                              3.9G     0  3.9G   0% /dev/shmtmpfs                              5.0M     0  5.0M   0% /run/locktmpfs                              3.9G     0  3.9G   0% /sys/fs/cgroup/dev/vda1                          547M   77M  430M  16% /boottmpfs                              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:~# lsblkNAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTvda                       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  /datavdd                       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/vdeGNU Parted 3.2Using /dev/vdeWelcome 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 NAMEprint [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 deviceset 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 NAMEprint [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 deviceset 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 PartedWarning: 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/vdeProceed anyway? (y,N) yCreating filesystem with 550502400 4k blocks and 137625600 inodesFilesystem UUID: a08b03f2-05b7-4029-b317-0e07f93fb6d2Superblock 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): doneWriting 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--newsqlresize2fs 1.44.1 (24-Mar-2018)Filesystem at /dev/mapper/vg--newsql-lv--newsql is mounted on /data; on-line resizing requiredold_desc_blocks = 2, new_desc_blocks = 3The 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 -hFilesystem                         Size  Used Avail Use% Mounted onudev                               3.9G     0  3.9G   0% /devtmpfs                              798M  3.2M  795M   1% /run/dev/vda2                           39G  2.8G   35G   8% /tmpfs                              3.9G     0  3.9G   0% /dev/shmtmpfs                              5.0M     0  5.0M   0% /run/locktmpfs                              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
登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认