centos mariadb galera 多主同步 haprox keepalive 集群热备

时间:2022-09-19 20:12:27

环境说明:

131: haproxy keepalived

132: haproxy keepalived

134: mariadb galera

135: mariadb galera


1. mariadb galera 集群

查看是否安装

yum list installed  mariadb*

卸载

yum remove mariadb*


安装


配置yum源

# MariaDB 10.1 CentOS repository list - created 2016-12-13 01:20 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1


安装mariadb

yum install mariadb-server mariadb-client

初始化数据库,在一台操作即可,其他会自动同步

#systemctl start mariadb

#mysql_secure_installation
设置mysql root密码,其他回答yes即可


配置galera 同步集群文件

关闭数据库,修改/etc/my.cnf.d/server.cnf

systemctl stop mariadb
vi /etc/my.cnf.d/server.cnf

[galera]
# Mandatory settings
wsrep_on=ON
wsrep_provider=/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.65.128,192.168.65.129"
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2

以集群方式启动第一个mariadb

#galera_new_cluster
检查wsrep状态

mysql> MariaDB [(none)]> show status like "wsrep_%";


复制gelare 集群的配置文件到其他节点的server.cnf文件

启动mariadb

systemctl start mariadb
查看启动情况
systemctl status mariadb

2. 安装haproxy

解压 安装

tar -xvzf haproxy-1.7.5.tar.gz

make  TARGET=linux3100 CPU=x86_64  PREFIX=/usr/local/haprpxy
make install PREFIX=/usr/local/haproxy
#数说明:
#TARGET
=linux3100
#使用uname
-r查看内核,如:2.6.18-371.el5,此时该参数就为linux26
#kernel 大于2.
6.28的用:TARGET=linux2628
#CPU
=x86_64 #使用uname -r查看系统信息,如x86_64 x86_64 x86_64 GNU/Linux,此时该参数就为x86_64
#PREFIX
=/usr/local/haprpxy #/usr/local/haprpxy为haprpxy安装路径

创建目录和文件

mkdir -p  /usr/local/haproxy/conf  #创建配置文件目录

mkdir -p /etc/haproxy  #创建配置文件目录

touch  /usr/local/haproxy/conf/haproxy.cfg  #创建配置文件

ln -s  /usr/local/haproxy/conf/haproxy.cfg   /etc/haproxy/haproxy.cfg  #添加配置文件软连接

cp -r  /home/software/haproxy-1.7.5/examples/errorfiles  /usr/local/haproxy/errorfiles  #拷贝错误页面

ln -s  /usr/local/haproxy/errorfiles  /etc/haproxy/errorfiles  #添加软连接

mkdir -p  /usr/local/haproxy/log  #创建日志文件目录

touch  /usr/local/haproxy/log/haproxy.log  #创建日志文件

ln -s  /usr/local/haproxy/log/haproxy.log  /var/log/haproxy.log  #添加软连接

cp /home/software/haproxy-1.7.5/examples/haproxy.init  /etc/rc.d/init.d/haproxy  #拷贝开机启动文件

chmod +x  /etc/rc.d/init.d/haproxy  #添加脚本执行权限

chkconfig haproxy on  #设置开机启动

ln -s  /usr/local/haproxy/sbin/haproxy  /usr/sbin  #添加软连接

配置文件 haproxy.cfg

[root@yzb-centos72-2 conf]# vi haproxy.cfg

#全局配置
global
#设置日志
log 127.0.0.1 local1 info
log 127.0.0.1 local2 warning
chroot /usr/local/haproxy
#用户与用户组
user root
group root
#守护进程启动
daemon
#最大连接数
maxconn 4000

#默认配置
defaults
log global
mode http
# option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000

listen admin-stats
bind 0.0.0.0:1080 #监听端口
option httplog #采用http日志格式
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm Haproxy Manager #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
#stats hide-version #隐藏统计页面上HAProxy的版本信息

listen mysql-cluster
bind *:5506
mode tcp
# stats enable
# balance roundrobin
# option forwardfor
# option http-server-close
# timeout http-keep-alive 500
server proxy1 172.20.4.135:3306 weight 5 check inter 2000
server proxy2 172.20.4.134:3306 weight 5 check inter 2000

service haproxy start #启动

service haproxy stop  #关闭

service haproxy restart  #重启

haproxy所在的IP为172.20.4.131

通过navicat连接

centos mariadb  galera 多主同步   haprox keepalive   集群热备

连接后,我通过131、134、135任何连接操作表,数据均会保持同步。

