Raid & LVM 之一 Raid
Raid
Raid介绍
- RAID,全称Redundant Arrays of Inexpensive(Independent)Disks。简单翻译叫磁盘阵列。
- 通俗一点讲就是多个磁盘合成一个“阵列”来提供更好的性能、冗余,或者两者都提供。
- 目的有两个:一个是提高磁盘的IO能力,提高耐用性。
- 实现方式有三种,如下:
- 外接式磁盘阵列:通过raid卡提供适配能力
- 内接式RAID:主板集成RAID控制器,在安装OS之前需要在BIOS进行设置
- 软件RAID:通过OS软件实现
常见的Raid级别
管理工具介绍
- mdadm
[root@centos6-node3 ~]# rpm -qa | grep mdadm
mdadm-3.3.4-8.el6.x86_64
[root@centos6-node3 ~]# cat /proc/mdstat #查看mdstat状态
Personalities :
unused devices:
----------------------------------------
mdadm -h
-C:创建模式
-n:定义创建RAID设备个数
-l:指明要创建的RAID的级别
-c CHUNK_SIZE: 指明块大小
-x : 指明空闲盘的个数
-A:装配
-D:显示RAID的详细信息
-F:监控
-f:标记指定磁盘为损坏
-r:移除磁盘
-a:添加磁盘
- 准备条件:
- centos6x
- 6块磁盘
Raid0实操
特点:
- raid0性能比较高
- 空间0浪费
- 0容错
- 读写性能佳
准备两块盘
- 确认准备的两块盘,能被系统读到
[root@centos6-node3 ~]# ls -l /dev/ |grep sd brw-rw---- 1 root disk 8, 0 Dec 8 21:51 sda brw-rw---- 1 root disk 8, 1 Dec 8 21:51 sda1 brw-rw---- 1 root disk 8, 2 Dec 8 21:51 sda2 brw-rw---- 1 root disk 8, 16 Dec 8 21:51 sdb brw-rw---- 1 root disk 8, 32 Dec 8 21:51 sdc brw-rw---- 1 root disk 8, 48 Dec 8 21:51 sdd brw-rw---- 1 root disk 8, 64 Dec 8 21:51 sde brw-rw---- 1 root disk 8, 80 Dec 8 21:51 sdf brw-rw---- 1 root disk 8, 96 Dec 8 21:51 sdg [root@centos6-node3 ~]# lsblk -l #或者用这个命令也行,推荐用这个
- 检查准备的两块盘有没有raid信息
[root@centos6-node3 ~]# mdadm --examine /dev/sd[b-c] mdadm: No md superblock detected on /dev/sdb. mdadm: No md superblock detected on /dev/sdc.
- 创建raid分区
[root@centos6-node3 ~]# fdisk /dev/sdb #创建分区 Command (m for help): n #新建分区 p #选择主分区 Partition number (1-4): 1 #区号选择 First cylinder (1-261, default 1): #默认回车 Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): #使用全部分区 Command (m for help): p #插看创建的分区 Device Boot Start End Blocks Id System /dev/sdb1 1 261 2096451 83 Linux Command (m for help): L #列出可选的类型 Command (m for help): t #修改分区类型 Selected partition 1 Hex code (type L to list codes): fd #根据L出来的状态选择raid对应的fd Command (m for help): p #查看 我们所做的修改 Device Boot Start End Blocks Id System /dev/sdb1 1 261 2096451 fd Linux raid autodetect Command (m for help): w #保存配置,自动退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
- sdc也需要同样的操作去创建raid类型的分区
[root@centos6-node3 ~]# fdisk /dev/sdc n p 1 //回车 //回车 t fd w
- 检查磁盘是否正确定义Raid:
[root@centos6-node3 ~]# mdadm --examine /dev/sd[b-c] #看全盘 /dev/sdb: MBR Magic : aa55 Partition[0] : 4192902 sectors at 63 (type fd) /dev/sdc: MBR Magic : aa55 Partition[0] : 4192902 sectors at 63 (type fd) [root@centos6-node3 ~]# mdadm --examine /dev/sd[b-c]1 #看分区 mdadm: No md superblock detected on /dev/sdb1. mdadm: No md superblock detected on /dev/sdc1.
- 创建Raid设备
[root@centos6-node3 ~]# mdadm -C /dev/md0 -l raid0 -n 2 /dev/sd[b-c]1 #创建raid0 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started. [root@centos6-node3 ~]# cat /proc/mdstat #查看raid状态 Personalities : [raid0] md0 : active raid0 sdc1[1] sdb1[0] 4190208 blocks super 1.2 512k chunks
unused devices:
其他查看状态的方法:
``` bash
[root@centos6-node3 ~]# mdadm -E /dev/sd[b-c]1
[root@centos6-node3 ~]# mdadm -D /dev/md0
- 给raid创建文件系统,并挂在/mnt/raid0下:
[root@centos6-node3 ~]# mkdir /mnt/raid0 [root@centos6-node3 ~]# mkfs.ext4 /dev/md0 [root@centos6-node3 ~]# mount /dev/md0 /mnt/raid0/ [root@centos6-node3 ~]# df -Th #测试 Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_centos6node3-lv_root ext4 37G 1000M 35G 3% / tmpfs tmpfs 491M 0 491M 0% /dev/shm /dev/sda1 ext4 477M 52M 400M 12% /boot /dev/md0 ext4 3.9G 8.0M 3.7G 1% /mnt/raid0 [root@centos6-node3 ~]# cp /tmp/* /mnt/raid0/ -r #写文件 cp: overwrite `/mnt/raid0/yum.log'? y [root@centos6-node3 ~]# ll /mnt/raid0/ #查看 total 24 drwx------ 2 root root 16384 Dec 9 01:16 lost+found drwxr-xr-x 3 root root 4096 Dec 9 01:18 opt drwxr-xr-x 2 root root 4096 Dec 9 01:18 script -rw------- 1 root root 0 Dec 9 01:18 yum.log [root@centos6-node3 ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg_centos6node3-lv_root 38645208 1023300 35652144 3% / tmpfs 502056 0 502056 0% /dev/shm /dev/sda1 487652 52916 409136 12% /boot /dev/md0 4058816 8212 3841096 1% /mnt/raid0
- 写入fstab
[root@centos6-node3 ~]# echo "/dev/md0 /mnt/raid0 ext4 deaults 0 0" >> /etc/fstab [root@centos6-node3 ~]# mount -av #挂载点检查 mount: UUID=5783f034-6989-4b6d-a187-3475e1a63eb8 already mounted on /boot mount: tmpfs already mounted on /dev/shm mount: devpts already mounted on /dev/pts mount: sysfs already mounted on /sys mount: proc already mounted on /proc mount: /dev/md0 already mounted on /mnt/raid0 nothing was mounted
- 保存Raid信息
[root@centos6-node3 ~]# mdadm -E -s -v >> /etc/mdadm.conf [root@centos6-node3 ~]# cat /etc/mdadm.conf ARRAY /dev/md/0 level=raid0 metadata=1.2 num-devices=2 UUID=e285a4e4:6f3f4984:853672cb:d24dfc1a name=centos6-node3:0 devices=/dev/sdc1,/dev/sdb1
至此:raid0搞完了
Raid1实操
- 将原先的raid0干掉:
- 卸载
- 删除
- 清fstab,/etc/mdadm.conf
[root@centos6 ~]#umount /mnt/raid0 <==取消挂载 [root@centos6 ~]#mdadm -S /dev/md0 <==停止设备 mdadm: stopped /dev/md0 [root@centos6 ~]#mdadm --zero-superblock /dev/sdb1 <==删除raid信息 [root@centos6 ~]#mdadm --zero-superblock /dev/sdc1
- 创建/dev/sd[b-c]1 并完成格式化
参考raid0即可[root@centos6-node3 ~]# fdisk /dev/sdb [root@centos6-node3 ~]# fdisk /dev/sdc [root@centos6-node3 ~]# mdadm -E /dev/sd[b-c] [root@centos6-node3 ~]# mdadm -E /dev/sd[b-c]1
- 创建raid设备
[root@centos6-node3 ~]# mdadm -C /dev/md1 -l raid1 -n 2 /dev/sd[b-c]1 ###查状态的两种姿势 [root@centos6-node3 ~]# mdadm -D /dev/md1 [root@centos6-node3 ~]# mdadm -E /dev/sd[b-c]1 [root@centos6-node3 ~]# mkfs.ext4 /dev/md1 #格式化
- 挂载并测试
[root@centos6-node3 ~]# mkdir /mnt/raid1 [root@centos6-node3 ~]# mount /dev/md1 /mnt/raid1/ [root@centos6-node3 ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg_centos6node3-lv_root 38645208 1021344 35654100 3% / tmpfs 502056 0 502056 0% /dev/shm /dev/sda1 487652 52916 409136 12% /boot /dev/md1 2029712 3072 1921872 1% /mnt/raid1 [root@centos6-node3 ~]# dd if=/dev/zero of=/mnt/raid1/f1 bs=1M count=200 #文件写入测试 [root@centos6-node3 ~]# ll /mnt/raid1/ -h #查看写入的文件 total 201M -rw-r--r-- 1 root root 200M Dec 9 02:15 f1 drwx------ 2 root root 16K Dec 9 02:13 lost+found
Raid5
- 清掉上述的raid1信息
[root@centos6 ~]#umount /mnt/raid1 <==取消挂载 [root@centos6 ~]#mdadm -S /dev/md1 <==停止设备 mdadm: stopped /dev/md1 [root@centos6 ~]#mdadm --zero-superblock /dev/sdb1 <==删除raid信息 [root@centos6 ~]#mdadm --zero-superblock /dev/sdc1
- 创建raid5,4块盘,一块备用
[root@centos6-node3 ~]# mdadm -C /dev/md5 -l raid5 -n 3 -x 1 /dev/sd[b-e]1
- 挂盘&测试
[root@centos6-node3 ~]# mkfs.ext4 /dev/md5 [root@centos6-node3 ~]# mkdir /mnt/raid5 [root@centos6-node3 ~]# mount /dev/md5 /mnt/raid5 [root@centos6-node3 ~]# dd if=/dev/zero of=/mnt/raid5/f1 bs=1M count=200 200+0 records in 200+0 records out 209715200 bytes (210 MB) copied, 0.443447 s, 473 MB/s [root@centos6-node3 ~]# ll /mnt/raid5/ total 204816 -rw-r--r-- 1 root root 209715200 Dec 9 02:29 f1 drwx------ 2 root root 16384 Dec 9 02:28 lost+found
Raid10实操
有三步操作:
- 清掉上述的raid1信息
- 清空上述的raid5信息
- 创建raid0,raid1,两个md
- 合并raid0,raid1,就好
- 清空raid5信息
[root@centos6-node3 ~]# umount /mnt/raid5/ [root@centos6-node3 ~]# mdadm -S /dev/md5 mdadm: stopped /dev/md5 [root@centos6-node3 ~]# mdadm --zero-superblock /dev/sd[b-e]1 [root@centos6-node3 ~]# mdadm -E /dev/sd[b-e]1 #测试成功 mdadm: No md superblock detected on /dev/sdb1. mdadm: No md superblock detected on /dev/sdc1. mdadm: No md superblock detected on /dev/sdd1. mdadm: No md superblock detected on /dev/sde1.
- 搞一对个raid1
[root@centos6-node3 ~]# mdadm -C /dev/md1 -l raid1 -n 2 /dev/sd[b-c]1 [root@centos6-node3 ~]# mdadm -C /dev/md2 -l raid1 -n 2 /dev/sd[e-f]1
- 将两个raid1合并成raid0(这样就高出了raid10)
[root@centos6-node3 ~]# mdadm -C /dev/md10 -l raid0 -n 2 /dev/md1 /dev/md2 1 9 2 1 active sync /dev/md2 [root@centos6-node3 ~]# cat /proc/mdstat #查看状态 Personalities : [raid0] [raid1] [raid6] [raid5] [raid4] md10 : active raid0 md2[1] md1[0] 4188160 blocks super 1.2 512k chunks
- 清空raid5信息
md2 : active raid1 sdf1[1] sde1[0]
2095424 blocks super 1.2 [2/2] [UU]
md1 : active raid1 sdc1[1] sdb1[0]
2095424 blocks super 1.2 [2/2] [UU]
- 挂载&测试
``` bash
[root@centos6-node3 ~]# mkdir /mnt/raid10
[root@centos6-node3 ~]# mkfs.ext4 /dev/md10
[root@centos6-node3 ~]# mount /dev/md10 /mnt/raid10/
[root@centos6-node3 ~]# dd if=/dev/zero of=/mnt/raid10/f1 bs=1M count=200
200+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 0.312253 s, 672 MB/s
- 所有的raid操作搞完之后就需要保存配置
[root@centos6-node3 ~]# mdadm -D -s -v >> /etc/mdadm.conf
超级简单的Raid10制作方法
- 清除上述老版本的raid10信息
[root@centos6-node3 ~]# umount /mnt/raid10/ [root@centos6-node3 ~]# mdadm -S /dev/md10 mdadm: stopped /dev/md10 [root@centos6-node3 ~]# mdadm -S /dev/md1 mdadm: stopped /dev/md1 [root@centos6-node3 ~]# mdadm -S /dev/md2 mdadm: stopped /dev/md2 [root@centos6-node3 ~]# mdadm --zero-superblock /dev/sd[b-c]1 mdadm: Unrecognised md component device - /dev/sdb1 mdadm: Unrecognised md component device - /dev/sdc1 [root@centos6-node3 ~]# mdadm --zero-superblock /dev/sd[d-e]1 #查看状态 mdadm: Unrecognised md component device - /dev/sdd1 mdadm: Unrecognised md component device - /dev/sde1 [root@centos6-node3 ~]# mdadm -E /dev/sd[b-e]1 mdadm: No md superblock detected on /dev/sdb1. mdadm: No md superblock detected on /dev/sdc1. mdadm: No md superblock detected on /dev/sdd1. mdadm: No md superblock detected on /dev/sde1. [root@centos6-node3 ~]# rm -fr /dev/md1 [root@centos6-node3 ~]# rm -fr /dev/md2 [root@centos6-node3 ~]# rm -fr /dev/md0 [root@centos6-node3 ~]# rm -fr /dev/md10 [root@centos6-node3 ~]# rm -fr /dev/md5
- 一步到位做法:
[root@centos6-node3 ~]# mdadm -C /dev/md10 -l raid10 -n 4 /dev/sd[b-e]1 #直接搞 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md10 started. [root@centos6-node3 ~]# mkfs.ext4 /dev/md10 #格式化
- 挂载&测试
[root@centos6-node3 ~]# mkdir /mnt/raid10/ [root@centos6-node3 ~]# mount /dev/md10 /mnt/raid10/ [root@centos6-node3 ~]# dd if=/dev/zero of=/mnt/raid10/f2 bs=1M count=1024 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied, 2.70463 s, 397 MB/s
最后小结
所有操作完成之后,一定要有两个操作:
- 将raid信息写入fstab,以防断电重启掉盘
- 所有的raid操作搞完之后就需要保存配置
[root@centos6-node3 ~]# mdadm -D -s -v >> /etc/mdadm.conf
Raid故障解决
Raid测试方案
演示一下raid1的测试,需要两块工作盘和一块备用盘;完成好相关的分区就行,先别挂载。
- 工具:mdadm --manage
[root@centos6-node3 ~]# mdadm --manage --help Usage: mdadm arraydevice options component devices... This usage is for managing the component devices within an array. The --manage option is not needed and is assumed if the first argument is a device name or a management option. The first device listed will be taken to be an md array device, any subsequent devices are (potential) components of that array. Options that are valid with management mode are: --add -a : hotadd subsequent devices to the array --re-add : subsequent devices are re-added if there were : recent members of the array --remove -r : remove subsequent devices, which must not be active --fail -f : mark subsequent devices a faulty --set-faulty : same as --fail --replace : mark device(s) to be replaced by spares. Once : replacement completes, device will be marked faulty --with : Indicate which spare a previous '--replace' should : prefer to use --run -R : start a partially built array --stop -S : deactivate array, releasing all resources --readonly -o : mark array as readonly --readwrite -w : mark array as readwrite
- 功能:
- 重新将设备加入到磁盘阵列
- 将设备标记为故障
- 从阵列中删除设备
- 使用备用设备更换故障设备
- 先停止部分阵列
- 停止阵列
- 标记阵列为ro或者rw
为raid阵列添加设备
- 先创建一个raid1
- 清空上述的raid10
[root@centos6-node3 ~]# mdadm -S /dev/md10 mdadm: stopped /dev/md10 [root@centos6-node3 ~]# mdadm --zero-superblock /dev/sd[b-e]1 [root@centos6-node3 ~]# mdadm -E /dev/sd[b-e]1
- 清空上述的raid10
- 新建raid1
[root@centos6-node3 ~]# mdadm -C /dev/md1 -l raid1 -n 2 /dev/sd[b-c]1 [root@centos6-node3 ~]# mkfs.ext4 /dev/md1 [root@centos6-node3 ~]# mount /dev/md1 /mnt/raid1
- 添加raid设备
[root@centos6-node3 ~]# mdadm --manage /dev/md1 --add /dev/sdd1 [root@centos6-node3 ~]# mdadm --manage /dev/md1 --add /dev/sde1
将一块磁盘标记为故障并清理出阵列
标记故障:
[root@centos6-node3 ~]# mdadm --manage /dev/md1 --fail /dev/sdd1 mdadm: set /dev/sdd1 faulty in /dev/md1 [root@centos6-node3 ~]# mdadm -D /dev/md1 [root@centos6-node3 ~]# mdadm --manage /dev/md1 --fail /dev/sde1
彻底清除:因为标记故障的时候还没清理
[root@centos6-node3 ~]# mdadm --manage /dev/md1 --remove /dev/sdd1 [root@centos6-node3 ~]# mdadm -D /dev/md1 #查看结果
重新添加设备,来替代已经移除的设备
刚才移除了一块/dev/sdd1
- 现在我们把丫给挂上去:
[root@centos6-node3 ~]# mdadm --manage /dev/md1 --re-add /dev/sdd1 mdadm: --re-add for /dev/sdd1 to /dev/md1 is not possible
傻了吧,报错啦!
因为阵列中的磁盘已经达到了最大的数量。因此,我们有两个选择:a)将 /dev/sdb1 添加为备用的,如例1;或 b)从阵列中删除 /dev/sdd1 然后重新添加 /dev/sdb1。
我们选择选项 b),先停止阵列然后重新启动:
[root@centos6-node3 ~]# umount /mnt/raid1
[root@centos6-node3 ~]# mdadm --stop /dev/md1
[root@centos6-node3 ~]# mdadm --assemble /dev/md1 /dev/sdd1 /dev/sde1
mdadm: /dev/md1 assembled from 0 drives and 2 spares - not enough to start the array. #完犊子了
[root@centos6-node3 ~]# mdadm -C /dev/md1 -l raid1 -n 2 /dev/sd[b-c]1 #重启raid1
[root@centos6-node3 ~]# mdadm -D /dev/md1
[root@centos6-node3 ~]# mdadm --manage /dev/md1 --add /dev/sdd1
[root@centos6-node3 ~]# mount /dev/md1 /mnt/raid1
[root@centos6-node3 ~]# ll /mnt/raid1 #原来的数据还在
total 1048596
-rw-r--r-- 1 root root 1073741824 Dec 9 03:31 f2
drwx------ 2 root root 16384 Dec 9 03:27 lost+found
使用特定磁盘更换raid设备
[root@centos6-node3 ~]# mdadm --manage /dev/md1 --replace /dev/sdd1 --with /dev/sdc1
-----END