1、哨兵经典架构
1.1sentinel结构
2、集群环境
2.1Linux服务器列表
IP | HOSTNAME |
---|---|
192.168.48.13 | node3.xzsyr.com |
192.168.48.14 | node4.xzsyr.com |
192.168.48.15 | node5.xzsyr.com |
2.2Redis服务部署环境
启动多个Redis sentinel服务,构成Redis sentinel集群
IP | HOSTNAME | Sentinel |
---|---|---|
192.168.48.13 | node3.xzsyr.com | √ |
192.168.48.14 | node4.xzsyr.com | √ |
192.168.48.15 | node5.xzsyr.com | √ |
启动多个Redis服务节点,构成redis一主多从集群
配置如下所示:
IP | HOSTNAME | MASTER | SLAVE | 备注 |
---|---|---|---|---|
192.168.48.13 | node3.xzsyr.com | √ | 启动Redis服务,设置成主节点 | |
192.168.48.14 | node4.xzsyr.com | √ | 启动Redis服务,设置成192.168.48.13的从节点 | |
192.168.48.15 | node5.xzsyr.com | √ | 启动Redis服务,设置成192.168.48.13的从节点 |
2.3配置并启动Redis主从集群
1.修改redis.conf配置文件
主节点的redis配置文件使用默认的配置文件就可以了,从节点的redis配置文件修改如下:
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 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 slaves.
# 2) Redis slaves 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 slaves automatically try to reconnect to masters
# and resynchronize with them.
#
# 主从同步。通过 slaveof 配置来实现Redis实例的备份。
# 注意,这里是本地从远端复制数据。也就是说,本地可以有不同的数据库文件、绑定不同的IP、监听不同的端口。
#
# slaveof <masterip> <masterport>
slaveof 192.168.48.13 6379
注意:两台从节点都要改。
2、启动Redis主从集群
先启动192.168.48.13主节点,使用默认配置,脚本:
[root@node3 src]# ps aux|grep redis
root 3011 0.1 0.2 142976 2196 ? Ssl 18:01 0:00 ./redis-server 192.168.48.13:6379
root 3018 0.0 0.0 112704 976 pts/0 S+ 18:01 0:00 grep --color=auto redis
再启动192.168.110.102和192.168.110.103从节点,使用刚才的配置,脚本:
[root@node4 src]# ./redis-server ../redis.conf
4556:C 11 Jun 18:00:16.694 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4556:C 11 Jun 18:00:16.694 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=4556, just started
4556:C 11 Jun 18:00:16.694 # Configuration loaded
[root@node5 src]# ./redis-server ../redis.conf
4168:C 11 Jun 18:00:31.438 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4168:C 11 Jun 18:00:31.439 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=4168, just started
4168:C 11 Jun 18:00:31.439 # Configuration loaded
3、查看集群
192.168.48.13主节点Replication信息
[root@node3 src]# ./redis-cli -h 192.168.48.13 -p 6379 info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.48.14,port=6379,state=online,offset=154,lag=1
slave1:ip=192.168.48.15,port=6379,state=online,offset=154,lag=1
master_replid:b464227f0b12b0347ca5247b5f711e6daf636bf5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:154
192.168.48.14从节点Replication信息
[root@node4 src]# ./redis-cli -h 192.168.48.14 -p 6379 info Replication
# Replication
role:slave
master_host:192.168.48.13
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:252
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b464227f0b12b0347ca5247b5f711e6daf636bf5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:252
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:252
You have new mail in /var/spool/mail/root
192.168.48.15从节点Replication信息
[root@node5 src]# ./redis-cli -h 192.168.48.15 -p 6379 info Replication
# Replication
role:slave
master_host:192.168.48.13
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:336
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b464227f0b12b0347ca5247b5f711e6daf636bf5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:336
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:336
此时,存储到192.168.48.13主节点的数据,在从节点中都可以查询到。从节点会备份主节点的数据。
如下所示,从节点获取主节点保存的key:
[root@node4 src]# ./redis-cli -h 192.168.48.14 -p 6379
192.168.48.14:6379> keys *
1) "ko"
192.168.48.14:6379>
[root@node5 src]# ./redis-cli -h 192.168.48.15 -p 6379
192.168.48.15:6379> keys *
1) "ko"
192.168.48.15:6379>
2.4配置sentinel集群并启动
在所有redis节点下执行操作
1.编辑sentinel.conf配置文件
port 26379
# sentinel announce-ip <ip>
# sentinel announce-port <port>
dir /tmp
#sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster 192.168.48.13 6379 2
# sentinel auth-pass <master-name> <password>
sentinel down-after-milliseconds master001 30000
sentinel parallel-syncs master001 1
sentinel failover-timeout master001 180000
# sentinel notification-script <master-name> <script-path>
# sentinel client-reconfig-script <master-name> <script-path>
# 可以配置多个master节点
daemonize yes #配置后台进程启动
配置文件说明:
1. port :当前Sentinel服务运行的端口
- dir : Sentinel服务运行时使用的临时文件夹
3.sentinel monitor mymaster 192.168.48.13 6379 2:Sentinel去监视一个名为mymaster的主redis实例,这个主实例的IP地址为本机地址192.168.48.13,端口号为6379,而将这个主实例判断为失效至少需要2个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行
4.sentinel down-after-milliseconds mymaster 30000:指定了Sentinel认为Redis实例已经失效所需的毫秒数。当实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。只有一个 Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线,这时自动故障迁移才会执行
5.sentinel parallel-syncs mymaster 1:指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长
6.sentinel failover-timeout mymaster 180000:如果在该时间(ms)内未能完成failover操作,则认为该failover失败
7.sentinel notification-script :指定sentinel检测到该监控的redis实例指向的实例异常时,调用的报警脚本。该配置项可选,但是很常用
8.sentinel实例daemonize yes 配置后台进程启动
2、启动sentinel集群
3个sentinel.conf配置文件:sentinel.conf并修改端口号分别为:26379、26379、26379并启动服务:
192.168.48.13主节点下执行:
/opt/module/redis-4.0.10/src
[root@node3 src]# ./redis-sentinel ../sentinel.conf --sentinel
3051:X 11 Jun 18:20:50.140 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3051:X 11 Jun 18:20:50.140 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=3051, just started
3051:X 11 Jun 18:20:50.140 # Configuration loaded
192.168.48.14从节点下执行:
/opt/module/redis-4.0.10/src
[root@node4 src]# ./redis-sentinel ../sentinel.conf --sentinel
4606:X 11 Jun 18:21:34.862 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4606:X 11 Jun 18:21:34.862 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=4606, just started
4606:X 11 Jun 18:21:34.862 # Configuration loaded
192.168.48.15从节点下执行:
/opt/module/redis-4.0.10/src
[root@node5 src]# ./redis-sentinel ../sentinel.conf --sentinel
4224:X 11 Jun 18:22:14.376 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4224:X 11 Jun 18:22:14.376 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=4224, just started
4224:X 11 Jun 18:22:14.376 # Configuration loaded
3、查看sentinel集群状态
[root@node3 src]# redis-cli -h 192.168.48.13 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.48.13:6379,slaves=2,sentinels=3
2.5测试sentinel集群
[root@node3 src]# ps aux|grep redis
root 3011 0.2 0.2 143120 2352 ? Ssl 18:01 0:04 ./redis-server 192.168.48.13:6379
root 3052 0.4 0.2 141932 2316 ? Ssl 18:20 0:03 ./redis-sentinel *:26379 [sentinel]
root 3072 0.0 0.0 112704 972 pts/0 S+ 18:32 0:00 grep --color=auto redis
You have new mail in /var/spool/mail/root
1.停止192.168.48.13主节点
停止192.168.48.13主节点后,在查看Replication信息。
2.再启动192.168.48.13主节点
再启动192.168.48.13主节点后,在查看Replication信息。
都是完全可以的。