存储高可用――DRBD
DRDB官网:http://drbd.linbit.com/home/what-is-drbd/
拓扑图说明:
分别在两台机器上实现网络 raid0 功能
试验环境:workstation 10
操作系统:rhel6.2(64位)
内核版本:2.6.32-220.el6.x86_64
两台节点配置
节点角色 |
主机名 | IP地址 |
DRBD分区 |
Primary节点 | node01 | 192.168.1.12 | /dev/sdb1 |
Secondary节点 | node02 | 192.168.1.13 | /dev/sdb1 |
软件下载列表地址: http://oss.linbit.com/drbd/
软件版本:drbd-8.4.3.tar.gz
------------------------------------以下是node01上的操作------------------------------------
[root@localhost ~]# sed -i 's/HOSTNAME=localhost.localdomain/HOSTNAME=node01/g' /etc/sysconfig/network #修改主机名
[root@localhost ~]# echo -e "192.168.1.12 node01\n192.168.1.13 node02">>/etc/hosts #设置域名临时解析
[root@localhost ~]# cat>fdisk.sh #建立自动分区脚本 #!/bin/bash fdisk /dev/sdb <<EOF n p 1 1 wq EOF
[root@localhost ~]# sh fdisk.sh #分区
[root@localhost ~]# fdisk -l|grep "sd" Disk /dev/mapper/VolGroup-lv_root doesn't contain a valid partition table Disk /dev/mapper/VolGroup-lv_swap doesn't contain a valid partition table Disk /dev/sda: 10.7 GB, 10737418240 bytes /dev/sda1 * 1 64 512000 83 Linux /dev/sda2 64 1306 9972736 8e Linux LVM Disk /dev/sdb: 16.1 GB, 16106127360 bytes /dev/sdb1 1 1958 15727603+ 83 Linux
注:/dev/sdb1为我上一步所分的区
[root@localhost ~]# mkdir /data #建立分区的挂载目录
[root@localhost ~]# reboot #这里最好重启下,目的是使刚才的设置生效
[root@node01 ~]# uname -r #查看内核版本 2.6.32-220.el6.x86_64
[root@node01 ~]# yum install -y kernel-headers kernel-devel flex warning: kernel-headers-2.6.32-220.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Preparing... ########################################### [100%] 1:kernel-headers ########################################### [100%]
注:kernel-devel和kernel-headers版本要和uname -r显示的内核版本一致,建议用本地源安装
[root@node01 ~]# yum install -y wget #安装wget工具
[root@node01 ~]# wget http://oss.linbit.com/drbd/8.4/drbd-8.4.3.tar.gz #下载drbd软件
[root@node01 ~]# tar xvf drbd-8.4.3.tar.gz #解压软件包 drbd-8.4.3/.gitignore drbd-8.4.3/COPYING drbd-8.4.3/ChangeLog ......
[root@node01 drbd-8.4.3]# yum install -y *gcc* #安装编译工具
[root@node01 drbd-8.4.3]# ./configure --prefix=/usr/local/drbd --with-km
注意:--with-km是启用内核模块
[root@node01 drbd-8.4.3]# make KDIR=/usr/src/kernels/2.6.32-220.el6.x86_64/
注:KDIR的路径 (这个内核源码路径需要根据自己的系统修改)
[root@node01 drbd-8.4.3]# make install
[root@node01 drbd-8.4.3]# mkdir -p /usr/local/drbd/var/run/drbd [root@node01 drbd-8.4.3]# cp /usr/local/drbd/etc/rc.d/init.d/drbd /etc/rc.d/init.d/ [root@node01 drbd-8.4.3]# chkconfig --add drbd [root@node01 drbd-8.4.3]# chkconfig drbd on
以下是安装drbd模块:
[root@node01 drbd-8.4.3]# cd drbd [root@node01 drbd]# make clean rm -rf .tmp_versions Module.markers Module.symvers modules.order rm -f *.[oas] *.ko .*.cmd .*.d .*.tmp *.mod.c .*.flags .depend .kernel* rm -f compat/*.[oas] compat/.*.cmd [root@node01 drbd]# make KDIR=/usr/src/kernels/2.6.32-220.el6.x86_64/ Calling toplevel makefile of kernel source tree, which I believe is in KDIR=/usr/src/kernels/2.6.32-220.el6.x86_64/ make -C /usr/src/kernels/2.6.32-220.el6.x86_64/ SUBDIRS=/root/drbd-8.4.3/drbd modules make[1]: Entering directory `/usr/src/kernels/2.6.32-220.el6.x86_64' CC [M] /root/drbd-8.4.3/drbd/drbd_buildtag.o CC [M] /root/drbd-8.4.3/drbd/drbd_bitmap.o CC [M] /root/drbd-8.4.3/drbd/drbd_proc.o CC [M] /root/drbd-8.4.3/drbd/drbd_worker.o ......
[root@node01 drbd]# cp drbd.ko /lib/modules/`uname -r`/kernel/lib/ [root@node01 drbd]# modprobe drbd #加载drbd模块 [root@node01 drbd]# lsmod |grep drbd #验证模块加载是否成功 drbd 328626 0 libcrc32c 1246 1 drbd
以下是drbd配置部分:
[root@node01 etc]# pwd /usr/local/drbd/etc [root@node01 etc]# more drbd.conf #主配置文件里面已经包含了全局配置文件和drbd目录下以.res结尾文件,所需要修改全局配置文件和建立.res文件即可。 # You can find an example in /usr/share/doc/drbd.../drbd.conf.example include "drbd.d/global_common.conf"; include "drbd.d/*.res";
[root@node01 drbd.d]# vi global_common.conf #修改drbd全局配置文件
global {
usage-count no;
# minor-count dialog-refresh disable-ip-verification
}
common {
protocol C;#使用drbd的同步协议
handlers {
# These are EXAMPLE handlers only.
# They may have severe implications,
# like hard resetting the node under certain circumstances.
# Be careful when chosing your poison.
pri-on-incon-degr "/usr/lib/drbd/notify-pri-on-incon-degr.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f"; #去掉注释
pri-lost-after-sb "/usr/lib/drbd/notify-pri-lost-after-sb.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f"; #去掉注释
local-io-error "/usr/lib/drbd/notify-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ; halt -f"; #去掉注释
# fence-peer "/usr/lib/drbd/crm-fence-peer.sh";
# split-brain "/usr/lib/drbd/notify-split-brain.sh root";
# out-of-sync "/usr/lib/drbd/notify-out-of-sync.sh root";
# before-resync-target "/usr/lib/drbd/snapshot-resync-target-lvm.sh -p 15 -- -c 16k";
# after-resync-target /usr/lib/drbd/unsnapshot-resync-target-lvm.sh;
}
startup {
# wfc-timeout degr-wfc-timeout outdated-wfc-timeout wait-after-sb
}
options {
# cpu-mask on-no-data-accessible
}
disk {
on-io-error detach;#配置I/O错误处理策略为分离
rate 50M; #设置主备节点同步时的网络速率
}
net {
# protocol timeout max-epoch-size max-buffers unplug-watermark
# connect-int ping-int sndbuf-size rcvbuf-size ko-count
# allow-two-primaries cram-hmac-alg shared-secret after-sb-0pri
# after-sb-1pri after-sb-2pri always-asbp rr-conflict
# ping-timeout data-integrity-alg tcp-cork on-congestion
# congestion-fill congestion-extents csums-alg verify-alg
# use-rle}
注:加黑字部分为修改或添加
[root@node01 drbd.d]#touch /usr/local/drbd/etc/drbd.d/drbd.res #建立资源配置文件
[root@node01 drbd.d]# more drbd.res
resource r1 { on node01 { address 192.168.1.12:7801;#设置DRBD的监听端口,用于与另一台主机通信。 volume 0 { device /dev/drbd1; disk /dev/sdb1; meta-disk internal; } } on node02 { address 192.168.1.13:7801;#设置DRBD的监听端口,用于与另一台主机通信。 volume 0 { device /dev/drbd1; disk /dev/sdb1; meta-disk internal; } } }
[root@node01 drbd.d]# drbdadm create-md all #初始化资源 Writing meta data... initializing activity log NOT initializing bitmap New drbd meta data block successfully created.
--------------------------------------以上是node01上的操作----------------------------------------
node02上的操作和node01上的操作全部相同,在此就不重复了
[root@node01 drbd.d]# /etc/init.d/drbd start #node01启动drbd
Starting DRBD resources: [
create res: r1
prepare disk: r1
adjust disk: r1
adjust net: r1
]
..........
***************************************************************
DRBD's startup script waits for the peer node(s) to appear.
- In case this node was already a degraded cluster before the
reboot the timeout is 0 seconds. [degr-wfc-timeout]
- If the peer was available before the reboot the timeout will
expire after 0 seconds. [wfc-timeout]
(These values are for resource 'r1'; 0 sec -> wait forever)
To abort waiting enter 'yes' [ -- ]: [ 10]:[ 11]:[ 12]:
.
[root@node02 ~]# /etc/init.d/drbd start #node01启动drbd Starting DRBD resources: [ create res: r1 prepare disk: r1 adjust disk: r1 adjust net: r1 ]
[
[root@node01 ~]# netstat -lanput|grep 7801 #确认监听端口起来
tcp 0 0 192.168.1.12:7801 192.168.1.13:41242 ESTABLISHED -
tcp 0 0 192.168.1.12:50401 192.168.1.13:7801 ESTABLISHED -
[root@node02 ~]# netstat -lanput|grep 7801 #确认监听端口起来
tcp 0 0 192.168.1.13:7801 192.168.1.12:50401 ESTABLISHED -
tcp 0 0 192.168.1.13:41242 192.168.1.12:7801 ESTABLISHED -
root@node01 ~]# cat /proc/drbd
version: 8.4.3 (api:1/proto:86-101)
GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@node01, 2015-01-25 12:08:01
1: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r-----
ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:15727084
[root@node01 ~]# drbdadm -- --overwrite-data-of-peer primary all #设置node01为主节点 [root@node01 ~]# cat /proc/drbd
version: 8.4.3 (api:1/proto:86-101)
GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@node01, 2015-01-25 12:08:01
1: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r---n-
ns:197192 nr:0 dw:0 dr:201368 al:0 bm:11 lo:1 pe:2 ua:5 ap:0 ep:1 wo:f oos:15531500
[>....................] sync'ed: 1.3% (15164/15356)M
finish: 0:01:19 speed: 195,584 (195,584) K/sec
[root@node01 ~]# mkfs.ext4 /dev/drbd1 #格式化文件系统
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
983040 inodes, 3931771 blocks
......
[root@node01 ~]# mount /dev/drbd1 /data/ #挂载文件系统
[root@node01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
7.5G 1.3G 5.9G 18% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 485M 31M 429M 7% /boot
/dev/sr0 3.4G 3.4G 0 100% /iso
/dev/drbd1 15G 166M 14G 2% /data
[root@node01 ~]# cp /boot/. /data/ -rvf #模拟数据的写入
`/boot/./.vmlinuz-2.6.32-220.el6.x86_64.hmac' -> `/data/./.vmlinuz-2.6.32-220.el6.x86_64.hmac'
`/boot/./System.map-2.6.32-220.el6.x86_64' -> `/data/./System.map-2.6.32-220.el6.x86_64'
`/boot/./symvers-2.6.32-220.el6.x86_64.gz' -> `/data/./symvers-2.6.32-220.el6.x86_64.gz'
`/boot/./initramfs-2.6.32-220.el6.x86_64.img' -> `/data/./initramfs-2.6.32-220.el6.x86_64.img'
......
[root@node01 ~]# ls /data/
config-2.6.32-220.el6.x86_64 grub System.map-2.6.32-220.el6.x86_64
efi initramfs-2.6.32-220.el6.x86_64.img symvers-2.6.32-220.el6.x86_64.gz
vmlinuz-2.6.32-220.el6.x86_64
主从切换
以下是主从切换:因为上面设置的是node01为primary,node02为secondary,现在目标是将node01设置为secondary,将node02设置为primary:
首先在node01上执行以下命令:
[root@node01 ~]# ls /data/
config-2.6.32-220.el6.x86_64 grub lost+found System.map-2.6.32-220.el6.x86_64
efi initramfs-2.6.32-220.el6.x86_64.img symvers-2.6.32-220.el6.x86_64.gz vmlinuz-2.6.32-220.el6.x86_64
[root@node01 ~]# cat /proc/drbd
version: 8.4.3 (api:1/proto:86-101)
GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@node01, 2015-01-25 12:08:01
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:12 nr:8 dw:20 dr:2066 al:1 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
[root@node01 ~]# umount /data/ [root@node01 ~]# drbdadm secondary all
然后在node02上执行以下命令: [root@node02 ~]# cat /proc/drbd
version: 8.4.3 (api:1/proto:86-101)
GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@node02, 2015-01-25 13:16:26
1: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r-----
ns:8 nr:16 dw:24 dr:1033 al:1 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
[root@node02 ~]# drbdadm primary all
[root@node02 ~]# cat /proc/drbd
version: 8.4.3 (api:1/proto:86-101)
GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@node02, 2015-01-25 13:16:26
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:8 nr:16 dw:24 dr:1697 al:1 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
[root@node02 ~]# mount /dev/drbd1 /data/ #并不需要在 node02 上在对磁盘 mkfs.ext4
[root@node02 ~]# ls /data/ #biao #切换后看到拷贝的数据就表示成功
config-2.6.32-220.el6.x86_64 grub System.map-2.6.32-220.el6.x86_64
efi initramfs-2.6.32-220.el6.x86_64.img symvers-2.6.32-220.el6.x86_64.gz
vmlinuz-2.6.32-220.el6.x86_64
drbd脑裂的模拟借鉴:
http://zhangmaojun023.blog.163.com/blog/static/10919584620128270550813/
截图是对drbd的一些说明