一、配置環境介紹
操做平臺:VMware workstation
測試環境:centos7 計算機一臺,添加一塊新的硬盤,建立4個分區。模擬4塊新硬盤
二、實踐目的
一、配置RAID5
二、模擬硬盤故障,檢驗RAID5容錯能力,並實現故障盤替換
三、RAID陣列上建立分區
三、建立RAID準備工做
一、首先查看硬盤標籤類型centos
[root@adong ~]# fdisk -l /dev/sdc Disk /dev/sdb: 20 GiB, 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 Disklabel type: gpt #爲GPT類型 Disk identifier: 0x25cc0f39
二、由於硬盤標籤類型爲GPT,左移咱們用gdisk命令建立分區便可bash
[root@adong ~]# gdisk /dev/sdc GPT fdisk (gdisk) version 1.0.3 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): n #新建分區 Partition number (1-128, default 1): #默認分區編號便可,默認從1開始 First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: #指定起始扇區大小,默認便可 Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +5G #指定分區大小 Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): fd00 #更改分區類型爲LinuxRAID Changed type of partition to 'Linux RAID'
重複上述步驟,創建4個分區,而後以下操做保存退出ide
Command (? for help): w #保存分區 Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): Y #上面提示將寫入GPT分區數據,這樣會覆蓋原來的分區,是否繼續 OK; writing new GUID partition table (GPT) to /dev/sdc. The operation has completed successfully.
此時用於模擬4塊硬盤的分區就建立好了
三、使用mdadm工具建立RAID,陣列的成員能夠使用通配符工具
[root@adong ~]# mdadm --create /dev/md0 --level=5 --raid-devices=3 --spare-device=1 /dev/sdc[1-4] mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started.
說明:由於作RAID5最少須要3塊磁盤,此前咱們建立了4個分區模擬4個磁盤,因此建立時咱們經過--raid-devices選項指定用於建立raid的磁盤數爲3,而後將剩餘的一個磁盤經過--spare-device選項設置爲備用磁盤。
四、查看RAID配置的詳細信息測試
[root@adong ~]# mdadm --detail /dev/md0 /dev/md0: Version : 1.2 Creation Time : Sat Dec 12 10:03:04 2020 Raid Level : raid5 Array Size : 10475520 (9.99 GiB 10.73 GB) Used Dev Size : 5237760 (5.00 GiB 5.36 GB) Raid Devices : 3 Total Devices : 4 Persistence : Superblock is persistent Update Time : Sat Dec 12 10:03:31 2020 State : clean Active Devices : 3 Working Devices : 4 Failed Devices : 0 Spare Devices : 1 Layout : left-symmetric Chunk Size : 512K Consistency Policy : resync Name : adong.com:0 (local to host adong.com) UUID : 597cac50:2c13e44b:f5a87d17:c0f2fc8e Events : 18 Number Major Minor RaidDevice State 0 8 33 0 active sync /dev/sdc1 1 8 34 1 active sync /dev/sdc2 4 8 35 2 active sync /dev/sdc3 3 8 36 - spare /dev/sdc4
經過上述信息,咱們能夠看到,有3塊硬盤處於active激活的狀態並sync同步中,一塊硬盤處於spare備用狀態
五、模擬磁盤故障、查看備用盤的工做狀況ui
[root@adong ~]# mdadm /dev/md0 --fail /dev/sdc2 mdadm: set /dev/sdc2 faulty in /dev/md0
經過--fail選項模擬其中一塊磁盤故障centos7
[root@adong ~]# mdadm --detail /dev/md0 /dev/md0: Version : 1.2 Creation Time : Sat Dec 12 10:03:04 2020 Raid Level : raid5 Array Size : 10475520 (9.99 GiB 10.73 GB) Used Dev Size : 5237760 (5.00 GiB 5.36 GB) Raid Devices : 3 Total Devices : 4 Persistence : Superblock is persistent Update Time : Sat Dec 12 10:14:34 2020 State : clean, degraded, recovering Active Devices : 2 Working Devices : 3 Failed Devices : 1 Spare Devices : 1 Layout : left-symmetric Chunk Size : 512K Consistency Policy : resync Rebuild Status : 21% complete Name : adong.com:0 (local to host adong.com) UUID : 597cac50:2c13e44b:f5a87d17:c0f2fc8e Events : 23 Number Major Minor RaidDevice State 0 8 33 0 active sync /dev/sdc1 3 8 36 1 spare rebuilding /dev/sdc4 4 8 35 2 active sync /dev/sdc3 1 8 34 - faulty /dev/sdc2
此時咱們發現原來備用狀態的/dev/sdc4,此時正在進行備用重建,加入到RAID中,替換故障磁盤
六、故障盤修復或者更換新的物理盤,替換原來的問題磁盤
若是是現實物理環境,將新的物理磁盤更換上去便可,此處咱們模擬環境,因此首先要手動移除故障磁盤spa
[root@adong ~]# mdadm /dev/md0 --remove /dev/sdc2 mdadm: hot removed /dev/sdc2 from /dev/md0
而後將新的磁盤添加上去操作系統
[root@adong ~]# mdadm /dev/md0 --add /dev/sdb1 mdadm: added /dev/sdb1
此時咱們查看RAID的信息,發現新添加上的磁盤,處於備用狀態。一切恢復正常code
[root@adong ~]# mdadm --detail /dev/md0 /dev/md0: Version : 1.2 Creation Time : Sat Dec 12 10:03:04 2020 Raid Level : raid5 Array Size : 10475520 (9.99 GiB 10.73 GB) Used Dev Size : 5237760 (5.00 GiB 5.36 GB) Raid Devices : 3 Total Devices : 4 Persistence : Superblock is persistent Update Time : Sat Dec 12 10:21:39 2020 State : clean Active Devices : 3 Working Devices : 4 Failed Devices : 0 Spare Devices : 1 Layout : left-symmetric Chunk Size : 512K Consistency Policy : resync Name : adong.com:0 (local to host adong.com) UUID : 597cac50:2c13e44b:f5a87d17:c0f2fc8e Events : 39 Number Major Minor RaidDevice State 0 8 33 0 active sync /dev/sdc1 3 8 36 1 active sync /dev/sdc4 4 8 35 2 active sync /dev/sdc3 5 8 17 - spare /dev/sdb1
注意:更換的磁盤大小必定要和現有RAID中的磁盤大小同樣
七、在RAID陣列上建立分區,並格式化文件系統爲xfs
陣列磁盤對操做系統而言,同樣是一個邏輯磁盤,因此咱們能夠像操做單個磁盤同樣對他進行建立分區和格式化文件系統
[root@adong ~]# gdisk /dev/md0 GPT fdisk (gdisk) version 1.0.3 Partition table scan: MBR: not present BSD: not present APM: not present GPT: not present Creating new GPT entries. Command (? for help): n #新建分區 Partition number (1-128, default 1): #指定分區編號 First sector (34-20951006, default = 2048) or {+-}size{KMGTP}: #指定起始扇區 Last sector (2048-20951006, default = 20951006) or {+-}size{KMGTP}: +5G #設置分區大小 Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): Changed type of partition to 'Linux filesystem' Command (? for help): w #保存分區 Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): Y #將要寫入GPT分區去數據,原來的分區數據將被覆蓋,是否繼續 OK; writing new GUID partition table (GPT) to /dev/md0. The operation has completed successfully.
八、查看分區狀況,發現生成一個/dev/mdop1的分區
[root@adong ~]# fdisk -l /dev/md0 Disk /dev/md0: 10 GiB, 10726932480 bytes, 20951040 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: gpt Disk identifier: BD1E493E-652F-4676-AA07-FCC0D601F15C Device Start End Sectors Size Type /dev/md0p1 2048 10487807 10485760 5G Linux filesystem
九、針對上述生成的分區記性格式化文件系統xfs
[root@adong ~]# mkfs.xfs /dev/md0p1 meta-data=/dev/md0p1 isize=512 agcount=8, agsize=163712 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 data = bsize=4096 blocks=1309696, imaxpct=25 = sunit=128 swidth=256 blks naming =version 2 bsize=4096 ascii-ci=0, ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
十、掛載分區到指定目錄/mnt
[root@adong ~]# mount /dev/md0p1 /mnt/
十一、查看磁盤分區使用狀況,能夠看到/dev/mdop1/被掛載到了/mnt/使用空間爲5G
[root@adong ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 383M 0 383M 0% /dev tmpfs 399M 0 399M 0% /dev/shm tmpfs 399M 11M 388M 3% /run tmpfs 399M 0 399M 0% /sys/fs/cgroup /dev/sda2 23G 12G 11G 53% / /dev/sda3 6.0G 83M 6.0G 2% /home /dev/sda1 3.7G 203M 3.3G 6% /boot tmpfs 80M 4.0K 80M 1% /run/user/0 /dev/md0p1 5.0G 69M 5.0G 2% /mnt
總結:額外經常使用的一些軟RAID操做命令1.中止RAIDmdadm --stop /dev/md0 二、刪除RAID首先中止RAID而後清除RAID陣列中全部磁盤元數據,這樣就完全刪除了,重啓系統也不會被自動安裝mdadm --zero-superblock /dev/sdc1三、監控RAID,若是出現故障,通知管理員nohup mdadm --monitor --mail=root@localhost.com --delay=180 /dev/md0nohup:將監控任務放置後臺運行delay選項:每一個多久對陣列檢查一下