哨兵模式
工作机制
Redis Sentinel 基于主从复制模式,只是引入了哨兵来监控与自动处理故障。
- 监控master、slave是否正常运行
- 当master出现故障时,能自动将一个slave转换为master
- 多个哨兵可以监控同一个Redis,哨兵之间也会自动监控
优缺点
优点:
- 哨兵模式基于主从复制模式,所以主从复制模式有的优点,哨兵模式也有
- 哨兵模式下,master挂掉可以自动进行切换,系统可用性更高
缺点:
- 同样也继承了主从模式难以在线扩容的缺点,Redis的容量受限于单机配置
- 需要额外的资源来启动sentinel进程,实现相对复杂一点,同时slave节点作为备份节点不提供服务
集群搭建
目录结构如上图
sentinel.conf 内容
port 26379
dir /tmp
sentinel monitor mymaster 172.18.0.2 6379 2
sentinel auth-pass mymaster 12345678
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 172.18.0.2 6379 2
此处ip获取方式
执行命令: docker inspect redis-master
可以找到 “IPAddress”: “172.18.0.2”
拷贝3份文件
cp sentinel.conf sentinel1.conf
cp sentinel.conf sentinel2.conf
cp sentinel.conf sentinel3.conf
docker-compose.yml 内容
version: '3'
services:
sentinel1:
image: redis:6.2.6
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
sentinel2:
image: redis:6.2.6
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
sentinel3:
image: redis:6.2.6
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:
default:
external:
name: majj
执行命令 docker-compose up -d
验证
验证已经完成了, 我们看到哨兵模式已经生效了.