RAID磁盤陣列的搭建
RAID(Redundant Array of Independent Disks,獨立磁盤冗余陣列)可以提供較普通磁盤更高的速度、安全性,所以服務器在安裝時都會選擇創建RAID。
RAID的創建有兩種方式:軟RAID(通過操作系統軟件來實現)和硬RAID(使用硬件陣列卡);在企業中用的最多的是:raid1、raid10和raid5。
一、 RAID常見的幾種分類
模式 | 優點 | 缺點 | 備註 |
RAID 0 | 在RAID 0狀態下,存儲數據被分割成兩部分,分別存儲在兩塊硬盤上,此時移動硬盤的理論存儲速度是單塊硬盤的2倍,實際容量等於兩塊硬盤中較小一塊硬盤的容量的2倍。 | 任何一塊硬盤發生故障,整個RAID上的數據將不可恢復。 | 存儲高清電影比較適合。 |
RAID 1 | 兩塊硬盤互為鏡像。當一個硬盤受損時,換上一塊全新硬盤(大於或等於原硬盤容量)替代原硬盤即可自動恢復資料和繼續使用,移動硬盤的實際容量等於較小一塊硬盤的容量,存儲速度與單塊硬盤相同。RAID 1的優勢在於任何一塊硬盤出現故障是,所存儲的數據都不會丟失。 | 可使用的硬盤實際容量比較小,僅僅為兩顆硬盤中最小硬盤的容量。 | 非常重要的資料,如數據庫,個人資料,是萬無一失的存儲方案。 |
RAID 5 | RAID5不對存儲的數據進行備份,而是把數據和相對應的奇偶校驗信息存儲到組成RAID5的各個磁盤上,並且奇偶校驗信息和相對應的數據分別存儲於不同的磁盤上。當RAID5的一個磁盤數據發生損壞後,利用剩下的數據和相應的奇偶校驗信息去恢復被損壞的數據。 | 損失一定的磁盤空間 | 服務器常用方案 |
RAID 10 | 把2塊硬盤組成一個RAID1,然後兩組RAID1組成一個RAID0。它提供了200%的速度和單磁盤損壞的數據安全性。 | RAID10最少需要四塊硬盤才能完成,50%的磁盤浪費。 | 服務器常用方案 |
二、 RAID使用簡明註意事項
使用前請先備份硬盤的資料,一旦進行RAID設定或是變更RAID模式,將會清除硬盤裏的所有資料,以及無法恢復。
建立RAID時,建議使用相同品牌、型號和容量的硬盤,以確保性能和穩定。
請勿隨意更換或取出硬盤,如果取出了硬盤,請記下硬盤放入兩個倉位的順序不得更改,以及請勿只插入某一塊硬盤使用,以避免造成資料損壞或丟失。
如果舊硬盤曾經在RAID模式
下使用,請先進清除硬盤RAID信息,讓硬盤回復至出廠狀態,以免RAID建立失敗。
RAID0模式下,其中一個硬盤損壞時,其它硬盤所有資料都將丟失。
RAID1模式下,如果某一塊硬盤受損,可以用一塊大於或等於受損硬盤容量的新硬盤替換壞硬盤然後開機即可自動恢復和修復資料以及RAID模式。此過程需要一定時間,請耐心等待。
三、 軟RAID搭建方法
mdadm是Linux下用於創建和管理軟RAID的命令,是一個模塊化命令。
常用參數如下:
-C 或 --creat | 建立一個新陣列 | -r | 移除設備 |
-A | 激活磁盤陣列 | -l 或 --level= | 設定磁盤陣列的級別 |
-D 或 --detail | 打印陣列設備的詳細信息 | -n 或 --raid-devices= | 指定陣列成員(分區/次磁盤)的數量 |
-s 或 --scan | 掃面配置文件或/proc/mdstat得到陣列缺失信息 | -x 或 --spare-devicds= | 指定陣列中備用盤的數量 |
-f | 將設備狀態定位故障 | -c 或--chunk= | 設定陣列的塊 chunk 大小,單位為KB |
-a 或 --add | 添加設備到陣列 | -G 或 --grow | 改變陣型大小或形態 |
-v | 顯示詳細信息 |
組建RAID0
環境:關閉電源,添加兩個硬盤。
啟動Linux,將新加入的硬盤分區。使用命令:fdisk
[[email protected] ~]# fdisk /dev/sdb WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to #警告:DOS兼容模式已經過時。強烈建議 switch off the mode (command ‘c‘) and change display units to 關閉模式(命令“c”)並將顯示單元更 sectors (command ‘u‘). 改為扇區(命令“u”)。 Command (m for help): n #新建一個分區 Command action e extended #e為邏輯分區 p primary partition (1-4) #p為主分區 p Partition number (1-4): 1 #設置分區號為1 First cylinder (1-2610, default 1): #默認從第一塊開始 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G #將分區添加1G Command (m for help): n #新建分區 Command action e extended p primary partition (1-4) p #主分區 Partition number (1-4): 2 #設置分區號為2 First cylinder (133-2610, default 133): Using default value 133 Last cylinder, +cylinders or +size{K,M,G} (133-2610, default 2610): +1G #將分區添加1G Command (m for help): w #w保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
備註:已經將硬盤分為兩個區,分別是sdb1和sdb2。
查詢是否安裝mdadm命令。
[[email protected] ~]# rpm -q mdadm mdadm-3.3.4-8.el6.x86_64
[[email protected] ~]# mdadm -C /dev/md0 -l 0 -n 2 /dev/sdb1 /dev/sdb2 #新建一個md0在/dev下,陣列等級為1,硬盤數量為2,分別是/dev/sdb1和/dev/sdb2 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started.
查詢陣列信息
[[email protected] ~]# mdadm -Ds ARRAY /dev/md0 metadata=1.2 name=hyx:0 UUID=def743c7:9b129ae0:44f88da4:0b844d03
[[email protected] ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Thu Aug 3 15:38:53 2017 Raid Level : raid0 Array Size : 2117632 (2.02 GiB 2.17 GB) Raid Devices : 2 Total Devices : 2 Persistence : Superblock is persistent Update Time : Thu Aug 3 15:38:53 2017 State : clean Active Devices : 2 Working Devices : 2 Failed Devices : 0 Spare Devices : 0 Chunk Size : 512K Name : hyx:0 (local to host hyx) UUID : def743c7:9b129ae0:44f88da4:0b844d03 Events : 0 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 18 1 active sync /dev/sdb2
已成功創建名為md0的RAID0陣列。
將配置信息保存到mdadm的配置文件,默認沒有配置文件,需要手工創建。
[[email protected] ~]# mkdir /etc/mdadm.conf
[[email protected] ~]# mdadm -Ds > /etc/mdadm.conf
對創建的RAID0進行分區
[[email protected] ~]# fdisk /dev/md0 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xa899a63c. 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-529408, default 257): Using default value 257 Last cylinder, +cylinders or +size{K,M,G} (257-529408, default 529408): Using default value 529408 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
格式化分區,並且將分區掛載到系統中
[[email protected] ~]# mkfs.ext4 /dev/md0p1 mke2fs 1.41.12 (17-May-2010) 文件系統標簽= 操作系統:Linux 塊大小=4096 (log=2) 分塊大小=4096 (log=2) Stride=128 blocks, Stripe width=256 blocks 132464 inodes, 529152 blocks 26457 blocks (5.00%) reserved for the super user 第一個數據塊=0 Maximum filesystem blocks=545259520 17 block groups 32768 blocks per group, 32768 fragments per group 7792 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 正在寫入inode表: 完成 Creating journal (16384 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.
創建一個目錄並且將raid0掛載到該目錄下
[[email protected] ~]# mkdir /raid0 [[email protected] ~]# mount /dev/md0p1 /raid0/
查看是否掛載成功
[[email protected] ~]# df -h | grep md0 /dev/md0p1 2.0G 3.1M 1.9G 1% /raid0
設置開機自動掛載
[[email protected] ~]# vim /etc/fstab
# /etc/fstab
# Created by anaconda on Wed Jul 26 06:20:11 2017
#
# 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/VolGroup-lv_root / ext4 defaults
1 1
UUID=4c386817-8682-457f-925d-769a25c85b37 /boot ext4 def
aults 1 2
/dev/mapper/VolGroup-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
/dev/md0p1 /raid0 ext4 defaults 0 0
保存退出設置完畢!
組建RAID1
將新硬盤sdc分區
[[email protected] dev]# fdisk /dev/sdc 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-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +2G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (263-2610, default 263): Using default value 263 Last cylinder, +cylinders or +size{K,M,G} (263-2610, default 2610): +2G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (525-2610, default 525): Using default value 525 Last cylinder, +cylinders or +size{K,M,G} (525-2610, default 2610): +2G Command (m for help): p Disk /dev/sdc: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x94ab7db1 Device Boot Start End Blocks Id System /dev/sdc1 1 262 2104483+ 83 Linux /dev/sdc2 263 524 2104515 83 Linux /dev/sdc3 525 786 2104515 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
將硬盤分為三個區,分別是sdc1,sdc2,sdc3。其中一個是熱備盤。
[[email protected] dev]# mdadm -C -v /dev/md1 -l 1 -n 2 -x 1 /dev/sdc{1,2,3} mdadm: /dev/sdc1 appears to be part of a raid array: level=raid1 devices=3 ctime=Fri Aug 4 05:21:15 2017 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 mdadm: size set to 2102400K Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started.
對RAID1進行分區
[[email protected] dev]# fdisk /dev/md1 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x9845350d. 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-525600, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-525600, default 525600): Using default value 525600 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
將分區格式化為ext4格式
[[email protected] dev]# mkfs.ext4 /dev/md1p1 mke2fs 1.41.12 (17-May-2010) 文件系統標簽= 操作系統:Linux 塊大小=4096 (log=2) 分塊大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131648 inodes, 525600 blocks 26280 blocks (5.00%) reserved for the super user 第一個數據塊=0 Maximum filesystem blocks=541065216 17 block groups 32768 blocks per group, 32768 fragments per group 7744 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 正在寫入inode表: 完成 Creating journal (16384 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
查詢是否成功創建,並且在根分區下創建一個目錄,用於掛載RAID1。
[[email protected] dev]# cat /proc/mdstat Personalities : [raid1] md1 : active raid1 sdc3[2](S) sdc2[1] sdc1[0] 2102400 blocks super 1.2 [2/2] [UU] unused devices: <none>
[[email protected] ~]# mkdir /raid1
[[email protected] ~]# mount /dev/md1p1 /raid1
[[email protected] dev]# df -h | grep md1 /dev/md1p1 2.0G 3.1M 1.9G 1% /raid1
成功創建RAID1
組建RAID5
添加新硬盤sdb,將硬盤分區。
[[email protected] dev]# 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 0xa671eab8. 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-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (133-2610, default 133): Using default value 133 Last cylinder, +cylinders or +size{K,M,G} (133-2610, default 2610): +1G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (265-2610, default 265): Using default value 265 Last cylinder, +cylinders or +size{K,M,G} (265-2610, default 2610): +1G Command (m for help): n Command action e extended p primary partition (1-4) e Selected partition 4 First cylinder (397-2610, default 397): Using default value 397 Last cylinder, +cylinders or +size{K,M,G} (397-2610, default 2610): Using default value 2610 Command (m for help): n First cylinder (397-2610, default 397): Using default value 397 Last cylinder, +cylinders or +size{K,M,G} (397-2610, default 2610): +1G Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0xa671eab8 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 83 Linux /dev/sdb2 133 264 1060290 83 Linux /dev/sdb3 265 396 1060290 83 Linux /dev/sdb4 397 2610 17783955 5 Extended /dev/sdb5 397 528 1060258+ 83 Linux Command (m for help): n First cylinder (529-2610, default 529): Using default value 529 Last cylinder, +cylinders or +size{K,M,G} (529-2610, default 2610): +1G Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0xa671eab8 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 83 Linux /dev/sdb2 133 264 1060290 83 Linux /dev/sdb3 265 396 1060290 83 Linux /dev/sdb4 397 2610 17783955 5 Extended /dev/sdb5 397 528 1060258+ 83 Linux /dev/sdb6 529 660 1060258+ 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
創建md5
[[email protected] dev]# mdadm -C -v /dev/md5 -l 5 -n 3 -c 32 -x1 /dev/sdb{1,2,3,5} #為什麽要用1.2.3.5,而不是1.2.3.4,因為4是擴展分區,並不是真正的有效分區,所以用5. mdadm: layout defaults to left-symmetric mdadm: layout defaults to left-symmetric mdadm: size set to 1059232K mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md5 started.
將RAID5分區
[[email protected] dev]# fdisk /dev/md5 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x040e784b. 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-529616, default 17): Using default value 17 Last cylinder, +cylinders or +size{K,M,G} (17-529616, default 529616): Using default value 529616 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
格式化RAID5
[[email protected] dev]# mkfs.ext4 /dev/md5p1 mke2fs 1.41.12 (17-May-2010) 文件系統標簽= 操作系統:Linux 塊大小=4096 (log=2) 分塊大小=4096 (log=2) Stride=8 blocks, Stripe width=16 blocks 132464 inodes, 529600 blocks 26480 blocks (5.00%) reserved for the super user 第一個數據塊=0 Maximum filesystem blocks=545259520 17 block groups 32768 blocks per group, 32768 fragments per group 7792 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 正在寫入inode表: 完成 Creating journal (16384 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 28 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
[[email protected] dev]# mkdir /raid5
[[email protected] dev]# mount /dev/md5p1 /raid5
[[email protected] dev]# df -h | grep md5 /dev/md5p1 2.0G 3.1M 1.9G 1% /raid5
RAID5創建完畢。
組建RAID10
建立RAID1+0雙層架構的方法。
方法:先創建RAID1,再使用創建的RAID1設備創建RAID0.
先創建兩個底層的RAID1
[[email protected] ~]# mdadm -C -v /dev/md11 -l 1 -n 2 /dev/sdd1 /dev/sdd2 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 mdadm: size set to 2102400K Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md11 started. [[email protected] ~]# mdadm -C -v /dev/md12 -l 1 -n 2 /dev/sdd3 /dev/sdd4 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 mdadm: size set to 2102464K Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md12 started.
再創建上層的RAID0
[[email protected] ~]# mdadm -C -v /dev/md10 -l 0 -n 2 /dev/md11 /dev/md12 mdadm: chunk size defaults to 512K mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md10 started.
將raid10分區,然後格式化raid10.
掛載,查詢是否掛載成功
[[email protected] ~]# mount /dev/md10p1 /raid10 [[email protected] ~]# df -h | grep 10 /dev/md10p1 3.9G 8.1M 3.7G 1% /raid10
註意事項
要取消RAID之前先要取消掛載。
例如取消RAID10。
[[email protected] dev]# umount /raid10 [[email protected] dev]# mdadm -S /dev/md10 mdadm: stopped /dev/md10
查看raid狀態
[[email protected] ~]# cat /proc/mdstat [[email protected] dev]# cat /proc/mdstat Personalities : [raid1] [raid6] [raid5] [raid4] [raid0] md10 : active raid0 md11[0] md12[1] 4200448 blocks super 1.2 512k chunks md11 : active raid1 sdd1[0] sdd2[1] 2102400 blocks super 1.2 [2/2] [UU] md1 : active raid1 sdc1[0] sdc3[2](S) sdc2[1] 2102400 blocks super 1.2 [2/2] [UU] md5 : active raid5 sdb1[0] sdb5[3](S) sdb3[4] sdb2[1] 2118464 blocks super 1.2 level 5, 32k chunk, algorithm 2 [3/3] [UUU] md12 : active raid1 sdd3[0] sdd4[1] 2102464 blocks super 1.2 [2/2] [UU] unused devices: <none>
重新啟動raid
[[email protected] ~]# mdadm -As
本文出自 “PooV的博客” 博客,請務必保留此出處http://xyhms.blog.51cto.com/12505169/1953945
RAID磁盤陣列的搭建