以redis 7.0为例子
直接上代码
docker-compose.yaml
version: '3.3'
services:
master:
image: redis:7.0
container_name: redis-master
#restart: always
command: redis-server --port 6379 --requirepass root --appendonly yes --masterauth root --replica-announce-ip 192.168.2.168 --replica-announce-port 6379
volumes:
- ./data/master:/data
ports:
- 6379:6379
slave1:
image: redis:7.0
container_name: redis-slave-1
#restart: always
command: 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 6380
volumes:
- ./data/slave1:/data
ports:
- 6380:6379
slave2:
image: redis:7.0
container_name: redis-slave-2
#restart: always
command: 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 6381
volumes:
- ./data/slave1:/data
ports:
- 6381:6379
sentinel1:
image: redis:7.0
container_name: redis-sentinel-1
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
#restart: always
ports:
- 26379:26379
volumes:
- ./config/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
sentinel2:
image: redis:7.0
container_name: redis-sentinel-2
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
#restart: always
ports:
- 26380:26379
volumes:
- ./config/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
sentinel3:
image: redis:7.0
container_name: redis-sentinel-3
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
#restart: always
ports:
- 26381:26379
volumes:
- ./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
校验集群