redis哨兵故障转移恢复

1主redis宕掉后,发现db02自动变为主库:

[root@db01 ~]# pkill redis
[root@db01 ~]# redis-cli -h db03 -p 6379 set k1 v1
(error) READONLY You can't write against a read only slave.
[root@db01 ~]# redis-cli -h db02 -p 6379 set k1 v1
OK
[root@db01 ~]# 

2修复旧主库redis上线(先启动redis再启动哨兵):

[root@db01 ~]# redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf
[root@db01 ~]# ps -ef|grep redis
root       1029      1  0 17:44 ?        00:00:00 redis-server 10.0.0.201:6379
root       1033    953  0 17:45 pts/0    00:00:00 grep --color=auto redis
[root@db01 ~]# redis-sentinel /opt/redis_cluster/redis_26379/conf/redis_26379.conf
[root@db01 ~]# ps -ef|grep redis
root       1029      1  0 17:44 ?        00:00:00 redis-server 10.0.0.201:6379
root       1035      1  1 17:46 ?        00:00:00 redis-sentinel 10.0.0.201:26379 [sentinel]
root       1056    953 13 17:47 pts/0    00:00:00 grep --color=auto redis

3修复db01上线后,发现配置文件自动更新了,进一步检查发现db01已经变为了新主库的从库


[root@db01 ~]# cat /opt/redis_cluster/redis_26379/conf/redis_26379.conf
bind 10.0.0.201
port 26379
daemonize yes
logfile "/opt/redis_cluster/redis_26379/logs/redis_26379.log"
dir "/data/redis_cluster/redis_26379"
sentinel myid 32683a0682eb6db5bbb180b825fed609ed46c5c8
sentinel monitor mymaster 10.0.0.202 6379 2
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 18000
# Generated by CONFIG REWRITE
sentinel config-epoch mymaster 1
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 10.0.0.203 6379
sentinel known-slave mymaster 10.0.0.201 6379
sentinel known-sentinel mymaster 10.0.0.203 26379 ed81a1b04c2c9d94cd9fbbab3bd8a843acc0b226
sentinel known-sentinel mymaster 10.0.0.202 26379 c935a3cf1827195dbf9437ec1f12fba40194d0c8
sentinel current-epoch 1
[root@db01 ~]# redis-cli
127.0.0.1:6379> config get slaveof
1) "slaveof"
2) "10.0.0.202 6379"
127.0.0.1:6379>

 4因为db01性能最好,我还想让db01变为主

谁权重大,谁就是主,默认每个节点的权重都是100,所以就选谁的id大谁就是主

第一种解决方案:

(1)在db02和db03中操作,把db02和db03的权重变小

[root@db02 ~]# redis-cli
127.0.0.1:6379> config get slave-priority
1) "slave-priority"
2) "100"
127.0.0.1:6379> config set slave-priority 0
OK
127.0.0.1:6379> config get slave-priority
1) "slave-priority"
2) "0"
127.0.0.1:6379> 

(2)在db01中操作,让哨兵执行新的选主操作

[root@db01 ~]# redis-cli -h db01 -p 26379 Sentinel failover mymaster
OK
[root@db01 ~]# 

(3)分别在db02和db03中操作,检查是否db01变成了主库:

127.0.0.1:6379> config get slaveof
1) "slaveof"
2) "10.0.0.201 6379"
127.0.0.1:6379> 

(4)为了保证下次故障时哨兵还能自动切换选主,我们需要将db02和db03的权重恢复默认值:

127.0.0.1:6379> config set slave-priority 100
OK

 

上一篇:利用工具部署redis集群


下一篇:mysql:基础管理、体系结构、升级降级