环境:
真实IP:10.1.4.25 10.1.4.26 10.1.4.27 10.1.4.28
vip: 10.1.4.85 10.1.4.86 10.1.4.87
监控机:10.1.3.185
目的:通过redis-sentinel+脚本实现高可用故障切换!
主从搭建(省略)
10.1.4.25/6381 10.1.4.26/6382 10.1.4.27/6383 多实例备库10.1.4.28/6381/6382/6383
脚本 redis_failover_4_25.sh redis_failover_4_26.sh redis_failover_4_27.sh
只放在真是机器上,监控机不用放。赋权chmod +x
脚本:
#!/bin/bash MASTER_IP=$6 VIP='10.1.4.85' # VIP NETMASK='24' # Netmask INTERFACE='eth1' # 接口 MY_IP="`ifconfig ${INTERFACE} | awk -F '[ :]+' 'NR==2{print $4}'`" # 每个Server本身的IP if [ ${MASTER_IP} = ${MY_IP} ]; then /sbin/ip addr add ${VIP}/${NETMASK} dev ${INTERFACE} label ${INTERFACE}:1 /sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE} exit 0 else /sbin/ip addr del ${VIP}/${NETMASK} dev ${INTERFACE} label ${INTERFACE}:1 exit 0 fi exit 1
3. 哨兵sentinel
3.185上启动3个哨兵 sentinel_4_25.conf sentinel_4_26.conf sentinel_4_27.conf
4.25上启动一个哨兵:sentinel_4_25.conf
4.26上启动一个哨兵:sentinel_4_26.conf
4.27上启动一个哨兵:sentinel_4_27.conf
4.28上启动3个哨兵 sentinel_4_25.conf sentinel_4_26.conf sentinel_4_27.conf
模板:sentinel_4_25.conf
# sentinel 配置 # by : 王老虎 port 26325 daemonize yes logfile "/u01/redis/log/sentinel26325.log" sentinel monitor dbamaster1 10.1.4.25 6381 2 sentinel down-after-milliseconds dbamaster1 3000 sentinel failover-timeout dbamaster1 30000 sentinel client-reconfig-script dbamaster1 /etc/redis/redis_failover_4_25.sh sentinel parallel-syncs mymaster 1
略解释下名词:
sentinel monitor dbamaster1 10.1.4.25 6381 2#监控的IP 端口 以及几路检测判断
sentinel down-after-milliseconds dbamaster1 3000 #两路检测3s都没连上 启动切换
sentinel failover-timeout dbamaster1 30000 #稍后解释
sentinel client-reconfig-script dbamaster1 /etc/redis/redis_failover_4_25.sh#执行脚本
sentinel parallel-syncs mymaster 1 #出现问题后允许几个备库同步数据
5. VIP
ip addr add 10.1.4.85/24 label eth1:1 dev eth1
ip addr add 10.1.4.86/24 label eth1:1 dev eth1
ip addr add 10.1.4.87/24 label eth1:1 dev eth1
启动哨兵: