以redis 7.0为例子
直接上代码
docker-compose.yaml
version: '3.3'
services:master:image: redis:7.0container_name: redis-master#restart: alwayscommand: redis-server --port 6379 --requirepass root --appendonly yes --masterauth root --replica-announce-ip 192.168.2.168 --replica-announce-port 6379volumes:- ./data/master:/dataports:- 6379:6379slave1: image: redis:7.0container_name: redis-slave-1#restart: alwayscommand: redis-server --slaveof 192.168.2.168 6379 --port 6379 --requirepass root --masterauth root --appendonly yes --replica-announce-ip 192.168.2.168 --replica-announce-port 6380volumes:- ./data/slave1:/dataports:- 6380:6379slave2:image: redis:7.0container_name: redis-slave-2#restart: alwayscommand: redis-server --slaveof 192.168.2.168 6379 --port 6379 --requirepass root --masterauth root --appendonly yes --replica-announce-ip 192.168.2.168 --replica-announce-port 6381volumes:- ./data/slave1:/dataports:- 6381:6379sentinel1:image: redis:7.0container_name: redis-sentinel-1command: redis-sentinel /usr/local/etc/redis/sentinel.conf#restart: alwaysports:- 26379:26379volumes:- ./config/sentinel1.conf:/usr/local/etc/redis/sentinel.confsentinel2:image: redis:7.0container_name: redis-sentinel-2command: redis-sentinel /usr/local/etc/redis/sentinel.conf#restart: alwaysports:- 26380:26379volumes:- ./config/sentinel2.conf:/usr/local/etc/redis/sentinel.confsentinel3:image: redis:7.0container_name: redis-sentinel-3command: redis-sentinel /usr/local/etc/redis/sentinel.conf#restart: alwaysports:- 26381:26379volumes:- ./config/sentinel3.conf:/usr/local/etc/redis/sentinel.conf
ip | 端口号 | 说明 |
192.168.2.168 | 6379 | 主 |
192.168.2.168 | 6380 | 从1 |
192.168.2.168 | 6381 | 从2 |
192.168.2.168 | 26379 | 哨兵1 |
192.168.2.168 | 26380 | 哨兵2 |
192.168.2.168 | 26381 | 哨兵3 |
sentinel1.conf
#sentinel1.conf
port 26379
dir /tmp
sentinel monitor mymaster 192.168.2.168 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster root
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel announce-ip 192.168.2.168
sentinel announce-port 26379
sentinel2.conf
#sentinel2.conf
port 26380
dir /tmp
sentinel monitor mymaster 192.168.2.168 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster root
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel announce-ip 192.168.2.168
sentinel announce-port 26380
sentinel3.conf
#sentinel3.conf
port 26381
dir /tmp
sentinel monitor mymaster 192.168.2.168 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster root
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel announce-ip 192.168.2.168
sentinel announce-port 26381
启动docker
docker-compose up -d
校验集群