docker-compose搭建redis集群之哨兵模式

一、环境准备

本文基于上一篇主从模式文章docker-compose搭建redis集群之主从复制

  1. 已有docker、docker-compose环境
  2. 虚拟机
服务器端口角色
192.168.2.26379master
192.168.2.226379sentinel-master
192.168.2.46379slave1
192.168.2.426379sentinel-slave1
192.168.2.56379slave2
192.168.2.526379sentinel-slave2

二、哨兵模式搭建

  1. sentinel.conf
# 哨兵sentinel实例运行的端口 默认26379  
port 26379
# 哨兵sentinel的工作目录
dir /tmp
# 自定义集群名,其中 192.168.2.2 为 redis-master 的 ip,6379 为 redis-master 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 2)
sentinel monitor mymaster 192.168.2.2 6379 2
# 指定多少毫秒之后 主节点没有应答哨兵sentinel 此时 哨兵主观上认为主节点下线 默认30秒  
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster 123456
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
  1. 192.168.2.2 sentinel-master主节点
version: '3.7'
services:
  sentinel1:
    image: redis
    container_name: redis-sentinel-1
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    restart: always
    ports:
      - 26379:26379
    volumes:
      - /home/redis-sentinel/sentinel.conf:/usr/local/etc/redis/sentinel.conf
  
  1. 192.168.2.4 sentinel-slave1从节点
version: '3.7'
services:
    sentinel2:
        image: redis
        container_name: redis-sentinel-2
        command: redis-sentinel /usr/local/etc/redis/sentinel.conf
        restart: always
        ports:
          - 26379:26379
        volumes:
          - /home/redis-sentinel/sentinel.conf:/usr/local/etc/redis/sentinel.conf
  1. 192.168.2.5 sentinel-slave2从节点
version: '3.7'
services:
    sentinel3:
        image: redis
        container_name: redis-sentinel-2
        command: redis-sentinel /usr/local/etc/redis/sentinel.conf
        restart: always
        ports:
          - 26379:26379
        volumes:
          - /home/redis-sentinel/sentinel.conf:/usr/local/etc/redis/sentinel.conf

在各自主从服务器中docker-compose up -d 执行

三、测试验证

  1. 进入redis-sentinel容器中,查看redis连接状态
# 查看redis-sentinel是否启动成功
docker ps 
# 进入redis-sentinel容器中,查看redis连接状态
docker exec -it 容器id bash
redis-cli -p 26379
# 执行如下命令,查看redis主信息
sentinel master mymaster

显示带红框的信息则表明成功
在这里插入图片描述

 # 执行如下命令,查看从redis信息是否正常
sentinel slaves mymaster

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值