学海无涯 个人blog lnmps.com 新站
分类: LINUX
2013-08-07 23:18:02
2011-12-10 09:50:11| 分类: 服务器集群 |字号
一、keepalived主备配置文件keepalived.conf
MasterDB的配置:
! Configuration File for keepalived
global_defs {
notification_email {
houzb@flyfot.cn
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server smtp.flyfot.cn
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_run {
script "/root/keepalived_check_mysql.sh"
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_run
}
virtual_ipaddress {
10.8.10.110
}
}
virtual_server 10.8.10.110 3306 {
delay_loop 6
lb_algo wcl
lb_kind DR
#nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 10.8.10.111 3306 {
weight 1
notify_down /root/mysql_down.sh
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 10.8.10.112 3306 {
weight 1
notify_down /root/mysql_down.sh
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
SlaveDB的配置:
! Configuration File for keepalived
global_defs {
notification_email {
houzb@flyfot.cn
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server smtp.flyfot.cn
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_run {
script "/root/keepalived_check_mysql.sh"
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_run
}
virtual_ipaddress {
10.8.10.110
}
}
virtual_server 10.8.10.110 3306 {
delay_loop 6
lb_algo wcl
lb_kind DR
#nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 10.8.10.111 3306 {
weight 1
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 10.8.10.112 3306 {
weight 1
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
二、mysql状态检测脚本/root/bin/keepalived_check_mysql.sh
主备相同
!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=1234
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1
if [ $?=0 ];then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ]; then
CHECK_TIME=0
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
/etc/init.d/keepalived stop
exit 1
fi
sleep 1
done
检测数据库关闭时执行的脚本mysql_down.sh:
!/bin/bash
service keepalived stop
三、vrrp协议 使用224.0.0.18地址组播
iptables -I INPUT -d 224.0.0.18 -j ACCEPT
四、操作
1.安装mysql,设置双机热备
2.安装keepalived然后进行相应配置
3.进行测试
注:两台mysql可以同步,停止keepalived可以跳转,停止MasterDB的
mysql服务可以跳转,MasterDB会抢做主服务器(没设置nopreempt——不抢占)