1-15-1 RAID磁盘阵列的原理和搭建

时间:2023-03-08 20:00:05

大纲:

1.1-1-企业级RAID磁盘阵列

RAID磁盘阵列的原理

RAID0,1,5,10的搭建

硬件RAID卡

1.2-1-使用廉价的磁盘搭建RAID磁盘阵列

实战-配置RAID0带区卷

========================================

1.1 企业级RAID磁盘阵列

RAID磁盘阵列的原理:

RAID(Redundant Array of Independent Disks)独立磁盘冗余阵列

可以提供较普通磁盘更高的速度、安全性,所以服务器在安装时,都会选择创建RAID

RAID有两种创建方式:第一种-->软RAID(通过操作系统软件实现);第二种--->硬RAID(通过硬件阵列卡实现)。

在企业中使用最多的是:raid1、raid10和raid5。不过随着云的高速发展,供应商一般可以把硬件问题解决掉。

RAID磁盘阵列的分类:

raid 阵列有7个基本的级别:0-6,最常用的是0、1、5

不同raid级别的作用、性能、成本是不同的

==========================================

RAID磁盘阵列的工作原理:

raid 0:条带(strping)

需要两块或以上磁盘(最好大小相同)

原理:将数据分段后,分别传输到两块磁盘中。

提高的写入和读取的速度

(即搬运工,原来一个人搬,现在多了一个人,所以速度成倍增加)。

但是由此,每一个磁盘的数据都是很重要的,不能损坏。

特点:成本低、提高整个磁盘性能和吞吐量,即速度快

缺点:没有冗余或错误修复能力

磁盘利用率:100%

任何一个磁盘的损坏将损坏全部数据(分别存储,均分)

1-15-1 RAID磁盘阵列的原理和搭建

1-15-1 RAID磁盘阵列的原理和搭建

==========================================

RAID 1: mirroring(镜像卷)

需要两块及以上磁盘,当两块磁盘大小不一样时,RAID磁盘阵列大小已最小的计算

原理:把一个磁盘中的数据镜像到另一个磁盘(即同步)。

两个磁盘中的内容是完全一致的。

我们还可以准备一个热备盘,当其中一个磁盘短暂暂停工作时,

可以让热备盘顶替使用。在恢复工作后,自动恢复。

特点:有容错能力

缺点:速度慢、占用率低

磁盘利用率:50%

2块100G磁盘组成RAID1,实际使用容量还是100G,而且两块磁盘中数据是相同的

1-15-1 RAID磁盘阵列的原理和搭建

1-15-1 RAID磁盘阵列的原理和搭建

1-15-1 RAID磁盘阵列的原理和搭建

==========================================

RAID 5:

需要三块或以上磁盘

原理:每次存储数据时,数据被平均分配到n-1个磁盘中,随机选中一个磁盘生成校验码。当有一个磁盘坏掉时,还可使用其他磁盘,将这个磁盘中的数据恢复。但若有两个磁盘损坏时,数据将全部损坏。

可以提供热背叛实现故障修复;只损坏一块,没有问题,但是损坏两份,则数据全部损坏

空间利用率:n-1/n

每次存储,随意的会有一个盘存储校验码,校验码为异或运算结果

奇偶校验信息的作用:

当RAID5的一个磁盘数据发生损坏后,利用剩下的数据和相应的奇偶校验信息去恢复被损坏的数据。

扩展异或运算:相对简单的异或运算(相同为0,相异为1)

A

B

Xor

0

0

0

1

0

1

0

1

1

1

1

0

1-15-1 RAID磁盘阵列的原理和搭建

==========================================

RAID 10:镜像+条带(组合)

原理:将RAID1与RAID0 组合在一起使用,

 

RAID 0+1的特点:特别适用于既有大量数据需要存取,同时又对数据安全性要求严格的领域。例如:银行、金融、商业超市、仓库库房、各种档案管理等。

