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

Posted Weblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis + keepalived 高可用行配置检测脚本相关的知识,希望对你有一定的参考价值。

Redis 在生产配置中;除redis集群、哨兵模式之外;主从模式还是比较普遍的。

配置 redis 多主从;由 keepalived 做 VIP 地址漂移。可以实现redis的高可用性。

 

keepalived 配置示例;(master 主节点配置,backup根据更改就可以了。)

! Configuration File for keepalived
global_defs {
   router_id redis1
}

vrrp_script chk_redis 
{ 
     script "/etc/keepalived/scripts/redis_check.sh" 
     interval 5 
     timeout 10
     fall 10
     rise 3     
}

vrrp_instance redis {
     state MASTER
     interface eth0
     virtual_router_id 99
     priority  101
     advert_int 10
         authentication {
         auth_type PASS
         auth_pass 12345
    }
    virtual_ipaddress {
                 172.16.2.36/24
    }
    track_script { 
         chk_redis 
    } 
     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 状态检测脚本。

redis_check  Redis 状态检测;

#!/bin/bash 
ports=(6379 6389 6399)
port_num=${#ports[@]}
pong_num=0
LOGFILE="/opt/keepalived/keepalived-redis-check.log"
echo ----------------- >> $LOGFILE

for port in ${ports[*]}
do
    if [ $port -eq 6374 ] ;then
        ALIVE=`/usr/bin/redis-cli -p $port -h 172.16.2.56 -a tkfJfnMjvniitHDG PING`
    else
        ALIVE=`/usr/bin/redis-cli -p $port -h 172.16.2.56 -a ffdca1b6f2d4c6d4  PING`
    fi
    echo "[CHECK]" >> $LOGFILE
    date >> $LOGFILE
    if [ "$ALIVE" == "PONG" ];then
        pong_num=$[$pong_num+1]
        echo "Success: redis-cli -p $port PING $ALIVE" >> $LOGFILE 2>&1
    else 
        echo "Failed:redis-cli -p $port PING $ALIVE " >> $LOGFILE 2>&1
    fi
done

if [ $port_num -eq $pong_num ];then
    echo "check is ok" >> $LOGFILE
    exit 0
else
    echo "check is error" >> $LOGFILE
    exit 1
fi

master 检测;

#!/bin/bash
ports=(6379 6389 6399)
port_num=${#ports[@]}
LOGFILE="/opt/keepalived/keepalived-redis-state.log"
echo ------------- >> $LOGFILE
for port in ${ports[*]}
do
    echo "[master]" >> $LOGFILE
    date >> $LOGFILE
    echo "Being master...." >> $LOGFILE 2>&1
    echo "Run MASTER cmd ..." >> $LOGFILE 2>&1
    echo "Run -p $port SLAVEOF NO ONE cmd ..." >> $LOGFILE
    if [ $port -eq 6374 ];then
        /usr/bin/redis-cli -h 172.16.2.56 -a 123456798 -p $port slaveof no one >> $LOGFILE 2>&1
    else
        /usr/bin/redis-cli -h 172.16.2.56 -a 123456789 -p $port slaveof no one >> $LOGFILE 2>&1
    fi
done

backup 检测;

#!/bin/bash
ports=(6379 6389 6399)
port_num=${#ports[@]}
LOGFILE="/opt/keepalived/keepalived-redis-state.log"
other_ip="172.16.2.57"
echo ---------------- >> $LOGFILE
for port in ${ports[*]}
do
    echo "[backup]" >> $LOGFILE
    date >> $LOGFILE
    echo "Being backup...." >> $LOGFILE 2>&1
    echo "Run BACKUP cmd ..." >> $LOGFILE 2>&1
    echo "Run -p $port SLAVEOF $other_ip cmd ..." >> $LOGFILE
    if [ $port -eq 6374 ];then
        /usr/bin/redis-cli -h 172.16.2.56 -p $port -a 123456798 slaveof $other_ip $port >> $LOGFILE 2>&1
    else
        /usr/bin/redis-cli -h 172.16.2.56 -p $port -a 123456789 slaveof $other_ip $port >> $LOGFILE 2>&1
    fi
done

redis_fault  服务错误检测;

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

redis 服务停止检测;

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

 

 

以上是关于Redis + keepalived 高可用行配置检测脚本的主要内容,如果未能解决你的问题,请参考以下文章

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

keepalived实现redis双机高可用

单机redis多端口实例+keepalived高可用

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

SuSE11环境下Redis+Keepalived实现高可用技术

Redis 之 主从复制 + Keepalived 实现高可用