Redis主从复制
首先创建两个目录用于存放配置文件
- redis
- sentinel
在redis文件下创建docker-compose.yml文件
version: '3.1'
services:
master:
image: redis
container_name: redis-master
ports:
- 6379:6379
networks:
- custom_net
slave1:
image: redis
container_name: redis-slave-1
ports:
- 6380:6379
command: redis-server --slaveof redis-master 6379
networks:
- custom_net
slave2:
image: redis
container_name: redis-slave-2
ports:
- 6381:6379
command: redis-server --slaveof redis-master 6379
networks:
- custom_net
networks:
custom_net:
external:
name: app_net
创建网络组
docker network create app_net
启动docker-compose,这时redis主从复制就配置好了
docker-compose up -d
哨兵模式
首先在sentinel目录下创建docker-compose.yml文件
version: '3.1'
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
ports:
- 26379:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf
networks:
- custom_net
sentinel2:
image: redis
container_name: redis-sentinel-2
ports:
- 26380:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf
networks:
- custom_net
sentinel3:
image: redis
container_name: redis-sentinel-3
ports:
- 26381:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
- custom_net
networks:
custom_net:
external:
name: app_net
在sentinel目录下创建三个哨兵的配置文件:
sentinel1.conf,sentinel2.conf,sentinel3.conf
port 26379
dir /tmp
# 自定义集群名,其中 127.0.0.1 为 redis-master 的 ip,6379 为 redis-master 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 2)
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
启动docker-compose
docker-compose up -d
查看哨兵日志
docker logs redis-sentinel-1
哨兵1监控到一主二从二哨兵