CentOS环境搭建Redis哨兵模式集群

1.安装yum添加epel源

复制代码
yum install epel-release
  1. 安装Redis

    yum install redis

  2. 目录和配置文件管理

    创建目录

    mkdir -p /etc/redis/{6380,6381,6382}
    mkdir -p /etc/redis-sentinel/{26380,26381,26382}

    复制配置文件,并修改文件名以区分不同实例

    cp /etc/redis.conf /etc/redis/6380/redis.conf
    cp /etc/redis.conf /etc/redis/6381/redis.conf
    cp /etc/redis.conf /etc/redis/6382/redis.conf

    注意:sentinel.conf文件通常不是直接通过cp创建的,而是需要手动编辑

    touch /etc/redis-sentinel/26380/sentinel.conf
    touch /etc/redis-sentinel/26381/sentinel.conf
    touch /etc/redis-sentinel/26382/sentinel.conf

  3. 数据节点配置(redis.conf)
    对于每个Redis实例的配置文件(redis.conf),需要修改以下关键项:

    ... 其他配置项 ...

    指定端口

    port 6380 # 根据实例修改

    绑定所有地址

    bind 0.0.0.0

    PID文件路径

    pidfile /var/run/redis_6380.pid # 根据实例修改

    日志文件路径

    logfile /var/log/redis/redis_6380.log # 根据实例修改

    数据目录路径

    dir /var/lib/redis/6380 # 根据实例修改

    密码配置(可选)

    masterauth your_master_password # 如果设置了主节点密码
    requirepass your_redis_password # Redis实例密码

    ... 其他配置项 ...

注意:在slaveof指令中,不要在配置文件中硬编码主节点的IP地址,因为哨兵会负责故障转移和更新从节点的配置。

5.哨兵配置(sentinel.conf)

对于每个哨兵的配置文件(sentinel.conf),需要修改以下关键项:

复制代码
# ... 其他配置项 ...  
  
# 指定哨兵端口  
port 26380 # 根据哨兵实例修改  
  
# 日志文件路径  
logfile "/var/log/redis/sentinel_26380.log" # 根据哨兵实例修改  
  
# 哨兵监控的主节点配置  
sentinel monitor mymaster <master-ip> 6380 2 # 使用实际的主节点IP地址  
  
# 哨兵判断主节点宕机需要的毫秒数  
sentinel down-after-milliseconds mymaster 3000  
  
# ... 其他配置项 ...

注意:应该替换为实际的主节点IP地址,而不是127.0.0.0。

6.启动配置文件修改

复制代码
# redis-6380.service
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis/6380/redis_6380.conf --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

# redis-sentinel-26380.service  
[Unit]  
Description=Redis Sentinel  
After=network.target  
  
[Service]  
ExecStart=/usr/bin/redis-sentinel /etc/redis-sentinel/26380/sentinel_26380.conf --supervised systemd  
ExecStop=/usr/bin/redis-cli -p 26380 SHUTDOWN  
Type=notify  
User=redis  
Group=redis  
RuntimeDirectory=redis  
RuntimeDirectoryMode=0755  
Restart=on-failure  
  
[Install]  
WantedBy=multi-user.target

现在,您可以按照以下命令来更新systemd、使能服务为开机自启,并启动它们:

复制代码
# 更新启动项  
systemctl daemon-reload  
  
# 设置开机自启(其他配置依次执行)  
systemctl enable redis-6380.service  
systemctl enable redis-6381.service  
systemctl enable redis-6382.service  
systemctl enable redis-sentinel-26380.service  
systemctl enable redis-sentinel-26381.service  
systemctl enable redis-sentinel-26382.service  
  
# 服务启动(先主后从)  
systemctl start redis-6380.service  
systemctl start redis-6381.service  
systemctl start redis-6382.service  
  
# 启动哨兵  
systemctl start redis-sentinel-26380.service  
systemctl start redis-sentinel-26381.service  
systemctl start redis-sentinel-26382.service  
  
# 查看进程  
ps -ef | grep redis  
# 或者查看systemd服务的状态  
systemctl status redis-6380.service  
systemctl status redis-sentinel-26380.service  
# 以此类推,查看其他服务和哨兵的状态

遇到问题:

(1)在/tmp文件夹下创建/tmp/redis-sentinel/26380目录
(2)Could not create server TCP listening socket 0.0.0.0:6380: bind: Permission denied

复制代码
# 停止firewalld服务
systemctl stop firewalld
# 禁用firewalld服务,确保其在系统重新启动后不会自动启动
systemctl disable firewalld

码客笔记,分享更有用的技术知识,助你成为更优秀的开发者!快来关注我们吧!

相关推荐
wingaso25 分钟前
[经验总结]删除gitlab仓库分支报错:错误:无法推送一些引用到“http:”
linux·数据仓库·git
独行soc29 分钟前
2025年渗透测试面试题总结-阿里云[实习]阿里云安全-安全工程师(题目+回答)
linux·经验分享·安全·阿里云·面试·职场和发展·云计算
勤不了一点40 分钟前
小白上手RPM包制作
linux·运维·服务器·软件工程
麦a~M了M2 小时前
ansible
linux·运维·ansible
QQ_4376643143 小时前
Linux下可执行程序的生成和运行详解(编译链接汇编图解)
linux·运维·c语言·汇编·caffe
窦再兴4 小时前
来一个复古的技术FTP
linux·运维·服务器
xiaobin889994 小时前
【2025最新版】VMware虚拟机下载安装教程 保姆级图文详解(附安装包+常用镜像Linux,win11,ubuntu,centos)
linux·其他·ubuntu·centos
小白学大数据5 小时前
基于Scrapy-Redis的分布式景点数据爬取与热力图生成
javascript·redis·分布式·scrapy
ALex_zry5 小时前
Ubuntu 20.04 C++开发环境搭建指南(2025版)
linux·c++·ubuntu
Kookoos5 小时前
Redis + ABP vNext 构建分布式高可用缓存架构
redis·分布式·缓存·架构·c#·.net