1.环境准备
| 角色 | 主机 | IP | 端口 |
| ---- | ---- | ----------- | ---- |
| 主库 | db01 | 172.16.1.51 | 6379 |
| 从库 | db02 | 172.16.1.52 | 6379 |
| 从库 | db03 | 172.16.1.53 | 6379 |
2.恢复主从状态
# 修复坏掉的主库
[root@db01 ~]# redis-server /service/redis/6379/redis.conf
[root@db01 ~]# redis-cli -h 172.16.1.51
172.16.1.51:6379> info replication
172.16.1.51:6379> SLAVEOF 172.16.1.52 6379
OK
172.16.1.51:6379> info replication
# 主库查看
172.16.1.52:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.16.1.53,port=6379,state=online,offset=4229,lag=1
slave1:ip=172.16.1.51,port=6379,state=online,offset=4229,lag=1
master_repl_offset:4229
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:4228
3.配置sentinel哨兵
# 创建目录(随便创建)
[root@db01 ~]# mkdir /service/redis/26379
# 编辑sentinel配置文件
[root@db01 ~]# vim /service/redis/26379/sentinel.conf
port 26379
daemonize yes
pidfile /service/redis/26379/sentinel.pid
logfile /service/redis/26379/sentinel.log
dir /service/redis/26379
bind 172.16.1.51 127.0.0.1
sentinel monitor mymaster 172.16.1.52 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
4.启动sentinel
[root@db01 ~]# redis-sentinel /service/redis/26379/sentinel.conf
#启动之后配置文件会发生改变
[root@db01 ~]# vim /service/redis/26379/sentinel.conf
port 26379
daemonize yes
pidfile "/service/redis/26379/sentinel.pid"
logfile "/service/redis/26379/sentinel.log"
dir "/service/redis/26379"
bind 172.16.1.51 127.0.0.1
sentinel myid 7d430385a1269307819e5300ecf09dfbf92b46f5
sentinel monitor mymaster 172.16.1.52 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel config-epoch mymaster 0
# Generated by CONFIG REWRITE
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 172.16.1.51 6379
sentinel known-slave mymaster 172.16.1.53 6379
sentinel current-epoch 0
5.停止sentinel
[root@db01 ~]# redis-cli -p 26379 shutdown
6.测试sentinel
# 关闭主库的redis
[root@db02 ~]# redis-cli shutdown
# 查看其它从库主从状态
172.16.1.51:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.16.1.53,port=6379,state=online,offset=586,lag=1
master_repl_offset:723
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:722
7.恢复故障节点
# 修复故障节点
[root@db02 ~]# redis-server /service/redis/6379/redis.conf
# 查看主库状态
172.16.1.51:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.16.1.53,port=6379,state=online,offset=5077,lag=1
slave1:ip=172.16.1.52,port=6379,state=online,offset=5077,lag=1
master_repl_offset:5077
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:5076
8.sentinel管理命令(不常用)
#连接sentinel管理端口
[root@db01 ~]# redis-cli -p 26379
# 检测状态,返回PONG
127.0.0.1:26379> ping
PONG
# 列出所有被监视的主服务器
127.0.0.1:26380> SENTINEL masters
# 列出所有被监视的从服务器
127.0.0.1:26380> SENTINEL slaves mymaster
# 返回给定名字的主服务器的IP地址和端口号
127.0.0.1:26380> SENTINEL get-master-addr-by-name mymaster
1) "172.16.1.51"
2) "6379
# 重置所有名字和给定模式
127.0.0.1:26380> SENTINEL reset mymaster
# 当主服务器失效时,在不询问其他Sentinel意见的情况下,强制开始一次自动故障迁移。
127.0.0.1:26380> SENTINEL failover mymaster
9.设置权重,指定主库的优先级
# 查看db02的权重
172.16.1.52:6379> CONFIG GET slave-priority
1) "slave-priority"
2) "100"
# 修改db02的权重值为0
172.16.1.52:6379> CONFIG set slave-priority 0
OK
172.16.1.52:6379> CONFIG GET slave-priority
1) "slave-priority"
2) "0"
# 权重值越低越不会优先切换为主库
# 强制开始一次自动故障迁移
127.0.0.1:26380> SENTINEL failover mymaster
# 再次查看,主库为db03
172.16.1.53:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.16.1.52,port=6379,state=online,offset=71377,lag=0
slave1:ip=172.16.1.51,port=6379,state=online,offset=71377,lag=0
master_repl_offset:71514
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:70496
repl_backlog_histlen:1019