前言
在上文中 【Mysql 高可用 Haproxy】,曾经探讨过一个mysql高可用的方案。但是该方案存在一个致命性的问题
就是,如果
Haproxy
如果发生故障,那么这个服务整体就瘫痪了。本文接着探讨一种mysql的高可用方案,即使用
Keepalived
检测服务器的状态 ,来进行故障转移
参考文献
Keepalived 下载:https://www.keepalived.org/download.html
Keepalived 教程文档:http://www.yunweipai.com/35361.html
Keepalived 学习博文:https://www.cnblogs.com/clsn/p/8052649.html
演示架构图简版(思维导图):https://kdocs.cn/l/sgVGyjR4O590
图示说明
前期准备,需要服务器5台:
server1:安装keepalived1+haproxy1进行服务监控和服务处理
server2:安装keepalived2+haproxy2进行备用的服务监控和服务处理
server3:做master主数据库服务器
server4:做slave从数据库服务器
server5:做slave从数据库服务器
图示架构说明:
1、首先建立两个 keepalived 服务器,然后这两个服务会虚拟出来一个相同的ip。当k1发生故障的时候,这个ip会漂移到 keepalived2,完成故障转移
2、keeplived 会根据 haproxy 服务器的可用性选择服务进行连接
3、haproxy 服务会根据 slave 数据库服务器的可用性选择服务
keeplived介绍与配置
Keepalived介绍
Keepalived的作用是检测服务器的状态,如果有一台服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其它服务器代替该服务器的工作,当服务器工作 正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
Keepalived安装
安装Keepalived需要的扩展
# 依赖安装
[root@localhost keepalived]# yum install gcc gcc-c++ openssl openssl-devel
# 软件下载
[root@localhost home]# wget https://www.keepalived.org/software/keepalived-1.2.18.tar.gz
解压Keepalived并安装
[root@localhost home]# tar -zxvf keepalived-1.2.18.tar.gz
[root@localhost home]# cd keepalived-1.2.18
[root@localhost keepalived-1.2.18]# ./configure --prefix=/usr/local/keepalived
[root@localhost keepalived-1.2.18]# make && make install
将 keepalived 安装成 Linux 系统服务
因为没有使用 keepalived 的默认路径安装(默认是/usr/local) ,安装完成之后,需要做一些工作 复制默认配置文件到默认路径
[root@localhost keepalived-1.2.18]# mkdir /etc/keepalived
[root@localhost keepalived-1.2.18]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
复制 keepalived 服务脚本到默认的地址
[root@localhost keepalived-1.2.18]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@localhost keepalived-1.2.18]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost keepalived-1.2.18]# ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
设置 keepalived 服务开机启动
[root@localhost keepalived-1.2.18]# chkconfig keepalived on
mysql高可用搭建
Keepalived配置
keepalived主机配置
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER
}
vrrp_script chk_haproxy {
script "/etc/keepalived/haproxy_check.sh" ## 检测 haproxy 状态的脚本路径
interval 2 ## 检测时间间隔
weight 2 ## 如果条件成立,权重+2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 79 # 权重,主的slave建议权重高一些
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.29.101
}
track_script {
chk_haproxy
}
}
# 写VIP virtual_server,只配置本地机器
virtual_server 192.168.29.101 3307 {# 定义虚拟服务器,地址与上面的virtual_ipaddress相同
delay_loop 3 # 健康检查时间间隔,3秒
lb_algo rr # 负载均衡调度算法:rr|wrr|lc|wlc|sh|dh|lblc
lb_kind DR # 负载均衡转发规则:NAT|DR|TUN
# persistence_timeout 5 # 会话保持时间5秒,动态服务建议开启
protocol TCP # 转发协议protocol,一般有tcp和udp两种
real_server 192.168.29.106 3307 {
weight 1 # 权重越大负载分越大,0表示失效
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
Keepalived从机配置
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER
}
vrrp_script chk_haproxy {
script "/etc/keepalived/haproxy_check.sh" ## 检测 haproxy 状态的脚本路径
interval 2 ## 检测时间间隔
weight 2 ## 如果条件成立,权重+2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 79
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.29.101
}
track_script {
chk_haproxy
}
}
# 写VIP virtual_server,只配置本地机器
virtual_server 192.168.29.101 3307 {# 定义虚拟服务器,地址与上面的virtual_ipaddress相同
delay_loop 3 # 健康检查时间间隔,3秒
lb_algo rr # 负载均衡调度算法:rr|wrr|lc|wlc|sh|dh|lblc
lb_kind DR # 负载均衡转发规则:NAT|DR|TUN
# persistence_timeout 5 # 会话保持时间5秒,动态服务建议开启
protocol TCP # 转发协议protocol,一般有tcp和udp两种
real_server 192.168.29.107 3307 {
weight 1 # 权重越大负载分越大,0表示失效
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
haproxy状态检测脚本
#!/bin/bash
START_HAPROXY="/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg" #haproxy启动命令
LOG_FILE="/usr/local/keepalived/log/haproxy-check.log" # 日志文件
HAPS=`ps -C haproxy --no-header |wc -l` # 检测haproxy的状态,0代表未启动,1已经启动
date "+%Y-%m-%d %H:%M:%S" >> $LOG_FILE #在日志文件当中记录检测时间
echo "check haproxy status" >> $LOG_FILE # 记录haproxy的状态
if [ $HAPS -eq 0 ];then #执行haproxy判断
echo $START_HAPROXY >> $LOG_FILE #记录启动命令
/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg #启动haproxy
sleep 3
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
echo "start haproxy failed, killall keepalived" >> $LOG_FILE
killall keepalived
service keepalived stop
fi
fi
haproxy状态检测脚本不执行问题,如果是使用的service keeplived start 或者是 systemctl 方式启动,脚本可能会不执行,可以使用 Keepalived -f /etc/keepalived/keepalived.conf方式启动Keepalived
keepalived配置注意点 - 配置完成但是ip不生效;
- 查看虚拟机/机器系统时间是否一致
- virtual_router_id路由id不对,不能冲突。可以通过/var/log/messages查看此错误
测试
在本地通过192.168.199.101去连接mysql,查询sever_id,端口使用haproxy定义的3307
可以看到同样的连接方式,但是查询到的server_id确是不同机器的server_id.