使用MySQL主主复制技术+Keepalived是一种简单、便捷的解决方案,在高可用集群环境中,keepalived使用虚拟VIP,使 用Keepalived自带的服务监控功能和自定义脚本来实现MySQL故障时自动切换,非常灵活。如果有一台MySQL服务器死机,或工作出现故 障,keepalived将检测到,并将有故障的MySQL服务器从系统中去除。当MySQL服务器工作正常时,则自动将MySQL服务器加入到服务器集 群中,无需人工干预。下面环境为debugo01、debugo02两个节点,使用CentOS 6.6系统。MySQL server的版本是Percona Server 5.6.21,已经配置好了两边的MySQL实例。
1. 配置MySQL Multi-Master Replication
编辑my.cnf文件,添加相关复制选项。
Shell
vim /etc/my.cnf server-id=1002 log-bin=mysql-bin relay-log=mysqld-relay-bin binlog_format=mixed replicate_ignore_db=information_schema auto-increment-increment=2 auto-increment-offset=1 #debugo01 mysql> show master status \G *************************** 1. row *************************** File: mysql-bin.000001 Position: 648 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.01 sec) mysql> grant replication slave,replication client on *.* to rep@'%' identified by 'debugo'; mysql> flush privileges; #debugo02 mysql> grant replication slave,replication client on *.* to rep@'%' identified by 'debugo'; mysql> flush privileges; mysql> CHANGE MASTER TO MASTER_HOST='debugo01', MASTER_USER='rep', MASTER_PASSWORD='debugo', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=648; mysql> start slave; Query OK, 0 rows affected (0.01 sec) #debugo01 mysql> CHANGE MASTER TO MASTER_HOST='debugo02', MASTER_USER='rep', MASTER_PASSWORD='debugo', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=426; mysql> start slave;
检查日志,没有错误说明配置成功。
2. 配置keepalived
CentOS 6.6的光盘中已经带了keepalived-1.2.13-4的软件包,通过yum安装即可:
yum install keepalived完成后编辑keepalived.conf文件
vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id Keepalived_MySQL } vrrp_script check_run { script "/etc/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 100 advert_int 1 nopreempt track_script { check_run } authentication { auth_type PASS auth_pass debugo } virtual_ipaddress { 10.62.228.200 } }
配置说明(参考官方文档, Keepalived+LVS 集群的部署 和 keepalived.conf 文件详解 )
keepalived 的全局配置文件对keepalived起效的配置,而VRRPD 是keepalived 的核心,LVS配置只是需要使用keepalived来配置和管理LVS时需要使用,当然如果是只用keepalived 配置高可用集群是,LVS模块就不需要配置。配置文件都是以块的形式组织的,每个模块的配置都需要用 { } 的范围之内
在keepalived 的主配置文件中主要分为三个文本模块分别是:
全局模块(Global Configuration),VRRPD配置模块(VRRP 实例模块),LVS模块(虚拟服务模块);
1. 全局配置(Global Configuration)包括两个部分: 即全局定义(global definition) 和静态地址路由(Static ipaddress /routes)。 全局定义:主要是设置Keeplived 的通知机制和标识,同时也不建议用该方法进行状态监控,进行状态监控时nagios 是不二的选择。静态地址路由:该处是配置静态地址和静态路由的,换句话说不随着Vrrp instance的开关的变化而变化,当然VIP不是static IPaddress,会随着VRRPD而添加和删除,设置静态地址和静态路由的格式如:src $SRC_IP to $DST_IP dev $SRC_DEVICE 或者src $SRC_IP to $DST_IP via $GW dev $SRC_DEVICE2. VRRPD 配置包括三个部分: 分别是VRRP脚本(VRRP scripts) . VRRP同步组(syncgriuzation group) 和VRRP 实例(VRRP Instance):
2. VRRP scripts: 脚本会安周期性执行,每秒( 或者按照自己设定的时间间隔),被监控的实例会记录退出的代码,这些脚本被至少有被监控的实例的权重为o的实例才能执行,因此任意脚本可能的声明都不会被 系统所记录,如果没有特殊的设置,权重等于2,就意味着那些被监控的实例的优先级会被增加2,反过来,如果失败,则那些实例的优先级会被减去2
3. syncgriuzation group: 如果不使用 VRRP Sync Groups 如果keepalived 主机有两个网段,每个网段开启一个VRRP 实例,如果对外的网段出现问题,VRRPD认为自己仍然认为健康,因此 Master和Backup 相互切换,从而导致服务不能正常使用,同时高可用集群也不能正常运行,Sync group 就是为了解决该问题
注意:
router_id : 标识当前节点.两个节点即可以相同,也可以不同。
virtual_router_id : 这个标识是同一个vrrp实例使用唯一的标识。即同一个vrrp_stance,MASTER和BACKUP的virtual_router_id是一致的,同时在整个vrrp内是唯一的。
而priority 一般MASTER可以设置大一些。
vrrp_script中,MySQL的检测脚本如下,当检测到MySQL失败后,virtual_ipaddress会飘逸到BACKUP上。
Shell
vim /etc/keepalived/check_MySQL.sh #!/bin/bash MYSQL=/usr/bin/mysql MYSQL_HOST=localhost MYSQL_USER=root CHECK_COUNT=5 counter=1 while true do mysql -h $MYSQL_HOST -u $MYSQL_USER -e "show status;" >/dev/null 2>&1 i=$? ps aux | grep mysqld | grep -v grep > /dev/null 2>&1 j=$? if [ $i = 0 ] && [ $j = 0 ] then exit 0 else if [ $i = 1 ] && [ $j = 0 ] then exit 0 else if [ $counter -gt $CHECK_COUNT ] then break fi let counter++ continue fi fi done /etc/init.d/keepalived stop exit 1
3. 测试
..... Jan 21 19:58:35 debugo02 Keepalived_vrrp[22065]: VRRP_Script(check_run) succeeded ......
下面关闭debugo01(MASTER)的MySQL
service mysql s top#debugo01 keepalived log an 21 20:00:23 debugo01 Keepalived[25202]: Stopping Keepalived v1.2.13 (10/15,2014) Jan 21 20:00:23 debugo01 Keepalived_vrrp[25204]: VRRP_Instance(VI_1) sending 0 priority Jan 21 20:00:23 debugo01 Keepalived_vrrp[25204]: VRRP_Instance(VI_1) removing protocol VIPs. #debugo02 keepalived log Jan 21 19:58:35 debugo02 Keepalived_vrrp[22065]: VRRP_Script(check_run) succeeded Jan 21 20:00:24 debugo02 Keepalived_vrrp[22065]: VRRP_Instance(VI_1) Transition to MASTER STATE Jan 21 20:00:24 debugo02 Keepalived_vrrp[22065]: VRRP_Group(VG1{) Syncing instances to MASTER state Jan 21 20:00:25 debugo02 Keepalived_vrrp[22065]: VRRP_Instance(VI_1) Entering MASTER STATE Jan 21 20:00:25 debugo02 Keepalived_vrrp[22065]: VRRP_Instance(VI_1) setting protocol VIPs. Jan 21 20:00:25 debugo02 Keepalived_vrrp[22065]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.62.228.200 Jan 21 20:00:25 debugo02 Keepalived_healthcheckers[22064]: Netlink reflector reports IP 10.62.228.200 added Jan 21 20:00:30 debugo02 Keepalived_vrrp[22065]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.62.228.200
检查IP地址:
#debugo02 ip addr show ...... inet 10.62.228.200/32 scope global eth1 ......
切换成功^^
- 本文来自:Linux学习教程网