Redis 主从 keepalived高可用 实现 VIP 自动漂移

Posted Weblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis 主从 keepalived高可用 实现 VIP 自动漂移相关的知识,希望对你有一定的参考价值。

Redis 多主写多从度 配置启动OK ;直接配 keepalived  相关配置;

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 

 实现VIP 地址检测漂移;VRRP 组播地址是必须要通信的 ;

iptables 设置添加 需要开放的端口和地址
224.0.0.18 是vrrp组播地址 ;
iptables -A INPUT -d 224.0.0.18 -j ACCEPT

Master - redis  主节点1  keepalived.conf  配置

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 - redis  主节点2  keepalived.conf  配置

 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 备份脚本

# 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 检测脚本 

# 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 故障判断脚本

# redis_fault.sh

#!/bin/bash 
LOGFILE=/etc/keepalived/log/keepalived-redis-state.log 
echo "[fault]" >> $LOGFILE
date >> $LOGFILE 

 -----

redis 主节点检测脚本

# 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 检测停止脚本

# redis_stop.sh

#!/bin/bash 
LOGFILE=/etc/keepalived/log/keepalived-redis-state.log 
echo "[stop]" >> $LOGFILE 
date >> $LOGFILE 

 








以上是关于Redis 主从 keepalived高可用 实现 VIP 自动漂移的主要内容,如果未能解决你的问题,请参考以下文章

redis主从复制及keepalived方式实现高可用测试

Redis+Keepalived高可用环境部署记录

利用redis主从+keepalived实现高可用

Redis + keepalived 高可用行配置检测脚本

Redis+Keepalived实现高可用

MySQL之keepalived高可用