创建步骤:应先创建RAID1,然后使用创建的RAID1设备创建RAID0

1-15-1 RAID磁盘阵列的原理和搭建

==========================================

硬件RAID卡

RAID卡可以分为硬RAID卡和软RAID卡两种。

硬RAID卡:通过硬件来实现RAID功能的就是硬RAID,独立的RAID卡。

主板集成的RAID芯片都是硬RAID。

软RAID卡:通过软件并使用CPU的RAID卡(指使用CPU来完成RAID的常用计算)

软RAID占用CPU资源较高,绝大部分服务器设备是硬件RAID。

Adaptec SATA RAID 2410SA

亚当普特克 ¥550

1-15-1 RAID磁盘阵列的原理和搭建

亚当普特克RAID卡

RAID功能:RAID0、1、5、10、JBOD

接口:Serial ATA

数据传输率:1.5Gb/s 普通硬盘速率:6G

通道数:4

最多连接设备:4

1-15-1 RAID磁盘阵列的原理和搭建

1-15-1 RAID磁盘阵列的原理和搭建

1-15-1 RAID磁盘阵列的原理和搭建

1-15-1 RAID磁盘阵列的原理和搭建

==========================================

管理软raid工具---mdadm

在Linux系统中,我们使用mdadm命令,来管理软RAID

参数

######################

###  mdadm --help  ###

######################

mdadm is used for building, managing, and monitoring

Linux md devices (aka RAID arrays)

Usage: mdadm --create device options...  #创建一个磁盘阵列 –C

            Create a new array from unused devices.

       mdadm --assemble device options...  #激活一个磁盘阵列 –A

            Assemble a previously created array.

       mdadm --build device options...   #创建一个没有metadata的磁盘阵列,很少用

            Create or assemble an array without metadata.

       mdadm --manage device options...  #对已存在的磁盘阵列进行修改

            make changes to an existing array.

       mdadm --misc options... devices  # 报告或修改相关设备

            report on or modify various md related devices.

       mdadm --grow options device  #更新修改的磁盘阵列

            resize/reshape an active array

       mdadm --incremental device  #从一个阵列中添加或移除一个设备(插拔)

            add/remove a device to/from an array as appropriate

       mdadm --monitor options...  #监控重大变化的一个或多个数组。

            Monitor one or more array for significant changes.

       mdadm device options...

            Shorthand for --manage.  #管理阵列使用--manage选项

Any parameter that does not start with '-' is treated as a device name

or, for --examine-bitmap, a file name.

The first such name is often the name of an md device.  Subsequent

names are often names of component devices.

For detailed help on the above major modes use --help after the mode

e.g.

         mdadm --assemble --help

For general help on options use

         mdadm --help-options

##############################

###  mdadm --help-options  ###

##############################

Any parameter that does not start with '-' is treated as a device name

or, for --examine-bitmap, a file name.

The first such name is often the name of an md device.  Subsequent

names are often names of component devices.

Some common options are:

  --help        -h   : General help message or, after above option,

                       mode specific help message

  --help-options     : This help message

  --version     -V   : Print version information for mdadm #版本信息

  --verbose     -v   : Be more verbose about what is happening  #过程信息

  --quiet       -q   : Don't print un-necessary messages #不打印不需要的信息

  --brief       -b   : Be less verbose, more brief #显示简短的过程信息

  --export      -Y   : With --detail, --detail-platform or --examine use

                       key=value format for easy import into environment #

  --force       -f   : Override normal checks and be more forceful #强制执行

  --assemble    -A   : Assemble an array #激活磁盘阵列

  --build       -B   : Build an array without metadata

  --create      -C   : Create a new array

  --detail      -D   : Display details of an array #打印阵列设备详细信息

  --examine     -E   : Examine superblock on an array component ##检测设备块是否在一个阵列数组中

  --examine-bitmap -X: Display the detail of a bitmap file #

  --examine-badblocks: Display list of known bad blocks on device #

  --monitor     -F   : monitor (follow) some arrays #监控

  --grow        -G   : resize/ reshape and array #

  --incremental -I   : add/remove a single device to/from an array as appropriate

  --query       -Q   : Display general information about how a

                       device relates to the md driver

  --auto-detect      : Start arrays auto-detected by the kernel

