目标架构
- 机器 A:Redis 主节点 + Sentinel
- 机器 B:Redis 从节点 + Sentinel
- 机器 C:Redis 从节点 + Sentinel
+-------------------+ +-------------------+ +-------------------+
| Sentinel 1 | | Sentinel 2 | | Sentinel 3 |
| (Monitoring) |<---->| (Monitoring) |<---->| (Monitoring) |
+-------------------+ +-------------------+ +-------------------+
| | |
v v v
+-------------------+ +-------------------+ +-------------------+
| Redis Master |<---->| Redis Slave 1 |<---->| Redis Slave 2 |
| (Port: 6379) | | (Port: 6380) | | (Port: 6381) |
+-------------------+ +-------------------+ +-------------------+
准备工作
- 确保每台机器都可以通过内网 IP 地址互相访问。
- 在每台机器上安装 Docker 和 Docker Compose。
假设机器的内网 IP 地址分别为:
- 机器 A:
192.168.1.10 - 机器 B:
192.168.1.11 - 机器 C:
192.168.1.12
第一步:创建项目目录并准备配置文件
在每台机器上创建相同的项目目录结构,例如 /opt/redis-sentinel,并在该目录下创建以下文件:
1. docker-compose.yml
这个文件将定义 Redis 和 Sentinel 服务。请注意,每台机器上的 docker-compose.yml 文件内容会有所不同,特别是在指定 replicaof 和 Sentinel 配置时。(目的是主从发现机制)
机器 A (192.168.1.10) 的 docker-a-compose.yml
version: '3.8'
services:
redis-master:
image: redis:7.0
container_name: redis-master
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
volumes:
- ./redis-master.conf:/usr/local/etc/redis/redis.conf
- redis-data-master:/data
networks:
- redis-net
ports:
- "6379:6379"
sentinel:
image: redis:7.0
container_name: sentinel-1
command: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]
volumes:
- ./sentinel-1.conf:/usr/local/etc/redis/sentinel.conf
networks:
- redis-net
ports:
- "26379:26379"
depends_on:
- redis-master
volumes:
redis-data-master:
networks:
redis-net:
driver: bridge
机器 B (192.168.1.11) 的 docker-b-compose.yml
version: '3.8'
services:
redis-slave:
image: redis:7.0
container_name: redis-slave-1
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
volumes:
- ./redis-slave-1.conf:/usr/local/etc/redis/redis.conf
- redis-data-slave-1:/data
networks:
- redis-net
ports:
- "6380:6379"
sentinel:
image: redis:7.0
container_name: sentinel-2
command: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]
volumes:
- ./sentinel-2.conf:/usr/local/etc/redis/sentinel.conf
networks:
- redis-net
ports:
- "26379:26379"
depends_on:
- redis-slave
volumes:
redis-data-slave-1:
networks:
redis-net:
driver: bridge
机器 C (192.168.1.12) 的 docker-c-compose.yml
version: '3.8'
services:
redis-slave:
image: redis:7.0
container_name: redis-slave-2
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
volumes:
- ./redis-slave-2.conf:/usr/local/etc/redis/redis.conf
- redis-data-slave-2:/data
networks:
- redis-net
ports:
- "6381:6379"
sentinel:
image: redis:7.0
container_name: sentinel-3
command: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]
volumes:
- ./sentinel-3.conf:/usr/local/etc/redis/sentinel.conf
networks:
- redis-net
ports:
- "26379:26379"
depends_on:
- redis-slave
volumes:
redis-data-slave-2:
networks:
redis-net:
driver: bridge
2. Redis 配置文件
机器 A 的 redis-master.conf
# redis-master.conf
port 6379
bind 0.0.0.0
protected-mode yes
appendonly yes
requirepass RedisPass123! # 设置访问密码
masterauth RedisPass123! # 如果有从节点连接到此主节点时使用的认证密码
机器 B 的 redis-slave-1.conf
配置了replicaof表示是从服务
# redis-slave-1.conf
port 6379
bind 0.0.0.0
protected-mode yes
appendonly yes
requirepass RedisPass123! # 设置访问密码
masterauth RedisPass123! # 连接到主节点时使用的认证密码
replicaof 192.168.1.10 6379 # 指定主节点的 IP 地址和端口号
机器 C 的 redis-slave-2.conf
port 6379
bind 0.0.0.0
protected-mode yes
appendonly yes
requirepass RedisPass123!
masterauth RedisPass123!
replicaof 192.168.1.10 6379
3. Sentinel 配置文件sentinel-1.conf 机器A\B\C通用
在 Redis Sentinel 中,心跳 命令的发送频率是固定的,每秒发送一次,这个行为是硬编码在 Sentinel 的实现中,并不能通过配置文件直接修改,这里的sentinel down-after-milliseconds mymaster 5000 表示5次请求redis主节点没回复就认为是宕机了,不可达。
# sentinel-1.conf, sentinel-2.conf, sentinel-3.conf
port 26379
dir /tmp
sentinel monitor mymaster 192.168.1.10 6379 2 # 监控主节点,至少需要2个Sentinel同意才能触发故障转移
sentinel auth-pass mymaster RedisPass123! # 设置与主节点通信时使用的密码
sentinel down-after-milliseconds mymaster 5000 # 多久后认为主节点不可达,默认5秒
sentinel failover-timeout mymaster 60000 # 故障转移超时时间,默认60秒
sentinel parallel-syncs mymaster 1 # 在一次故障转移过程中同时同步数据的从节点数
logfile "/dev/stdout" # 日志输出到标准输出
第二步:启动服务
在每台机器上进入项目目录 /opt/redis-sentinel 并运行以下命令启动服务:】
机器 A
# 确保docker-compose.yml存在 要不然直接运行docker-compose up 会报错的找到配置文件
cd /opt/redis-sentinel
docker-compose up -d
机器 B
cd /opt/redis-sentinel
docker-compose up -d
机器 C
cd /opt/redis-sentinel
docker-compose up -d

1549

被折叠的 条评论
为什么被折叠?



