Redis Cluster 集群部署

一、环境准备

1.1 服务器基础信息确认

操作系统:UOS Server 20 (1070a)

服务器IP:

  • 192.169.188.85
  • 192.169.188.86
  • 192.169.188.87

1.2 端口规划(3主3从,其实由redis自己分配,一般不是一主一从在同一台机器上)

服务器IP 主节点端口 从节点端口 角色分配(不确定)
192.169.188.85 6371 6372 主1 + 从2
192.169.188.86 6373 6374 主2 + 从3
192.169.188.87 6375 6376 主3 + 从1

1.3 下载redis源码

https://redis.io/download/

二、安装Redis(所有节点执行)

2.1 安装编译依赖(使用yum)

bash 复制代码
# 安装依赖
yum install -y gcc make 

2.2 编译Redis最新版本

bash 复制代码
# 创建/data目录
mkdir -p /data/redis
chmod 755 /data/redis

# 解压
tar xzf redis-stable.tar.gz
cd redis-stable

# 编译安装
make
make install PREFIX=/data/redis

# 验证安装
/data/redis/bin/redis-server --version
/data/redis/bin/redis-cli --version

2.3 创建集群目录结构(所有节点执行)

bash 复制代码
mkdir -p /data/redis/cluster

# 85节点
if [ "$(hostname -I | awk '{print $1}')" = "192.169.188.85" ]; then
    mkdir -p /data/redis/cluster/6371 /data/redis/cluster/6372
fi

# 86节点  
if [ "$(hostname -I | awk '{print $1}')" = "192.169.188.86" ]; then
   mkdir -p /data/redis/cluster/6373 /data/redis/cluster/6374
fi

# 87节点
if [ "$(hostname -I | awk '{print $1}')" = "192.169.188.87" ]; then
    mkdir -p /data/redis/cluster/6375 /data/redis/cluster/6376
fi

三、配置文件准备

3.1 通用配置模板(每个节点)

在源码包里面,找到默认的配置文件 redis.conf ,去掉注释,方便配置

bash 复制代码
sed -e '/^[[:space:]]*#/d' -e '/^[[:space:]]*$/d' redis.conf >redis.conf.template

移动至目录

bash 复制代码
/data/redis/cluster/

修改或添加如下配置

bash 复制代码
# /data/redis/cluster/redis.conf.template

# 网络配置
port %port%
bind 0.0.0.0
protected-mode no

# 基础配置
daemonize yes
pidfile /var/run/redis_%port%.pid
logfile "/data/redis/cluster/%port%/redis.log"
dir "/data/redis/cluster/%port%"

# 集群配置
cluster-enabled yes
cluster-config-file nodes-%port%.conf
cluster-node-timeout 15000

# 安全配置-设置密码
requirepass "YourStrongPassword123!"
masterauth "YourStrongPassword123!"

# 持久化配置开启AOF
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec

3.2 为每个节点生成具体配置

bash 复制代码
# 85节点
cd /data/redis/cluster/
cp redis.conf.template 6371/redis.conf
sed -i 's/%port%/6371/g' 6371/redis.conf

# 85节点
cp redis.conf.template 6372/redis.conf
sed -i 's/%port%/6372/g' 6372/redis.conf

# 86节点
cp redis.conf.template 6373/redis.conf
sed -i 's/%port%/6373/g' 6373/redis.conf

# 86节点
cp redis.conf.template 6374/redis.conf
sed -i 's/%port%/6374/g' 6374/redis.conf

# 87节点
cp redis.conf.template 6375/redis.conf
sed -i 's/%port%/6375/g' 6375/redis.conf

# 87节点
cp redis.conf.template 6376/redis.conf
sed -i 's/%port%/6376/g' 6376/redis.conf

四、启动Redis节点

4.1 创建启动脚本

bash 复制代码
vim /data/redis/cluster/start_redis_nodes.sh
bash 复制代码
#!/bin/bash

# 根据当前IP确定需要启动的节点
CURRENT_IP=$(hostname -I | awk '{print $1}')
NODES=()

case $CURRENT_IP in
    "192.169.188.85")
        NODES=("6371" "6372")
        ;;
    "192.169.188.86")
        NODES=("6373" "6374")
        ;;
    "192.169.188.87")
        NODES=("6375" "6376")
        ;;
    *)
        echo "Unknown IP: $CURRENT_IP"
        exit 1
        ;;
