3. 双机故障切换
Keepalived的作用是检测服务器的状态,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中。
(1)在两台主机上分别装上Keepalived。
由于在安装过程中遇到很多坑,故将步骤描述一下
1. 下载keepalived-1.2.21.tar.gz。不同版本目录结构有一定区别,以下步骤不一定适用于其他版本。
2. 在/usr/local/目录下,tar zxvf keepalived-1.2.21.tar.gz
3. cd keepalived-1.2.21
4. ./configure => make => make install
5. ln -s /usr/local/keepalived-1.2.21/bin/keepalived /usr/bin/keepalived //将keepalived命令软连接到/usr/bin下
6. cp /usr/local/keepalived-1.2.21/keepalived/etc/init.d/keepalived.init /etc/init.d/keepalived //这三步添加启动脚本且方便用service管理
7. chmod 755 /etc/init.d/keepalived //添加执行权限
8. chkconfig keepalived on //开机启动
9. 修改/etc/init.d/keepalived中的配置文件路径,将. /etc/sysconfig/keepalived改为. /usr/local/keepalived-1.2.21/keepalived/etc/init.d/keepalived.sysconfig
10. mkdir /etc/keepalived
11. ln -s /usr/local/keepalived-1.2.21/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
12. service keepalived restart
(2)主服务器keepalived配置
/etc/keepalived/keepalived.conf编辑如下:
! Configuration File for keepalived
global_defs {
notification_email {
***@163.com
}
notification_email_from ***@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
#router_id MYSQL_HA
#vrrp_skip_check_adv_addr
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
}
vrrp_script check_mysql {
script "/usr/local/keepalived-1.2.21/check_mysql.sh" #定义监控mysql进程的脚本
interval 2 ##监控时间间隔
weight 2 #负载参数
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736 #通过ifconfig查看网卡名称
virtual_router_id 51 #虚拟路由的标志,一组lvs的虚拟路由标识必须相同,这样才能切换
priority 100 #服务启动优先级,值越大,优先级越高,BACKUP 不能大于MASTER
advert_int 1 #服务器之间的存活检查时间
nopreempt #不主动抢占资源,只在master这台优先级高的设置,backup不设置
authentication {
auth_type PASS #认证类型
auth_pass 1111 #认证密码,一组lvs 服务器的认证密码必须一致
}
track_script {
check_mysql
}
virtual_ipaddress {
172.16.188.135
}
}
/usr/local/keepalived-1.2.21/check_mysql.sh如下,通过能否连接mysql来监控服务器是否可用
#! /bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=123
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" > /dev/null 2>&1
if [ $? == 0 ]
then
echo "$MYSQL_HOST mysql login successfully"
exit 0
else
/etc/init.d/keepalived stop
exit 2
fi
(3)另一台服务器keepalived配置
与第(2)步中基本一致,除了
- state 设为 BACKUP
- priority设为90
- nopreempt不设置
注意:interface eno16777736 #通过ifconfig查到网卡名称为eno16777736
(4)两台主机防火墙配置
由于Centos7中用firewalld替代了iptables, 考虑到以下的配置,选择关闭firewall并安装iptables。
1、关闭firewall:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
2、安装iptables防火墙
yum install iptables-services #安装
在/etc/sysconfig/iptables防火墙配置文件中添加以下3行
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -d 172.16.188.135/32 -j ACCEPT#172.16.188.135为虚拟主机地址
-A INPUT -d 224.0.0.18 -j ACCEPT#添加VRRP通讯支持
systemctl restart iptables.service #最后重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
重要提示:不能删除firewall,否则会删除很多关联的系统组件
以上步骤,即实现了故障自动切换。可通过以下方式验证。
- 通过ip addr命令查看VIP位于哪台服务器上
- 通过mysql -h 172.16.188.135 -u user -p看能否连接成功
注:当故障恢复的时候,要关闭重启keepalived,否则VIP无法自动切换回来