Redis 主从不再重复

默认路径 :/usr/local/redis 


keepalived 默认/etc/keepalived

检测监控 redis_backup.sh,redis_check.sh,redis_fault.sh redis_master.sh,redis_stop.sh脚本默认 /etc/keepalived/script 


iptables 设置添加 需要开放的端口和地址

224.0.0.18 是vrrp组播地址 ;

iptables -A INPUT -d 224.0.0.18 -j ACCEPT


Master 配置


global_defs {

   router_id redis1

}

vrrp_script chk_redis 

     script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379" 

     interval 2 

     timeout 2

     fall 3

}

vrrp_instance redis {

     state MASTER 

     interface bond0  

     virtual_router_id 100 

     priority  100       

     nopreempt # no seize,must add

     advert_int 1

         authentication {   #all node must same

         auth_type PASS

         auth_pass 1111

    }   

    virtual_ipaddress {  

                 1.2.3.4/24

    }

    track_script { 

         chk_redis 

    } 

     notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1  1.2.3.1 6379"

     notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1  1.2.3.1 6379"

     notify_fault /etc/keepalived/scripts/redis_fault.sh 

     notify_stop /etc/keepalived/scripts/redis_stop.sh 

}


Salve 配置


global_defs {

   router_id redis101

}

vrrp_script chk_redis 

     script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379" 

     interval 2

     timeout 2

     fall 3

}


vrrp_instance redis {

    state BACKUP   

    interface bond0 

    virtual_router_id 90 

    priority  100       

    advert_int 1

authentication {   #all node must same

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress { 

    1.2.3.4/24

    }

    track_script { 

         chk_redis 

    } 

    notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1  1.2.3.2 6379"

    notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1  1.2.3.2 6379"

    notify_fault /etc/keepalived/scripts/redis_fault.sh 

    notify_stop /etc/keepalived/scripts/redis_stop.sh 

}


VIP 检测 漂移


# redis_backup.sh


#!/bin/bash 

REDISCLI="/usr/local/redis/src/redis-cli -h $1 -p $3" 

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

echo "[backup]" >> $LOGFILE 

date >> $LOGFILE 

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

$REDISCLI SLAVEOF $2 $3 >> $LOGFILE 2>&1 

# echo "Being slave...." >> $LOGFILE 2>&1 

sleep 15 #delay 15 s wait data sync exchange role


 -----


# redis_check.sh


#!/bin/bash 

ALIVE=`/usr/local/redis/src/redis-cli -h $1 -p $2 PING` 

LOGFILE="/etc/keepalived/log/keepalived-redis-check.log" 

echo "[CHECK]" >> $LOGFILE

date >> $LOGFILE

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

   echo "Success: redis-cli -h $1 -p $2 PING $ALIVE" >> $LOGFILE 2>&1

   exit 0 

else 

   echo "Failed:redis-cli -h $1 -p $2 PING $ALIVE " >> $LOGFILE 2>&1

   exit 1 

fi 


  -----

 

# redis_fault.sh

#!/bin/bash 

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

echo "[fault]" >> $LOGFILE

date >> $LOGFILE 


 -----

 

# redis_master.sh


#!/bin/bash 

REDISCLI="/usr/local/redis/src/redis-cli -h $1 -p $3" 

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

echo "[master]" >> $LOGFILE 

date >> $LOGFILE 

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

echo "Run MASTER cmd ..." >> $LOGFILE 2>&1

$REDISCLI SLAVEOF $2 $3 >> $LOGFILE  

sleep 10 #delay 10 s wait data async cancel sync

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

$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1


 -----

 

# redis_stop.sh


#!/bin/bash 

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

echo "[stop]" >> $LOGFILE 

date >> $LOGFILE