Redis双机热备方案(2)

cd /usr/local/keepalived

#将keepalived.conf备份: mv /usr/local/keepalived/etc/keepalived/keepalived.conf /usr/local/keepalived/etc/keepalived/keepalived.conf-bak

#在Master:192.168.1.218上创建如下配置文件(可根据实际情况调整):

vi /usr/local/keepalived/etc/keepalived/keepalived.conf

! Configuration File for keepalived #global_defs { # notification_email { # } # notification_email_from Alexandre.Cassen@firewall.loc # router_id node3 #} vrrp_script chk_Redis { script "/usr/local/keepalived/etc/keepalived/scripts/redis_check.sh" #如果脚本执行结果非0,并且weight配置的值小于0,则优先级相应的减少;如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加;其他情况,维持原本prority的优先级。 # weight -20 interval 10 #设置脚本执行的频率。10秒一次 } vrrp_instance VI_1 { state BACKUP #要设置恢复时不抢占,需要将主,从服务器的此项都设置成BACKUP,nopreempt才会生效。 #state MASTER interface eth3 virtual_router_id 51 priority 100 nopreempt #设置不抢占。在priority值比较高的服务器上设置即可。priority值比较低的服务器启动时,发现值高的服务器为master,自动不抢占。 #advert_int的作用是巡检的次数。keepalived默认是在启动完成后3秒向state:MASTER切换。若此处设置成2,则是2*3=6秒后才开启切换。 advert_int 1 authentication { auth_type PASS auth_pass redis } virtual_ipaddress { 192.168.1.220 } track_script { chk_redis } notify_master /usr/local/keepalived/etc/keepalived/scripts/master.sh notify_backup /usr/local/keepalived/etc/keepalived/scripts/backup.sh notify_fault /usr/local/keepalived/etc/keepalived/scripts/fault.sh notify_stop /usr/local/keepalived/etc/keepalived/scripts/stop.sh }

#在Slave:192.168.1.219上创建如下配置文件(可根据实际情况调整):
vi /usr/local/keepalived/etc/keepalived/keepalived.conf

! Configuration File for keepalived #global_defs { # notification_email { # } # notification_email_from Alexandre.Cassen@firewall.loc # router_id node3 #} vrrp_script chk_redis { script "/usr/local/keepalived/etc/keepalived/scripts/redis_check.sh" #如果脚本执行结果非0,并且weight配置的值小于0,则优先级相应的减少;如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加;其他情况,维持原本prority的优先级。 # weight -20 interval 10 #设置脚本执行的频率。10秒一次 } vrrp_instance VI_1 { state BACKUP interface eth5 garp_master_delay 10 virtual_router_id 51 priority 90 #nopreempt #advert_int的作用是巡检的次数。keepalived默认是在启动完成后3秒向state:MASTER切换。若此处设置成2,则是2*3=6秒后才开启切换。 advert_int 1 authentication { auth_type PASS auth_pass redis } virtual_ipaddress { 192.168.1.220 } track_script { chk_redis } notify_master /usr/local/keepalived/etc/keepalived/scripts/master.sh #当keepalived切换成master时,会触发执行master.sh notify_backup /usr/local/keepalived/etc/keepalived/scripts/backup.sh #当keepalived切换成slave时,会触发执行slave.sh notify_fault /usr/local/keepalived/etc/keepalived/scripts/fault.sh #当keepalived出错时,会触发执行fault.sh notify_stop /usr/local/keepalived/etc/keepalived/scripts/stop.sh #当keepalived停止时,会触发执行stop.sh }

#指定keepalived的日志文件 vi /usr/local/keepalived/etc/sysconfig/keepalived

#KEEPALIVED_OPTIONS="-D" KEEPALIVED_OPTIONS="-D -d -S 0"

#RedHat6.0以下服务器修改/etc/syslog.conf,redhat6.0以上(包括6.0)服务器修改/etc/rsyslog.conf,新增以下内容:

#Save keepalived message to keepalived.log local0.* /usr/local/keepalived/logs/keepalived.log

#重启日志服务

service rsyslog restart

#将文件拷贝到相应的位置

cp -r * /

#在Master和Slave上创建监控Redis的相关脚本脚本,以下脚本都是master上的配置,slave上只需修改相应的IP地址。
mkdir /usr/local/keepalived/etc/keepalived/scripts

vi /usr/local/keepalived/etc/keepalived/scripts/redis_check.sh

#!/bin/bash KPATH=/usr/local/keepalived RPATH=/usr/local/redis REDISCLI=$RPATH/bin/redis-cli LOGFILE=$KPATH/logs/redis-state.log LOCALIP="192.168.1.218" REMOTEIP="192.168.1.219" PORT="6379" PID=$$ ALIVE=`$REDISCLI PING` if [ "$ALIVE" == "PONG" ]; then echo "[INFO]`date +'%Y-%m-%d:%H:%M:%S'` :$LOCALIP local redis is health." >> $LOGFILE exit 0 else echo "[ERROR]`date +'%Y-%m-%d:%H:%M:%S'` :$LOCALIP local redis is not health." >> $LOGFILE #当发现本地redis无法连接时,等待一秒后再进行一次检查。若恢复,则提示;若仍无法连接,则关闭本地keepalived,将虚拟ip漂移到另外一台服务器上。 sleep 1 ALIVE1=`$REDISCLI PING` if [ "$ALIVE1" == "PONG" ];then echo "[NOTICE]`date +'%Y-%m-%d:%H:%M:%S'` :$LOCALIP local redis become health." >> $LOGFILE exit 0 else echo "[ERROR]`date +'%Y-%m-%d:%H:%M:%S'` :$LOCALIP local redis is error." >> $LOGFILE echo "[ERROR]`date +'%Y-%m-%d:%H:%M:%S'` :$LOCALIP shutdown local keepalived." >> $LOGFILE /etc/init.d/keepalived stop if [ "$?" != "0" ];then echo "[ERROR]`date +'%Y-%m-%d:%H:%M:%S'` :$LOCALIP keepalived shutdown error." >> $LOGFILE else echo "[INFO]`date +'%Y-%m-%d:%H:%M:%S'` :$LOCALIP keepalived shutdown completed." >> $LOGFILE fi exit 1 fi fi

#######################################################################

vi /usr/local/keepalived/etc/keepalived/scripts/master.sh

#!/bin/bash KPATH=/usr/local/keepalived RPATH=/usr/local/redis REDISCLI=$RPATH/bin/redis-cli LOGFILE=$KPATH/logs/redis-state.log LOCALIP="192.168.1.218" REMOTEIP="192.168.1.219" PORT="6379" PID=$$ #当此服务器的keepalived恢复成master时,即虚拟IP切换到本机时,将本机的redis切换成role:master echo "[WARM]-----------keepalived change to master,change local redis to master---------------" >> $LOGFILE echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[slave]" >> $LOGFILE #先切换成role:slave echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[slave] Run 'SLAVEOF $REMOTEIP $PORT'" >> $LOGFILE $REDISCLI SLAVEOF $REMOTEIP $PORT >> $LOGFILE 2>&1 #同步数据 echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[slave] wait 10 sec for data sync from old master" >> $LOGFILE sleep 10 #等待10秒(此时间要根据实际业务需要进行调整),待数据同步完,再切换成role:master echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[slave] data rsync from old mater ok..." >> $LOGFILE echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master] Run slaveof no one,close master/slave" >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master] wait other slave connect...." >> $LOGFILE echo "-------------------------------------complete!------------------------------------------" >> $LOGFILE

#######################################################################

vi /usr/local/keepalived/etc/keepalived/scripts/backup.sh

