2009年4月7日星期二

heartbeat+DRBD+MySQL(一)


最近测试了下使用heartbeat+DRBD+MySQL来实现mysql的高可用性,规划如下图。无论那台机器死了 另一台都可以监控到并在设定的时间内接管对方的服务 这样就保证了mysql的高可用性,



测试之前 先按照我之前写的“一张盘安装centos5.2”安装好系统,
具体测试环境如下:

虚拟机 虚拟多一块已硬盘 和多一张虚拟网卡
系统 centos5.2安装在虚拟机里

第一台虚拟机器
node-a 主服务器
eth0 192.168.1.11
eth0 192.168.1.10 虚拟ip 由HeartBeat控制接管作为主要提供mysql访问的ip
eth1 192.168.100.11 提供给DRBD传输数据使用

第二台虚拟机器
node-b 次服务器
eth0 192.168.1.12
eth0 192.168.1.10 虚拟ip 由HeartBeat控制接管作为主要提供mysql访问的ip
eth1 192.168.100.12 提供给DRBD传输数据使用


系统分区采用lvm模式

[root@node-a ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-logV0100
7.7G 601M 6.7G 9% /
/dev/hda1 99M 12M 83M 13% /boot
tmpfs 125M 0 125M 0% /dev/shm



[root@node-a ~]# lvdisplay -C
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
LogVol01 VolGroup00 -wi-ao 512.00M
logV0100 VolGroup00 -wi-ao 7.88G
lvol0 VolGroup00 -wi-a- 3.47G <---这个分区就是留给drbd使用的 安装的时候先不要格式化


安装drbd


[root@node-a yum.repos.d]# yum -y install drbd82 kmod-drbd82

[root@node-a yum.repos.d]# rpm -qi kmod-drbd82 <--- 查看kmod-drbd82对应的内核版本 Name : kmod-drbd82 Relocations: (not relocatable) Version : 8.2.6 Vendor: CentOS
Release : 2 Build Date: Sat 04 Oct 2008 12:46:57 AM CST
Install Date: Fri 27 Mar 2009 11:56:57 PM CST Build Host: c5-i386-build Group : System Environment/Kernel Source RPM: drbd82-kmod-8.2.6-2.src.rpm Size : 2047377 License: GPL
Signature : DSA/SHA1, Sat 04 Oct 2008 12:47:52 AM CST, Key ID a8a447dce8562897 URL : http://www.drbd.org/ Summary : drbd82 kernel module(s) Description :
This package provides the drbd82 kernel modules built for the
Linux kernel 2.6.18-92.1.13.el5 for the i686 family of processors.<--- 这里显示出来的是kernel 2.6.18-92.1.13.el5 所以我们更新下内核版本
[root@node-a yum.repos.d]# uname -r <--- 查看但前系统使用的内核版本 2.6.18-92.el5 [root@node-a yum.repos.d]# rpm -q kernel kernel-2.6.18-92.el5 kernel-2.6.18-92.1.22.el5
[root@node-a yum.repos.d]# yum -y install yum-allowdowngrade
[root@node-a yum.repos.d]# yum --allow-downgrade -y install kernel-2.6.18-92.1.13.el5 安装上drbd对应的内核版本 reboot 重启机器
[root@node-a ~]# uname -r 2.6.18-92.1.13.el5 <--- 内核版本已经正确对应上了


设置node-a的drbd配置了

[root@node-a ~]# vi /etc/drbd.conf
#
# please have a a look at the example configuration file in
# /usr/share/doc/drbd82/drbd.conf
#
global {
minor-count 1;
#usage-count yes;
}

common { syncer { rate 100M; } }


resource mysql {
protocol C; # There are A, B and C protocols. Stick with C.
# incon-degr-cmd "echo 'DRBD Degraded!' | wall; sleep 60 ; halt -f";
# If a cluster starts up in degraded mode, it will echo a message to all
# users. It'll wait 60 seconds then halt the system.

net {
cram-hmac-alg sha1;
shared-secret "FooFunFactory";
}

on node-a {
device /dev/drbd0; # The name of our drbd device.
disk /dev/VolGroup00/lvol0; # Partition we wish drbd to use.
address 192.168.100.11:7789; # node0 IP address and port number.
meta-disk internal; # Stores meta-data in lower portion of sdb1.
}

on node-b {
device /dev/drbd0; # Our drbd device, must match node0.
disk /dev/VolGroup00/lvol0; # Partition drbd should use.
address 192.168.100.12:7789; # IP address of node1, and port number.
meta-disk internal; #Stores meta-data in lower portion of sdb1.
}

disk {
on-io-error detach; # What to do when the lower level device errors.
}


startup {
wfc-timeout 0; # drbd init script will wait infinitely on resources.
degr-wfc-timeout 120; # 2 minutes.
}
} # End of resource mysql



安装一些相关的套件


yum -y install mysql-server.i386 which ntsysv ntp crontabs


安装heartbeat


yum -y install heartbeat 出现错误提示
useradd: user hacluster exists
error: %pre(heartbeat-2.1.3-3.el5.centos.i386) scriptlet failed, exit status 9
error: install: %pre scriptlet failed (2), skipping heartbeat-2.1.3-3.el5.centos

再安装一次 yum -y install heartbeat 就好了

node-a 执行
drbdadm create-md mysql <--- drbd 初始化
启动drbd
[root@node-a ~]# /etc/rc.d/init.d/drbd start Starting DRBD resources: [ d(mysql) s(mysql) n(mysql) ]. .......... *************************************************************** 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 120 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 'mysql'; 0 sec -> wait forever)
To abort waiting enter 'yes' [ 17]:yes <-- 输入yes
chkconfig drbd on 设置开机启动 drbd
设置node-a 防火墙不要开启 SELinux disabled 掉
shutdown -h now 把node-a 关机


为了方便 可以直接把node-a的虚拟机器所在的目录copy一份出来 做为node-b 这样就不用重新安装系统了 启动node-b 后 登陆机器 修改一下配置
1.修改 /etc/hosts and /etc/sysconfig/networking 设置机器名为node-b
2.修改两张网卡为正确的ip
3.重启启动node-b机器


将node-a机器重启开机

[root@node-a ~]# cat /proc/drbd <-- 查看drbd的状态 自己是Secondary 对方为Unknown version: 8.2.6 (api:88/proto:86-88) GIT-hash: 3e69822d3bb4920a8c1bfdf7d647169eba7d2eb4 build by buildsvn@c5-i386-build, 2008-10-03 11:42:32 0: cs:WFConnection st:Secondary/Unknown ds:Inconsistent/DUnknown C r--- ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 oos:3637100 Inconsistent 表示还没有同步


登录node-b机器
编辑
[root@node-b ~]# vi /etc/drbd.conf
#
# please have a a look at the example configuration file in
# /usr/share/doc/drbd82/drbd.conf
#
global {
minor-count 1;
#usage-count yes;
}

common { syncer { rate 100M; } }


resource mysql {
protocol C; # There are A, B and C protocols. Stick with C.
# incon-degr-cmd "echo 'DRBD Degraded!' | wall; sleep 60 ; halt -f";
# If a cluster starts up in degraded mode, it will echo a message to all
# users. It'll wait 60 seconds then halt the system.

net {
cram-hmac-alg sha1;
shared-secret "FooFunFactory";
}

on node-a {
device /dev/drbd0; # The name of our drbd device.
disk /dev/VolGroup00/lvol0; # Partition we wish drbd to use.
address 192.168.100.11:7789; # node0 IP address and port number.
meta-disk internal; # Stores meta-data in lower portion of sdb1.
}

on node-b {
device /dev/drbd0; # Our drbd device, must match node0.
disk /dev/VolGroup00/lvol0; # Partition drbd should use.
address 192.168.100.12:7789; # IP address of node1, and port number.
meta-disk internal; #Stores meta-data in lower portion of sdb1.
}

disk {
on-io-error detach; # What to do when the lower level device errors.
}


startup {
wfc-timeout 0; # drbd init script will wait infinitely on resources.
degr-wfc-timeout 120; # 2 minutes.
}
} # End of resource mysql

同样执行
drbdadm create-md mysql

/etc/rc.d/init.d/drbd start

[root@node-b ~]# cat /proc/drbd <-- 状态是Secondary version: 8.2.6 (api:88/proto:86-88) GIT-hash: 3e69822d3bb4920a8c1bfdf7d647169eba7d2eb4 build by buildsvn@c5-i386-build, 2008-10-03 11:42:32 0: cs:Connected st: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 oos:3637100

0 评论: