RAID簡介
獨立硬盤冗余陣列(RAID, Redundant Array of Independent Disks),舊稱廉價磁盤冗余陣列(Redundant Array of Inexpensive Disks),簡稱磁盤陣列。主要目的是把多塊硬盤組合起來提高數據處理能力、容錯功能等
根據實現方式分為硬件和軟件兩種
硬件RAID:通過RAID卡或主板集成的RAID控制器來支持實現RAID功能
軟件RAID:通過軟件模擬實現RAID功能
注:軟件RAID不建議在生產環境使用,作為學習了解RAID的一種方式。
常見RAID級別
-
RAID 0
RAID 0亦稱為條帶卷。它將兩塊以上的硬盤并聯起來,成為一個大容量的硬盤。數據會分割成多個數據塊(chunk)平均分散存儲到硬盤上,因此讀寫能力提升,但不具備冗余和容錯功能,一塊硬盤損壞,所有數據丟失。
-
RAID 1
RAID 1亦稱為鏡像卷。它由兩塊或多塊硬盤相互組成鏡像。比如兩塊硬盤組RAID 1 數據會在兩塊硬盤都保存一份,使得硬盤讀性能提升但寫性能略有下降,具備冗余和容錯功能。
-
RAID 4
RAID 4是由3塊或3塊以上硬盤組成,其中一塊硬盤作為校驗盤存儲校驗碼,使其具備容錯能力允許損壞一塊硬盤,其可通過異或運算還原數據。在讀寫操作時校驗盤相比其它盤的負載壓力大,更易損壞。
-
RAID 5
RAID 5可以理解為RAID 4的升級版,相比于RAID 4其將校驗碼分散存儲在各個盤上,使得負載均衡,整體性能提升,并且提供了一塊硬盤的冗余。
-
RAID 6
相比于RAID 5增加一個校驗系統,提供了兩塊硬盤的冗余。根據其設計硬盤數至少4塊.
混合類型
-
RAID 10
RAID 10是將所有硬盤先組成N組RAID 1,然后再組成RAID 0。最高可支持半數硬盤損壞而不丟失數據。
-
RAID 01
RAID 10是將所有硬盤先組成N組RAID 0,然后再組成RAID 1。當有一塊硬盤損壞,其同組RAID 0的其它硬盤都會停止工作。運氣不好可能損壞兩塊硬盤就導致所有數據丟失。
-
RAID 50
RAID 5與RAID 0的組合,先作RAID 5,再作RAID 0。至少需要6塊硬盤,最多只能壞一塊硬盤。
-
JBOD(Just a Bunch Of Disks)
主要功能是將多塊硬盤的空間合并成一個大的連續空間使用。
注:以上圖片轉自維基百科
總結和對比
RAID級別 | 讀取性能 | 寫入性能 | 可用空間 | 最大容錯 | 極端情況 | 最少硬盤 |
---|---|---|---|---|---|---|
單一硬盤(參考) | 1 | 1 | 1 | 0 | 1 | 1 |
RAID 0 | N | N | N*min(S1…Sn) | 0 | 1 | 2 |
RAID 1 | N | 略有下降 | 1*min(S1…Sn) | N-1 | N | 2 |
RAID 4 | 提高 | 提高 | (N-1)*min(S1…Sn) | 1 | 2 | 3 |
RAID 5 | N-1 | N-1 | (N-1)*min(S1…Sn) | 1 | 2 | 3 |
RAID 6 | N-2 | N-2 | (N-2)*min(S1…Sn) | 2 | 3 | 4 |
RAID 10 | N | M | M*min(S1…SN) | N-M | N/M | 4 |
RAID 01 | N | N/M | N*min(S1…SN)/M | N*(M-1) | M | 4 |
JBOD | 1 | 1 | 容量之和 | 0 | 1 | 2 |
注:
N表示硬盤數
min(S1…Sn)表示最小硬盤容量
RAID 10中M表示RAID 1的組數,RAID 01中M表示RAID 0的組數
極端情況表示最少幾塊硬盤損壞就會導致數據丟失
CentOS 6上的軟件RAID的實現
在CentOS 6中使用mdadm工具與內核中的md(multi devices)模塊通信完成創建管理軟RAID
-
mdadm簡介
命令的語法格式:
mdadm [mode] <raiddevice> [options] <component-devices>
支持的RAID級別:LINEAR, RAID0, RAID1, RAID4, RAID5, RAID6, RAID10模式:
創建模式:-C
裝配模式:-A
監控模式:-F
管理模式:-f,-r,-a
: /dev/md#
: 任意塊設備-C:創建模式
-n #:使用#個塊設備來創建此RAID
-l #:指明要創建的RAID的級別
-a {yes|no}:自動創建目標RAID設備的設備文件
-c CHUNK_SIZE:指明塊大小
-x #:指明空閑盤的個數-D:顯示raid的詳細信息
mdadm -D /dev/md#
管理模式:
-f:標記指定磁盤為損壞
-a:添加磁盤
-r:移除磁盤觀察md的狀態:
cat /proc/mdstat
停止md設備:mdadm -S /dev/md#
軟件RAID實現過程
實驗目標:創建一個可用空間為10G的RAID 1設備,要求其chunk大小為128k,文件系統為ext4,有一個空閑盤,開機可自動掛載至/backup目錄
-
系統環境
操作系統:CentOS 6.7 磁盤信息: Disk /dev/sda: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00013624 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 15666 125316096 8e Linux LVM Disk /dev/sdb: 35.4 GB, 35433480192 bytes 255 heads, 63 sectors/track, 4307 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
注:為了簡便實驗操作將對sdb進行分區,在此基礎上創建RAID。
-
磁盤分區
[root@centos6 ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x3610f0c2. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-4307, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-4307, default 4307): +10G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (1307-4307, default 1307): Using default value 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-4307, default 4307): +10G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (2613-4307, default 2613): Using default value 2613 Last cylinder, +cylinders or +size{K,M,G} (2613-4307, default 4307): +10G Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): fd #避免問題發生軟RAID一定要調整分區類型為Linux raid auto(即id為fd) Changed system type of partition 1 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): fd Changed system type of partition 2 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-4): 3 Hex code (type L to list codes): fd Changed system type of partition 3 to 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@centos6 ~]# partx -a /dev/sdb BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3
-
RAID設備創建
使用mdadm工具創建RAID 1,chunk為128k,有一個空閑盤
[root@centos6 ~]# mdadm -C /dev/md0 -a yes -n 2 -x 1 -c 128 -l 1 /dev/sdb{1,2,3} mdadm: Note: this array has metadata at the start and may not be suitable as a boot device. If you plan to store '/boot' on this device please ensure that your boot-loader understands md/v1.x metadata, or use --metadata=0.90 Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started.
查看md的狀態
[root@centos6 ~]# cat /proc/mdstat #同步中 Personalities : [raid1] md0 : active raid1 sdb3[2](S) sdb2[1] sdb1[0] 10482176 blocks super 1.2 [2/2] [UU] [===>.................] resync = 18.9% (1989376/10482176) finish=0.6min speed=221041K/sec unused devices: <none> [root@centos6 ~]# cat /proc/mdstat #同步完成 Personalities : [raid1] md0 : active raid1 sdb3[2](S) sdb2[1] sdb1[0] 10482176 blocks super 1.2 [2/2] [UU] unused devices: <none>
查看創建的RAID設備的詳細信息
[root@centos6 ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Mon Apr 11 20:17:28 2016 Raid Level : raid1 Array Size : 10482176 (10.00 GiB 10.73 GB) Used Dev Size : 10482176 (10.00 GiB 10.73 GB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Mon Apr 11 20:18:20 2016 State : clean Active Devices : 2 Working Devices : 3 Failed Devices : 0 Spare Devices : 1 Name : centos6.7:0 (local to host centos6.7) UUID : 5a709c74:6b893df0:83f19941:565699f7 Events : 17 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 18 1 active sync /dev/sdb2 2 8 19 - spare /dev/sdb3
RAID設備格式化,文件系統為ext4
[root@centos6 ~]# mke2fs -t ext4 /dev/md0 mke2fs 1.41.12 (17-May-2010) 文件系統標簽= 操作系統:Linux 塊大小=4096 (log=2) 分塊大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 655360 inodes, 2620544 blocks 131027 blocks (5.00%) reserved for the super user 第一個數據塊=0 Maximum filesystem blocks=2684354560 80 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 正在寫入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 31 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
-
掛載RAID設備
將RAID設備設置開機可自動掛載至/backup目錄
[root@centos6 ~]# mkdir /backup #創建/backup目錄 [root@centos6 ~]# blkid /dev/md0 #查詢設備的UUID,推薦使用UUID格式掛載 /dev/md0: UUID="f57b163a-0bc3-4b21-a388-a1586d84e943" TYPE="ext4" [root@centos6 ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Fri Mar 4 11:09:09 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/vg_centos6-lv_root / ext4 defaults 1 1 UUID=3efc9d2d-ff60-4491-84c4-e1beb6701b83 /boot ext4 defaults 1 2 /dev/mapper/vg_centos6-lv_home /home ext4 defaults 1 2 /dev/mapper/vg_centos6-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 UUID=f57b163a-0bc3-4b21-a388-a1586d84e943 /backup ext4 defaults 0 0 [root@centos6 ~]# mount -a #掛載支持自動掛載的設備 [root@centos6 ~]# df #查看是否掛載成功 Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg_centos6-lv_root 51475068 4071360 44782268 9% / tmpfs 502384 0 502384 0% /dev/shm /dev/sda1 487652 37084 424968 9% /boot /dev/mapper/vg_centos6-lv_home 69608928 54412 66011888 1% /home /dev/md0 10186552 23028 9639416 1% /backup #表明掛載成功
-
測試
[root@centos6 backup]# ls /backup lost+found [root@centos6 backup]#cp /etc/fstab /backup [root@centos6 ~]# ls /backup fstab lost+found [root@centos6 ~]# mdadm /dev/md0 -f /dev/sdb1 #模擬一塊硬盤損壞 mdadm: set /dev/sdb1 faulty in /dev/md0 [root@centos6 ~]# mdadm -D /dev/md0 #查看設備信息 /dev/md0: Version : 1.2 Creation Time : Mon Apr 11 20:17:28 2016 Raid Level : raid1 Array Size : 10482176 (10.00 GiB 10.73 GB) Used Dev Size : 10482176 (10.00 GiB 10.73 GB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Mon Apr 11 21:03:48 2016 State : clean, degraded, recovering #處于降級恢復模式 Active Devices : 1 Working Devices : 2 Failed Devices : 1 Spare Devices : 1 Rebuild Status : 43% complete Name : centos6.7:0 (local to host centos6.7) UUID : 5a709c74:6b893df0:83f19941:565699f7 Events : 26 Number Major Minor RaidDevice State 2 8 19 0 spare rebuilding /dev/sdb3 #空閑盤替代損壞硬盤,數據重建中 1 8 18 1 active sync /dev/sdb2 0 8 17 - faulty /dev/sdb1 #提示該硬盤損壞 [root@centos6 ~]# mdadm /dev/md0 -f /dev/sdb2 #模擬再出現硬盤損壞 mdadm: set /dev/sdb2 faulty in /dev/md0 [root@centos6 ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Mon Apr 11 20:17:28 2016 Raid Level : raid1 Array Size : 10482176 (10.00 GiB 10.73 GB) Used Dev Size : 10482176 (10.00 GiB 10.73 GB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Mon Apr 11 21:10:20 2016 State : clean, degraded #處于降級模式 Active Devices : 1 Working Devices : 1 Failed Devices : 2 Spare Devices : 0 Name : centos6.7:0 (local to host centos6.7) UUID : 5a709c74:6b893df0:83f19941:565699f7 Events : 38 Number Major Minor RaidDevice State 2 8 19 0 active sync /dev/sdb3 2 0 0 2 removed #此處硬盤缺失 0 8 17 - faulty /dev/sdb1 1 8 18 - faulty /dev/sdb2 [root@centos6 ~]# cat /backup/fstab #數據可正常訪問 # # /etc/fstab # Created by anaconda on Fri Mar 4 11:09:09 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/vg_centos6-lv_root / ext4 defaults 1 1 UUID=3efc9d2d-ff60-4491-84c4-e1beb6701b83 /boot ext4 defaults 1 2 /dev/mapper/vg_centos6-lv_home /home ext4 defaults 1 2 /dev/mapper/vg_centos6-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 UUID=f57b163a-0bc3-4b21-a388-a1586d84e943 /backup ext4 defaults 0 0
原創文章,作者:Net18-海濱,如若轉載,請注明出處:http://www.www58058.com/14676