#!/bin/bash KPATH=/usr/local/keepalived RPATH=/usr/local/redis REDISCLI=$RPATH/bin/redis-cli LOGFILE=$KPATH/logs/redis-state.log LOCALIP="192.168.1.218" REMOTEIP="192.168.1.219" PORT="6379" PID=$$ #当此服务器的keepalived恢复成slave时,即虚拟IP切换到其他服务器时,将本机redis切换成role:slave echo "[WARM]------------keepalived change to slave,change local redis to slave----------------" >> $LOGFILE echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master]" >> $LOGFILE echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master] Being slave state..." >> $LOGFILE 2>&1 #切换时,等待10秒,让对方同步数据(此时间要根据实际业务需要进行调整) echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master] wait 10 sec for data sync from old master" >> $LOGFILE sleep 10 echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master] data rsync from old mater ok..." >> $LOGFILE #等数据同步完,再切换成role:slave echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[slave] Run 'SLAVEOF $REMOTEIP $PORT'" >> $LOGFILE $REDISCLI SLAVEOF $REMOTEIP $PORT >> $LOGFILE 2>&1 echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[slave] slave connect to $REMOTEIP $PORT ok..." >> $LOGFILE echo "-------------------------------------complete!------------------------------------------" >> $LOGFILE

#######################################################################

vi /usr/local/keepalived/etc/keepalived/scripts/stop.sh

#!/bin/sh KPATH=/usr/local/keepalived RPATH=/usr/local/redis REDISCLI=$RPATH/bin/redis-cli LOGFILE=$KPATH/logs/redis-state.log LOCALIP="192.168.1.218" REMOTEIP="192.168.1.219" PORT="6379" PID=$$ #当主服务器的keepalived停止时,将本机redis切换成role:slave echo "[ERROR]-----------------keepalived stop,change local redis to slave---------------------" >> $LOGFILE echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master]" >> $LOGFILE echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master] Being slave state..." >> $LOGFILE 2>&1 #切换时,等待10秒,让对方同步数据(此时间要根据实际业务需要进行调整) echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master] wait 10 sec for data sync from old master" >> $LOGFILE sleep 10 echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master] data rsync from old mater ok..." >> $LOGFILE #等数据同步完,再切换成role:slave echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[slave] Run 'SLAVEOF $REMOTEIP $PORT'" >> $LOGFILE $REDISCLI SLAVEOF $REMOTEIP $PORT >> $LOGFILE 2>&1 echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[slave] slave connect to $REMOTEIP $PORT ok..." >> $LOGFILE echo "-------------------------------------complete!------------------------------------------" >> $LOGFILE

#######################################################################

vi /usr/local/keepalived/etc/keepalived/scripts/fault.sh

#!/bin/bash KPATH=/usr/local/keepalived RPATH=/usr/local/redis REDISCLI=$RPATH/bin/redis-cli LOGFILE=$KPATH/logs/redis-state.log LOCALIP="192.168.1.218" REMOTEIP="192.168.1.219" PORT="6379" PID=$$ #当此服务器的keepalived出错时,将本机redis切换成role:slave echo "[ERROR]---------------keepalived is fault,change local redis to slave-------------------" >> $LOGFILE echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master]" >>$LOGFILE echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master] Being slave state..." >> $LOGFILE 2>&1 #切换时,等待10秒,让对方同步数据(此时间要根据实际业务需要进行调整) echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master] wait 10 sec for data sync from old master" >> $LOGFILE sleep 10 echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[master] data rsync from old mater ok..." >> $LOGFILE #等数据同步完,再切换成role:slave echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[slave] Run 'SLAVEOF $REMOTEIP $PORT'" >> $LOGFILE $REDISCLI SLAVEOF $REMOTEIP $PORT >> $LOGFILE 2>&1 echo "`date +'%Y-%m-%d:%H:%M:%S'`|$PID|state:[slave] slave connect to $REMOTEIP $PORT ok..." >> $LOGFILE echo "-------------------------------------complete!------------------------------------------" >> $LOGFILE

####################################################################### 修改监控脚本的权限: chmod -R 750 /usr/local/keepalived/etc/keepalived/scripts/

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/fa64eade9baa9808f81e3ca40998d4bb.html