基于 docker 搭建 redis-sentinel 集群

3,708 阅读3分钟

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 导致集群无法正常连接。

3、搭建过程

1、目录结构

2、sentinel 配置文件

1、sentinel1.conf

#端口号
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

#端口号
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

#端口号
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

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连接集群

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

3、关闭master查看主备切换

sentinel 正常联通
主节点从16379 切换 至16381

结尾

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