esac

echo "启动 Redis 节点  $CURRENT_IP: ${NODES[@]}"

for port in "${NODES[@]}"; do
    echo "Starting Redis node on port $port..."
    /data/redis/bin/redis-server /data/redis/cluster/$port/redis.conf
    sleep 2
    
    # 检查是否启动成功
    if ps -ef | grep "redis-server .*:$port" | grep -v grep > /dev/null; then
        echo "Redis node $port started successfully"
    else
        echo "Failed to start Redis node $port"
        exit 1
    fi
done

echo "所有Redis节点成功启动!"

4.2 执行启动脚本

bash 复制代码
cd /data/redis/cluster/
chmod +x start_redis_nodes.sh
./start_redis_nodes.sh

4.3 验证节点状态

bash 复制代码
# 检查进程
ps -ef | grep redis-server

# 检查端口
netstat -tlnp | grep redis

# 检查日志
tail -f /data/redis/cluster/6371/redis.log

五、创建Redis Cluster

5.1 在任意一个节点上执行集群创建命令

bash 复制代码
/data/redis/bin/redis-cli -a "YourStrongPassword123!" --cluster create \
192.169.188.85:6371 192.169.188.85:6372 \
192.169.188.86:6373 192.169.188.86:6374 \
192.169.188.87:6375 192.169.188.87:6376 \
--cluster-replicas 1

5.2 确认集群创建(输入yes)

bash 复制代码
Can I set the above configuration? (type 'yes' to accept):

六、验证集群状态

6.1 检查集群信息

bash 复制代码
/data/redis/bin/redis-cli -p 6371 -a "YourStrongPassword123!" cluster info
/data/redis/bin/redis-cli -p 6371 -a "YourStrongPassword123!" cluster nodes

6.2 验证主从关系

bash 复制代码
# 检查主节点
#85
/data/redis/bin/redis-cli -p 6371 -a "YourStrongPassword123!" info replication
#86
/data/redis/bin/redis-cli -p 6373 -a "YourStrongPassword123!" info replication  
#87
/data/redis/bin/redis-cli -p 6375 -a "YourStrongPassword123!" info replication

# 检查从节点
#85
/data/redis/bin/redis-cli -p 6372 -a "YourStrongPassword123!" info replication
#86
/data/redis/bin/redis-cli -p 6374 -a "YourStrongPassword123!" info replication
#87
/data/redis/bin/redis-cli -p 6376 -a "YourStrongPassword123!" info replication

6.3 测试集群写入

bash 复制代码
/data/redis/bin/redis-cli -c -p 6371 -a "YourStrongPassword123!"
> set test_key "Hello Redis Cluster"
> get test_key
> cluster keyslot test_key
相关推荐
heimeiyingwang2 分钟前
【架构实战】MySQL索引优化:从慢查询到毫秒响应
数据库·mysql·架构
得闲喝茶3 分钟前
跨表数据匹配——VLOOKUP、XLOOKUP
大数据·数据库·笔记·信息可视化·数据分析·excel
snow@li19 分钟前
数据库:如何设计最佳索引 / 数据库最佳索引设计指南
数据库
渣渣灰飞39 分钟前
MySQL 系统学习 第二阶段 第四章:DCL(Data Control Language)第二节:权限管理与角色(Role)
数据库·学习·mysql
肖永威1 小时前
金仓数据库麒麟环境SQL终端使用入门实践(增删改查篇)
数据库·sql·金仓数据库
m0_715674431 小时前
2026年医疗行业数据库风险监测产品综合能力排名分析
网络·数据库
渣渣灰飞2 小时前
MySQL 系统学习 第四阶段:MySQL 高级 第一节:索引(Index)
数据库·学习·mysql
山峰哥2 小时前
数据库工程与SQL优化实战指南‌
数据库·sql·oracle·深度优先·宽度优先
Quincy_Freak3 小时前
Python 轻量化数据存储实践:国产化环境下SQLite高效管理方案
数据库·sqlite·数据库管理·大数据分析·sqlitego
Devin~Y3 小时前
电商场景下的Java面试实战:从Spring Boot微服务到Kafka、Redis与AI RAG
java·spring boot·redis·elasticsearch·spring cloud·微服务·kafka