Redis 源码 tar 包安装 Redis 哨兵模式(Sentinel)

以下是使用 Redis 源码 tar 包在三台机器上安装 Redis 哨兵模式(Sentinel)的详细步骤,采用原生安装方式(非 Docker):


环境准备

  • 三台 CentOS/Ubuntu 服务器 (假设 IP 如下):
    • node1: 192.168.1.10 (主节点)
    • node2: 192.168.1.11 (从节点)
    • node3: 192.168.1.12 (从节点)
  • Redis 版本:7.0.12(以最新稳定版为例)
  • 端口规划
    • Redis: 6379
    • Sentinel: 26379

1. 在所有节点安装 Redis

(1)安装依赖
bash 复制代码
# CentOS
yum install -y gcc make tcl

# Ubuntu
apt-get update && apt-get install -y build-essential tcl
(2)下载并编译 Redis
bash 复制代码
wget https://download.redis.io/releases/redis-7.0.12.tar.gz
tar xzf redis-7.0.12.tar.gz
cd redis-7.0.12
make && make install
(3)创建配置文件目录
bash 复制代码
mkdir -p /etc/redis /var/lib/redis /var/log/redis

2. 配置主节点(node1)

(1)编辑 Redis 配置文件
bash 复制代码
vim /etc/redis/redis.conf

关键配置

ini 复制代码
bind 0.0.0.0
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
dir /var/lib/redis
appendonly yes
requirepass yourpassword  # 设置密码(可选)
masterauth yourpassword   # 如果设置了密码,从节点需要此项
(2)启动 Redis
bash 复制代码
redis-server /etc/redis/redis.conf

3. 配置从节点(node2 和 node3)

(1)编辑 Redis 配置文件
bash 复制代码
vim /etc/redis/redis.conf

额外添加

ini 复制代码
replicaof 192.168.1.10 6379  # 指向主节点
masterauth yourpassword      # 如果主节点有密码
(2)启动 Redis
bash 复制代码
redis-server /etc/redis/redis.conf

4. 配置 Sentinel(所有节点)

(1)编辑 Sentinel 配置文件
bash 复制代码
vim /etc/redis/sentinel.conf

内容如下

ini 复制代码
port 26379
daemonize yes
sentinel monitor mymaster 192.168.1.10 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel auth-pass mymaster yourpassword  # 如果主节点有密码
(2)启动 Sentinel
bash 复制代码
redis-sentinel /etc/redis/sentinel.conf

5. 验证集群

(1)检查主从复制
bash 复制代码
# 在主节点执行
redis-cli info replication

输出应显示:

复制代码
role:master
connected_slaves:2
slave0:ip=192.168.1.11,port=6379,state=online
slave1:ip=192.168.1.12,port=6379,state=online
(2)测试 Sentinel 故障转移
bash 复制代码
# 手动关闭主节点 Redis
redis-cli -h 192.168.1.10 shutdown

# 查看 Sentinel 选举结果
redis-cli -p 26379 sentinel get-master-addr-by-name mymaster

约 10 秒后应返回新的主节点 IP。


6. 设置开机自启(所有节点)

(1)创建 systemd 服务文件
bash 复制代码
vim /etc/systemd/system/redis.service

内容

ini 复制代码
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target
(2)启动服务
bash 复制代码
systemctl daemon-reload
systemctl enable redis
systemctl start redis

关键配置说明

参数 作用
sentinel monitor mymaster... 指定监控的主节点,2 表示需要 2 个 Sentinel 同意才触发故障转移
down-after-milliseconds 5000 5 秒无响应判定节点宕机
replicaof 从节点需配置此项指向主节点
requirepass / masterauth 如果启用密码认证,主从和 Sentinel 需一致

常见问题解决

  1. 主从无法同步

    • 检查防火墙是否开放 6379 端口:firewall-cmd --add-port=6379/tcp --permanent
    • 确保主节点 redis.confbind 0.0.0.0
  2. Sentinel 不触发故障转移

    • 确认至少有两个 Sentinel 能联通主节点
    • 检查 sentinel.conf 中的主节点 IP 和密码是否正确
  3. 从节点无法提升为主节点

    • 确保 redis.conf 中未设置 replica-read-only no(从节点默认只读)

扩展建议

  • 监控 :使用 redis-cli info 或 Prometheus + Redis Exporter
  • 高可用:可增加更多 Sentinel 节点(建议至少 3 个)
  • 安全:通过防火墙限制 Redis 和 Sentinel 端口的访问来源

此方案适合生产环境,如需更自动化部署,可结合 Ansible 或 Shell 脚本批量操作。

相关推荐
曲幽1 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
倔强的石头_18 小时前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
jiayou642 天前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
李广坤3 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
爱可生开源社区4 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1774 天前
《从零搭建NestJS项目》
数据库·typescript
加号35 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
シ風箏5 天前
MySQL【部署 04】Docker部署 MySQL8.0.32 版本(网盘镜像及启动命令分享)
数据库·mysql·docker
李慕婉学姐5 天前
Springboot智慧社区系统设计与开发6n99s526(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
百锦再5 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip