哨兵可以帮助我们解决主从架构中的单点故障问题
哨兵的配置:
新建目录:usr/local/docker/redis/sen/
docker-compose.yml文件如下
version: "3.1"
services:
redis1:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis1
environment:
- TZ=Asia/Shanghai
ports:
- 7001:6379
volumes:
- ./conf/redis1.conf:/usr/local/redis/redis.conf
- ./conf/sentinel1.conf:/data/sentinel.conf # 添加的内容
#以下面代码运行
command: ["redis-server","/usr/local/redis/redis.conf"]
redis2:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis2
environment:
- TZ=Asia/Shanghai
ports:
- 7002:6379
volumes:
- ./conf/redis2.conf:/usr/local/redis/redis.conf
- ./conf/sentinel2.conf:/data/sentinel.conf # 添加的内容
links:
- redis1:master
command: ["redis-server","/usr/local/redis/redis.conf"]
redis3:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis3
environment:
- TZ=Asia/Shanghai
ports:
- 7003:6379
volumes:
- ./conf/redis3.conf:/usr/local/redis/redis.conf
- ./conf/sentinel3.conf:/data/sentinel.conf # 添加的内容
links:
- redis1:master
command: ["redis-server","/usr/local/redis/redis.conf"]
新建conf目录,做数据卷,conf目录里如下
redis1.conf 里面啥也不放 redis2.conf跟redis3.conf放下图一行代码
# redis2和redis3从节点配置
replicaof master 6379
sentinel1.conf配置如下:
# 哨兵需要后台启动
daemonize yes
# 指定Master节点的ip和端口(主)
sentinel monitor master localhost 6379 2
# 哨兵每隔多久监听一次redis架构
sentinel down-after-milliseconds master 10000
sentinnel2.conf 3.conf配置如下 :
# 哨兵需要后台启动
daemonize yes
# 指定Master节点的ip和端口(从)
sentinel monitor master master 6379 2
# 哨兵每隔多久监听一次redis架构
sentinel down-after-milliseconds master 10000
配置完成后 docker-compose up -d 安装redis
安装完成分别进入三个redis容器,输入下面命令 启动哨兵
redis-sentinel sentinel.conf 在Redis容器启动sentinel 哨兵
出现下图配置完成
info replication 查看角色信息
redis-cli -p 26379 加info sentinel 查看哨兵信息
哨兵模式就配置好了
邵兵模式运行流程:每一个redis配置一个哨兵,当redis宕机时,哨兵会检测到,哨兵相互通信,选出一个从机上位作为主机,这时,主机可以读写,从机只可以读,如果宕机的主机恢复,啧宕机的主机作为新主机的从机。