1: 服务器 :192.168.182.128 (主)
192.168.182.129 (备)
2:keepalived的安装 (两台服务器都安装keepalived)
a)yum install keepalived -y
b)cd /etc/keepalived 默认的安装路径
c)vim keepalived.conf 主备的配置文件区别在与 state 和priority两个参数,其他的都一样的配置
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
script_user root
enable_script_security
}
vrrp_script check_run
{
script "/etc/keepalived/mysql_check.sh"
interval 5
}
vrrp_instance VI_1 {
state MASTER #表示状态为主节点MASTER还是备用机BACKUP
interface ens33 #当前示例绑定的网卡,根据ip addr查出来的网卡
virtual_router_id 51
priority 100 #优先级/权重,master权重一般高于backup,在master挂掉后谁的优先级高,谁就能成为maste
advert_int 1
#认证授权的密码,防止非法节点进入,主备需要一致
authentication {
auth_type PASS
auth_pass 1234
}
track_script {#执行检测的脚本
check_run
}
virtual_ipaddress {#虚拟的ip
192.168.182.111
}
}
d)执行的脚本
#!/bin/bash
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
mysql -uroot -p123456 -e "select @@version;" >/dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
echo $MYSQL_OK
if [ $MYSQL_OK = 1 ] ; then
CHECK_TIME=0
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
pkill keepalived
exit 1
fi
sleep 1
done
e)赋予脚本执行的权限 chmod + x msyql_check.sh
3:启动keepalive
systemctl start keepalived;
4: 查看虚拟ip 是否生效
ip addr
5:在一台服务上结束mysql,看虚拟ip是否漂移过去了
service mysqld stop
主备切换到此就结束了
注意中途出现的问题
1:查看虚拟的ip用 ip addr ,而ifconfig 查看不到新增的虚拟的ip
2:两个主服务器都出现虚拟ip的情况
解决方法: CentOS7下配置防火墙放过Keepalived
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
3: 脚本不执行的问题
track_script 和 { 直接要有空格
4:查看keepalived 错误信息
tail -f /var/log/messages;