Centos 7搭建Raid5案例

介绍:

RAID5:分布式奇偶校验的独立磁盘结构

它的奇偶校验码存在于所有磁盘上。RAID5的读出效率很高,写入效率一般,块式的集体访问效率不错。因为奇偶校验码在不同的磁盘上,所以提高了可靠性。但是它对数据传输的并行性解决不好,而且控制器的设计也相当困难。在RAID 5中有“写损失”,即每一次写操作,将产生四个实际的读/写操作,其中两次读旧的数据及奇偶信息,两次写新的数据及奇偶信息。

RAID0大幅度提升了设备的读写性能,但不具备容错能力。RAID1虽然十分注重数据安全,但磁盘利用率太低。RAID5就是raid0和RAID5的一种折中,既提升了磁盘读写能力,又有一定的容错能力,成本也低。RAID10就是RAID0和raid1的组合,大幅度提升读写能力,较强的容错能力,成本也较高。一般中小企业用RAID5,大企业采用RAID10。

一、添加硬盘

1.编辑设置

Centos 7搭建Raid5案例_第1张图片

2.创建新的虚拟磁盘

Centos 7搭建Raid5案例_第2张图片

3.根据实际需求设置磁盘大小

Centos 7搭建Raid5案例_第3张图片

4.默认

Centos 7搭建Raid5案例_第4张图片

5.点击完成

Centos 7搭建Raid5案例_第5张图片

6.同样方法创建四个新的硬盘

Centos 7搭建Raid5案例_第6张图片

二、重启主机并对磁盘进行初始化设置

1、添加磁盘立即查看是看不到的,需要重启主机

[root@localhost ~]# fdisk -l|grep sd
Disk /dev/sda: 17.2 GB, 17179869184 bytes, 33554432 sectors
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    33554431    15727616   8e  Linux LVM
[root@localhost ~]# reboot

2、查看磁盘情况,发现四块硬盘被检测到了

[root@localhost ~]# fdisk -l|grep sd
Disk /dev/sdc: 1073 MB, 1073741824 bytes, 2097152 sectors
Disk /dev/sdd: 1073 MB, 1073741824 bytes, 2097152 sectors
Disk /dev/sda: 17.2 GB, 17179869184 bytes, 33554432 sectors
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    33554431    15727616   8e  Linux LVM
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Disk /dev/sde: 1073 MB, 1073741824 bytes, 2097152 sectors

3、磁盘初始化设置

[root@localhost ~]# 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 0x416e5dd3.

Command (m for help): n    #创建一个分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)    #p是主分区,e是扩展分区
   e   extended
Select (default p): p    #选择创建主分区
Partition number (1-4, default 1):     #默认主分区号为1
First sector (2048-2097151, default 2048):     #起始位置默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151):    #raid5要用到整块磁盘,默认就是整块磁盘。选择默认即可。 
Using default value 2097151
Partition 1 of type Linux and of size 1023 MiB is set

Command (m for help): t    #修改分区类型
Selected partition 1    #因为只有一个主分区,所以自动选择1
Hex code (type L to list all codes): 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 C:  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 eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): fd    #选择类型
Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): w    #保存退出
The partition table has been altered!

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

4、检查分区修改情况(硬盘被分为一个主分区,类型位raid)

[root@localhost ~]# fdisk -l|grep sdb
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
/dev/sdb1            2048     2097151     1047552   fd  Linux raid autodetect

5、以同样的操作创建剩余磁盘,然后再查看

[root@localhost ~]# fdisk -l|grep sd[b-e]
Disk /dev/sdc: 1073 MB, 1073741824 bytes, 2097152 sectors
/dev/sdc1            2048     2097151     1047552   fd  Linux raid autodetect
Disk /dev/sdd: 1073 MB, 1073741824 bytes, 2097152 sectors
/dev/sdd1            2048     2097151     1047552   fd  Linux raid autodetect
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
/dev/sdb1            2048     2097151     1047552   fd  Linux raid autodetect
Disk /dev/sde: 1073 MB, 1073741824 bytes, 2097152 sectors
/dev/sde1            2048     2097151     1047552   fd  Linux raid autodetect