1-15-1 RAID磁盘阵列的原理和搭建

热备份盘(hot spare or hot standby driver):为了加强容错的功能以及使系统在磁盘故障的情况下能迅速的重建数据,以维持系统的性能,一般的磁盘阵列系统都可使用热备份功能。

Chunk(块): raid存储数据时每个数据段的大小。 4K,64K

chunk(块):每个数据段大小。以K为单位

=============================================================

实战:搭建RAID0磁盘阵列

第一步:添加两块硬盘,并检查

1-15-1 RAID磁盘阵列的原理和搭建

[root@localhost ~]# ls /dev/sd*

/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb  /dev/sdc

1-15-1 RAID磁盘阵列的原理和搭建

第二步:分别对两块磁盘进行分区操作,并检查

使用以下方法,分别对/dev/sdb、/dev/sdc进行分区操作

[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 0x630e77d6.

Command (m for help): n

Partition type:

   p   primary (0 primary, 0 extended, 4 free)

   e   extended

Select (default p): #默认

Using default response p

Partition number (1-4, default 1): #默认

First sector (2048-41943039, default 2048): #默认

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): #默认

Using default value 41943039

Partition 1 of type Linux and of size 20 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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

Disk label type: dos

Disk identifier: 0x630e77d6

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048    41943039    20970496   83  Linux

Command (m for help): 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     

Command (m for help): t

Selected partition 1

Hex code (type L to list all codes): fd

Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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

Disk label type: dos

Disk identifier: 0x630e77d6

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048    41943039    20970496   fd  Linux raid autodetect

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

[root@localhost ~]# mdadm -E /dev/sd[b-c] #检查sdb 和 sdc状态

/dev/sdb:

   MBR Magic : aa55

Partition[0] :     41940992 sectors at         2048 (type fd)

/dev/sdc:

   MBR Magic : aa55

Partition[0] :     41940992 sectors at         2048 (type fd)

[root@localhost ~]# mdadm -E /dev/sd[b-c]1 #检查sdb1和sdc1状态

mdadm: No md superblock detected on /dev/sdb1.

mdadm: No md superblock detected on /dev/sdc1.

[root@localhost ~]#

第三步:使用mdadm命令,创建RAID0磁盘阵列,并检查

[root@localhost ~]# mdadm -C -v /dev/md1 -l 0 -n 2 /dev/sd[b-c]1

mdadm: chunk size defaults to 512K

mdadm: Defaulting to version 1.2 metadata

mdadm: array /dev/md1 started.

1-15-1 RAID磁盘阵列的原理和搭建

检查:

[root@localhost ~]# mdadm -E /dev/sd[b-c]

/dev/sdb:

   MBR Magic : aa55

Partition[0] :     41940992 sectors at         2048 (type fd)

/dev/sdc:

   MBR Magic : aa55

Partition[0] :     41940992 sectors at         2048 (type fd)

[root@localhost ~]# mdadm -E /dev/sd[b-c]1

/dev/sdb1:

          Magic : a92b4efc

        Version : 1.2

    Feature Map : 0x0

     Array UUID : b7ca701c:f851d48b:c04edb48:5e427145

           Name : localhost.localdomain:1  (local to host localhost.localdomain)

  Creation Time : Thu Aug 18 21:57:07 2016

   Raid Level : raid0

Raid Devices : 2

Avail Dev Size : 41908224 (19.98 GiB 21.46 GB)

    Data Offset : 32768 sectors

   Super Offset : 8 sectors

   Unused Space : before=32680 sectors, after=0 sectors

          State : clean

    Device UUID : 8239d484:1a906768:b758cc5b:aaacda58

    Update Time : Thu Aug 18 21:57:07 2016

  Bad Block Log : 512 entries available at offset 72 sectors

       Checksum : 45d25591 - correct

         Events : 0

