文章目录
环境
- docker desktop for windows 4.23.0
- redis 7.2
一、前提准备
1. 主从集群
首先需要有一个redis主从集群,才能接着做redis哨兵。具体可以参考下面这篇文章
Docker-Compose部署Redis(v7.2)主从模式(之后简称"主从模式博文
")
2. 文件夹结构
和主从模式不同的是,redis sentinel
(哨兵)会更改你的conf
文件,无论是redis server
节点还是sentinel
节点本身,都可能被修改,所以这里需要注意文件权限问题。不然会一直警告Sentinel was not able to save the new configuration on disk
。
有兴趣可以参考以下几个帖子,或者接着本文做就行了。
- WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
- Redis sentinel 6.0.9 in docker: Could not create tmp config file (Permission denied) #8172
- WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy #287
总的来说,需要对主从模式博文
里提到的文件夹结构做一定改善和添加,具体如下:
cluster/
├── docker-compose.yaml
├── master
│ └── conf
│ └── redis.conf
├── replica1
│ └── conf
│ └── redis.conf
├── replica2
│ └── conf
│ └── redis.conf
├── sentinel1
│ └── conf
│ └── sentinel.conf
├── sentinel2
│ └── conf
│ └── sentinel.conf
└── sentinel3
└── conf
└── sentinel.conf
其中redis.conf
和docker-compose.yaml
和主从模式博文
内容暂时保持一致,其余的都是新增的,暂时保持空白即可。
二、配置文件
1. redis server配置文件
保持不变
2. redis sentinel配置文件
对于上述三个sentinel.conf
内容都填入以下
sentinel monitor mymaster 172.30.1.2 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster 1009
dir "/data"
意思分别是
- 监控的主节点:通过
sentinel monitor
指定要监控的主节点。这包括一个用户定义的名称(如mymaster
)、主节点的地址、端口号和一个“仲裁”阈值,后者表示要进行故障转移所需的最小Sentinel
投票数量。 - 故障检测:设置
Sentinel
判断主节点是否下线所需的时间 - 故障转移设置:配置故障转移的行为,如故障转移的超时时间
- 认证密码(如果主节点设置了密码):如果主节点设置了密码,
Sentinel
需要这个密码来连接主节点和副本节点 - 设置
Sentinel
的工作目录
3. docker compose文件
version: '3.8'
networks:
redis-network:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.30.1.0/24
services:
redis-master:
container_name: redis-master