通过使用docker-compose搭建一主二从,三哨兵的高可用Redis测试环境。
最终效果是:
Redis主节点地址:<最终访问IP地址>:6379
Redis从节点地址:<最终访问IP地址>:6380、<最终访问IP地址>:6381
哨兵节点地址:<最终访问IP地址>:56180、<最终访问IP地址>:56280、<最终访问IP地址>:56380
需要说明的是:
- 主备节点之间的通讯可以使用docker内部IP地址通讯,但是需要通过
--replica-announce-ip
暴露最终访问IP地址,通过--replica-announce-port
暴露最终端口号; - 哨兵节点一定要通过最终访问IP地址连接主节点,一定要通过
sentinel announce-ip
暴露最终访问IP地址,通过sentinel announce-port
暴露最终端口号。
目录结构:
root@VM-4-14-ubuntu:/srv/docker/redis# tree
.
├── config
│ ├── sentinel-1.conf
│ ├── sentinel-2.conf
│ └── sentinel-3.conf
└── docker-compose.yml
docker-compose.yml文件:
version: "3.8"
services:
master:
image: redis:7.2.4
container_name: redis-master
command: redis-server --requirepass <password> --masterauth <password> --replica-announce-ip <最终访问IP地址> --replica-announce-port 6379
restart: unless-stopped
ports:
- "6379:6379"
slave1:
image: redis:7.2.4
container_name: redis-slave-1
command: redis-server --replicaof <最终访问IP地址> 6379 --requirepass <password> --masterauth <password> --replica-announce-ip <最终访问IP地址> --replica-announce-port 6380
restart: unless-stopped
ports:
- "6380:6379"
slave2:
image: redis:7.2.4
container_name: redis-slave-2
command: redis-server --replicaof <最终访问IP地址> 6379 --requirepass <password> --masterauth <password> --replica-announce-ip <最终访问IP地址> --replica-announce-port 6381
restart: unless-stopped
ports:
- "6381:6379"
sentinel1:
image: redis:7.2.4
container_name: redis-sentinel-1
command: redis-sentinel /etc/redis/sentinel-1.conf
volumes:
- ./config:/etc/redis
restart: unless-stopped
ports:
- "56180:26379"
sentinel2:
image: redis:7.2.4
container_name: redis-sentinel-2
command: redis-sentinel /etc/redis/sentinel-2.conf
volumes:
- ./config:/etc/redis
restart: unless-stopped
ports:
- "56280:26379"
sentinel3:
image: redis:7.2.4
container_name: redis-sentinel-3
command: redis-sentinel /etc/redis/sentinel-3.conf
volumes:
- ./config:/etc/redis
restart: unless-stopped
ports:
- "56380:26379"
config/sentinel-1.conf
文件:
port 26379
sentinel monitor mymaster <最终访问IP地址> 6379 2
sentinel auth-pass mymaster <password>
sentinel announce-ip <最终访问IP地址>
sentinel announce-port 56180
config/sentinel-2.conf
文件:
port 26379
sentinel monitor mymaster <最终访问IP地址> 6379 2
sentinel auth-pass mymaster <password>
sentinel announce-ip <最终访问IP地址>
sentinel announce-port 56280
config/sentinel-3.conf
文件:
port 26379
sentinel monitor mymaster <最终访问IP地址> 6379 2
sentinel auth-pass mymaster <password>
sentinel announce-ip <最终访问IP地址>
sentinel announce-port 56380