Docker-compose多服务器搭建redis集群实现一主二从三哨兵
1 服务器准备
服务器准备 | 开放端口 |
---|---|
192.168.10.10 | 6379、6380、26379 |
192.168.10.11 | 6381、26380、26381 |
备注:
主节点:6379、
从节点:6380、6381
哨兵结点:26379、26380、26381
两台服务器都创建相同的目录结构
[root@ecs-aaa1-0710870 redis]# pwd
/opt/redis
[root@ecs-aaa1-0710870 redis]# ls
config data docker-compose.yaml
# 服务器1号
[root@ecs-aaa1-0710870 redis]# cd data/
[root@ecs-aaa1-0710870 data]# ls
master sentinel1 slave1
[root@ecs-aaa1-0710870 redis]# cd config/
[root@ecs-aaa1-0710870 config]# ls
redis.conf sentinel1.conf slave1.conf
# 服务器2号
[root@ecs-b913-0803965 redis]# cd data/
[root@ecs-b913-0803965 data]# ls
sentinel2 sentinel3 slave2
[root@ecs-b913-0803965 redis]# cd config/
[root@ecs-b913-0803965 config]# ls
sentinel2.conf sentinel3.conf slave2.conf
结构如下:
服务器1号
[root@ecs-aaa1-0710870 redis]# tree
.
├── config
│ ├── redis.conf
│ ├── sentinel1.conf
│ └── slave1.conf
├── data
│ ├── master
│ ├── sentinel1
│ └── slave1
└── docker-compose.yaml
服务器2号:
[root@ecs-b913-0803965 redis]# tree
.
├── config
│ ├── sentinel2.conf
│ ├── sentinel3.conf
│ └── slave2.conf
├── data
│ ├── sentinel2
│ ├── sentinel3
│ └── slave2
└── docker-compose.yaml
2.文件准备
2.1 一号服务器配置
2.1.1 docker-compose.yaml
# docker-compose.yaml
version: "3.1"
services:
# 主节点
redis-master:
image: redis
container_name: redis-master
restart: always
environment:
TZ: Asia/Shanghai
ports:
- 6379:6379
command: /usr/local/bin/redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./config/redis.conf:/usr/local/etc/redis/redis.conf:rw
- ./data/master:/data
networks:
- redis-network
# 从节点
redis-slave1:
image: redis
container_name: redis-slave1
restart: always
environment:
TZ: Asia/Shanghai
command: /usr/local/bin/redis-server /usr/local/etc/redis/redis.conf
ports:
- 6380:6380
volumes:
- ./config/slave1.conf:/usr/local/etc/redis/redis.conf:rw
- ./data/slave1:/data
networks:
- redis-network
# 1号哨兵
redis-sentinel1:
image: redis
container_name: redis-sentinel1
restart: always
environment:
TZ: Asia/Shanghai
command: /usr/local/bin/redis-sentinel /usr/local/etc/redis/sentinel.conf
ports:
- 26379:26379
volumes:
- ./config/sentinel1.conf:/usr/local/etc/redis/sentinel.conf:rw
- ./data/sentinel1:/data
networks:
- redis-network
networks:
redis-network:
2.1.2 redis.conf
文件编写–主节点
redis.conf开启6379端口作为容器内部端口
# redis.conf
bind 0.0.0.0
port 6379
protected-mode no
dir /data
appendonly yes
requirepass RedisMiMa
masterauth RedisMiMa
slave-read-only no
2.1.3 slave1.conf
文件编写从节点1号
slave1.conf开启6390端口
# slave1.conf
bind 0.0.0.0
port 6380
# 关闭密码保护
protected-mode no
dir /data
appendonly yes
# 主结点地址以及端口号
replicaof 192.168.10.10 6379
requirepass RedisMiMa
masterauth RedisMiMa
slave-read-only no
2.1.4 sentinel1.conf
文件编写–哨兵1号
# sentinel1.conf
bind 0.0.0.0
protected-mode no
port 26379
dir /data
sentinel monitor mymaster 192.168.10.10 6379 2
sentinel known-slave mymaster 192.168.10.10 6380
sentinel known-slave mymaster 192.168.10.11 6381
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1
2.2 二号服务器配置
2.2.1 docker-compose.yaml
# docker-compose.yaml
version: "3.1"
services:
redis-slave2:
image: redis
container_name: redis-slave2
restart: always
environment:
TZ: Asia/Shanghai
command: /usr/local/bin/redis-server /usr/local/etc/redis/redis.conf
ports:
- 6381:6381
volumes:
- ./config/slave2.conf:/usr/local/etc/redis/redis.conf:rw
- ./data/slave2:/data
networks:
- redis-network
redis-sentinel2:
image: redis
container_name: redis-sentinel2
restart: always
environment:
TZ: Asia/Shanghai
command: /usr/local/bin/redis-sentinel /usr/local/etc/redis/sentinel.conf
ports:
- 26380:26379
volumes:
- ./config/sentinel2.conf:/usr/local/etc/redis/sentinel.conf:rw
- ./data/sentinel2:/data
networks:
- redis-network
redis-sentinel3:
image: redis
container_name: redis-sentinel3
restart: always
environment:
TZ: Asia/Shanghai
command: /usr/local/bin/redis-sentinel /usr/local/etc/redis/sentinel.conf
ports:
- 26381:26379
volumes:
- ./config/sentinel3.conf:/usr/local/etc/redis/sentinel.conf:rw
- ./data/sentinel3:/data
networks:
- redis-network
networks:
redis-network:
2.2.2 slave2.conf
bind 0.0.0.0
port 6381
protected-mode no
dir /data
appendonly yes
replicaof 192.168.10.10 6379
requirepass RedisMiMa
masterauth RedisMiMa
slave-read-only no
2.2.3 sentinel2.conf
bind 0.0.0.0
port 26380
protected-mode no
dir /data
sentinel monitor mymaster 192.168.10.10 6379 2
sentinel auth-pass mymaster RedisMiMa
sentinel known-slave mymaster 192.168.10.10 6380
sentinel known-slave mymaster 192.168.10.11 6381
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1
2.2.3 sentinel3.conf
bind 0.0.0.0
port 26381
protected-mode no
dir /data
sentinel monitor mymaster 192.168.10.10 6379 2
sentinel auth-pass mymaster RedisMiMa
sentinel known-slave mymaster 192.168.10.10 6380
sentinel known-slave mymaster 192.168.10.11 6381
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1
3.启动Reids
在每个服务器的/opt/redis位置进行启动
docker-compose -f docker-compose.yaml up -d
至此配置部署完成!