温馨提示×

centos redis配置集群如何搭

小樊
39
2026-09-21 03:39:36
栏目: 云计算

下面给你一份**在 CentOS 上搭建 Redis 集群(Redis Cluster)**的实用步骤,适用于 Redis 5+(推荐 6/7),以 3 主 3 从(6 节点) 为例。


一、环境准备

1. 服务器规划(示例)

节点 IP 端口
redis1 192.168.1.11 7000
redis2 192.168.1.12 7000
redis3 192.168.1.13 7000
redis4 192.168.1.14 7000
redis5 192.168.1.15 7000
redis6 192.168.1.16 7000

也可以在一台机器上用不同端口模拟(测试用)


二、安装 Redis(所有节点)

CentOS 7/8

yum install -y gcc make
wget https://download.redis.io/redis-stable.tar.gz
tar xzf redis-stable.tar.tar.gz
cd redis-stable
make
make install

三、Redis 集群配置

1. 创建目录

mkdir -p /etc/redis/cluster/7000

2. 配置文件 /etc/redis/cluster/7000/redis.conf

port 7000
bind 0.0.0.0
daemonize yes
pidfile /var/run/redis_7000.pid
logfile /var/log/redis_7000.log

cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000

appendonly yes
dir /etc/redis/cluster/7000

每个节点改端口和路径即可


四、启动 Redis 节点

redis-server /etc/redis/cluster/7000/redis.conf

检查:

ps -ef | grep redis

五、创建 Redis 集群

方式一(推荐,Redis 5+)

在任意一台机器执行:

redis-cli --cluster create \
192.168.1.11:7000 \
192.168.1.12:7000 \
192.168.1.13:7000 \
192.168.1.14:7000 \
192.168.1.15:7000 \
192.168.1.16:7000 \
--cluster-replicas 1
  • --cluster-replicas 1:1 主 1 从
  • 输入 yes 确认

六、验证集群

redis-cli -c -h 192.168.1.11 -p 7000
cluster info
cluster nodes

测试:

set test 123
get test

七、防火墙(重要)

firewall-cmd --zone=public --add-port=7000/tcp --permanent
firewall-cmd --reload

集群通信还需要 +10000 端口(17000)


八、常见问题

1. 无法形成集群

  • 时间不同步
  • 防火墙未开
  • 没有 cluster-enabled yes

2. 集群重启

直接重启 redis 进程即可,集群信息自动恢复


九、生产建议

  • 至少 3 主 3 从
  • 使用 systemd 管理
  • 开启密码(requirepass + masterauth)
  • 监控:redis-cli cluster info

如果你需要:

  • 单机多端口模拟
  • systemd 配置
  • 带密码的集群
  • Redis 7 新特性

告诉我你的场景,我可以给你定制版本。

0 踩