keepalived结合MHA实现mysql高可用
本例结合本博另一篇文章 MHA-大杀器实现MYSQL单主宕机时,VIP漂移实现高可用,环境请参考上一篇文章
本例使用keepalived-1.1.20.tar.gz版本
[root@node1 keepalived-1.1.20]# ./configure --sysconf=/etc/ #此项表示设置keepalived的根目录
如果编译报错“configure: error: Popt libraries is required” 则yum -y install popt-devel
[root@node1 keepalived-1.1.20]# make && make install
[root@node1 keepalived]# cp /usr/local/sbin/keepalived /usr/sbin/ #拷贝命令文件
#####################主机的配置文件设置####################
[root@node1 keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { #全局配置项
router_id node1 #本机标识号,设置全局唯一
}
vrrp_script check_run { #设置检测服务的脚本
script "/root/check_mysql.sh"
interval 1 #每隔一秒检测一次
}
vrrp_instance mha { #定义keepalived组
state MASTER #定义本机的keepalived状态
interface eth0 #设置监听网卡
virtual_router_id 100 #设置虚拟router-id,所有keepalived的机器都得是一样
priority 100 #设置权值,master必须高于backup
advert_int 5 #定义master与backup之间同步检查间隔,单位是秒
track_interface { #设置监听网卡组
eth0
eth1
}
authentication { #设置keepalived组的验证类型和密码,所有主机须一样
auth_type PASS
auth_pass 1111
}
track_script { #设置服务监测脚本运行
check_run
}
virtual_ipaddress { #设置VIP
10.2.16.250/24
}
}
#####################备机的配置文件设置####################
[root@node2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id node2
}
vrrp_script check_run {
script "/root/check_mysql.sh"
interval 1
}
vrrp_instance mha {
state BACKUP
interface eth0
virtual_router_id 100
priority 99
advert_int 5
track_interface {
eth0
eth1
}
track_interface {
eth0
eth1
}
track_script {
check_run
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.2.16.250/24
}
}
mysql服务检测脚本内容,按需修改:
[root@node2 ~]# cat check_mysql.sh
#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=127.0.0.1
MYSQL_USER=root
MYSQL_PASSWORD=123456
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 -e "show status;" --password=123456 >/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
pkill keepalived
exit 1
fi
sleep 1
done
启动Keepalived:
[root@node1 ~]#service keepalived start
启动日志:
[root@node1 ~]# tail -f /var/log/messages
Jun 27 11:48:47 node2 Keepalived: Starting VRRP child process, pid=21545
Jun 27 11:48:47 node2 Keepalived_vrrp: Registering Kernel netlink reflector
Jun 27 11:48:47 node2 Keepalived_vrrp: Registering Kernel netlink command channel
Jun 27 11:48:47 node2 Keepalived_vrrp: Registering gratutious ARP shared channel
Jun 27 11:48:47 node2 Keepalived_vrrp: Opening file ‘/etc/keepalived/keepalived.conf‘.
Jun 27 11:48:47 node2 Keepalived_vrrp: Configuration is using : 65535 Bytes
Jun 27 11:48:47 node2 Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Jun 27 11:48:47 node2 Keepalived_vrrp: VRRP_Instance(mha) Entering BACKUP STATE
Jun 27 11:48:47 node2 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(9,10)]
Jun 27 11:48:47 node2 Keepalived_vrrp: VRRP_Script(check_run) succeeded
Jun 27 11:49:03 node2 Keepalived_vrrp: VRRP_Instance(mha) Transition to MASTER STATE
Jun 27 11:49:08 node2 Keepalived_vrrp: VRRP_Instance(mha) Entering MASTER STATE
Jun 27 11:49:08 node2 Keepalived_vrrp: VRRP_Instance(mha) setting protocol VIPs.
Jun 27 11:49:08 node2 Keepalived_vrrp: VRRP_Instance(mha) Sending gratuitous ARPs on eth0 for 10.2.16.250
Jun 27 11:49:13 node2 Keepalived_vrrp: VRRP_Instance(mha) Sending gratuitous ARPs on eth0 for 10.2.16.250
查看 VIP:
[root@node1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c5:22:55 brd ff:ff:ff:ff:ff:ff
inet 10.2.16.253/24 brd 10.2.16.255 scope global eth0
inet 10.2.16.250/24 brd 10.2.16.255 scope global eth0
inet6 fe80::20c:29ff:fec5:2255/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c5:22:5f brd ff:ff:ff:ff:ff:ff
inet 10.0.0.1/24 brd 10.0.0.255 scope global eth1
inet6 fe80::20c:29ff:fec5:225f/64 scope link
valid_lft forever preferred_lft forever
此VIP用ifconfig看不到,但是真实存在的。
####check_mysql.sh 需要在两台机器上都存在
###############keepalived+mha+check_mysql运行原理#################
实现VIP飘移,需要将node1的Mysql服务停掉,此时keepalived中运行的脚本会检测mysql,检测到mysql挂了之后,会pkill keepalived
当BACKUP-keepalived检测到主keepalived挂掉之后,会自动启动VIP,而MHA检测到master的mysql挂掉之后,也会将Master移至BAKCUP上
此时所有的权限都移至Mysql-backup上,并且他已经成为MASTER,接替宕机的master所有工作,从而实现高可用。
本文出自 “Fate” 博客,请务必保留此出处http://czybl.blog.51cto.com/4283444/1431693
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。