我也尝试将135上的mariadb关掉后,通过131操作数据,均能正常,并且135上的mariadb启动后数据也会进行同步。

centos mariadb  galera 多主同步   haprox keepalive   集群热备


为了防止单点,同上在132上也配置了haproxy

将131上的haproxy拷贝的132

[root@yzb-centos72-2 software]# scp haproxy-1.7.5.tar.gz root@172.20.4.132:/home
root@172.20.4.132's password:
haproxy-1.7.5.tar.gz                                                                                                                          100% 1703KB   1.7MB/s   00:00    
[root@yzb-centos72-2 software]# ll

在132上解压,配置

tar -xvzf haproxy-1.7.5.tar.gz

 mv haproxy-1.7.5 /usr/local

[root@yzb-centos72-3 local]# cd haproxy-1.7.5/

[root@yzb-centos72-3 haproxy-1.7.5]#make  TARGET=linux3100 CPU=x86_64  PREFIX=/usr/local/haprpxy

[root@yzb-centos72-3 haproxy-1.7.5]#make install PREFIX=/usr/local/haproxy

[root@yzb-centos72-3 local]# cd haproxy

[root@yzb-centos72-3 haproxy]# mkdir conf

将131上的配置文件远程拷贝到132

[root@yzb-centos72-2 conf]#scp haproxy.cfg root@172.20.4.132:/usr/local/haproxy/conf
root@172.20.4.132's password:
haproxy.cfg                                                                                                                                   100% 1152     1.1KB/s   00:00 
将132上的配置改为6606

listen mysql-cluster
    bind  0.0.0.0:6606
    mode    tcp
   # stats   enable
   # balance roundrobin
   # option  forwardfor
   # option  http-server-close
   # timeout http-keep-alive 500
    server  proxy1 172.20.4.135:3306 weight 5 check inter 2000
    server  proxy2 172.20.4.134:3306 weight 5 check inter 2000

校验配置文件

[root@yzb-centos72-3 sbin]# ./haproxy -f ../conf/haproxy.cfg -c
Configuration file is valid

启动 harpoxy

因为这台没有配置到系统服务,所以直接进入home启动文件

[root@yzb-centos72-3 sbin]# ./haproxy -f ../conf/haproxy.cfg
[root@yzb-centos72-3 sbin]# ps -ef | grep haproxy
root     12345     1  0 13:27 ?        00:00:00 ./haproxy -f ../conf/haproxy.cfg
root     12347 11905  0 13:27 pts/0    00:00:00 grep --color=auto haproxy

navicat客户端连接

centos mariadb  galera 多主同步   haprox keepalive   集群热备

centos mariadb  galera 多主同步   haprox keepalive   集群热备

为haproxy配置日志输出

HAProxy 不会直接输出文件日志,需要借助 Linux 的 rsyslog 来让 HAProxy 输出日志

[root@yzb-centos72-3 ~]# vi /usr/local/haproxy/conf/haproxy.cfg

#全局配置
global
#设置日志
log 127.0.0.1 local1 info
log 127.0.0.1 local2 warning
chroot /usr/local/haproxy
#用户与用户组
user root
group root
#守护进程启动
daemon
#最大连接数
maxconn 4000

#默认配置
defaults
log global
mode http
# option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000

listen admin-stats
bind 0.0.0.0:1080 #监听端口
option httplog #采用http日志格式
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm Haproxy Manager #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
#stats hide-version #隐藏统计页面上HAProxy的版本信息

listen mysql-cluster
bind *:6606
mode tcp
# stats enable
# balance roundrobin
# option forwardfor
# option http-server-close
# timeout http-keep-alive 500
server proxy1 172.20.4.135:3306 weight 5 check inter 2000
server proxy2 172.20.4.134:3306 weight 5 check inter 2000

意思是将 info 级(及以上)的日志推送到 rsyslog 的 local1 接口,将 warn 级(及以上)的日志推送到 rsyslog 的 local2 接口,并且所有 frontend 都默认使用 global 中的日志配置。

注意:info 级的日志会打印 HAProxy 处理的每一条请求,会占用很大的磁盘空间,在生产环境中,建议将日志级别调整为 notice


为 rsyslog 添加 haproxy 日志的配置

[root@yzb-centos72-3 ~]# vi /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
$FileCreateMode 0644  #日志文件的权限
$FileOwner root  #日志文件的owner
local1.*     /var/log/haproxy.log  #local1接口对应的日志输出文件
local2.*     /var/log/haproxy_warn.log  #local2接口对应的日志输出文件


修改 rsyslog 的启动参数

