前言:keepalive 基于vrrp协议它保证当主机的下一跳路由器坏掉时,可以及时由另一台路由器来代替,从而保证证通讯的连续性和可靠性
最近的项目上遇到了这样的需求,客户要求实现两台机器的高可用。客户给了2台服务器。
服务器环境:
- 172.17.17.165
- 172.17.17.166
1.165服务器配置
(1)redis配置:
1.关闭持久化
2.配置master密码
(2)nginx配置:无特殊配置
(3)keepalived配置:
! Configuration File for keepalived
#全局配置无需特殊处理
global_defs {
notification_email {
zhojy@outlook.com
}
notification_email_from M1@keepalived.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id M1
}
#监听nginx80端口节点是否存活,宕机权重下降,重启恢复权重
vrrp_script check_nginx_exists {
script "/usr/bin/nc -4zv localhost 80"
weight -60
}
vrrp_instance TBC5.5 {
state MASTER
interface ens33
virtual_router_id 112
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2019
}
virtual_ipaddress {
172.17.17.167
}
#添加跟踪脚本任何一个出现故障进入故障状态
track_script {
check_nginx_exists
}
}
vrrp_script chk_redis {
script "/usr/local/keepalived/scripts/redis/redis_check.sh" ###监控脚本
#script "/usr/bin/nc -4zv localhost 6379"
#weight -60
interval 2 ###监控时间
}
vrrp_instance TBC5.5_redis {
state MASTER
interface ens33
virtual_router_id 111
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2019
}
virtual_ipaddress {
172.17.17.168
}
track_script {
chk_redis
}
#nginx切换为主节点,执行此脚本
notify_master "/usr/local/keepalived/scripts/redis/redis_master.sh 172.17.17.166 6379"
notify_backup "/usr/local/keepalived/scripts/redis/redis_backup.sh 172.17.17.166 6379"
#故障
notify_fault "/usr/local/keepalived/scripts/redis/redis_fault.sh"
#vrrp停止时
notify_stop "/usr/local/keepalived/scripts/redis/redis_stop.sh"
}
(4).脚本配置
redis_master.sh
#!/bin/bash
REDISCLI="/winning/winmid/redis/bin/redis-cli -a winning.2019 "
LOGFILE="/etc/keepalived/log/keepalived-redis-state.log"
sleep 15
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >>$LOGFILE 2>&1
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 172.17.17.166 6379 >>$LOGFILE 2>&1
if [ $? -ne 0 ];then
echo "data rsync fail." >>$LOGFILE 2>&1
else
echo "data rsync OK." >> $LOGFILE 2>&1
fi
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
if [ $? -ne 0 ];then
echo "Run SLAVEOF NO ONE cmd fail." >>$LOGFILE 2>&1
else
echo "Run SLAVEOF NO ONE cmd OK." >> $LOGFILE 2>&1
fi
redis_backup.sh
#!/bin/bash
REDISCLI="/winning/winmid/redis/bin/redis-cli -a winning.2019 "
LOGFILE="/etc/keepalived/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #..15....................
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF $1 $2 >> $LOGFILE 2>&1
2.166服务器配置
(1)redis配置:
配置master密码
(2)nginx配置:无特殊配置
(3)keepalived配置:
global_defs {
notification_email {
zhojy@outlook.com
}
notification_email_from M1@keepalived.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id M1
}
vrrp_script check_nginx_exists {
script "/usr/bin/nc -4zv localhost 80"
weight -60
}
vrrp_instance TBC5.5 {
state BACKUP
interface ens33
virtual_router_id 112
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2019
}
virtual_ipaddress {
172.17.17.167
}
track_script {
check_nginx_exists
}
}
vrrp_script chk_redis {
script "/usr/local/keepalived/scripts/redis/redis_check.sh" ###监控脚本
#script "/usr/bin/nc -4zv localhost 6379"
#weight -60
interval 2 ###监控时间
}
vrrp_instance TBC5.5_redis {
state MASTER
interface ens33
virtual_router_id 111
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 2019
}
virtual_ipaddress {
172.17.17.168
}
track_script {
chk_redis
}
notify_master "/usr/local/keepalived/scripts/redis/redis_master.sh 172.17.17.165 6379"
notify_backup "/usr/local/keepalived/scripts/redis/redis_backup.sh 172.17.17.165 6379"
notify_fault "/usr/local/keepalived/scripts/redis/redis_fault.sh"
notify_stop "/usr/local/keepalived/scripts/redis/redis_stop.sh"
}
(4).脚本配置
#!/bin/bash
REDISCLI="/winning/winmid/redis/bin/redis-cli -a winning.2019 "
LOGFILE="/etc/keepalived/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >>$LOGFILE 2>&1
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 172.17.17.165 6379 >>$LOGFILE 2>&1
if [ $? -ne 0 ];then
echo "data rsync fail." >>$LOGFILE 2>&1
else
echo "data rsync OK." >> $LOGFILE 2>&1
fi
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
if [ $? -ne 0 ];then
echo "Run SLAVEOF NO ONE cmd fail." >>$LOGFILE 2>&1
else
echo "Run SLAVEOF NO ONE cmd OK." >> $LOGFILE 2>&1
fi
redis_backup.sh
#!/bin/bash
REDISCLI="/winning/winmid/redis/bin/redis-cli -a winning.2019 "
LOGFILE="/etc/keepalived/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #..15....................
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF $1 $2 >> $LOGFILE 2>&1
共用脚本:
redis_check.sh
#!/bin/bash
ALIVE=`/winning/winmid/redis/bin/redis-cli -a winning.2019 PING 2>/dev/null`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
redis_stop.sh
#!/bin/bash
LOGFILE=/etc/keepalived/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
redis_fault.sh
#!/bin/bash
LOGFILE=/etc/keepalived/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
参考文章: