概述
Redis Sentinel为Redis提供高可用性。Redis Sentinel是一个分布式系统,Sentinel本身设计为在有多个Sentinel进程协同合作的配置中运行。具有多个Sentinel进程进行协作的优点如下:
1、当多个Sentinel就给定的主机不再可用这一事实达成共识时,将执行故障检测。这降低了误报的可能性。
2、即使不是所有的Sentinel进程都在工作,Sentinel仍能正常工作,从而使系统能够应对故障。
部署前有关Sentinel的基本知识
1、一个健壮的部署至少需要三个Sentinel实例。
2、应将三个Sentinel实例放置到被认为以独立方式发生故障的计算机或虚拟机中。因此,例如在不同的可用区域上执行的不同物理服务器或虚拟机。
3由于Redis使用异步复制,因此Sentinel + Redis分布式系统不能保证在故障期间保留已确认的写入。但是,有一些部署Sentinel的方法使窗口丢失写入仅限于某些时刻,而还有其他一些不太安全的方法来部署它。
4、您的客户需要Sentinel支持。流行的客户端库具有Sentinel支持,但不是全部。
5、如果您不在开发环境中不时进行测试,则没有HA设置是安全的;如果可以,则在生产环境中甚至可以更好地进行设置。
6、Sentinel,Docker或其他形式的网络地址转换或端口映射应格外小心:Docker执行端口重新映射,破坏Sentinel对其他Sentinel进程的自动发现以及主副本的列表。有关更多信息,请参阅本文档后面有关Sentinel和Docker的部分。
先决条件
·准备好至少三台redis服务,本文档示例提供的三台redis计算机分别如下:
node1 192.168.194.134
node2 192.168.194.135
node3 192.168.194.136
主从复制
Redis Sentinel本身并不提供数据同步,所以需要组建主从复制模式,只需要在从节点redis配置文件redis.conf中加入如下配置即可(红色粗体部分):
......
################################# REPLICATION #################################
# Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# +------------------+ +---------------+
# | Master | ---> | Replica |
# | (receive writes) | | (exact copy) |
# +------------------+ +---------------+
#
# 1) Redis replication is asynchronous, but you can configure a master to
# stop accepting writes if it appears to be not connected with at least
# a given number of replicas.
# 2) Redis replicas are able to perform a partial resynchronization with the
# master if the replication link is lost for a relatively small amount of
# time. You may want to configure the replication backlog size (see the next
# sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
# network partition replicas automatically try to reconnect to masters
# and resynchronize with them.
#
# replicaof <masterip> <masterport>
replicaof 192.168.194.134 6379
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
# masterauth <master-password>
......
此文档示例以 node1为主节点以node2、node3为从节点
配置Redis Sentinel
1、在node1、node2、node3节点上为redis服务创建Sentinel配置文件my_sentinel.cnf并附加如下内容:
bind 0.0.0.0
daemonize yes
port 5000
sentinel monitor mymaster 192.168.194.134 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
启动redis服务
1、按顺序依次在node1、node2、node3上启动redis服务以及Sentinel进程,执行命令如下所示(红色粗体部分):
node1节点
[seap@node1 redis-5.0.8]$ src/redis-server ./redis.conf
14356:C 16 Mar 2020 07:46:30.475 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14356:C 16 Mar 2020 07:46:30.475 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=14356, just started
14356:C 16 Mar 2020 07:46:30.475 # Configuration loaded
[seap@node1 redis-5.0.8]$ src/redis-sentinel ./my_sentinel.cnf
14362:X 16 Mar 2020 07:46:57.769 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14362:X 16 Mar 2020 07:46:57.769 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=14362, just started
14362:X 16 Mar 2020 07:46:57.769 # Configuration loaded
node2节点
[seap@node2 redis-5.0.8]$ src/redis-server ./redis.conf
9816:C 16 Mar 2020 15:48:35.137 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9816:C 16 Mar 2020 15:48:35.137 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=9816, just started
9816:C 16 Mar 2020 15:48:35.137 # Configuration loaded
[seap@node2 redis-5.0.8]$ src/redis-sentinel ./my_sentinel.cnf
9821:X 16 Mar 2020 15:48:41.202 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9821:X 16 Mar 2020 15:48:41.202 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=9821, just started
9821:X 16 Mar 2020 15:48:41.202 # Configuration loaded
node3节点
[seap@node3 redis-5.0.8]$ src/redis-server ./redis.conf
14082:C 16 Mar 2020 15:48:51.493 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14082:C 16 Mar 2020 15:48:51.493 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=14082, just started
14082:C 16 Mar 2020 15:48:51.493 # Configuration loaded
[seap@node3 redis-5.0.8]$ src/redis-sentinel ./my_sentinel.cnf
14088:X 16 Mar 2020 15:48:57.564 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14088:X 16 Mar 2020 15:48:57.564 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=14088, just started
14088:X 16 Mar 2020 15:48:57.564 # Configuration loaded
主从复制测试
1、使用如下命令寻找主节点(红色粗体部分):
[seap@node2 redis-5.0.8]$ src/redis-cli -p 5000
127.0.0.1:5000> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "192.168.194.134"
5) "port"
6) "6379"
7) "runid"
8) "9e88235203e9c86bfd7b590ed6e0c94cae347914"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "273"
19) "last-ping-reply"
20) "273"
21) "down-after-milliseconds"
22) "5000"
23) "info-refresh"
24) "4413"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "104784"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "60000"
39) "parallel-syncs"
40) "1"
127.0.0.1:5000>
由此可知目前主节点为192.168.194.134 端口号为 6379 也就是本文档示例中的node1节点
2、在主节点上执行如下命令新增测试数据(红色粗体部分):
[seap@node1 redis-5.0.8]$ src/redis-cli -p 6379
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> keys *
1) "foo"
3、在从节点上查看数据是否同步成功(红色粗体部分):
[seap@node3 redis-5.0.8]$ src/redis-cli
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> keys *
1) "foo"
故障转移测试
1、执行如下命令使主节点睡眠30秒测试主节点宕机(红色粗体部分):
[seap@node1 redis-5.0.8]$ src/redis-cli -p 6379 DEBUG sleep 30
OK
2、执行命令重新查看故障转移后主节点(红色粗体部分):
[seap@node1 redis-5.0.8]$ src/redis-cli -p 5000
127.0.0.1:5000> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "192.168.194.135"
5) "port"
6) "6379"
7) "runid"
8) "a6e5e7fcceedeb0014ba37381b8629a39ee4a2e2"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "860"
19) "last-ping-reply"
20) "860"
21) "down-after-milliseconds"
22) "5000"
23) "info-refresh"
24) "552"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "110949"
29) "config-epoch"
30) "1"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "60000"
39) "parallel-syncs"
40) "1"
127.0.0.1:5000>
到此为止故障转移成功,主节点由node1转移到node2(192.168.194.135)上。