三、搭建raid5

1、安装madam(mdadm是linux下的raid管理工具)

[root@localhost ~]# yum install -y mdadm

关于mdadm的相关参数汇总:

-C  --create   创建阵列;
-a  --auto    同意创建设备,如不加此参数时必须先使用mknod 命令来创建一个RAID设备,不过推荐使用-a yes参数一次性创建;
-l   --level   阵列模式,支持的阵列模式有 linear, raid0, raid1, raid4, raid5, raid6, raid10, multipath, faulty, container;
-n     --raid-devices    阵列中活动磁盘的数目,该数目加上备用磁盘的数目应该等于阵列中总的磁盘数目;
-x    热备盘数目

-D  --detail  查看raid设备的详细信息
-f  使一块raid磁盘故障
-a  增加一块raid磁盘
-r  移除一块故障的raid磁盘
-s  --scan:扫描配置文件或 /proc/mdstat以搜寻丢失的信息
-S  停止raid磁盘阵列

2、创建阵列(用sdb1,sdc1,sdd1,sde1四块磁盘其中3块创建raid5,名称为md0,另外一块作为热备份)

[root@localhost ~]# mdadm -C /dev/md0 -ayes -l5 -n3 -x1 /dev/sd[b-e]1
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

3、查看raid5状态

[root@localhost ~]# cat /proc/mdstat 
Personalities : [raid6] [raid5] [raid4]     #三块硬盘组成raid5阵列,一块磁盘作为热备份
md0 : active raid5 sdd1[4] sde1[3](S) sdc1[1] sdb1[0]
      2091008 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]    #三块硬盘三块都是完好的
      
unused devices: 
[root@localhost ~]# mdadm -D /dev/md0 
/dev/md0:
           Version : 1.2
     Creation Time : Wed Aug 21 03:13:08 2019
        Raid Level : raid5        #阵列类型为raid5
        Array Size : 2091008 (2042.00 MiB 2141.19 MB)
     Used Dev Size : 1045504 (1021.00 MiB 1070.60 MB)
      Raid Devices : 3
     Total Devices : 4
       Persistence : Superblock is persistent

       Update Time : Wed Aug 21 03:14:37 2019
             State : clean 
    Active Devices : 3            #活跃的磁盘数目
   Working Devices : 4            #所有的磁盘数目
    Failed Devices : 0            #故障的磁盘数目
     Spare Devices : 1            #热备份磁盘数目

            Layout : left-symmetric
        Chunk Size : 512K

Consistency Policy : resync

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : 1842de96:7b3a5c19:2f2e8315:2de281b0
            Events : 18

    Number   Major   Minor   RaidDevice State                 #三款磁盘组成raid5
       0       8       17        0      active sync   /dev/sdb1
       1       8       33        1      active sync   /dev/sdc1
       4       8       49        2      active sync   /dev/sdd1

       3       8       65        -      spare   /dev/sde1      #一块作为热备份

4、添加raid5到raid配置文件并做修改

[root@localhost ~]# echo 'DEVICE /dev/sd[b-e]1'>> /etc/mdadm.conf
[root@localhost ~]# mdadm -Ds >>/etc/mdadm.conf
[root@localhost ~]# cat /etc/mdadm.conf 
DEVICE /dev/sd[b-e]1
ARRAY /dev/md0 metadata=1.2 spares=1 name=localhost.localdomain:0 UUID=1842de96:7b3a5c19:2f2e8315:2de281b0

5、格式化磁盘阵列

[root@localhost ~]# mkfs.xfs /dev/md0
meta-data=/dev/md0               isize=512    agcount=8, agsize=65408 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=522752, imaxpct=25
         =                       sunit=128    swidth=256 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=8 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

