Redis集群的哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。 哨兵模式作用:
通过发送命令,让Redis服务器返回监控其运行状态,包括主服务器和从服务器。
当哨兵监测到master宕机,会自动将slave切换成master,然后通过发布订阅模式通知其他的从服务器,修改配置文件,让它们切换主机。
除了监控Redis服务之外,哨兵之间也会互相监控。本文采用一主、双从、三哨兵方式
第一步:创建redis docker-compose.yml配置文件
version: '3.4'
services:
master: #自定义服务名
image: redis
restart: always
container_name: redis-master
# network_mode: "host" # 网络模式 默认是 bridge 桥接模式
command: redis-server --port 16380 --requirepass 123456 --protected-mode no --daemonize no #容器启动后,运行的命令
ports:
- 16380:16380 #宿主机的16380映射容器内部的16380端口
#volumes: #目录挂载 。分号 : 右边代表容器内部的目录,分号左边表示宿主机的目录,
#- /app/redis-master/redis.conf:/redis.conf
slave1:
image: redis
restart: always
container_name: redis-slave-1
# network_mode: "host"
command: redis-server --slaveof 123.249.21.189 16380 --port 16381 --requirepass 123456 --masterauth 123456 --protected-mode no --daemonize no
ports:
- 16381:16381
slave2:
image: redis
restart: always
container_name: redis-slave-2
# network_mode: "host"
command: redis-server --slaveof 123.249.21.189 16380 --port 16382 --requirepass 123456 --masterauth 123456 --protected-mode no --daemonize no
ports:
- 16382:16382
第二步:执行启动命令 在当前目录下执行启动命令
docker-compose -f docker-compose.yml up -d

第三步:创建sentinel docker-compose.yml配置文件目录
version: '3.4'
services:
sentinel1:
image: redis
restart: always
container_name: redis-sentinel-1
# network_mode: "host"
ports:
- 26380:26380
command: redis-sentinel /app/sentinel/sentinel1.conf # 自定义路径,可更改但是必须和volumes中的路径一致
volumes:
- ./sentinel1.conf:/app/sentinel/sentinel1.conf
sentinel2:
image: redis
restart: always
container_name: redis-sentinel-2
# network_mode: "host"
ports:
- 26381:26381
command: redis-sentinel /app/sentinel/sentinel2.conf
volumes:
- ./sentinel2.conf:/app/sentinel/sentinel2.conf
sentinel3:
image: redis
restart: always
container_name: redis-sentinel-3
# network_mode: "host"
ports:
- 26380:26380
command: redis-sentinel /app/sentinel/sentinel3.conf
volumes:
- ./sentinel3.conf:/app/sentinel/sentinel3.conf
sentinel1.conf
# 哨兵sentinel实例运行的端口 默认26379
port 26380
daemonize no
pidfile /var/run/redis-sentinel1.pid
dir /tmp
sentinel monitor mymaster 123.249.21.189 16380 2
sentinel auth-pass mymaster 123456
# 指定多少毫秒之后 主节点没有应答哨兵sentinel 此时 哨兵主观上认为主节点下线 默认30秒
sentinel down-after-milliseconds mymaster 30000
# 指定了在发生failover主备切换时最多可以有多少个slave同时对新的master进行同步,这个数字越小,完成failover所需的时间就越长
sentinel parallel-syncs mymaster 1
# 故障转移的超时时间
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel2.conf
port 26381
daemonize no
pidfile /var/run/redis-sentinel2.pid
dir /tmp
sentinel monitor mymaster 123.249.21.189 16380 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel3.conf
port 26382
daemonize no
pidfile /var/run/redis-sentinel3.pid
dir /tmp
sentinel monitor mymaster 123.249.21.189 16380 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
第四步:执行启动命令 在当前目录下执行启动命令
docker-compose -f docker-compose.yml up -d

至此我们集群便搭建完毕了让我们来查看下
docker ps

搭建成功