独立磁盘冗余阵列:RAID
RAID
RAID:獨立磁盤冗余陣列(Redundant Arrays of Independent(最早是Inexpensive,后來改成Independent) Disks,RAID)
計算機核心部件:CPU,內存,IO設備
部分硬盤接口格式:
| IDE | Integrated Drive Electronics | 133Mbps | 并行接口 |
| SATA1 | Serial Advanced Technology Attachment1.0 | 300Mbps | 串行接口 |
| SATA2 | 600Mbps | 串行接口 | |
| SATA3 | 6Gbps | 串行接口 | |
| USB3.0 | Universal Serial Bus | 480Mbps | 串行接口 |
| SCSI | small computer system interface | 1Gbps | 并行 |
| SAS |
驅動:將邏輯指令轉化為對應設備的控制指令的程序。
將cpu指令轉化為各設備可以理解的指令,在轉換處有一個控制設備,集成再主板上的叫控制器,獨立在主板外的叫適配器;本質上是同一種東西。即控制器(適配器)是連接主板和IO設備(外接IO設備,如磁盤、USB等)中間的橋梁,負責讓CPU和IO設備良好通信的介質。
RAID級別
RAID級別僅僅代表磁盤組織方式不同(應用不同場景需求),沒有上下之分??紤]因素:數據傳輸速度,完整性,可靠性,安全性等。
RAID0:條帶技術
所謂條帶技術,即RAID0,就是將一各數據分為很多片,通過控制器,存放在不同的磁盤設備上,這樣可以大大提高讀寫速度,緩解IO端口的讀寫瓶頸問題。但是帶來的問題:如果任意一個磁盤損壞,此文件就無法使用,這也大大降低了磁盤存儲的可靠性。
RAID1:磁盤鏡像技術
磁盤鏡像技術,即RAID1,就是在通過控制器向磁盤存儲數據時,將一份數據分別存放在不同的磁盤上,每個磁盤存放的數據都是完整的,相同的。這種技術,提高了磁盤存儲的可靠性,即是有一個磁盤損壞,也可以從別的磁盤上備份得到;但是缺毫無冗余能力,即讀寫速度并沒有提升(相反,寫的速度反而降低),硬盤利用率只有二分之一。
RAID4:校驗碼技術
校驗碼技術,即RAID4,磁盤群提供一個校驗碼盤,負責記錄其他磁盤存儲的總和數據,如:在上圖,存儲數據磁盤上分別存儲1、2、2,則在校驗碼盤存儲5(1+2+2);如果存儲數據盤有一個損壞,有能通過校驗碼盤和其他正常工作的盤,恢復出損壞盤的數據。校驗碼技術,提升了讀寫速度,并且有一定的可靠性(壞一塊磁盤并不影響數據完整性)。
即便如此,因為恢復數據(有一塊盤損壞)時,需要校驗碼盤和其他盤共同參與,這也提升了IO壓力,和損壞風險。同時,校驗碼盤容易成為整個技術的瓶頸。
RAID5:輪流校驗碼技術
RAID5和RAID4很相似,差別在于:RAID5的校驗碼盤不是固定的,即磁盤群內,互為校驗碼盤,這樣就緩解了RAID4暴露出來的問題(固定的校驗碼盤容易成為整個技術的瓶頸)。
RAID級別示意圖
| RAID0 | 條帶技術 | 不允許磁盤壞 | 傳輸速度快(讀寫性能提升N倍),可靠性極差(沒有冗余(錯)能力) |
| RAID1 | 磁盤鏡像技術 | 壞一塊不影響 | 傳輸速度慢(寫性能下降,讀性能提升),可靠性高(較強的冗余能力) |
| RAID2 | 漢明碼技術 | 同0 | 在0的基礎上,提升讀寫數據時的糾錯能力 |
| RAID3 | 漢明碼技術 | 同1 | 在1的基礎上,提升讀寫數據時的糾錯能力 |
| RAID4 | 校驗碼技術 | 壞一塊可修復 | 傳輸速度快(讀寫性能提升),具備冗余能力(不高),修復危險度高 |
| RAID5 | 輪流校驗碼技術 | 壞一塊可修復 | 同4,避免單一校驗硬盤成為瓶頸問題 |
| RAID6 |
RAID組合
RAID01
所謂RAID01就是先進行RAID0(條帶化),在進行RAID1(鏡像化),這樣可一提升IO速度,也可以確??煽啃浴?/span>
但是,如果有一塊磁盤損壞,可能整個磁盤體系都需要被調用。
RAID10
所謂RAID10即先進行RAID1(鏡像技術),再進行RAID0(條帶技術),這樣可一提升IO速度,也可以確??煽啃?。如果有一塊磁盤損壞,則不需要調用整個磁盤體系,只需要調用和損壞磁盤互為鏡像的磁盤進行數據恢復即可。
RAID50
所謂RAID10即先進行RAID5(鏡像技術),再進行RAID0(條帶技術)。此處不再過多解釋。
RAID50大大提升對數據的讀寫能力,同時具備冗余能力??臻g利用率為(總盤數-校驗盤個數)/總盤數
JBOD技術
JBOD磁盤技術,適合Hadoop。
性能無提升,不具備冗余能力,空間利用率百分之百
RAID盤選取
早期:
IDE
SCSI
如今:
STAT
? 盤大,價格便宜
SAS
? 盤小,價格昂貴
硬件RAID
模型1
主機連接有插槽,RAID控制器通過線纜連接到插槽(插槽鑲嵌再主機上),將硬盤插入插槽,組成RAID陣列。
模型2
磁盤陣列放在一個大的磁盤盒子內,通過一個向外接口
主機沒有插槽,通過一個外接線纜,將主機RAID控制器和磁盤陣列盒子連接在一起。
軟件RAID
內核必須支持軟件RAID,LInux內核中有一個模塊md:multi disks(多磁盤)
使用md模擬一個raid(邏輯RAID),/dev/mdx
mdadm
RAID需要在不同磁盤上做,才有意義(突破單個磁盤的IO瓶頸),實驗狀態下,使用一塊磁盤不同分區來實現(真實環境是毫無意義的,因為磁盤的IO端口依然是傳輸瓶頸)。
mdadm是個模式化的命令:
創建模式 -C
? 專用選項:-l(指定級別);-n(設備個數);-a(是否自動為其創建設備文件(yes,no));-c(指定chunk大小,即數據塊大小)
? -x : 指定空閑盤的個數。留著一個備份盤,如果有一個使用的盤壞掉,備份盤立刻頂上去,保證數據安全。
管理模式
–add(-a) --remove(-r)等
mdadm /dev/mdx --fail /dev/sdxx
mdadm -S(–stop) /dev/mdx
監控模式 -F
增長模式 -G
裝配模式 -A
RAID0
將/dev/vdb分為四個分區,并創建其每個分區的系統類型為Linux raid autodetect。
[root@raid ~]# fdisk /dev/vdbDevice contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x05300968.
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-2080, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2080, default 2080): +200M
Command (m for help): n
Command action
e ? extended
p ? primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (408-2080, default 408):
Using default value 408
Last cylinder, +cylinders or +size{K,M,G} (408-2080, default 2080): +200M
Command (m for help): n
Command action
e ? extended
p ? primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (815-2080, default 815):
Using default value 815
Last cylinder, +cylinders or +size{K,M,G} (815-2080, default 2080): +200M
?
Command (m for help): n
Command action
e ? extended
p ? primary partition (1-4)
p
Selected partition 4
First cylinder (1222-2080, default 1222):
Using default value 1222
Last cylinder, +cylinders or +size{K,M,G} (1222-2080, default 2080):
Using default value 2080
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 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): t
Partition number (1-4): 4
Hex code (type L to list codes): fd
Changed system type of partition 4 to fd (Linux raid autodetect)
Command (m for help): w
將/dev/vdb1和/dev/vdb4作為磁盤陣列(RAID0模式)。
?
[root@raid ~]# mdadm -C /dev/md0 -a yes -l 0 -n 2 /dev/vdb{1,4}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
?
[root@raid ~]# cat /proc/mdstat //查看創建結果
Personalities : [raid0]
md0 : active raid0 vdb4[1] vdb1[0]//所有啟用的RAID設備
637440 blocks super 1.2 512k chunks //chunk大小為512k
unused devices: <none>
在操作系統看來,/dev/md0就是一個塊設備,和/dev/sda2沒有區別,RAID機制的實現是在物理層面。即塊設備可以被操作系統識別后,就需要進行格式化:
[root@raid ~]# mkfs.ext3 /dev/md0 //格式化/dev/md0為ext3格式
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=128 blocks, Stripe width=256 blocks
39840 inodes, 159360 blocks
7968 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=163577856
5 block groups
32768 blocks per group, 32768 fragments per group
7968 inodes per group
Superblock backups stored on blocks:
32768, 98304
?
Writing inode tables: done ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
?
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
查看md0文件系統
[root@raid ~]# fdisk -l /dev/md0
?
Disk /dev/md0: 652 MB, 652738560 bytes
2 heads, 4 sectors/track, 159360 cylinders
Units = cylinders of 8 * 512 = 4096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 524288 bytes / 1048576 bytes
Disk identifier: 0x00000000
掛載文件設備
[root@raid ~]# mkdir /fsx
[root@raid ~]# mount /dev/md0 /fsx/
[root@raid ~]# cd /fsx/
[root@raid fsx]# ls
lost+found
RAID1
前期步驟和創建RAID0基本一樣,即創建分區,設備分區系統類型等。然后,創建RAID1:
root@raid fsx]# mdadm -C /dev/md1 -a yes -l 1 -n 2 /dev/vda1 /dev/vda2
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/md1 started.
查看/dev/md1設備塊信息
[root@raid fsx]# fdisk -l /dev/md1
?
Disk /dev/md1: 209 MB, 209846272 bytes//只是一個磁盤的大小,即操作系統看來,md1僅僅是一個磁盤,備份的任務是物理底層完成的
2 heads, 4 sectors/track, 51232 cylinders
Units = cylinders of 8 * 512 = 4096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
格式化,掛載md1,并寫入數據
[root@raid fsx]# mkfs.ext4 /dev/md1
mke2fs 1.41.12 (17-May-2010)
warning: 127 blocks unused.
?
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51400 inodes, 204801 blocks
10246 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2056 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
?
Writing inode tables: done ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
?
This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@raid fsx]# mount /dev/md1 /mnt/
[root@raid fsx]# echo “fsx123” > /mnt/fsx
[root@raid mnt]# cat fsx
fsx123
監控模式下,可以顯示RAID設備的詳細信息:
[root@raid mnt]# mdadm -D /dev/md1 //-D == --detail
/dev/md1:
Version : 1.2 //版本
Creation Time : Wed Apr 4 02:34:10 2018
Raid Level : raid1
Array Size : 204928 (200.16 MiB 209.85 MB) //陣列大小
Used Dev Size : 204928 (200.16 MiB 209.85 MB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
?
Update Time : Wed Apr 4 02:39:29 2018
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
?
Name : raid:1 (local to host raid)
UUID : d9cb6898:63c22e52:c3651fed:8650a65c
Events : 17
?
Number ? Major ? Minor ? RaidDevice State
0 ? ? 252 ? ? ? 1 ? ? ? 0 ? ? active sync ? /dev/vda1
1 ? ? 252 ? ? ? 2 ? ? ? 1 ? ? active sync ? /dev/vda2
?
管理模式下可以模擬一個磁盤損壞:
[root@raid mnt]# mdadm /dev/md1 -f /dev/vda1 //模擬損壞/dev/md1陣列中的/dev/vda1磁盤
mdadm: set /dev/vda1 faulty in /dev/md1
[root@raid mnt]# ls
fsx lost+found??????? //一個磁盤存壞,文件仍然可以讀取,RAID1的冗余能力。
[root@raid mnt]# vim fsx
[root@raid mnt]# cat fsx
fsx123
[root@raid mnt]# mdadm -D /dev/md1
/dev/md1:
Version : 1.2
Creation Time : Wed Apr 4 02:34:10 2018
Raid Level : raid1
Array Size : 204928 (200.16 MiB 209.85 MB)
Used Dev Size : 204928 (200.16 MiB 209.85 MB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
?
Update Time : Wed Apr 4 02:45:27 2018
State : clean, degraded
Active Devices : 1
Working Devices : 1
Failed Devices : 1
Spare Devices : 0
?
Name : raid:1 (local to host raid)
UUID : d9cb6898:63c22e52:c3651fed:8650a65c
Events : 21
?
Number ? Major ? Minor ? RaidDevice State
0 ? ? ? 0 ? ? ? 0 ? ? ? 0 ? ? removed //查看狀態,處與remove
1 ? ? 252 ? ? ? 2 ? ? ? 1 ? ? active sync ? /dev/vda2
?
0 ? ? 252 ? ? ? 1 ? ? ? - ? ? faulty ? /dev/vda1
?
移除損壞的設備
[root@raid mnt]# mdadm /dev/md1 -r /dev/vda1 //-r == --remove
mdadm: hot removed /dev/vda1 from /dev/md1
[root@raid mnt]# mdadm -D /dev/md1
/dev/md1:
Version : 1.2
Creation Time : Wed Apr 4 02:34:10 2018
Raid Level : raid1
Array Size : 204928 (200.16 MiB 209.85 MB)
Used Dev Size : 204928 (200.16 MiB 209.85 MB)
Raid Devices : 2
Total Devices : 1
Persistence : Superblock is persistent
?
Update Time : Wed Apr 4 02:47:40 2018
State : clean, degraded
Active Devices : 1
Working Devices : 1
Failed Devices : 0
Spare Devices : 0
?
Name : raid:1 (local to host raid)
UUID : d9cb6898:63c22e52:c3651fed:8650a65c
Events : 26
?
Number ? Major ? Minor ? RaidDevice State
0 ? ? ? 0 ? ? ? 0 ? ? ? 0 ? ? removed??????????? //被移除
1 ? ? 252 ? ? ? 2 ? ? ? 1 ? ? active sync ? /dev/vda2
重新給md1添加一塊設備
[root@raid mnt]# mdadm /dev/md1 -a /dev/vda1 //-a == --add
mdadm: added /dev/vda1
[root@raid mnt]# mdadm -D /dev/md1
/dev/md1:
Version : 1.2
Creation Time : Wed Apr 4 02:34:10 2018
Raid Level : raid1
Array Size : 204928 (200.16 MiB 209.85 MB)
Used Dev Size : 204928 (200.16 MiB 209.85 MB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
?
Update Time : Wed Apr 4 02:50:01 2018
State : clean, degraded, recovering
Active Devices : 1
Working Devices : 2
Failed Devices : 0
Spare Devices : 1
?
Rebuild Status : 47% complete
?
Name : raid:1 (local to host raid)
UUID : d9cb6898:63c22e52:c3651fed:8650a65c
Events : 37
?
Number ? Major ? Minor ? RaidDevice State
2 ? ? 252 ? ? ? 1 ? ? ? 0 ? ? spare rebuilding ? /dev/vda1
1 ? ? 252 ? ? ? 2 ? ? ? 1 ? ? active sync ? /dev/vda2
[root@raid mnt]# cat /proc/mdstat
Personalities : [raid0] [raid1]
md1 : active raid1 vda1[2] vda2[1]
204928 blocks super 1.2 [2/2] [UU]
md0 : active raid0 vdb4[1] vdb1[0]
637440 blocks super 1.2 512k chunks
unused devices: <none>
停止陣列
[root@raid /]# umount /mnt/ //停止前必須先解掛
[root@raid /]# mdadm -S /dev/md1 //-S == --stop
mdadm: stopped /dev/md1
重新啟動陣列
[root@raid /]# mdadm -A /dev/md1 /dev/vda1 /dev/vda2
mdadm: /dev/md1 has been started with 2 drives.
[root@raid /]# mdadm -D /dev/md1
/dev/md1:
Version : 1.2
Creation Time : Wed Apr 4 02:34:10 2018
Raid Level : raid1
Array Size : 204928 (200.16 MiB 209.85 MB)
Used Dev Size : 204928 (200.16 MiB 209.85 MB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
?
Update Time : Wed Apr 4 02:54:19 2018
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
?
Name : raid:1 (local to host raid)
UUID : d9cb6898:63c22e52:c3651fed:8650a65c
Events : 83
?
Number ? Major ? Minor ? RaidDevice State
2 ? ? 252 ? ? ? 1 ? ? ? 0 ? ? active sync ? /dev/vda1
3 ? ? 252 ? ? ? 2 ? ? ? 1 ? ? active sync ? /dev/vda2
總結
以上是生活随笔為你收集整理的独立磁盘冗余阵列:RAID的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EntityFramework进阶——数
- 下一篇: MyEclipes 设置代码自动提示