1 redis安装、  keepalived安装


2 keepalived配置

master:

/etc/keepalived/keepalived.conf

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

! Configuration File for keepalived

vrrp_script chk_redis { 

                script "/etc/keepalived/scripts/redis_check.sh"   ###监控脚本 

                interval 2                                        ###监控时间 

vrrp_instance VI_1 { 

        state MASTER                            ###设置为MASTER

        interface eth0                          ###监控网卡    

        virtual_router_id 51

        priority 110                           ###权重值

        authentication { 

                     auth_type PASS             ###加密 

                     auth_pass 7IB8LAW3RB       ###密码 

        } 

        track_script { 

                chk_redis                       ###执行上面定义的chk_redis

        } 

        virtual_ipaddress { 

             192.168.2.200                         ###VIP 

        }

        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 



redis_check.sh

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

#!/bin/bash

PORT="6379"

REQUIREPASS="password"


 

ALIVE=`/usr/local/redis/bin/redis-cli -p $PORT -a $REQUIREPASS PING`

if [ "$ALIVE" == "PONG" ]; then

  echo $ALIVE

  exit 0

else

  echo $ALIVE

  exit 1

fi



#########

/etc/keepalived/scripts/下创建脚本(注意脚本要有可执行权限):

redis_master.sh

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

#!/bin/bash

 

REDISCLI="/usr/local/redis/bin/redis-cli"

LOGFILE="/var/log/keepalived-redis-state.log"

 

echo "[master]" >> $LOGFILE

date >> $LOGFILE

echo "Being master...." >> $LOGFILE 2>&1

 

echo "Run SLAVEOF cmd ..." >> $LOGFILE

$REDISCLI -p 6379 -a admin SLAVEOF 192.168.2.82 6379 >> $LOGFILE  2>&1

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态

 

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE

$REDISCLI -p 6379 -a admin SLAVEOF NO ONE >> $LOGFILE 2>&1

########


redis_backup.sh

########

#!/bin/bash

 

REDISCLI="/usr/local/redis/bin/redis-cli"

LOGFILE="/var/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 -p 6379 -a admin SLAVEOF 192.168.2.82 6379 >> $LOGFILE  2>&1

########

192.168.2.82从机IP


redis_fault.sh

######

#!/bin/bash

 

LOGFILE=/var/log/keepalived-redis-state.log

 

echo "[fault]" >> $LOGFILE

date >> $LOGFILE

#####


redis_stop.sh

####

#!/bin/bash

 

LOGFILE=/var/log/keepalived-redis-state.log

 

echo "[fault]" >> $LOGFILE

date >> $LOGFILE

####



backup:

/etc/keepalived/keepalived.conf

##########

! Configuration File for keepalived

vrrp_script chk_redis { 

                script "/etc/keepalived/scripts/redis_check.sh"   ###监控脚本 

                interval 2                                        ###监控时间 

vrrp_instance VI_1 { 

        state BACKUP                                ###设置为BACKUP 

        interface eth0                              ###监控网卡

        virtual_router_id 51 

        priority 100                                ###比MASTRE权重值低 

        authentication { 

                     auth_type PASS 

                     auth_pass 7IB8LAW3RB           ###密码与MASTRE相同

        } 

        track_script { 

                chk_redis                       ###执行上面定义的chk_redis

        } 

        virtual_ipaddress { 

            192.168.2.200                         ###VIP 

        } 

        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 

}


/etc/keepalived/scripts/下创建脚本:

redis_master.sh

####

#!/bin/bash


REDISCLI="/usr/local/redis/bin/redis-cli"

LOGFILE="/var/log/keepalived-redis-state.log"


echo "[master]" >> $LOGFILE

date >> $LOGFILE

echo "Being master...." >> $LOGFILE 2>&1


echo "Run SLAVEOF cmd ..." >> $LOGFILE

#$REDISCLI -h 192.168.2.82 -p 6379 -a admin SLAVEOF NO ONE

$REDISCLI -p 6379 -a admin SLAVEOF 192.168.2.81 6379

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态


echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE

$REDISCLI -p 6379 -a admin SLAVEOF NO ONE >> $LOGFILE 2>&1

####

192.168.2.81为masterIP


redis_backup.sh

#####

#!/bin/bash


REDISCLI="/usr/local/redis/bin/redis-cli"

LOGFILE="/var/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 -h 192.168.2.81 -p 6379 -a admin >> $LOGFILE  2>&1

$REDISCLI  -h 192.168.2.82 -p 6379 -a admin slaveof 192.168.2.81 6379

####


redis_fault.sh

####

#!/bin/bash


LOGFILE=/var/log/keepalived-redis-state.log


echo "[fault]" >> $LOGFILE

date >> $LOGFILE

####


redis_stop.sh

####

#!/bin/bash


LOGFILE=/var/log/keepalived-redis-state.log


echo "[stop]" >> $LOGFILE

date >> $LOGFILE

####