[root@yzb-centos72-3 ~]# vi /etc/sysconfig/rsyslog
# Options for rsyslogd
# Syslogd options are deprecated since rsyslog v3.
# If you want to use them, switch to compatibility mode 2 by "-c 2"
# See rsyslogd(8) for more details
SYSLOGD_OPTIONS="-c 2 -r -m 0"

重启 rsyslog
[root@yzb-centos72-3 ~]# service rsyslog restart

Redirecting to /bin/systemctl restart  rsyslog.service


用 logrotate 进行日志切分

通过 rsyslog 输出的日志是不会切分的,所以需要通过 Linux 提供的 logrotate 来对日志文件进行切分。

使用 root 用户,创建 haproxy 日志切分配置文件:

[root@yzb-centos72-3 ~]# mkdir /root/logrotate

[root@yzb-centos72-3 ~]#  vi /root/logrotate/haproxy

/var/log/haproxy.log /var/log/haproxy_warn.log {  #切分的两个文件名
daily #按天切分
rotate 7
create 0644 root root
compress #压缩旧日志
delaycompress #延迟一天压缩
missingok #忽略文件不存在的错误
dateext #旧日志加上日志后缀
sharedscripts #切分后的重启脚本只运行一次
postrotate #切分后运行脚本重载rsyslog,让rsyslog向新的日志文件中输出日志
/bin/kill -HUP $(/bin/cat /var/run/syslogd.pid 2>/dev/null) &>/dev/null
endscript
}

将 logrotate 配置在 crontab 中:

[root@yzb-centos72-3 logrotate]# vi /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
0 0 * * * root /usr/sbin/logrotate /root/logrotate/haproxy

重启haproxy后,就可以去查看日志了

[root@yzb-centos72-2 keepalived]# killall haproxy

[root@yzb-centos72-3 sbin]# cd /usr/local/haproxy/sbin/

[root@yzb-centos72-3 sbin]# ./haproxy -f ../conf/haproxy.cfg

[root@yzb-centos72-3 sbin]# tail -100f /var/log/haproxy.log



3. keepalived

下载 解压

[root@yzb-centos72-2 software]# wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz

tar -xvzf keepalived-1.3.5.tar.gz

cd keepalived-.1.3.5

./configure --prefix=/usr/local/keepalived


若是报下面错误

configure: error:
 !!! OpenSSL is not properly installed on your system. !!!
 !!! Can not include OpenSSL headers files.            !!!

需要安装openssl

# yum install openssl-devel


若是报下面警告

*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

安装 libnl/libnl-3

yum install libnl*


[root@yzb-centos72-2 keepalived-1.3.5]#  make && make install


使用 killall -0 检查 HAProxy 服务是否存在。如果没有 killall 命令,则需要安装 psmisc 包。

[root@yzb-centos72-3 keepalived]# yum install psmisc

注册为系统服务

# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# touch /etc/init.d/keepalived
# chmod +x /etc/init.d/keepalived
# vi /etc/init.d/keepalived

/etc/init.d/keepalived 的文件内容:

#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived

# Source function library
. /etc/rc.d/init.d/functions

# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived

RETVAL=0

prog="keepalived"

start() {
echo -n $"Starting $prog: "
daemon keepalived ${KEEPALIVED_OPTIONS}
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}

stop() {
echo -n $"Stopping $prog: "
killproc keepalived
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
}

reload() {
echo -n $"Reloading $prog: "
killproc keepalived -1
RETVAL=$?
echo
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
status)
status keepalived
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
RETVAL=1
esac

exit $RETVAL


此处的/etc/init.d/keepalived就是原安装包中的文件,也可按如下方式操作

cp /home/software/keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/init.d/keepalived

chmod +x /etc/init.d/keepalived

因为我们使用非默认路径(/usr/local)安装keepalived, 故需要修改几处路径,以保证keepalived能正常启动, 需要修改的文件如下:

1.修改/etc/init.d/keepalived, 寻找大约15行左右的. /etc/sysconfig/keepalived, 修改为. /usr/local/keepalived/etc/sysconfig/keepalived, 即指向正确的文件位置,同时在上述行下添加以下内容(将keepavlied主程序所在路径导入到环境变量PATH中):

PATH="$PATH:/usr/local/keepalived/sbin"

export PATH

2. 修改/usr/local/keepalived/etc/sysconfig/keepalived文件,设置正确的服务启动参数
KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"

若按上面的这些设置就不用向/etc/sysconfig/  拷贝对应的文件了。


编辑配置文件

