搭建 Redis 集群
version: '3.7'services:master:image: rediscontainer_name: redis-masterports:- 6379:6379slave1:image: rediscontainer_name: redis-slave-1ports:- 6380:6379command: redis-server --slaveof redis-master 6379slave2:image: rediscontainer_name: redis-slave-2ports:- 6381:6379command: redis-server --slaveof redis-master 6379
搭建 Sentinel 集群
version: '3.7'services:sentinel1:image: rediscontainer_name: redis-sentinel-1ports:- 26379:26379command: redis-sentinel /usr/local/etc/redis/sentinel.confvolumes:- ./sentinel1.conf:/usr/local/etc/redis/sentinel.confsentinel2:image: rediscontainer_name: redis-sentinel-2ports:- 26380:26379command: redis-sentinel /usr/local/etc/redis/sentinel.confvolumes:- ./sentinel2.conf:/usr/local/etc/redis/sentinel.confsentinel3:image: rediscontainer_name: redis-sentinel-3ports:- 26381:26379command: redis-sentinel /usr/local/etc/redis/sentinel.confvolumes:- ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
修改 Sentinel 配置文件
需要三份 sentinel.conf 配置文件,分别为 sentinel1.conf,sentinel2.conf,sentinel3.conf,配置文件内容相同
port 26379dir /tmp# 自定义集群名,其中 127.0.0.1 为 redis-master 的 ip,6379 为 redis-master 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 2)sentinel monitor mymaster 127.0.0.1 6379 2sentinel down-after-milliseconds mymaster 30000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 180000sentinel deny-scripts-reconfig yes
查看集群是否生效
docker exec -it redis-sentinel-1 /bin/bashredis-cli -p 26379sentinel master mymastersentinel slaves mymaster
