配置keepalived实现高可用性mysql主主复制

时间:2022-09-14 20:21:07
     keepalived的工作原理是VRRP(Virtual Router Redundancy Protocol)虚拟路由冗余协议。
在VRRP中有两组重要的概念:VRRP路由器和虚拟路由器,主控路由器和备份路由器。

VRRP路由器是指运行VRRP的路由器,是物理实体,虚拟路由器是指VRRP协议创建的,是逻辑概念。一组VRRP路由器协同工作,共同构成一台虚拟路由器。 Vrrp中存在着一种选举机制,用以选出提供服务的路由即主控路由,其他的则成了备份路由。当主控路由失效后,备份路由中会重新选举出一个主控路由,来继续工作,来保障不间断服务。


一、配置mysql主主复制:

见以前文章:http://blog.csdn.net/crpp0902/article/details/76188059


二、安装keeplived:
下载keepalived-1.2.19.tar.gz 
解压tar xzvf keepalived-1.2.19.tar.gz 
[root@erptest install]# cd keepalived-1.3.2
[root@erptest keepalived-1.3.2]# ls
aclocal.m4  bin_install  configure     COPYING  genhash     keepalived          Makefile.am  README
ar-lib      ChangeLog    configure.ac  depcomp  INSTALL     keepalived.spec.in  Makefile.in  TODO
AUTHOR      compile      CONTRIBUTORS  doc      install-sh  lib                 missing
[root@erptest keepalived-1.3.2]# make
make: *** No targets specified and no makefile found.  Stop.
[root@erptest keepalived-1.3.2]# ./configure 
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
。。。。。。。。。。。。。。。。
configure: error: 
    !!! Please install libnfnetlink headers.              !!! 报错
    
解决:
rpm -ivh libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm 
rpm -ivh netlink-1.0.0-1.el6.x86_64xxx.rpm

