Docker Redis Sentinel 高可用集群搭建指南

Docker Redis Sentinel 高可用集群搭建指南

前提条件:

  • 已安装 Docker 和 docker-compose。
  • 配置了镜像源加速,确保能够正常拉取镜像。

一、手动搭建 Docker Redis Sentinel

1. 拉取 Redis 镜像

docker pull redis

2. Redis 集群 IP 和端口设置(单机测试)
主节点:ip:6379
从节点:ip:6380, ip:6381
Sentinel 节点:ip:26379, ip:26380, ip:26381
二、部署主从 Redis(3台服务器)
1. 文件配置
主节点及 Sentinel 配置
docker-compose.yml:

```yaml
services:
  redis-master:
    image: redis:7.0.13
    restart: always
    container_name: redis-master
    privileged: true
    ports:
      - '6379:6379'
    volumes:
      - redis-data:/opt/bitnami/redis/data
      - /root/redis.conf:/etc/redis.conf
      - /etc/localtime:/etc/localtime:ro
    command:
      - /bin/sh
      - -c
      - redis-server /etc/redis.conf

  redis-sentinel:
    image: redis:7.0.13
    restart: always
    container_name: redis-sentinel
    privileged: true
    ports:
      - '26379:26379'
    volumes:
      - /root/sentinel.conf:/etc/sentinel.conf
      - /etc/localtime:/etc/localtime:ro
    command:
      - /bin/sh
      - -c
      - redis-server /etc/sentinel.conf --sentinel

volumes:
  redis-data:

redis.conf:

daemonize no
port 6379
protected-mode no
bind 0.0.0.0
requirepass 123456

sentinel.conf:

port 26379
protected-mode no
sentinel monitor mymaster 192.168.91.4 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 3000

从节点及 Sentinel 配置
docker-compose.yml:

services:
  redis-slave:
    image: redis:7.0.13
    restart: always
    container_name: redis-slave
    privileged: true
    ports:
      - '6379:6379'
    volumes:
      - redis-data:/opt/bitnami/redis/data
      - /root/redis.conf:/etc/redis.conf
      - /etc/localtime:/etc/localtime:ro
    command:
      - /bin/sh
      - -c
      - redis-server /etc/redis.conf

  redis-sentinel:
    image: redis:7.0.13
    restart: always
    container_name: redis-sentinel
    privileged: true
    ports:
      - '26379:26379'
    volumes:
      - /root/sentinel.conf:/etc/sentinel.conf
      - /etc/localtime:/etc/localtime:ro
    command:
      - /bin/sh
      - -c
      - redis-server /etc/sentinel.conf --sentinel

volumes:
  redis-data:
redis.conf:
daemonize no
port 6379
protected-mode no
masterauth 123456
requirepass 123456
slave-read-only yes
bind 0.0.0.0
slaveof 192.168.91.4 6379

sentinel.conf:

port 26379
protected-mode no
sentinel monitor mymaster 192.168.91.4 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 3000

从节点02及 Sentinel 配置
docker-compose.yml:

services:
  redis-slave02:
    image: redis:7.0.13
    restart: always
    container_name: redis-slave02
    privileged: true
    ports:
      - '6379:6379'
    volumes:
      - redis-data:/opt/bitnami/redis/data
      - /root/redis.conf:/etc/redis.conf
      - /etc/localtime:/etc/localtime:ro
    command:
      - /bin/sh
      - -c
      - redis-server /etc/redis.conf

  redis-sentinel:
    image: redis:7.0.13
    restart: always
    container_name: redis-sentinel
    privileged: true
    ports:
      - '26379:26379'
    volumes:
      - /root/sentinel.conf:/etc/sentinel.conf
      - /etc/localtime:/etc/localtime:ro
    command:
      - /bin/sh
      - -c
      - redis-server /etc/sentinel.conf --sentinel

volumes:
  redis-data:

redis.conf:

daemonize no
port 6379
protected-mode no
masterauth 123456
requirepass 123456
slave-read-only yes
bind 0.0.0.0
slaveof 192.168.91.4 6379

sentinel.conf:

port 26379
protected-mode no
sentinel monitor mymaster 192.168.91.4 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 3000
  1. 启动集群
    在三台主机上分别执行以下命令启动 Redis 和 Sentinel 容器:
docker compose up -d
  1. 检查 Sentinel 状态
    启动成功后,可以通过以下命令查看 Sentinel 的状态:
docker exec redis-sentinel redis-cli -p 26379 -c info sentinel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值