前言
在单机时代,采用单块磁盘进行数据存储和读写的方式,由于寻址和读写的时间消耗,导致I/O性能非常低,且存储容量还会受到限制。另外,单块磁盘极其容易出现物理故障,经常导致数据的丢失。因此大家就在想,有没有一种办法将多块独立的磁盘结合在一起组成一个技术方案,来提高数据的可靠性和I/O性能呢。
在这种情况下,RAID技术就应运而生了。
1、RAID是什么
RAID ( Redundant Array of Independent Disks )即独立磁盘冗余阵列,简称为「磁盘阵列」,其实就是用多个独立的磁盘组成在一起形成一个大的磁盘系统,从而实现比单块磁盘更好的存储性能和更高的可靠性。
2、常见的RAID有哪些
-
RAID0
-
RAID1
-
RAID5
-
RAID6
-
RAID10
RAID0
俗称“条带”,它将两个或多个硬盘组成一个逻辑硬盘,每个硬盘最好大小相同,容量是所有硬盘之和,因为是多个硬盘组合成一个,故可并行写操作,写入速度提高,但此方式硬盘数据没有冗余,没有容错,一旦一个物理硬盘损坏,则所有数据均丢失。因而,RAID0 适合于对数据量大,但安全性要求不高的场景,比如音像、视频文件的存储等。
工作原理
RAID管理工具
Linux内核中有一个md(multiple devices)模块在底层管理RAID设备,它会在应用层给我们提供一个应用程序的工具mdadm ,mdadm是linux下用于创建和管理软件RAID的命令。
mdadm命令常见参数解释:
常用参数
RAID0搭建实验(软RAID)
- 添加两块硬盘sda和sdb
[root@yaoguang ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
sdb 8:16 0 20G 0 disk
- 创建一个级别为0新的磁盘列阵 /dev/md0
[root@yaoguang ~]# mdadm -C -v /dev/md0 -l 0 -n 2 /dev/sda /dev/sdb
mdadm: chunk size defaults to 512K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
- 查看相关信息
[root@yaoguang ~]# mdadm -Ds
ARRAY /dev/md0 metadata=1.2 name=yaoguang:0 UUID=6c29ef17:0f0def7e:0561705d:fdecc6c0
- 生成配置文件并查看详细信息
[root@yaoguang ~]# mdadm -Ds > /etc/mdadm.conf
[root@yaoguang ~]# cat /etc/mdadm.conf
ARRAY /dev/md0 metadata=1.2 name=yaoguang:0 UUID=6c29ef17:0f0def7e:0561705d:fdecc6c0
[root@yaoguang ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Thu Mar 3 21:57:14 2022
Raid Level : raid0
Array Size : 41908224 (39.97 GiB 42.91 GB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
Update Time : Thu Mar 3 21:57:14 2022
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
Layout : -unknown-
Chunk Size : 512K
Consistency Policy : none
Name : yaoguang:0 (local to host yaoguang)
UUID : 6c29ef17:0f0def7e:0561705d:fdecc6c0
Events : 0
Number Major Minor RaidDevice State
0 8 0 0 active sync /dev/sda
1 8 16 1 active sync /dev/sdb
- 对md0进行分区
[root@yaoguang ~]# fdisk /dev/md0
Welcome to fdisk (util-linux 2.32.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 0x39b83339.
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):
First sector (2048-83816447, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-83816447, default 83816447):
Created a new partition 1 of type 'Linux' and of size 40 GiB.
Command (m for help): p
Disk /dev/md0: 40 GiB, 42914021376 bytes, 83816448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 524288 bytes / 1048576 bytes
Disklabel type: dos
Disk identifier: 0x39b83339
Device Boot Start End Sectors Size Id Type
/dev/md0p1 2048 83816447 83814400 40G 83 Linux
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
- 格式化
[root@yaoguang ~]# mkfs -t xfs /dev/md0p1
log stripe unit (524288 bytes) is too large (maximum is 256KiB)
log stripe unit adjusted to 32KiB
meta-data=/dev/md0p1 isize=512 agcount=16, agsize=654720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=10475520, imaxpct=25
= sunit=128 swidth=256 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=5120, version=2
= sectsz=512 sunit=8 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
- 挂载
[root@yaoguang ~]# mkdir /mnt/raid0
[root@yaoguang ~]# mount /dev/md0p1 /mnt/raid0
[root@yaoguang ~]# df | tail -1
/dev/md0p1 41881600 325840 41555760 1% /mnt/raid0
- 设置开机自动挂载
[root@yaoguang ~]# vim /etc/fstab
[root@yaoguang ~]# umount /mnt/raid0
[root@yaoguang ~]# df | tail -1
tmpfs 379856 0 379856 0% /run/user/0
[root@yaoguang ~]# mount -av
/ : ignored
/boot : already mounted
none : ignored
/mnt/raid0 : successfully mounted