在 CentOS 上配置 Redis Sentinel(哨兵),通常用于实现 Redis 高可用(主从自动切换)。下面我用 实战步骤 给你一个完整示例,适合生产环境理解和使用。
假设你有 3 台 CentOS 服务器:
| 角色 | IP | 端口 |
|---|---|---|
| Redis 主节点 | 192.168.1.10 | 6379 |
| Redis 从节点1 | 192.168.1.11 | 6379 |
| Redis 从节点2 | 192.168.1.12 | 6379 |
| Sentinel | 三台都部署 | 26379 |
✅ 哨兵推荐 至少 3 个节点,避免脑裂
yum install -y gcc make
cd /usr/local/src
wget https://download.redis.io/redis-stable.tar.gz
tar -zxvf redis-stable.tar.gz
cd redis-stable
make
make install
安装完成后:
redis-server -v
/etc/redis/redis.conf
bind 0.0.0.0
protected-mode no
port 6379
daemonize yes
pidfile /var/run/redis.pid
logfile /var/log/redis.log
dir /var/lib/redis
启动:
redis-server /etc/redis/redis.conf
redis.conf
bind 0.0.0.0
protected-mode no
port 6379
daemonize yes
dir /var/lib/redis
slaveof 192.168.1.10 6379
启动:
redis-server /etc/redis/redis.conf
验证:
redis-cli
info replication
在 三台服务器 上都配置 /etc/redis/sentinel.conf:
bind 0.0.0.0
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 秒无响应认为宕机 |
| failover-timeout | 故障转移超时 |
| parallel-syncs | 同时同步的从节点数 |
redis-sentinel /etc/redis/sentinel.conf
检查:
redis-cli -p 26379
sentinel masters
sentinel get-master-addr-by-name mymaster
redis-cli -h 192.168.1.10 shutdown
观察:
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --add-port=26379/tcp --permanent
firewall-cmd --reload
✅ 推荐:3 / 5 / 7 个
如果是内网,建议:
protected-mode no
✅ Redis 主从 + Sentinel
✅ Sentinel 至少 3 台
✅ 客户端连接 Sentinel,而不是 Redis
✅ 使用密码(requirepass + sentinel auth-pass)
如果你愿意,我可以:
你现在用的是 CentOS 7 还是 8?Redis 版本是多少?