1. 程式人生 > 其它 >RAID磁碟陣列原理

RAID磁碟陣列原理

前言

在單機時代,採用單塊磁碟進行資料儲存和讀寫的方式,由於定址和讀寫的時間消耗,導致I/O效能非常低,且儲存容量還會受到限制。另外,單塊磁碟極其容易出現物理故障,經常導致資料的丟失。因此大家就在想,有沒有一種辦法將多塊獨立的磁碟結合在一起組成一個技術方案,來提高資料的可靠性和I/O效能呢。
在這種情況下,RAID技術就應運而生了。

1、RAID是什麼

RAID ( Redundant Array of Independent Disks )即獨立磁碟冗餘陣列,簡稱為「磁碟陣列」,其實就是用多個獨立的磁碟組成在一起形成一個大的磁碟系統,從而實現比單塊磁碟更好的儲存效能和更高的可靠性。

2、常見的RAID有哪些

  1. RAID0

  2. RAID1

  3. RAID5

  4. RAID6

  5. RAID10

RAID0

俗稱“條帶”,它將兩個或多個硬碟組成一個邏輯硬碟,每個硬碟最好大小相同,容量是所有硬碟之和,因為是多個硬碟組合成一個,故可並行寫操作,寫入速度提高,但此方式硬碟資料沒有冗餘,沒有容錯,一旦一個物理硬碟損壞,則所有資料均丟失。因而,RAID0 適合於對資料量大,但安全性要求不高的場景,比如音像、視訊檔案的儲存等。

工作原理


RAID管理工具

Linux核心中有一個md(multiple devices)模組在底層管理RAID裝置,它會在應用層給我們提供一個應用程式的工具mdadm ,mdadm是linux下用於建立和管理軟體RAID的命令。

mdadm命令常見引數解釋:


常用引數

RAID0搭建實驗(軟RAID)

  1. 新增兩塊硬碟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 
  1. 建立一個級別為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.
  1. 檢視相關資訊
[root@yaoguang ~]# mdadm -Ds
ARRAY /dev/md0 metadata=1.2 name=yaoguang:0 UUID=6c29ef17:0f0def7e:0561705d:fdecc6c0
  1. 生成配置檔案並檢視詳細資訊
[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
  1. 對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.
  1. 格式化
[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
  1. 掛載
[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
  1. 設定開機自動掛載
[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