6、建立挂载点并挂载

[root@localhost ~]# mkdir /raid5
[root@localhost ~]# mount /dev/md0  /raid5/
[root@localhost ~]# df 
Filesystem              1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root  14034944 1225416  12809528   9% /
devtmpfs                  8121264       0   8121264   0% /dev
tmpfs                     8133376       0   8133376   0% /dev/shm
tmpfs                     8133376    9032   8124344   1% /run
tmpfs                     8133376       0   8133376   0% /sys/fs/cgroup
/dev/sda1                 1038336  148500    889836  15% /boot
tmpfs                     1626676       0   1626676   0% /run/user/0
/dev/md0                  2080768   33184   2047584   2% /raid5    #挂载成功

7、将挂载写入开机启动项

[root@localhost ~]# vim /etc/fstab
在最后一行添加
/dev/md0		/raid5			xfs	defaults	0 0

四、测试raid5

1、建立文件a.txt和b.txt

[root@localhost ~]# cd /raid5/
[root@localhost raid5]# touch a.txt
[root@localhost raid5]# echo 'you are the clever boy' >>b.txt
[root@localhost raid5]# ll
total 4
-rw-r--r--. 1 root root  0 Aug 21 03:30 a.txt
-rw-r--r--. 1 root root 23 Aug 21 03:30 b.txt

2、模拟其中一块磁盘有坏道,手动使sdb1故障,然后查看raid阵列信息情况,发现热备sde1自动替换已故障的sdb1。并且资料无损

[root@localhost raid5]# mdadm /dev/md0 -f /dev/sdb1
mdadm: set /dev/sdb1 faulty in /dev/md0
[root@localhost raid5]# mdadm -D /dev/md0 
/dev/md0:
           Version : 1.2
     Creation Time : Wed Aug 21 03:13:08 2019
        Raid Level : raid5
        Array Size : 2091008 (2042.00 MiB 2141.19 MB)
     Used Dev Size : 1045504 (1021.00 MiB 1070.60 MB)
      Raid Devices : 3
     Total Devices : 4
       Persistence : Superblock is persistent

       Update Time : Wed Aug 21 03:44:56 2019
             State : clean, degraded, recovering 
    Active Devices : 2
   Working Devices : 3
    Failed Devices : 1                #故障硬盘
     Spare Devices : 1

            Layout : left-symmetric
        Chunk Size : 512K

Consistency Policy : resync

    Rebuild Status : 6% complete        #重建进度

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : 1842de96:7b3a5c19:2f2e8315:2de281b0
            Events : 21

    Number   Major   Minor   RaidDevice State
       3       8       65        0      spare rebuilding   /dev/sde1
       1       8       33        1      active sync   /dev/sdc1
       4       8       49        2      active sync   /dev/sdd1

       0       8       17        -      faulty   /dev/sdb1

重建完成

[root@localhost raid5]# mdadm -D /dev/md0 
/dev/md0:
           Version : 1.2
     Creation Time : Wed Aug 21 03:13:08 2019
        Raid Level : raid5
        Array Size : 2091008 (2042.00 MiB 2141.19 MB)
     Used Dev Size : 1045504 (1021.00 MiB 1070.60 MB)
      Raid Devices : 3
     Total Devices : 4
       Persistence : Superblock is persistent

       Update Time : Wed Aug 21 03:46:59 2019
             State : clean 
    Active Devices : 3
   Working Devices : 3
    Failed Devices : 1
     Spare Devices : 0

            Layout : left-symmetric
        Chunk Size : 512K

Consistency Policy : resync

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : 1842de96:7b3a5c19:2f2e8315:2de281b0
            Events : 37

    Number   Major   Minor   RaidDevice State
       3       8       65        0      active sync   /dev/sde1
       1       8       33        1      active sync   /dev/sdc1
       4       8       49        2      active sync   /dev/sdd1

       0       8       17        -      faulty   /dev/sdb1        #故障磁盘

