02.Redis主从集群的Sentinel配置

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:

阅读目录

回到顶部

1.集群环境

1.Linux服务器列表

使用4台CentOS Linux服务器搭建环境,其IP地址如下:

192.168.110.100192.168.110.101192.168.110.102192.168.110.103

2.Redis服务部署环境

192.168.110.100

   启动多个Redis sentinel服务,构成Redis sentinel集群

192.168.110.101

   启动Redis服务,设置成主节点

192.168.110.102

   启动Redis服务,设置成192.168.110.101的从节点

192.168.110.103

   启动Redis服务,设置成192.168.110.101的从节点


回到顶部

2.配置并启动Redis主从集群

1.修改redis.conf配置文件

主节点的redis配置文件使用默认的配置文件就可以了,从节点的redis配置文件修改如下:

# MasterSlave 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.110.101 6379

注意:两台从节点都要改。

2.启动Redis主从集群

先启动192.168.110.101主节点,使用默认配置,脚本:

[lizhiwei@localhost bin]$ ./redis-server

再启动192.168.110.102和192.168.110.103从节点,使用刚才的配置,脚本:

./redis-server redis.conf

3.查看集群

192.168.110.101主节点Replication信息

[lizhiwei@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication# Replicationrole:masterconnected_slaves:2slave0:ip=192.168.110.102,port=6379,state=online,offset=659,lag=1slave1:ip=192.168.110.103,port=6379,state=online,offset=659,lag=0master_repl_offset:659repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:658

192.168.110.102从节点Replication信息

[lizhiwei@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication # Replicationrole:slavemaster_host:192.168.110.101master_port:6379master_link_status:upmaster_last_io_seconds_ago:3master_sync_in_progress:0slave_repl_offset:701slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0

192.168.110.103从节点Replication信息

[lizhiwei@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication # Replicationrole:slavemaster_host:192.168.110.101master_port:6379master_link_status:upmaster_last_io_seconds_ago:9master_sync_in_progress:0slave_repl_offset:715slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0

此时,存储到192.168.110.101主节点的数据,在从节点中都可以查询到。从节点会备份主节点的数据。

回到顶部

3.配置sentinel集群并启动

1.创建sentinel.conf配置文件

port 26379# sentinel announce-ip <ip># sentinel announce-port <port>dir /tmp################################# master001 #################################sentinel monitor master001 192.168.110.101 6379 2# sentinel auth-pass <master-name> <password>sentinel down-after-milliseconds master001 30000sentinel parallel-syncs master001 1sentinel failover-timeout master001 180000# sentinel notification-script <master-name> <script-path># sentinel client-reconfig-script <master-name> <script-path># 可以配置多个master节点################################# master002 #################################

配置文件说明:

1. port :当前Sentinel服务运行的端口


2. dir : Sentinel服务运行时使用的临时文件夹


3.sentinel monitor master001 192.168.110.101 6379 2:Sentinel去监视一个名为master001的主redis实例,这个主实例的IP地址为本机地址192.168.110.101,端口号为6379,而将这个主实例判断为失效至少需要2个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行


4.sentinel down-after-milliseconds master001 30000:指定了Sentinel认为Redis实例已经失效所需的毫秒数。当实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。只有一个 Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线,这时自动故障迁移才会执行


5.sentinel parallel-syncs master001 1指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长

6.sentinel failover-timeout master001 180000如果在该时间(ms)内未能完成failover操作,则认为该failover失败

7.sentinel notification-script <master-name> <script-path>指定sentinel检测到该监控的redis实例指向的实例异常时,调用的报警脚本。该配置项可选,但是很常用

2.启动sentinel集群

创建3个sentinel.conf配置文件:sentinel001.conf、sentinel002.conf、sentinel003.conf并修改端口号分别为:263793637946379,并启动服务:

./redis-sentinel sentinel001.conf./redis-sentinel sentinel002.conf./redis-sentinel sentinel003.conf

启动三个sentinel服务后会在其控制台看到如下信息:

./redis-sentinel sentinel001.conf,端口:26379

[7743] 01 Oct 06:20:38.162 # Sentinel runid is ba6c42e1accc31290e11d5876275e1562564295d[7743] 01 Oct 06:20:38.162 # +monitor master master001 192.168.110.101 6379 quorum 2[7743] 01 Oct 06:20:39.110 * +slave slave 192.168.110.102:6379 192.168.110.102 6379 @ master001 192.168.110.101 6379[7743] 01 Oct 06:20:39.111 * +slave slave 192.168.110.103:6379 192.168.110.103 6379 @ master001 192.168.110.101 6379[7743] 01 Oct 06:25:07.595 * +sentinel sentinel 192.168.110.100:36379 192.168.110.100 36379 @ master001 192.168.110.101 6379[7743] 01 Oct 06:26:11.170 * +sentinel sentinel 192.168.110.100:46379 192.168.110.100 46379 @ master001 192.168.110.101 6379

./redis-sentinel sentinel002.conf,端口:36379

[7795] 01 Oct 06:25:05.538 # Sentinel runid is 52c14768b15837fb601b26328acf150c6bd30682[7795] 01 Oct 06:25:05.538 # +monitor master master001 192.168.110.101 6379 quorum 2[7795] 01 Oct 06:25:06.505 * +slave slave 192.168.110.102:6379 192.168.110.102 6379 @ master001 192.168.110.101 6379[7795] 01 Oct 06:25:06.515 * +slave slave 192.168.110.103:6379 192.168.110.103 6379 @ master001 192.168.110.101 6379[7795] 01 Oct 06:25:07.557 * +sentinel sentinel 192.168.110.100:26379 192.168.110.100 26379 @ master001 192.168.110.101 6379[7795] 01 Oct 06:26:11.168 * +sentinel sentinel 192.168.110.100:46379 192.168.110.100 46379 @ master001 192.168.110.101 6379

./redis-sentinel sentinel003.conf,端口:46379

[7828] 01 Oct 06:26:09.076 # Sentinel runid is c8509594be4a36660b2122b3b81f4f74060c9b04[7828] 01 Oct 06:26:09.076 # +monitor master master001 192.168.110.101 6379 quorum 2[7828] 01 Oct 06:26:10.063 * +slave slave 192.168.110.102:6379 192.168.110.102 6379 @ master001 192.168.110.101 6379[7828] 01 Oct 06:26:10.071 * +slave slave 192.168.110.103:6379 192.168.110.103 6379 @ master001 192.168.110.101 6379[7828] 01 Oct 06:26:11.516 * +sentinel sentinel 192.168.110.100:26379 192.168.110.100 26379 @ master001 192.168.110.101 6379[7828] 01 Oct 06:26:11.674 * +sentinel sentinel 192.168.110.100:36379 192.168.110.100 36379 @ master001 192.168.110.101 6379

每个sentinel服务能知道其他所有的服务!

回到顶部

4.测试sentinel集群

1.停止192.168.110.101主节点

停止192.168.110.101Redis主节点后,在查看Replication信息如下:

[lizhiwei@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication          Could not connect to Redis at 192.168.110.101:6379: Connection refused[lizhiwei@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication # Replicationrole:slavemaster_host:192.168.110.103master_port:6379master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:29128slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0[lizhiwei@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication # Replicationrole:masterconnected_slaves:1slave0:ip=192.168.110.102,port=6379,state=online,offset=30456,lag=1master_repl_offset:30456repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:30455[lizhiwei@localhost bin]$

发现192.168.110.101Redis主节点已经不能连接,192.168.110.103成了主节点!

2.再启动192.168.110.101主节点

再启动192.168.110.101Redis主节点后,在查看Replication信息如下:


  1. ### 启动脚本,仍然使用默认配置

  2. [lizhiwei@localhost bin]./redis-server

  3. [lizhiwei@localhost bin]./redis-cli -192.168.110.101 info Replication

  4. # Replication

  5. role:slave

  6. master_host:192.168.110.103

  7. master_port:6379

  8. master_link_status:up

  9. master_last_io_seconds_ago:1

  10. master_sync_in_progress:0

  11. slave_repl_offset:57657

  12. slave_priority:100

  13. slave_read_only:1

  14. connected_slaves:0

  15. master_repl_offset:0

  16. repl_backlog_active:0

  17. repl_backlog_size:1048576

  18. repl_backlog_first_byte_offset:0

  19. repl_backlog_histlen:0


  20. [lizhiwei@localhost bin]./redis-cli -192.168.110.102 info Replication

  21. # Replication

  22. role:slave

  23. master_host:192.168.110.103

  24. master_port:6379

  25. master_link_status:up

  26. master_last_io_seconds_ago:0

  27. master_sync_in_progress:0

  28. slave_repl_offset:60751

  29. slave_priority:100

  30. slave_read_only:1

  31. connected_slaves:0

  32. master_repl_offset:0

  33. repl_backlog_active:0

  34. repl_backlog_size:1048576

  35. repl_backlog_first_byte_offset:0

  36. repl_backlog_histlen:0


  37. [lizhiwei@localhost bin]./redis-cli -192.168.110.103 info Replication

  38. # Replication

  39. role:master

  40. connected_slaves:2

  41. slave0:ip=192.168.110.102,port=6379,state=online,offset=63247,lag=1

  42. slave1:ip=192.168.110.101,port=6379,state=online,offset=63247,lag=1

  43. master_repl_offset:63393

  44. repl_backlog_active:1

  45. repl_backlog_size:1048576

  46. repl_backlog_first_byte_offset:2

  47. repl_backlog_histlen:63392

  48. [lizhiwei@localhost bin]$

发现192.168.110.101节点启动后还再集群中,只不过成了从节点,192.168.110.103仍然是主节点,但是现在又有两个从节点了!

3.只留下一个sentinel服务,再停止192.168.110.103主节点,查看Redis集群是否出现新的主节点

停止sentinel服务,只留下一个sentinel服务,再停止Redis主节点,查看Replication信息如下:

[lizhiwei@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication # Replicationrole:slavemaster_host:192.168.110.103master_port:6379master_link_status:downmaster_last_io_seconds_ago:-1master_sync_in_progress:0slave_repl_offset:184231master_link_down_since_seconds:43slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0[lizhiwei@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication # Replicationrole:slavemaster_host:192.168.110.103master_port:6379master_link_status:downmaster_last_io_seconds_ago:-1master_sync_in_progress:0slave_repl_offset:184231master_link_down_since_seconds:52slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0[lizhiwei@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication Could not connect to Redis at 192.168.110.103:6379: Connection refused

发现192.168.110.103主节点已经不能连接了,也不存在Redis主节点,集群中无主节点了!!!分析原因是:sentinel.conf配置的sentinel monitor master001 192.168.110.101 6379 2最后一个参数是2导致,若是但节点此配置的最后一个参数要使用是1。(此原因我已证实)

注意:在生产环境下建议sentinel节点的数量能在3个以上,并且最好不要在同一台机器上(使用同一网卡)。

-------------------------------------------------------------------------------------------------------------------------------







     本文转自yzy121403725 51CTO博客,原文链接:http://blog.51cto.com/lookingdream/1812864,如需转载请自行联系原作者

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
13天前
|
NoSQL Linux Redis
06- 你们使用Redis是单点还是集群 ? 哪种集群 ?
**Redis配置:** 使用哨兵集群,结构为1主2从,加上3个哨兵节点,总计分布在3台Linux服务器上,提供高可用性。
33 0
|
21天前
|
负载均衡 监控 NoSQL
Redis的集群方案有哪些?
Redis集群包括主从复制(基础,手动故障恢复)、哨兵模式(自动高可用)和Redis Cluster(官方分布式解决方案,自动分片和容错)。此外,还有如Codis、Redisson和Twemproxy等第三方工具用于代理和负载均衡。选择方案需考虑应用场景、数据规模和并发需求。
31 2
|
1月前
|
NoSQL Java Redis
Springboot从2.x升级到3.x以后redis默认配置调整
Springboot从2.x升级到3.x以后redis默认配置调整
46 0
|
27天前
|
NoSQL Redis
Redis集群(六):集群常用命令及说明
Redis集群(六):集群常用命令及说明
27 0
|
1月前
|
NoSQL Linux Redis
Linux系统中安装redis+redis后台启动+常见相关配置
Linux系统中安装redis+redis后台启动+常见相关配置
|
21天前
|
NoSQL Java 测试技术
面试官:如何搭建Redis集群?
**Redis Cluster** 是从 Redis 3.0 开始引入的集群解决方案,它分散数据以减少对单个主节点的依赖,提升读写性能。16384 个槽位分配给节点,客户端通过槽位信息直接路由请求。集群是无代理、去中心化的,多数命令直接由节点处理,保持高性能。通过 `create-cluster` 工具快速搭建集群,但适用于测试环境。在生产环境,需手动配置文件,启动节点,然后使用 `redis-cli --cluster create` 分配槽位和从节点。集群动态添加删除节点、数据重新分片及故障转移涉及复杂操作,包括主从切换和槽位迁移。
31 0
面试官:如何搭建Redis集群?
|
25天前
|
存储 缓存 NoSQL
【Redis深度专题】「核心技术提升」探究Redis服务启动的过程机制的技术原理和流程分析的指南(集群功能分析)(一)
【Redis深度专题】「核心技术提升」探究Redis服务启动的过程机制的技术原理和流程分析的指南(集群功能分析)
61 0
|
1月前
|
NoSQL 关系型数据库 MySQL
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
206 0
|
1月前
|
NoSQL Redis Docker
使用Docker搭建一个“一主两从”的 Redis 集群(超详细步骤)
使用Docker搭建一个“一主两从”的 Redis 集群(超详细步骤)
61 0
|
1月前
|
存储 监控 NoSQL
Redis 架构深入:主从复制、哨兵到集群
大家好,我是小康,今天我们来聊下 Redis 的几种架构模式,包括主从复制、哨兵和集群模式。
Redis 架构深入:主从复制、哨兵到集群

热门文章

最新文章