Redis Sentinel的作用
Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务:
- 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
- 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
- 自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
启动 Sentinel
redis-server /path/to/sentinel.conf --sentinel
配置 Sentinel
#监控的主机ip:port,选举的最小节点数
sentinel monitor mymaster 127.0.0.1 6379 2
Sentinel演示
- 编辑三台sentinel的配置文件,最简单的配置
vi 26379.conf
port 26379
sentinel monitor mymaster 127.0.0.1 6379 2
vi 26380.conf
port 26380
sentinel monitor mymaster 127.0.0.1 6379 2
vi 26381.conf
port 26381
sentinel monitor mymaster 127.0.0.1 6379 2
- 启动三个redis实例,并设置主备
redis-server ./6379.conf
redis-server ./6380.conf --replicaof 127.0.0.1 6379
redis-server ./6381.conf --replicaof 127.0.0.1 6379
- 启动三个sentinel实例
redis-server ./26379.conf --sentinel
redis-server ./26380.conf --sentinel
redis-server ./26381.conf --sentinel
- 启动后观察sentinel的前台打印信息
#发现sentinel可以检测到所有其他redis实例和其他sentinel实例的信息,这里使用的使发布订阅
+monitor master mymaster 127.0.0.1 6379 quorum 2
5940:X 08 Jul 2020 23:33:38.388 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
5940:X 08 Jul 2020 23:33:38.390 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
5940:X 08 Jul 2020 23:33:39.764 * +sentinel sentinel 072109a1363db90c3c5907b5ab8c618941ead027 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
5940:X 08 Jul 2020 23:33:39.851 * +sentinel sentinel 863d8f09d4648dd6edf2cc48dd1e3c19fd96e84e 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
- 主节点宕机
#6379
Ctrl + c
#其他节点链接主节点失败
Connecting to MASTER 127.0.0.1:6379
MASTER <-> REPLICA sync started
Error condition on socket for SYNC: Connection refused
- 观察哨兵信息
在三台sentinel节点上,某一个节点的前台打印信息较长,此时是这个sentinel节点被选举为主节点,并对redis实例进行故障迁移
+sdown master mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:23.333 # +odown master mymaster 127.0.0.1 6379 #quorum 2/2
5911:X 08 Jul 2020 23:43:23.333 # +new-epoch 1
#连接不上主节点
5911:X 08 Jul 2020 23:43:23.333 # +try-failover master mymaster 127.0.0.1 6379
#执行选举过程
5911:X 08 Jul 2020 23:43:23.335 # +vote-for-leader 072109a1363db90c3c5907b5ab8c618941ead027 1
5911:X 08 Jul 2020 23:43:23.338 # 863d8f09d4648dd6edf2cc48dd1e3c19fd96e84e voted for 072109a1363db90c3c5907b5ab8c618941ead027 1
5911:X 08 Jul 2020 23:43:23.338 # ebfb88cc354bef72197f935557e7a5f3ba854809 voted for 072109a1363db90c3c5907b5ab8c618941ead027 1
5911:X 08 Jul 2020 23:43:23.407 # +elected-leader master mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:23.407 # +failover-state-select-slave master mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:23.469 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:23.469 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:23.535 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:23.692 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:23.692 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:23.756 * +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:23.759 * +slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:24.410 # -odown master mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:24.818 * +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:24.889 # +failover-end master mymaster 127.0.0.1 6379
5911:X 08 Jul 2020 23:43:24.889 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6380
#使6380作为主节点,6379,6381作为从节点
5911:X 08 Jul 2020 23:43:24.889 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
5911:X 08 Jul 2020 23:43:24.889 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
5911:X 08 Jul 2020 23:43:54.939 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
- 观察配置文件
vim ./26379.conf
#哨兵会自动修改配置文件
port 26379
sentinel myid 072109a1363db90c3c5907b5ab8c618941ead027
# Generated by CONFIG REWRITE
dir "/home/software/test"
protected-mode no
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel config-epoch mymaster 1
sentinel leader-epoch mymaster 1
sentinel known-replica mymaster 127.0.0.1 6379
sentinel known-replica mymaster 127.0.0.1 6381
sentinel known-sentinel mymaster 127.0.0.1 26381 ebfb88cc354bef72197f935557e7a5f3ba854809
sentinel known-sentinel mymaster 127.0.0.1 26380 863d8f09d4648dd6edf2cc48dd1e3c19fd96e84e
sentinel current-epoch 1
Sentinel间相互通信
Sentinel监控主节点,可以获得其他Redis实例的信息,那sentinel是怎样获得其他sentinel实例的信息呢 - 是通过我们之前介绍过的发布订阅功能
连接当前主节点
redis-cli -p 6380
PSUBSCRIBE *
哨兵通过发布订阅功能通信
"__sentinel__:hello"
4) "127.0.0.1,26379,072109a1363db90c3c5907b5ab8c618941ead027,1,mymaster,127.0.0.1,6380,1"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26381,ebfb88cc354bef72197f935557e7a5f3ba854809,1,mymaster,127.0.0.1,6380,1"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26380,863d8f09d4648dd6edf2cc48dd1e3c19fd96e84e,1,mymaster,127.0.0.1,6380,1"