[root@erptest keepalived-1.3.2]# ./configure 
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
。。。。。。。
config.status: creating lib/config.h
config.status: executing depfiles commands
Keepalived configuration
------------------------
Keepalived version       : 1.3.2
Compiler                 : gcc
Preprocessor flags       :   
Compiler flags           : -Wall -Wunused -Wstrict-prototypes -Wextra -g -O2  
Linker flags             : 
Extra Lib                : -ldl -lssl -lcrypto  -lnl -L/lib64 -liptc
Use IPVS Framework       : Yes
IPVS use libnl           : Yes
IPVS syncd attributes    : No
IPVS 64 bit stats        : No
fwmark socket support    : Yes
Use VRRP Framework       : Yes
Use VRRP VMAC            : No
Use VRRP authentication  : Yes
With ip rules/routes     : Yes
SNMP vrrp support        : No
SNMP checker support     : No
SNMP RFCv2 support       : No
SNMP RFCv3 support       : No
DBUS support             : No
SHA1 support             : No
Use Debug flags          : No
Stacktrace support       : No
Memory alloc check       : No
libnl version            : 1
Use IPv4 devconf         : No
Use libiptc              : Yes
Use libipset             : No
init type                : upstart
Build genhash            : Yes
Build documentation      : No
[root@erptest keepalived-1.2.19]# make
make -C lib || exit 1;
。。。。。。。。。。
strip ../bin/genhash
Make complete
make[1]: Leaving directory `/u02/install/keepalived-1.2.19/genhash'
Make complete
[root@erptest keepalived-1.2.19]make install 

另外一个节点同样执行上面步骤

三、配置keepalived

第一节点
[root@erptest install]# more /etc/keepalived/keepalived.conf 
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}


vrrp_instance VI_1 {
    state backup
    interface eth1
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
   10.81.35.10
    }
}

virtual_server 10.81.35.10 3306 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  persistence_timeout 50        #会话保持时间
  protocol TCP
  real_server 10.81.35.3 3306 {
      weight 3
      notify_down /usr/local/checkmysql.sh    #检测到mysql服务挂了就执行这个脚本(脚本要自己写哈)
      TCP_CHECK {
      connect_timeout 10        #连接超时时间
      nb_get_retry 3            #重连次数
      delay_before_retry 3      #重连间隔时间
      connect_port 3306        #健康检查端口
        }
      }
}

第二节点
[root@dev ~]# more /etc/keepalived/keepalived.conf 
! Configuration File for keepalived


global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state backup
    interface eth1
    virtual_router_id 51
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
   10.81.35.10
    }
}
virtual_server 10.81.35.10 3306 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  persistence_timeout 50        #会话保持时间 
  protocol TCP
  real_server 10.81.35.2 3306 {
      weight 3
      notify_down /usr/local/checkmysql.sh    #检测到mysql服务挂了就执行这个脚本(脚本要自己写哈)
      TCP_CHECK {
      connect_timeout 10        #连接超时时间
      nb_get_retry 3            #重连次数 
      delay_before_retry 3      #重连间隔时间
      connect_port 3306        #健康检查端口 
        }
      }
}

配置完成!!!

四、测试:
分别在两台机器上查看messages日志文件:
[root@dev keepalived]# tail -f /var/log/messages
Jul 27 16:12:17 dev vmnet-natd: RTM_DELADDR: index:3, addr:10.81.35.10
Jul 27 16:12:17 dev kernel: userif-3: sent link up event.
Jul 27 16:12:17 dev kernel: userif-3: sent link down event.
Jul 27 16:30:54 dev kernel: userif-3: sent link up event.
Jul 27 16:30:54 dev kernel: frmweb[16392]: segfault at 6974655e ip 00000000f75022c1 sp 00000000ffe739a8 error 4 in libsosdw.so.0[f74fe000+b000]
Jul 27 16:30:54 dev abrtd: Directory 'ccpp-2017-07-27-16:30:54-16392' creation detected
Jul 27 16:30:54 dev abrt[33529]: Saved core dump of pid 16392 (/u01/PROD/apps/tech_st/10.1.2/bin/frmweb) to /var/spool/abrt/ccpp-2017-07-27-16:30:54-16392 (40992768 bytes)
Jul 27 16:30:55 dev abrtd: Executable '/u01/PROD/apps/tech_st/10.1.2/bin/frmweb' doesn't belong to any package
Jul 27 16:30:55 dev abrtd: 'post-create' on '/var/spool/abrt/ccpp-2017-07-27-16:30:54-16392' exited with 1
Jul 27 16:30:55 dev abrtd: Corrupted or bad directory '/var/spool/abrt/ccpp-2017-07-27-16:30:54-16392', deleting


Jul 27 16:35:01 dev Keepalived[33657]: Starting Keepalived v1.2.19 (07/27,2017)
Jul 27 16:35:01 dev Keepalived[33658]: Starting Healthcheck child process, pid=33659
Jul 27 16:35:01 dev Keepalived[33658]: Starting VRRP child process, pid=33660
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Netlink reflector reports IP 10.81.35.2 added
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Netlink reflector reports IP 192.168.231.1 added
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Netlink reflector reports IP 172.16.153.1 added
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Netlink reflector reports IP 10.81.35.2 added
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Netlink reflector reports IP 192.168.122.1 added
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Netlink reflector reports IP 192.168.231.1 added
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Netlink reflector reports IP 172.16.153.1 added
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Netlink reflector reports IP 192.168.122.1 added
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5ff7 added
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5ff8 added
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Netlink reflector reports IP fe80::250:56ff:fec0:1 added
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5ff7 added
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Netlink reflector reports IP fe80::250:56ff:fec0:8 added
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5ff8 added
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Registering Kernel netlink reflector
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Netlink reflector reports IP fe80::250:56ff:fec0:1 added
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Registering Kernel netlink command channel
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Netlink reflector reports IP fe80::250:56ff:fec0:8 added
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Registering gratuitous ARP shared channel
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Registering Kernel netlink reflector
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Registering Kernel netlink command channel
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Configuration is using : 17487 Bytes
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Configuration is using : 68551 Bytes
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: Using LinkWatch kernel netlink reflector...
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jul 27 16:35:01 dev Keepalived_vrrp[33660]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)]
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Using LinkWatch kernel netlink reflector...
Jul 27 16:35:01 dev Keepalived_healthcheckers[33659]: Activating healthchecker for service [10.81.35.2]:3306
Jul 27 16:35:05 dev Keepalived_vrrp[33660]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jul 27 16:35:06 dev Keepalived_vrrp[33660]: VRRP_Instance(VI_1) Entering MASTER STATE
Jul 27 16:35:06 dev Keepalived_vrrp[33660]: VRRP_Instance(VI_1) setting protocol VIPs.
Jul 27 16:35:06 dev Keepalived_healthcheckers[33659]: Netlink reflector reports IP 10.81.35.10 added
Jul 27 16:35:06 dev vmnet-natd: RTM_NEWADDR: index:3, addr:10.81.35.10
Jul 27 16:35:06 dev Keepalived_vrrp[33660]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.81.35.10
Jul 27 16:35:06 dev kernel: userif-3: sent link down event.
Jul 27 16:35:11 dev Keepalived_vrrp[33660]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.81.35.10
Jul 27 16:39:52 dev kernel: userif-3: sent link up event.
Jul 27 16:39:52 dev kernel: frmweb[15170]: segfault at 6974655e ip 00000000f75b92c1 sp 00000000ffdac3b8 error 4 in libsosdw.so.0[f75b5000+b000]
Jul 27 16:39:52 dev abrtd: Directory 'ccpp-2017-07-27-16:39:52-15170' creation detected
Jul 27 16:39:52 dev abrt[33895]: Saved core dump of pid 15170 (/u02/PROD/apps/tech_st/10.1.2/bin/frmweb) to /var/spool/abrt/ccpp-2017-07-27-16:39:52-15170 (48631808 bytes)
Jul 27 16:39:52 dev abrtd: Executable '/u02/PROD/apps/tech_st/10.1.2/bin/frmweb' doesn't belong to any package
Jul 27 16:39:52 dev abrtd: 'post-create' on '/var/spool/abrt/ccpp-2017-07-27-16:39:52-15170' exited with 1
Jul 27 16:39:52 dev abrtd: Corrupted or bad directory '/var/spool/abrt/ccpp-2017-07-27-16:39:52-15170', deleting
Jul 27 16:42:26 dev Keepalived_healthcheckers[33659]: TCP connection to [10.81.35.2]:3306 failed !!!
Jul 27 16:42:26 dev Keepalived_healthcheckers[33659]: Removing service [10.81.35.2]:3306 from VS [10.81.35.10]:3306
Jul 27 16:42:26 dev Keepalived_healthcheckers[33659]: Executing [/usr/local/checkmysql.sh] for service [10.81.35.2]:3306 in VS [10.81.35.10]:3306
Jul 27 16:42:26 dev Keepalived_healthcheckers[33659]: Lost quorum 1-0=1 > 0 for VS [10.81.35.10]:3306
Jul 27 16:42:26 dev Keepalived_healthcheckers[33659]: Remote SMTP server [127.0.0.1]:25 connected.
Jul 27 16:42:26 dev Keepalived_healthcheckers[33659]: SMTP alert successfully sent.
Jul 27 16:42:26 dev Keepalived[33658]: Stopping Keepalived v1.2.19 (07/27,2017)
Jul 27 16:43:58 dev Keepalived[34526]: Starting Keepalived v1.2.19 (07/27,2017)
Jul 27 16:43:58 dev Keepalived[34527]: Starting Healthcheck child process, pid=34530
Jul 27 16:43:58 dev Keepalived[34527]: Starting VRRP child process, pid=34531
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Netlink reflector reports IP 10.81.35.2 added
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Netlink reflector reports IP 10.81.35.10 added
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Netlink reflector reports IP 192.168.231.1 added
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Netlink reflector reports IP 172.16.153.1 added
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Netlink reflector reports IP 192.168.122.1 added
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5ff7 added
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5ff8 added
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Netlink reflector reports IP fe80::250:56ff:fec0:1 added
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Netlink reflector reports IP fe80::250:56ff:fec0:8 added
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Registering Kernel netlink reflector
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Registering Kernel netlink command channel
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Registering gratuitous ARP shared channel
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Netlink reflector reports IP 10.81.35.2 added
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Netlink reflector reports IP 10.81.35.10 added
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Netlink reflector reports IP 192.168.231.1 added
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Netlink reflector reports IP 172.16.153.1 added
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Netlink reflector reports IP 192.168.122.1 added
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Configuration is using : 68551 Bytes
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5ff7 added
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: Using LinkWatch kernel netlink reflector...
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5ff8 added
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Netlink reflector reports IP fe80::250:56ff:fec0:1 added
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Netlink reflector reports IP fe80::250:56ff:fec0:8 added
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Registering Kernel netlink reflector
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Registering Kernel netlink command channel
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Configuration is using : 17487 Bytes
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jul 27 16:43:58 dev Keepalived_vrrp[34531]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)]
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: IPVS: Service already exists
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Using LinkWatch kernel netlink reflector...
Jul 27 16:43:58 dev Keepalived_healthcheckers[34530]: Activating healthchecker for service [10.81.35.2]:3306
Jul 27 16:44:41 dev Keepalived_vrrp[34531]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jul 27 16:44:42 dev Keepalived_vrrp[34531]: VRRP_Instance(VI_1) Entering MASTER STATE
Jul 27 16:44:42 dev Keepalived_vrrp[34531]: VRRP_Instance(VI_1) setting protocol VIPs.
Jul 27 16:44:42 dev Keepalived_vrrp[34531]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.81.35.10
Jul 27 16:44:47 dev Keepalived_vrrp[34531]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.81.35.10


[root@erptest keepalived]# ps -ef|grep keep
root     24173 11477  0 16:40 pts/0    00:00:00 grep keep
[root@erptest keepalived]# ps -ef|grep keepalived      
root     24181 11477  0 16:40 pts/0    00:00:00 grep keepalived
[root@erptest keepalived]# tail -f /var/log/messages
Jul 27 16:30:17 erptest Keepalived_healthcheckers[23119]: Netlink reflector reports IP 10.81.35.10 added
Jul 27 16:30:17 erptest Keepalived_vrrp[23120]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.81.35.10
Jul 27 16:30:18 erptest Keepalived_healthcheckers[23119]: TCP connection to [10.81.35.3]:3306 failed !!!
Jul 27 16:30:18 erptest Keepalived_healthcheckers[23119]: Removing service [10.81.35.3]:3306 from VS [10.81.35.10]:3306
Jul 27 16:30:18 erptest Keepalived_healthcheckers[23119]: Executing [/usr/local/checkmysql.sh] for service [10.81.35.3]:3306 in VS [10.81.35.10]:3306
Jul 27 16:30:18 erptest Keepalived_healthcheckers[23119]: Lost quorum 1-0=1 > 0 for VS [10.81.35.10]:3306
Jul 27 16:30:18 erptest Keepalived_healthcheckers[23119]: Remote SMTP server [192.168.200.1]:25 connected.
Jul 27 16:30:18 erptest Keepalived[23118]: Stopping Keepalived v1.2.19 (07/27,2017)
Jul 27 16:30:18 erptest Keepalived_vrrp[23120]: VRRP_Instance(VI_1) sending 0 priority
Jul 27 16:30:18 erptest Keepalived_vrrp[23120]: VRRP_Instance(VI_1) removing protocol VIPs.
Jul 27 16:40:38 erptest Keepalived[24205]: Starting Keepalived v1.2.19 (07/27,2017)
Jul 27 16:40:38 erptest Keepalived[24208]: Starting Healthcheck child process, pid=24209
Jul 27 16:40:38 erptest Keepalived[24208]: Starting VRRP child process, pid=24210
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: Netlink reflector reports IP 10.81.35.3 added
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: Netlink reflector reports IP 192.168.122.1 added
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5e43 added
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5e44 added
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: Netlink reflector reports IP fe80::2e4d:54ff:fe44:cd35 added
Jul 27 16:40:38 erptest Keepalived_healthcheckers[24209]: Netlink reflector reports IP 10.81.35.3 added
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: Netlink reflector reports IP fe80::2e4d:54ff:fe44:cd36 added
Jul 27 16:40:38 erptest Keepalived_healthcheckers[24209]: Netlink reflector reports IP 192.168.122.1 added
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: Registering Kernel netlink reflector
Jul 27 16:40:38 erptest Keepalived_healthcheckers[24209]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5e43 added
Jul 27 16:40:38 erptest Keepalived_healthcheckers[24209]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5e44 added
Jul 27 16:40:38 erptest Keepalived_healthcheckers[24209]: Netlink reflector reports IP fe80::2e4d:54ff:fe44:cd35 added
Jul 27 16:40:38 erptest Keepalived_healthcheckers[24209]: Netlink reflector reports IP fe80::2e4d:54ff:fe44:cd36 added
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: Registering Kernel netlink command channel
Jul 27 16:40:38 erptest Keepalived_healthcheckers[24209]: Registering Kernel netlink reflector
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: Registering gratuitous ARP shared channel
Jul 27 16:40:38 erptest Keepalived_healthcheckers[24209]: Registering Kernel netlink command channel
Jul 27 16:40:38 erptest Keepalived_healthcheckers[24209]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 27 16:40:38 erptest Keepalived_healthcheckers[24209]: Configuration is using : 17161 Bytes
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: Configuration is using : 68225 Bytes
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: Using LinkWatch kernel netlink reflector...
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jul 27 16:40:38 erptest Keepalived_vrrp[24210]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)]
Jul 27 16:40:38 erptest Keepalived_healthcheckers[24209]: Using LinkWatch kernel netlink reflector...
Jul 27 16:40:38 erptest Keepalived_healthcheckers[24209]: Activating healthchecker for service [10.81.35.3]:3306
Jul 27 16:42:21 erptest Keepalived_vrrp[24210]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jul 27 16:42:22 erptest Keepalived_vrrp[24210]: VRRP_Instance(VI_1) Entering MASTER STATE
Jul 27 16:42:22 erptest Keepalived_vrrp[24210]: VRRP_Instance(VI_1) setting protocol VIPs.
Jul 27 16:42:22 erptest Keepalived_healthcheckers[24209]: Netlink reflector reports IP 10.81.35.10 added
Jul 27 16:42:22 erptest Keepalived_vrrp[24210]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.81.35.10
Jul 27 16:42:27 erptest Keepalived_vrrp[24210]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.81.35.10
Jul 27 16:44:32 erptest Keepalived_healthcheckers[24209]: TCP connection to [10.81.35.3]:3306 failed !!!
Jul 27 16:44:32 erptest Keepalived_healthcheckers[24209]: Removing service [10.81.35.3]:3306 from VS [10.81.35.10]:3306
Jul 27 16:44:32 erptest Keepalived_healthcheckers[24209]: Executing [/usr/local/checkmysql.sh] for service [10.81.35.3]:3306 in VS [10.81.35.10]:3306
Jul 27 16:44:32 erptest Keepalived_healthcheckers[24209]: Lost quorum 1-0=1 > 0 for VS [10.81.35.10]:3306
Jul 27 16:44:32 erptest Keepalived_healthcheckers[24209]: Remote SMTP server [127.0.0.1]:25 connected.
Jul 27 16:44:32 erptest Keepalived_healthcheckers[24209]: SMTP alert successfully sent.
Jul 27 16:44:32 erptest Keepalived_vrrp[24210]: VRRP_Instance(VI_1) sending 0 priority
Jul 27 16:44:32 erptest Keepalived[24208]: Stopping Keepalived v1.2.19 (07/27,2017)
Jul 27 16:44:32 erptest Keepalived_vrrp[24210]: VRRP_Instance(VI_1) removing protocol VIPs.
Jul 27 16:46:03 erptest Keepalived[24805]: Starting Keepalived v1.2.19 (07/27,2017)
Jul 27 16:46:03 erptest Keepalived[24806]: Starting Healthcheck child process, pid=24807
Jul 27 16:46:03 erptest Keepalived[24806]: Starting VRRP child process, pid=24808
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: Netlink reflector reports IP 10.81.35.3 added
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: Netlink reflector reports IP 192.168.122.1 added
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5e43 added
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5e44 added
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: Netlink reflector reports IP fe80::2e4d:54ff:fe44:cd35 added
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: Netlink reflector reports IP fe80::2e4d:54ff:fe44:cd36 added
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: Registering Kernel netlink reflector
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: Registering Kernel netlink command channel
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: Registering gratuitous ARP shared channel
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: Netlink reflector reports IP 10.81.35.3 added
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: Netlink reflector reports IP 192.168.122.1 added
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5e43 added
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: Netlink reflector reports IP fe80::ea61:1fff:fe1a:5e44 added
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: Netlink reflector reports IP fe80::2e4d:54ff:fe44:cd35 added
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: Netlink reflector reports IP fe80::2e4d:54ff:fe44:cd36 added
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: Registering Kernel netlink reflector
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: Registering Kernel netlink command channel
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: Configuration is using : 68225 Bytes
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: Using LinkWatch kernel netlink reflector...
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: Configuration is using : 17161 Bytes
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jul 27 16:46:03 erptest Keepalived_vrrp[24808]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)]
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: IPVS: Service already exists
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: Using LinkWatch kernel netlink reflector...
Jul 27 16:46:03 erptest Keepalived_healthcheckers[24807]: Activating healthchecker for service [10.81.35.3]:3306


root@erptest ~]# service mysql stop  
Shutting down MySQL............[纭畾]
[root@erptest ~]# service mysql start
Starting MySQL.[纭畾]
[root@erptest ~]# keepalived -D -f /etc/keepalived/keepalived.conf 
[root@erptest ~]# 

可以看到message日志里VIP随着mysql的起停在不断的在双机之间切换。

[root@dtpiprda ~]# mysql -uroot -p12345 -h 10.81.35.10
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.6.37-log MySQL Community Server (GPL)


Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Reconnecting after a failed master event read
                  Master_Host: 10.81.35.2
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000002
          Read_Master_Log_Pos: 120
               Relay_Log_File: binlog.000006
                Relay_Log_Pos: 280
        Relay_Master_Log_File: binlog.000002
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 120
              Relay_Log_Space: 444
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 2003
                Last_IO_Error: error reconnecting to master 'repluser@10.81.35.2:3306' - retry-time: 60  retries: 2
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: cd2fab5f-7025-11e7-b37c-e8611f1a5ff8
             Master_Info_File: /u02/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 170727 16:43:16
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

停止10.81.35.3上的mysql;


mysql> show slave status \G
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql> show slave status \G
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    14
Current database: *** NONE ***

再查询,发现已经切换到的10.81.35.2上了。
*************************** 1. row ***************************
               Slave_IO_State: Reconnecting after a failed master event read
                  Master_Host: 10.81.35.3
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000002
          Read_Master_Log_Pos: 120
               Relay_Log_File: binlog.000008
                Relay_Log_Pos: 280
        Relay_Master_Log_File: binlog.000002
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 120
              Relay_Log_Space: 444
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 2003
                Last_IO_Error: error reconnecting to master 'repluser@10.81.35.3:3306' - retry-time: 60  retries: 1
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 2
                  Master_UUID: 98b6f1e4-7026-11e7-b381-e8611f1a5e44
             Master_Info_File: /u02/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 170727 16:44:46
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.01 sec)


mysql>