架构说明 mysql一主两从,使用MHA架构 mysql主,MHA master节点: 10.10.10.62:3306 mysql从,MHA node节点: 10.10.10.136:3306 mysql从,MHA node节点: 10.10.10.76:3306 dble1(Keepalived): 10.10.10.63 dble1(Keepalived): 10.10.10.129 上述架构已经安装好,此处不再赘述。 VIP:10.10.10.38 两台服务器搭建Keepalived: wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz tar xf keepalived-1.2.12.tar.gz cd keepalived-1.2.12 ./configure --prefix=/usr/local/keepalived make && make install cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ 设置Keepalived开机自启动 chkconfig keepalived on 配置keepalived的配置文件,注意需要分别查看129和63的网卡名, 129 的网卡名是 ens192 ,而63的网卡名是 ens160,所以在 interface 参数中会不一样。 在129上配置 cat << EOF > /etc/keepalived/keepalived.conf global_defs { notification_email { sysadmin@firewall.loc } notification_email_from localhost@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id dble_master } vrrp_script check_run { script "/etc/keepalived/check_dble.sh" interval 2 } vrrp_instance VI_1 { state BACKUP interface ens192 virtual_router_id 66 priority 100 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } track_script { check_run } virtual_ipaddress { 10.10.10.38 } } EOF 在63上配置 cat << EOF > /etc/keepalived/keepalived.conf global_defs { notification_email { sysadmin@firewall.loc } notification_email_from localhost@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id dble_backup } vrrp_script check_run { script "/etc/keepalived/check_dble.sh" interval 2 } vrrp_instance VI_1 { state BACKUP interface ens160 virtual_router_id 66 priority 90 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } track_script { check_run } virtual_ipaddress { 10.10.10.38 } } EOF 上面两台服务器的keepalived都设置为了BACKUP模式,在keepalived中2种模式,分别是master->backup模式和backup->backup模式。 这两种模式有很大区别。在master->backup模式下,一旦主库宕机,虚拟ip会自动漂移到从库,当主库修复后,keepalived启动后,还会把虚拟ip抢占过来,即使设置了非抢占模式(nopreempt)抢占ip的动作也会发生。 在backup->backup模式下,当主库宕机后虚拟ip会自动漂移到从库上,当原主库恢复和keepalived服务启动后,并不会抢占新主的虚拟ip,即使是优先级高于从库的优先级别,也不会发生抢占。为了减少ip漂移次数,通常是把修复好的主库当做新的备库。 (上述关于keepalived的描述来自博客:https://www.cnblogs.com/gomysql/p/3675429.html) /etc/keepalived/check_dble.sh 内容如下 #!/bin/bash DbleRunningCount=`/opt/dble/bin/dble status|grep "dble-server is running" | wc -l` time=$(date "+%Y-%m-%d %H:%M:%S") if [ $DbleRunningCount = 0 ]; then echo "$time : DbleRunningCount=$DbleRunningCount, dble is not running..." >> /var/log/check_dble.log /etc/init.d/keepalived stop exit 1 # 返回1说明脚本非正常执行,dble 不在运行中 else exit 0 # 返回0说明脚本正常执行,dble 正在运行中 fi 保存脚本文件后更改该文件权限: chmod 755 /etc/keepalived/check_dble.sh 先启动mysql,接着启动dble,然后启动realserver.sh,再启动keepalived 查看62服务器的网卡信息 [root@testhost129 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:fe:b6:9d brd ff:ff:ff:ff:ff:ff inet 10.10.10.129/24 brd 10.10.10.255 scope global noprefixroute ens192 valid_lft forever preferred_lft forever inet 10.10.10.38/32 scope global ens192 valid_lft forever preferred_lft forever inet6 fe80::4025:d369:a79f:eff0/64 scope link noprefixroute valid_lft forever preferred_lft forever 测试:分别停掉两边dble,观察IP地址是否漂移,如果能正常漂移,说明搭建成功。