下面以 CentOS 7/8 为例,说明 Redis Sentinel(哨兵) 的部署与配置步骤。假设你要做 1 主 2 从 + 3 哨兵 的高可用架构。
| 角色 | IP | 端口 |
|---|---|---|
| Redis 主 | 192.168.1.10 | 6379 |
| Redis 从1 | 192.168.1.11 | 6379 |
| Redis 从2 | 192.168.1.12 | 6379 |
| Sentinel1 | 192.168.1.10 | 26379 |
| Sentinel2 | 192.168.1.11 | 26379 |
| Sentinel3 | 192.168.1.12 | 26379 |
yum install -y epel-release
yum install -y redis
启动并设置开机自启:
systemctl enable redis
systemctl start redis
默认即可,确保:
bind 0.0.0.0
protected-mode no
编辑 /etc/redis.conf:
replicaof 192.168.1.10 6379
重启:
systemctl restart redis
redis-cli -h 192.168.1.10 info replication
vim /etc/redis-sentinel.conf
基础配置(三台一致,仅 IP 不同):
port 26379
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile /var/log/redis/sentinel.log
sentinel monitor mymaster 192.168.1.10 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1
说明:
mymaster:主节点名称(自定义)2:至少 2 个哨兵认为主挂了才切换down-after-milliseconds:5 秒无响应认为宕机systemctl enable redis-sentinel
systemctl start redis-sentinel
redis-cli -p 26379
> sentinel masters
> sentinel slaves mymaster
> sentinel get-master-addr-by-name mymaster
systemctl stop redis
tail -f /var/log/redis/sentinel.log
客户端应连接 哨兵端口,而非直接连 Redis:
sentinel1:26379
sentinel2:26379
sentinel3:26379
如果你需要:
可以直接告诉我。