vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from root@localhost smtp_server localhost smtp_connect_timeout 30 router_id LVS_REDIS } vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh" ###监控脚本 interval 2 ###监控时间 } vrrp_instance VI_1 { nopreempt ###不抢占,防止脑裂 state BACKUP #备的是BACKUP interface br0 virtual_router_id 51 priority 90 #备的是90 advert_int 1 authentication { auth_type PASS auth_pass redis } virtual_ipaddress { 192.168.68.45 } track_script { chk_redis ###执行上面定义的chk_redis } notify_master /etc/keepalived/scripts/redis_master.sh notify_backup /etc/keepalived/scripts/redis_backup.sh notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh } [x] 在Master和Slave上创建监控Redis的脚本:mkdir /etc/keepalived/scripts/
vi /etc/keepalived/scripts/redis_check.sh
vim /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash C_DATE=`date +"[%Y-%m-%d %H:%M:%S]"` LOGFILE=/var/log/keepalived-redis-state.log echo "${C_DATE} [fault]" >> $LOGFILEvim /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash C_DATE=`date +"[%Y-%m-%d %H:%M:%S]"` LOGFILE=/var/log/keepalived-redis-state.log echo "${C_DATE} [stop]" >> $LOGFILE [x] 在Master上创建notity_master与notify_backup脚本:vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash C_DATE=`date +"[%Y-%m-%d %H:%M:%S]"` REDISCLI="/opt/redis/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "${C_DATE} [master]" >> $LOGFILE #当keepalived配置为"抢占式"时,打开下面注释 #echo "Being master...." >> $LOGFILE 2>&1 #echo "Run SLAVEOF cmd ..." >> $LOGFILE #$REDISCLI SLAVEOF T2 6379 >> $LOGFILE 2>&1 #sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1vim /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash C_DATE=`date +"[%Y-%m-%d %H:%M:%S]"` REDISCLI="/opt/redis/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "${C_DATE} [backup]" >> $LOGFILE #当keepalived配置为"抢占式"时,打开下面注释 #echo "Being slave...." >> $LOGFILE 2>&1 #sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF T2 6379 >> $LOGFILE 2>&1 [x] 在Slave上创建notity_master与notify_backup脚本:vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash C_DATE=`date +"[%Y-%m-%d %H:%M:%S]"` REDISCLI="/opt/redis/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "${C_DATE} [master]" >> $LOGFILE #当keepalived配置为"抢占式"时,打开下面注释 #echo "Being master...." >> $LOGFILE 2>&1 #echo "Run SLAVEOF cmd ..." >> $LOGFILE #$REDISCLI SLAVEOF T1 6379 >> $LOGFILE 2>&1 #sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1