Redis 版本 Redis server v=3.2.12 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=7897e7d0e13773f
Keepalived 版本 Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
原理:
Keepalived 由权重最高获得虚拟地址,脚本执行不成功降低权重50
Sentinel 自动切换集群 Master Slave
主机名 | ip地址 | redis | Sentinel | keepalived |
node01 | 172.16.50.111 | 安装 | 安装 | 安装 |
node02 | 172.16.50.113 | 安装 | 安装 | 安装 |
node03 | 172.16.50.115 | 安装 | 安装 | 安装 |
node01 node02 node03 安装
yum install redis keepalived -y
node01配置redis
bind 0.0.0.0
node02 node03 配置redis
bind 0.0.0.0 slaveof 172.16.50.111 6379
启动 node01 node02 node03 节点redis
systemctl enable redis && systemctl start redis
node01查看slave状态
redis-cli info Replication # Replication role:master connected_slaves:2 slave0:ip=172.16.50.113,port=6379,state=online,offset=211,lag=0 slave1:ip=172.16.50.115,port=6379,state=online,offset=211,lag=0
配置Sentinel服务
protected-mode no sentinel monitor mymaster 172.16.50.111 6379 2
node01 node02 node03 分别启动Sentinel服务
systemctl enable redis-sentinel && systemctl start redis-sentinel
查看node01 sentinel 日志
12946:X 20 Feb 01:35:39.771 # Sentinel ID is 2c8b02a2536aa081f0b95e19e2948b5ac25e6306 12946:X 20 Feb 01:35:39.771 # +monitor master mymaster 172.16.50.111 6379 quorum 2 12946:X 20 Feb 01:35:39.772 * +slave slave 172.16.50.113:6379 172.16.50.113 6379 @ mymaster 172.16.50.111 6379 12946:X 20 Feb 01:35:39.789 * +slave slave 172.16.50.115:6379 172.16.50.115 6379 @ mymaster 172.16.50.111 6379 12946:X 20 Feb 01:36:25.103 * +sentinel sentinel 6eb0e3c142d97532d5558448cb09f5c038acc743 172.16.50.113 26379 @ mymaster 172.16.50.111 6379 12946:X 20 Feb 01:36:41.807 * +sentinel sentinel ed44244bb0357aa9cc70f3c3ddba466d30b4865c 172.16.50.115 26379 @ mymaster 172.16.50.111 6379
测试 sentinel 是否自动切换
停止掉node01 redis , 查看 sentinel 日志
12946:X 20 Feb 01:40:32.817 # +sdown master mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:32.870 # +odown master mymaster 172.16.50.111 6379 #quorum 2/2 12946:X 20 Feb 01:40:32.870 # +new-epoch 1 12946:X 20 Feb 01:40:32.870 # +try-failover master mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:32.888 # +vote-for-leader 2c8b02a2536aa081f0b95e19e2948b5ac25e6306 1 12946:X 20 Feb 01:40:32.961 # ed44244bb0357aa9cc70f3c3ddba466d30b4865c voted for 2c8b02a2536aa081f0b95e19e2948b5ac25e6306 1 12946:X 20 Feb 01:40:32.970 # 6eb0e3c142d97532d5558448cb09f5c038acc743 voted for 2c8b02a2536aa081f0b95e19e2948b5ac25e6306 1 12946:X 20 Feb 01:40:33.006 # +elected-leader master mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:33.006 # +failover-state-select-slave master mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:33.107 # +selected-slave slave 172.16.50.115:6379 172.16.50.115 6379 @ mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:33.107 * +failover-state-send-slaveof-noone slave 172.16.50.115:6379 172.16.50.115 6379 @ mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:33.174 * +failover-state-wait-promotion slave 172.16.50.115:6379 172.16.50.115 6379 @ mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:33.678 # +promoted-slave slave 172.16.50.115:6379 172.16.50.115 6379 @ mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:33.678 # +failover-state-reconf-slaves master mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:33.758 * +slave-reconf-sent slave 172.16.50.113:6379 172.16.50.113 6379 @ mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:34.092 # -odown master mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:34.662 * +slave-reconf-inprog slave 172.16.50.113:6379 172.16.50.113 6379 @ mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:35.724 * +slave-reconf-done slave 172.16.50.113:6379 172.16.50.113 6379 @ mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:35.800 # +failover-end master mymaster 172.16.50.111 6379 12946:X 20 Feb 01:40:35.800 # +switch-master mymaster 172.16.50.111 6379 172.16.50.115 6379 12946:X 20 Feb 01:40:35.800 * +slave slave 172.16.50.113:6379 172.16.50.113 6379 @ mymaster 172.16.50.115 6379 12946:X 20 Feb 01:40:35.801 * +slave slave 172.16.50.111:6379 172.16.50.111 6379 @ mymaster 172.16.50.115 6379
Redis Master 自动切换到 172.16.50.115 , 登录 172.16.50.115 查看状态
# Replication role:master connected_slaves:1 slave0:ip=172.16.50.113,port=6379,state=online,offset=17056,lag=1
测试完成启动node01 redis , 查看 sentinel 日志
12946:X 20 Feb 01:41:05.843 # +sdown slave 172.16.50.111:6379 172.16.50.111 6379 @ mymaster 172.16.50.115 6379 12946:X 20 Feb 01:42:56.146 # -sdown slave 172.16.50.111:6379 172.16.50.111 6379 @ mymaster 172.16.50.115 6379 12946:X 20 Feb 01:43:06.074 * +convert-to-slave slave 172.16.50.111:6379 172.16.50.111 6379 @ mymaster 172.16.50.115 6379
node01 node02 node03 配置Keepalived 服务
! Configuration File for keepalived global_defs { router_id node02 } vrrp_script chk_redis { script "/etc/keepalived/check_redis.sh" interval 2 weight -50 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_redis } virtual_ipaddress { 172.16.50.135 } }
脚本/etc/keepalived/check_redis.sh 内容如下:
#!/bin/bash ALIVE=`/usr/bin/redis-cli PING` rstatus=$(/usr/bin/redis-cli info Replication | grep "role" | awk -F ':' '{print $2}') a=$(echo $rstatus | grep "master") if [ "$ALIVE" == "PONG" ] && [ "$a" != "" ] ; then # 判断redis是否运行,及是否为master exit 0 else exit 1 fi
node01 node02 node03 分别启动Keepalived 服务
systemctl enable keepalived && systemctl start keepalived
当 Redis Master 在某一个节点上 , 虚拟IP就会漂移到这节点