# Redis哨兵模式一主一备配置操作方法 ## 一、哨兵模式概述 ### 1.1 Redis高可用方案 Redis Sentinel(哨兵)是Redis官方推荐的高可用性解决方案,通过监控、通知和自动故障转移三大核心功能保障服务连续性。当主节点出现故障时,哨兵系统能够自动将从节点提升为主节点,确保服务不间断。 ### 1.2 典型应用场景 - 需要7×24小时稳定运行的业务系统 - 对数据一致性要求较高的场景 - 避免单点故障的生产环境 ## 二、环境准备 ### 2.1 服务器规划 | 角色 | IP地址 | 端口 | 说明 | |------------|-------------|-------|---------------| | Master | 192.168.1.10 | 6379 | 主数据库节点 | | Slave | 192.168.1.11 | 6379 | 备用数据库节点 | | Sentinel1 | 192.168.1.10 | 26379 | 哨兵节点1 | | Sentinel2 | 192.168.1.11 | 26379 | 哨兵节点2 | | Sentinel3 | 192.168.1.12 | 26379 | 哨兵节点3 | ### 2.2 软件版本要求 - Redis ≥ 3.0(推荐5.0+) - Linux操作系统(CentOS/Ubuntu) ## 三、主从配置 ### 3.1 主节点配置 ```bash # redis_6379.conf bind 192.168.1.10 port 6379 daemonize yes pidfile /var/run/redis_6379.pid logfile "/var/log/redis_6379.log" requirepass yourpassword masterauth yourpassword
# redis_6379.conf bind 192.168.1.11 port 6379 daemonize yes pidfile /var/run/redis_6379.pid logfile "/var/log/redis_6379.log" requirepass yourpassword masterauth yourpassword replicaof 192.168.1.10 6379
# 启动服务 redis-server /path/to/redis_6379.conf # 主节点验证 redis-cli -h 192.168.1.10 info replication # 应显示role:master及connected_slaves:1 # 从节点验证 redis-cli -h 192.168.1.11 info replication # 应显示role:slave及master_link_status:up
# sentinel_26379.conf port 26379 daemonize yes logfile "/var/log/redis/sentinel_26379.log" sentinel monitor mymaster 192.168.1.10 6379 2 sentinel auth-pass mymaster yourpassword sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 10000 sentinel parallel-syncs mymaster 1
quorum 2
:至少需要2个哨兵同意才认为主节点不可用down-after-milliseconds
:5000毫秒无响应判定为下线failover-timeout
:故障转移超时时间(毫秒)redis-sentinel /path/to/sentinel_26379.conf
# 在主节点执行 redis-cli -h 192.168.1.10 DEBUG sleep 30
# 查看当前主节点 redis-cli -h 192.168.1.11 info replication # 应显示role:master # 查看哨兵日志 tail -f /var/log/redis/sentinel_26379.log
JedisSentinelPool pool = new JedisSentinelPool( "mymaster", new HashSet<String>(Arrays.asList( "192.168.1.10:26379", "192.168.1.11:26379", "192.168.1.12:26379")), config);
# 增加以下配置 min-replicas-to-write 1 min-replicas-max-lag 10
redis-cli -h <sentinel_ip> -p 26379 sentinel failover mymaster
# 增加哨兵检测频率 sentinel down-after-milliseconds mymaster 3000 # 调整故障转移超时 sentinel failover-timeout mymaster 60000
# 备份哨兵配置 cp /path/to/sentinel.conf /backup/sentinel.conf.$(date +%F) # 备份Redis数据 redis-cli -h 192.168.1.10 --rdb /backup/dump.rdb
文档版本控制
版本 | 日期 | 修改说明 |
---|---|---|
v1.0 | 2023-08-20 | 初始版本 |
v1.1 | 2023-09-15 | 增加故障处理章节 |
”`
注:本文实际约3000字,包含完整的配置示例和操作流程。根据实际环境需要调整IP地址、密码等参数。建议在测试环境验证后再部署到生产环境。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。