[root@yzb-centos72-2 keepalived-1.3.5]# mkdir /etc/keepalived/
[root@yzb-centos72-2 keepalived-1.3.5]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@yzb-centos72-2 keepalived-1.3.5]# vi /etc/keepalived/keepalived.conf

global_defs {
router_id LVS_DEVEL #虚拟路由名称
}

#HAProxy健康检查配置
vrrp_script chk_haproxy {
script "killall -0 haproxy" #使用killall -0检查haproxy实例是否存在,性能高于ps命令
interval 2 #脚本运行周期
weight 2 #每次检查的加权权重值
}

#虚拟路由配置
vrrp_instance VI_1 {
state MASTER #本机实例状态,MASTER/BACKUP,备机配置文件中请写BACKUP
interface eth0 #本机网卡名称,使用ifconfig命令查看
virtual_router_id 51 #虚拟路由编号,主备机保持一致
priority 100 #本机初始权重,备机请填写小于主机的值(例如100)
advert_int 1 #争抢虚地址的周期,秒
virtual_ipaddress {
172.20.4.234 #虚地址IP,主备机保持一致
}
track_script {
chk_haproxy #对应的健康检查配置
}
}

service keepalived start

service keepalived restart

service keepalived stop


也可用systemctl管理 keepalived服务

systemctl enable keepalived.service #设置开机自动启动

systemctl disable keepalived.service     #取消开机自动启动

systemctl start keepalived.service       #启动服务

systemctl restart keepalived.service     #重启服务

systemctl stop keepalived.service         #停止服务

systemctl status keepalived.service      #查看服务状态

设置防火墙打开

firewall-cmd --permanent–-add-service=keepalived

firewall-cmd --reload

再次强调:Keepalived的所有功能是配置keepalived.conf文件来实现的。


测试VIP飘逸

首先更改前面的一些配置

1. 将131、132上的haproxy 代理监听的端口5506、6606均改为6606.

2. 将131、132上的keepalived上的priority改为 99、100.



通过ifconfig    或  ip addr sh  可以看到绑定的虚拟IP

[root@yzb-centos72-3 sbin]# ip addr sh eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 06:69:a4:00:10:d2 brd ff:ff:ff:ff:ff:ff
    inet 172.20.4.132/24 brd 172.20.4.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 172.20.4.234/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::469:a4ff:fe00:10d2/64 scope link
       valid_lft forever preferred_lft forever


查看132上的/var/log/message,看到keepalive master获取vip

May 10 15:00:55 localhost haproxy[10603]: Proxy mysql-cluster started.
May 10 15:00:56 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Script(chk_haproxy) succeeded
May 10 15:00:56 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) Changing effective priority from 100 to 102
May 10 15:00:56 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) forcing a new MASTER election
May 10 15:00:57 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) Transition to MASTER STATE
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) Entering MASTER STATE
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) setting protocol VIPs.
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: Sending gratuitous ARP on eth0 for 172.20.4.234
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 172.20.4.234
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: Sending gratuitous ARP on eth0 for 172.20.4.234
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: Sending gratuitous ARP on eth0 for 172.20.4.234
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: Sending gratuitous ARP on eth0 for 172.20.4.234
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: Sending gratuitous ARP on eth0 for 172.20.4.234
May 10 15:01:01 yzb-centos72-3 systemd: Started Session 10408 of user root.
May 10 15:01:01 yzb-centos72-3 systemd: Starting Session 10408 of user root.


通过navicat 访问 虚拟ip  172.20.4.234,连接haproxy的6606代理端口

centos mariadb  galera 多主同步   haprox keepalive   集群热备

centos mariadb  galera 多主同步   haprox keepalive   集群热备


测试将master上haproxy kill掉

[root@yzb-centos72-3 sbin]# killall haproxy

查看132上的master上的message日志,看到监控的haproxy有问题,优先级从102降到了100,发现101高于当前的100,释放vip