3、移除已故障的磁盘,添加一个新的硬盘作为热备份盘,并查看数据情况

[root@localhost raid5]# mdadm /dev/md0 -r /dev/sdb1
mdadm: hot removed /dev/sdb1 from /dev/md0
[root@localhost raid5]# mdadm -D /dev/md0 
/dev/md0:
           Version : 1.2
     Creation Time : Wed Aug 21 03:13:08 2019
        Raid Level : raid5
        Array Size : 2091008 (2042.00 MiB 2141.19 MB)
     Used Dev Size : 1045504 (1021.00 MiB 1070.60 MB)
      Raid Devices : 3
     Total Devices : 3
       Persistence : Superblock is persistent

       Update Time : Wed Aug 21 03:53:53 2019
             State : clean 
    Active Devices : 3
   Working Devices : 3
    Failed Devices : 0
     Spare Devices : 0

            Layout : left-symmetric
        Chunk Size : 512K

Consistency Policy : resync

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : 1842de96:7b3a5c19:2f2e8315:2de281b0
            Events : 38

    Number   Major   Minor   RaidDevice State        #故障的硬盘已被移除
       3       8       65        0      active sync   /dev/sde1
       1       8       33        1      active sync   /dev/sdc1
       4       8       49        2      active sync   /dev/sdd1

4、添加一块硬盘作为热备份盘(注意:在实际中sdb1已经是另一块完好的硬盘了。不是说之前损坏的那块移除再添加就好了)

[root@localhost raid5]# mdadm /dev/md0 -a /dev/sdb1
mdadm: added /dev/sdb1
[root@localhost raid5]# mdadm -D /dev/md0 
/dev/md0:
           Version : 1.2
     Creation Time : Wed Aug 21 03:13:08 2019
        Raid Level : raid5
        Array Size : 2091008 (2042.00 MiB 2141.19 MB)
     Used Dev Size : 1045504 (1021.00 MiB 1070.60 MB)
      Raid Devices : 3
     Total Devices : 4
       Persistence : Superblock is persistent

       Update Time : Wed Aug 21 03:56:39 2019
             State : clean 
    Active Devices : 3
   Working Devices : 4
    Failed Devices : 0
     Spare Devices : 1

            Layout : left-symmetric
        Chunk Size : 512K

Consistency Policy : resync

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : 1842de96:7b3a5c19:2f2e8315:2de281b0
            Events : 39

    Number   Major   Minor   RaidDevice State
       3       8       65        0      active sync   /dev/sde1
       1       8       33        1      active sync   /dev/sdc1
       4       8       49        2      active sync   /dev/sdd1

       5       8       17        -      spare   /dev/sdb1

5、查看数据还是正常的

[root@localhost raid5]# ll
total 4
-rw-r--r--. 1 root root  0 Aug 21 03:30 a.txt
-rw-r--r--. 1 root root 23 Aug 21 03:30 b.txt
[root@localhost raid5]# cat b.txt 
you are the clever boy

五、维护raid5

1、向raid5中添加存储硬盘

[root@localhost ~]# mdadm /dev/md0 -a /dev/sdf1
mdadm: added /dev/sdf1
[root@localhost ~]# mdadm -D /dev/md0 
/dev/md0:
           Version : 1.2
     Creation Time : Wed Aug 21 03:13:08 2019
        Raid Level : raid5
        Array Size : 2091008 (2042.00 MiB 2141.19 MB)
     Used Dev Size : 1045504 (1021.00 MiB 1070.60 MB)
      Raid Devices : 3
     Total Devices : 5
       Persistence : Superblock is persistent

       Update Time : Wed Aug 21 04:02:05 2019
             State : clean 
    Active Devices : 3
   Working Devices : 5
    Failed Devices : 0
     Spare Devices : 2

            Layout : left-symmetric
        Chunk Size : 512K

