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

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

时间:2018-11-23 20:30:47      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:pad   eth0   ==   virtual   div   主从模式   strong   not   The   

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 高可用行配置检测脚本

标签:pad   eth0   ==   virtual   div   主从模式   strong   not   The   

原文地址:https://www.cnblogs.com/sharesdk/p/10009246.html

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