Chunk Size : 512K

Device Role : Active device 0

   Array State : AA ('A' == active, '.' == missing, 'R' == replacing)

/dev/sdc1:

          Magic : a92b4efc

    Version : 1.2

    Feature Map : 0x0

     Array UUID : b7ca701c:f851d48b:c04edb48:5e427145

           Name : localhost.localdomain:1  (local to host localhost.localdomain)

  Creation Time : Thu Aug 18 21:57:07 2016

     Raid Level : raid0

   Raid Devices : 2

Avail Dev Size : 41908224 (19.98 GiB 21.46 GB)

    Data Offset : 32768 sectors

   Super Offset : 8 sectors

   Unused Space : before=32680 sectors, after=0 sectors

          State : clean

  Device UUID : a4adb898:e645b661:175ed13a:9ca57053

    Update Time : Thu Aug 18 21:57:07 2016

  Bad Block Log : 512 entries available at offset 72 sectors

       Checksum : 2ca07dd2 - correct

         Events : 0

     Chunk Size : 512K

Device Role : Active device 1

   Array State : AA ('A' == active, '.' == missing, 'R' == replacing)

[root@localhost ~]# mdadm -D /dev/md1

/dev/md1:

    Version : 1.2

  Creation Time : Thu Aug 18 21:57:07 2016

     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 Aug 18 21:57:07 2016

          State : clean

Active Devices : 2

Working Devices : 2

Failed Devices : 0

  Spare Devices : 0

     Chunk Size : 512K

           Name : localhost.localdomain:1  (local to host localhost.localdomain)

           UUID : b7ca701c:f851d48b:c04edb48:5e427145

         Events : 0

    Number   Major   Minor   RaidDevice State

       0       8       17        0      active sync   /dev/sdb1

       1       8       33        1      active sync   /dev/sdc1

[root@localhost ~]# mdadm -E /dev/md1

mdadm: No md superblock detected on /dev/md1.

第四步:创建文件系统

[root@localhost ~]# mkfs.ext4 /dev/md1

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=128 blocks, Stripe width=256 blocks

2621440 inodes, 10477056 blocks

523852 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=2157969408

320 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

4096000, 7962624

Allocating group tables: done                           

Writing inode tables: done                           

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done  

[root@localhost ~]#

第五步:挂载,并设置开机自动挂载

[root@localhost ~]# mkdir /disk

[root@localhost ~]# mount /dev/md1 /disk

[root@localhost ~]# df | tail -1

/dev/md1               41118944   49176  38957976   1% /disk

[root@localhost ~]#

1-15-1 RAID磁盘阵列的原理和搭建

设置开机自动挂载:

[root@localhost ~]# blkid /dev/md1

/dev/md1: UUID="7b702168-4653-43cb-8fb0-6c692a6ff8df" TYPE="ext4"

[root@localhost ~]# vim /etc/fstab

1-15-1 RAID磁盘阵列的原理和搭建

[root@localhost ~]# df | tail -1

/dev/md1               41118944   49176  38957976   1% /disk

[root@localhost ~]# umount /disk

[root@localhost ~]# mount -a

[root@localhost ~]# df | tail -1

/dev/md1               41118944   49176  38957976   1% /disk

[root@localhost ~]#

1-15-1 RAID磁盘阵列的原理和搭建

第六步:模拟一块磁盘损坏

取消开机自动挂载后,直接卸载掉一块磁盘,重启即可。因为RAID0不具备容错能力,当有一个磁盘损坏时,磁盘阵列自然失败!!!

cat !$  # !$-->上一条命令的最后一个参数

watch cat /proc/mdstat #支持查看这条命令的执行结果

cat /proc/mdstat  #查看当前磁盘阵列状态