Consistency Policy : resync

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : 1842de96:7b3a5c19:2f2e8315:2de281b0
            Events : 40

    Number   Major   Minor   RaidDevice State
       3       8       65        0      active sync   /dev/sde1
       1       8       33        1      active sync   /dev/sdc1
       4       8       49        2      active sync   /dev/sdd1

       5       8       17        -      spare   /dev/sdb1
       6       8       81        -      spare   /dev/sdf1        #默认添加为热备份盘

2、由于默认是添加成为热备盘,得把热备盘改为活动盘(-G改变raid中的活动盘的数量,一般和-n连用。-n用来指定raid中活动盘的数量。最好保证要有足够的热备盘添加。)

[root@localhost ~]# mdadm /dev/md0 -G -n4
[root@localhost ~]# mdadm -D /dev/md0 
/dev/md0:
           Version : 1.2
     Creation Time : Wed Aug 21 03:13:08 2019
        Raid Level : raid5
        Array Size : 2091008 (2042.00 MiB 2141.19 MB)
     Used Dev Size : 1045504 (1021.00 MiB 1070.60 MB)
      Raid Devices : 4
     Total Devices : 5
       Persistence : Superblock is persistent

       Update Time : Wed Aug 21 04:04:27 2019
             State : clean, reshaping    #等构建完成这里只会显示clean
    Active Devices : 4                   #活动盘数量
   Working Devices : 5                   #总数量
    Failed Devices : 0                   #故障数量
     Spare Devices : 1                   #热备盘数量

            Layout : left-symmetric
        Chunk Size : 512K

Consistency Policy : resync

    Reshape Status : 5% complete            #等待进度跑完即可
     Delta Devices : 1, (3->4)

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : 1842de96:7b3a5c19:2f2e8315:2de281b0
            Events : 54

    Number   Major   Minor   RaidDevice State    #此时raid5已经有四块活动盘
       3       8       65        0      active sync   /dev/sde1
       1       8       33        1      active sync   /dev/sdc1
       4       8       49        2      active sync   /dev/sdd1
       6       8       81        3      active sync   /dev/sdf1

       5       8       17        -      spare   /dev/sdb1

3、查看文件系统发现总容量未变(resizefx适用于ext3,ext4等文件系统不适用于xfs文件系统)

[root@localhost ~]# df -lh
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   14G  1.2G   13G   9% /
devtmpfs                 7.8G     0  7.8G   0% /dev
tmpfs                    7.8G     0  7.8G   0% /dev/shm
tmpfs                    7.8G  8.9M  7.8G   1% /run
tmpfs                    7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/sda1               1014M  146M  869M  15% /boot
/dev/md0                 2.0G   33M  2.0G   2% /raid5
tmpfs                    1.6G     0  1.6G   0% /run/user/0
[root@localhost ~]# resize2fs /dev/md0 
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/md0
Couldn't find valid filesystem superblock.

4、此时需要对文件系统扩容(xfs_growfs对xfs文件系统进行扩容)

[root@localhost ~]# xfs_growfs /dev/md0 
meta-data=/dev/md0               isize=512    agcount=8, agsize=65408 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=522752, imaxpct=25
         =                       sunit=128    swidth=256 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=8 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 522752 to 784128
[root@localhost ~]# df -HT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        15G  1.3G   14G   9% /
devtmpfs                devtmpfs  8.4G     0  8.4G   0% /dev
tmpfs                   tmpfs     8.4G     0  8.4G   0% /dev/shm
tmpfs                   tmpfs     8.4G  9.3M  8.4G   1% /run
tmpfs                   tmpfs     8.4G     0  8.4G   0% /sys/fs/cgroup
/dev/sda1               xfs       1.1G  153M  912M  15% /boot
/dev/md0                xfs       3.3G   35M  3.2G   2% /raid5
tmpfs                   tmpfs     1.7G     0  1.7G   0% /run/user/0

 

 

你可能感兴趣的:(Linux)