Redis哨兵搭建教程
# Redis哨兵搭建教程
## 前言
Redis哨兵(Sentinel)是Redis官方提供的高可用解决方案,能够自动监控、通知和故障转移。本文将详细介绍如何在Linux服务器上搭建Redis哨兵集群。
## 环境准备
### 服务器规划
本文使用3台服务器搭建Redis哨兵集群:
| 服务器IP | Redis端口 | 哨兵端口 | 角色 |
|---|---|---|---|
| 192.168.1.10 | 6379 | 26379 | Master + Sentinel |
| 192.168.1.11 | 6379 | 26379 | Slave + Sentinel |
| 192.168.1.12 | 6379 | 26379 | Slave + Sentinel |
### 系统要求
- 操作系统:CentOS 7+ 或 Ubuntu 18.04+
- Redis版本:5.0+(建议6.0+)
- 内存:至少2GB RAM
- 网络:服务器之间网络互通
## 安装Redis
### CentOS安装
yum install -y epel-release
yum install -y redis
systemctl start redis
systemctl enable redis
### Ubuntu安装
apt-get update
apt-get install -y redis-server
systemctl start redis-server
systemctl enable redis-server
## 配置主从复制
### Master配置
编辑Master节点的配置文件 /etc/redis/redis.conf:
bind 0.0.0.0
port 6379
daemonize yes
requirepass "your_password"
masterauth "your_password"
启动Master节点:
systemctl restart redis
### Slave配置
编辑Slave节点的配置文件 /etc/redis/redis.conf:
bind 0.0.0.0
port 6379
daemonize yes
replicaof 192.168.1.10 6379
masterauth "your_password"
replica-read-only yes
启动Slave节点:
systemctl restart redis
### 验证主从复制
在Master节点上执行:
redis-cli -h 192.168.1.10 -p 6379 -a "your_password" info replication
在Slave节点上执行:
redis-cli -h 192.168.1.11 -p 6379 -a "your_password" info replication
## 配置哨兵
### 哨兵配置文件
创建哨兵配置文件 /etc/redis/sentinel.conf:
port 26379
daemonize yes
logfile /var/log/redis/sentinel.log
dir /var/lib/redis
# 监控主节点,quorum为2表示2个哨兵认为主节点下线才进行故障转移
sentinel monitor mymaster 192.168.1.10 6379 2
# 主节点密码
sentinel auth-pass mymaster your_password
# 30秒无响应认为下线
sentinel down-after-milliseconds mymaster 30000
# 故障转移超时时间180秒
sentinel failover-timeout mymaster 180000
# 故障转移时同时同步的从节点数量
sentinel parallel-syncs mymaster 1
### 启动哨兵
在三台服务器上分别启动哨兵:
redis-sentinel /etc/redis/sentinel.conf
### 验证哨兵状态
查看哨兵状态:
redis-cli -p 26379 info sentinel
查看监控的主节点:
redis-cli -p 26379 sentinel masters
查看从节点信息:
redis-cli -p 26379 sentinel slaves mymaster
## 故障转移测试
### 停止Master节点
redis-cli -h 192.168.1.10 -p 6379 -a "your_password" shutdown nosave
### 观察故障转移
查看哨兵日志:
tail -f /var/log/redis/sentinel.log
哨兵会自动选择一个Slave升级为Master,整个过程自动完成。
### 恢复原Master
重启原Master节点:
systemctl start redis
原Master会自动成为新Master的从节点。
## 客户端连接
### Python客户端
from redis.sentinel import Sentinel
sentinel = Sentinel([
('192.168.1.10', 26379),
('192.168.1.11', 26379),
('192.168.1.12', 26379)
])
# 获取主节点连接
master = sentinel.master_for('mymaster', password='your_password')
master.set('key', 'value')
# 获取从节点连接
slave = sentinel.slave_for('mymaster', password='your_password')
value = slave.get('key')
### Java客户端
Set sentinels = new HashSet<>();
sentinels.add("192.168.1.10:26379");
sentinels.add("192.168.1.11:26379");
sentinels.add("192.168.1.12:26379");
JedisSentinelPool pool = new JedisSentinelPool(
"mymaster", sentinels, "your_password"
);
Jedis jedis = pool.getResource();
jedis.set("key", "value");
String value = jedis.get("key");
### Go客户端
rdb := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "mymaster",
SentinelAddrs: []string{"192.168.1.10:26379", "192.168.1.11:26379", "192.168.1.12:26379"},
Password: "your_password",
})
rdb.Set(ctx, "key", "value", 0)
val, _ := rdb.Get(ctx, "key").Result()
## 监控维护
### 查看哨兵监控信息
# 查看主节点信息
redis-cli -p 26379 sentinel masters
# 查看从节点信息
redis-cli -p 26379 sentinel slaves mymaster
# 查看其他哨兵信息
redis-cli -p 26379 sentinel sentinels mymaster
### 查看Redis性能
redis-cli -h 192.168.1.10 -p 6379 -a "your_password" info stats
redis-cli -h 192.168.1.10 -p 6379 -a "your_password" info memory
## 常见问题
### 哨兵无法启动
# 检查配置文件
redis-sentinel /etc/redis/sentinel.conf --test-memory 1
# 检查端口
netstat -tlnp | grep 26379
### 故障转移失败
# 检查从节点状态
redis-cli -h 192.168.1.11 -p 6379 info replication
# 检查网络连通性
ping 192.168.1.10
### 主从同步延迟
在redis.conf中优化:
repl-backlog-size 10mb
repl-backlog-ttl 3600
## 最佳实践
1. 哨兵数量至少3个,建议5个(奇数)
2. 哨兵应该部署在不同的物理服务器上
3. 为所有Redis实例设置强密码
4. 定期备份RDB和AOF文件
5. 建立完善的监控告警体系
6. 生产环境建议启用SSL加密
7. 及时更新Redis版本
## 总结
Redis哨兵是成熟的高可用解决方案,能够有效保障Redis服务的稳定性。通过本文的介绍,你可以成功搭建Redis哨兵集群,实现自动故障转移。
记住:在生产环境中,务必做好充分的测试和监控!






