[root@cache-redis-01~/]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 11270963592@qq.com } notification_email_from 18978038798@189.cn smtp_server smtp.189.cn smtp_connect_timeout 30 router_id redis_service01 vrrp_skip_check_adv_addr # vrrp_strict # can not use ping vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_redis { script /etc/keepalived/scripts/redis_check.sh ###监控本地redis alive interval 2 ###监控时间间隔(秒) } vrrp_instance redis { state BACKUP interface eth0 virtual_router_id 55 priority 100 nopreempt ###不抢占MASTER advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_redis ###执行上面定义的chk_redis } virtual_ipaddress { 172.16.1.63/24 dev eth0 label eth0:63 } notify_master /etc/keepalived/scripts/redis_master.sh # 当此节点切换为master时执行的脚本 notify_backup "/etc/keepalived/scripts/redis_backup.sh cache-redis-02" # 当此节点切换为 |
[root@cache-redis-02~/]# cat /etc/keepalived/keepalived.conf |
跟从的脚本完全一样!!! |
mkdir -p /etc/keepalived/scripts
cat >/etc/keepalived/scripts/redis_master.sh<<-EOF
#!/bin/bash
PORT="${1:-6380}"
REDISCLI="/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
sleep 10 #延迟10秒待数据被对方同步完成之后再切换主从角色
echo -e "[keepalived master] $(date +%F_%T)
Run SLAVEOF NO ONE cmd ..." >>${LOGFILE}
$REDISCLI -h $(hostname) -p ${PORT} SLAVEOF NO ONE &>>${LOGFILE}
EOF
cat >/etc/keepalived/scripts/redis_backup.sh<<-EOF
#!/bin/bash
REMOTE_HOSTNAME="$1"
PORT="${2:-6380}"
REDISCLI="/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo -e "[keepalived backup] $(date +%F_%T)
Run SLAVEOF ${REMOTE_HOSTNAME} ${PORT} ..." >>${LOGFILE}
$REDISCLI -h $(hostname) -p ${PORT} SLAVEOF ${REMOTE_HOSTNAME} ${PORT} &>>${LOGFILE}
EOF
cat >/etc/keepalived/scripts/redis_check.sh<<-EOF
#!/bin/bash
PORT="${1:-6380}"
LOGFILE=/var/log/keepalived-redis-state.log
ALIVE=$(/bin/redis-cli -h $(hostname) -p ${PORT} PING)
if [ "$ALIVE" == "PONG" ]; then
exit 0
else
echo -e "[redis_check fault] $(date +%F_%T)" >> $LOGFILE
exit 1
fi
EOF
cat >/etc/keepalived/scripts/redis_fault.sh<<-EOF
#!/bin/bash
systemctl stop keepalived.service
EOF
cat >/etc/keepalived/scripts/redis_stop.sh<<-EOF
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo -e "[keepalived stop] $(date +%F_%T)" >> $LOGFILE
EOF
chmod +x /etc/keepalived/scripts/*.sh
|