码迷,mamicode.com
首页 > 其他好文 > 详细

keepalived Redis-master判定 VIP 实现自动飘移

时间:2018-03-29 17:24:17      阅读:452      评论:0      收藏:0      [点我收藏+]

标签:keepalived双活   redis多主多从   keepalived VIP地址漂移   Keepalived iptables   

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 



keepalived Redis-master判定 VIP 实现自动飘移

标签:keepalived双活   redis多主多从   keepalived VIP地址漂移   Keepalived iptables   

原文地址:http://blog.51cto.com/51log/2092559

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!