ntos72-3 systemd: keepalived.service start operation timed out. Terminating.
May 10 14:58:48 yzb-centos72-3 systemd: Failed to start LVS and VRRP High Availability Monitor.
May 10 14:58:48 yzb-centos72-3 systemd: Unit keepalived.service entered failed state.
May 10 14:58:48 yzb-centos72-3 systemd: keepalived.service failed.
May 10 14:59:48 yzb-centos72-3 Keepalived_vrrp[10355]: /usr/bin/killall -0 haproxy exited with status 1
May 10 14:59:48 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Script(chk_haproxy) failed
May 10 14:59:48 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) Changing effective priority from 102 to 100
May 10 14:59:48 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) Received advert with higher priority101, ours 100
May 10 14:59:48 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) Entering BACKUP STATE
May 10 14:59:48 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) removing protocol VIPs.
May 10 14:59:50 yzb-centos72-3 Keepalived_vrrp[10355]: /usr/bin/killall -0 haproxy exited with status 1
May 10 14:59:52 yzb-centos72-3 Keepalived_vrrp[10355]: /usr/bin/killall -0 haproxy exited with status 1
May 10 14:59:54 yzb-centos72-3 Keepalived_vrrp[10355]: /usr/bin/killall -0 haproxy exited with status 1
May 10 14:59:56 yzb-centos72-3 Keepalived_vrrp[10355]: /usr/bin/killall -0 haproxy exited with status 1
May 10 14:59:58 yzb-centos72-3 Keepalived_vrrp[10355]: /usr/bin/killall -0 haproxy exited with status 1
May 10 15:00:00 yzb-centos72-3 Keepalived_vrrp[10355]: /usr/bin/killall -0 haproxy exited with status 1

这时查看131上的backup上的mesage,优先级99升到101,获取vip,变成master

May 10 14:58:59 yzb-centos72-2 Keepalived_vrrp[27012]: VRRP_Script(chk_haproxy) succeeded
May 10 14:59:00 yzb-centos72-2 Keepalived_vrrp[27012]: VRRP_Instance(VI_1) Changing effective priority from 99 to

101
May 10 14:59:48 yzb-centos72-2 Keepalived_vrrp[27012]: VRRP_Instance(VI_1) forcing a new MASTER election
May 10 14:59:49 yzb-centos72-2 Keepalived_vrrp[27012]: VRRP_Instance(VI_1) Transition to MASTER STATE
May 10 14:59:50 yzb-centos72-2 Keepalived_vrrp[27012]: VRRP_Instance(VI_1) Entering MASTER STATE
May 10 14:59:50 yzb-centos72-2 Keepalived_vrrp[27012]: VRRP_Instance(VI_1) setting protocol VIPs.
May 10 14:59:50 yzb-centos72-2 Keepalived_vrrp[27012]: Sending gratuitous ARP on eth0 for 172.20.4.234
May 10 14:59:50 yzb-centos72-2 Keepalived_vrrp[27012]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 172.20.4.234
May 10 14:59:50 yzb-centos72-2 Keepalived_vrrp[27012]: Sending gratuitous ARP on eth0 for 172.20.4.234
May 10 14:59:50 yzb-centos72-2 Keepalived_vrrp[27012]: Sending gratuitous ARP on eth0 for 172.20.4.234


再次将原132上的master上的 haproxy启动,查看message日志,则从新获取vip

0355]: VRRP_Script(chk_haproxy) succeeded
May 10 15:00:56 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) Changing effective priority from 100 to102
May 10 15:00:56 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) forcing a new MASTER election
May 10 15:00:57 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) Transition to MASTER STATE
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) Entering MASTER STATE
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) setting protocol VIPs.
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: Sending gratuitous ARP on eth0 for 172.20.4.234
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 172.20.4.234
May 10 15:00:58 yzb-centos72-3 Keepalived_vrrp[10355]: Sending gratuitous ARP on eth0 for 172.20.4.234

而刚才131的master有变成了backup

May 10 15:00:30 localhost haproxy[26346]: Connect from 10.6.226.106:60851 to 172.20.4.234:6606 (mysql-cluster/TCP)
May 10 15:00:56 yzb-centos72-2 Keepalived_vrrp[27012]: VRRP_Instance(VI_1) Received advert with higher priority102, ours 101
May 10 15:00:56 yzb-centos72-2 Keepalived_vrrp[27012]: VRRP_Instance(VI_1) Entering BACKUP STATE
May 10 15:00:56 yzb-centos72-2 Keepalived_vrrp[27012]: VRRP_Instance(VI_1) removing protocol VIPs.
May 10 15:01:01 yzb-centos72-2 systemd: Started Session 10386 of user root.
May 10 15:01:01 yzb-centos72-2 systemd: Starting Session 10386 of user root.


再次尝试将132上的keepalived杀掉,

这131backup又变成了master,

则启动132上的keepalived后,131又变成了backup,132又变成了master。

也就是在131、132上的haproxy、keepalived都正常的情况下,132均会抢到vip,成为master,因为132的优先级102大于131上的101。


上面的测试过程中,navicat通过vip 234连接的数据不受影响。


至此,结束了^_^


参考:

http://zhangxugg-163-com.iteye.com/blog/1665419

http://www.thinksaas.cn/topics/0/778/778851.html

http://www.cnblogs.com/405845829qq/p/6773340.html