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
timeout
fall
rise
} vrrp_instance redis {
state MASTER
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
172.16.2.36/
}
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=
LOGFILE="/opt/keepalived/keepalived-redis-check.log"
echo '-----------------' >> $LOGFILE for port in ${ports[*]}
do
if [ $port -eq ] ;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+]
echo "Success: redis-cli -p $port PING $ALIVE" >> $LOGFILE >&
else
echo "Failed:redis-cli -p $port PING $ALIVE " >> $LOGFILE >&
fi
done if [ $port_num -eq $pong_num ];then
echo "check is ok" >> $LOGFILE
exit
else
echo "check is error" >> $LOGFILE
exit
fi
master 检测;
#!/bin/bash
ports=( )
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 >&
echo "Run MASTER cmd ..." >> $LOGFILE >&
echo "Run -p $port SLAVEOF NO ONE cmd ..." >> $LOGFILE
if [ $port -eq ];then
/usr/bin/redis-cli -h 172.16.2.56 -a -p $port slaveof no one >> $LOGFILE >&
else
/usr/bin/redis-cli -h 172.16.2.56 -a -p $port slaveof no one >> $LOGFILE >&
fi
done
backup 检测;
#!/bin/bash
ports=( )
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 >&
echo "Run BACKUP cmd ..." >> $LOGFILE >&
echo "Run -p $port SLAVEOF $other_ip cmd ..." >> $LOGFILE
if [ $port -eq ];then
/usr/bin/redis-cli -h 172.16.2.56 -p $port -a slaveof $other_ip $port >> $LOGFILE >&
else
/usr/bin/redis-cli -h 172.16.2.56 -p $port -a slaveof $other_ip $port >> $LOGFILE >&
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