基于docker搭建redis-sentinel集群的方法示例

2025-05-27 0 67

1、概述

redis 集群可以在一组 redis 节点之间实现高可用性和 sharding。在集群中会有 1 个 master 和多个 slave 节点。当 master 节点失效时,应选举出一个 slave 节点作为新的 master。然而 redis 本身(包括它的很多客户端)没有实现自动故障发现并进行主备切换的能力,需要外部的监控方案来实现自动故障恢复。

redis sentinel 是官方推荐的高可用性解决方案。它是 redis 集群的监控管理工具,可以提供节点监控、通知、自动故障恢复和客户端配置发现服务。

2、遇到的问题

1、docker host网络

docker使用host网络时对于windows 、mac不生效(没找到解决方案),最后放弃了windows 使用centos部署集群

2、不使用host网络的情况下sentinel 连接问题

不使用host网络的情况下连接sentinel集群时可以指定主节点端口故可以正常联通, 但在主节点故障时 sentinel 从主节点获取到的 ip 是容器内的虚拟 ip 导致集群无法正常连接。

基于docker搭建redis-sentinel集群的方法示例

3、搭建过程

1、目录结构

基于docker搭建redis-sentinel集群的方法示例

基于docker搭建redis-sentinel集群的方法示例

2、sentinel 配置文件

1、sentinel1.conf

?

1

2

3

4

5

6

7

8

9

10

11
#端口号

port 26379

dir /tmp

# mymaster:自定义集群名,2:投票数量必须2个sentinel才能判断主节点是否失败

sentinel monitor mymaster <ip> <port> 2

# 指的是超过5000秒,且没有回复,则判定主节点不可达

sentinel down-after-milliseconds mymaster 5000

# 表示在故障转移的时候最多有numslaves在同步更新新的master

sentinel parallel-syncs mymaster 1

# 故障转移超时时间

sentinel failover-timeout mymaster 5000

2、sentinel2.conf

?

1

2

3

4

5

6

7

8

9

10

11
#端口号

port 26380

dir /tmp

# mymaster:自定义集群名,2:投票数量必须2个sentinel才能判断主节点是否失败

sentinel monitor mymaster <ip> <port> 2

# 指的是超过5000秒,且没有回复,则判定主节点不可达

sentinel down-after-milliseconds mymaster 5000

# 表示在故障转移的时候最多有numslaves在同步更新新的master

sentinel parallel-syncs mymaster 1

# 故障转移超时时间

sentinel failover-timeout mymaster 5000

3、sentinel3.conf

?

1

2

3

4

5

6

7

8

9

10

11
#端口号

port 26381

dir /tmp

# mymaster:自定义集群名,2:投票数量必须2个sentinel才能判断主节点是否失败

sentinel monitor mymaster <ip> <port> 2

# 指的是超过5000秒,且没有回复,则判定主节点不可达

sentinel down-after-milliseconds mymaster 5000

# 表示在故障转移的时候最多有numslaves在同步更新新的master

sentinel parallel-syncs mymaster 1

# 故障转移超时时间

sentinel failover-timeout mymaster 5000

3、docker-compose.yml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53
version: '2'

services:

master:

image: redis:4.0

restart: always

container_name: redis-master

#使用主机网络

network_mode: "host"

command: redis-server --port 16379

slave1:

image: redis:4.0

restart: always

container_name: redis-slave-1

network_mode: "host"

# 指定端口并指定master ip 端口

command: redis-server --port 16380 --slaveof <master ip> 16379

slave2:

image: redis:4.0

restart: always

container_name: redis-slave-2

network_mode: "host"

command: redis-server --port 16381 --slaveof <master ip> 16379

sentinel1:

image: redis:4.0

restart: always

container_name: redis-sentinel-1

network_mode: "host"

# 指定sentinel文件位置

command: redis-sentinel /usr/local/etc/redis/sentinel.conf

# 使用数据卷映射文件到指定sentinel位置

volumes:

- ./sentinel/sentinel1.conf:/usr/local/etc/redis/sentinel.conf

sentinel2:

image: redis:4.0

restart: always

container_name: redis-sentinel-2

network_mode: "host"

command: redis-sentinel /usr/local/etc/redis/sentinel.conf

volumes:

- ./sentinel/sentinel2.conf:/usr/local/etc/redis/sentinel.conf

sentinel3:

image: redis:4.0

restart: always

container_name: redis-sentinel-3

network_mode: "host"

command: redis-sentinel /usr/local/etc/redis/sentinel.conf

volumes:

- ./sentinel/sentinel3.conf:/usr/local/etc/redis/sentinel.conf

4、使用centos 部署集群测试效果

1、测试通过sentinel1连接集群

基于docker搭建redis-sentinel集群的方法示例

2、测试主节点子节点数据同步

基于docker搭建redis-sentinel集群的方法示例

基于docker搭建redis-sentinel集群的方法示例

3、关闭master查看主备切换

基于docker搭建redis-sentinel集群的方法示例

sentinel 正常联通

基于docker搭建redis-sentinel集群的方法示例

主节点从16379 切换 至16381

基于docker搭建redis-sentinel集群的方法示例

结尾

端午之后偷了一周的懒,之前就搭建了一次sentinel 集群由于docker 网络模型问题导致主备节点切换后集群连接不上,昨天看到host不能在window上实现就放到centos上测试了一番完美搞定。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://juejin.im/post/5d07ac98e51d4577583ddccc

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 行业资讯 基于docker搭建redis-sentinel集群的方法示例 https://www.kuaiidc.com/66372.html

相关文章

发表评论
暂无评论