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
- 启动集群
在三台主机上分别执行以下命令启动 Redis 和 Sentinel 容器:
docker compose up -d
- 检查 Sentinel 状态
启动成功后,可以通过以下命令查看 Sentinel 的状态:
docker exec redis-sentinel redis-cli -p 26379 -c info sentinel
标签:mymaster,etc,Redis,6379,redis,sentinel,conf,Sentinel,Docker
From: https://blog.csdn.net/2401_84279412/article/details/143533507