redis-cluster集群实验及解析

一.实验设备

|--------|----------------|
| 主机 | IP |
| redis1 | 192.168.220.10 |
| redis2 | 192.168.220.20 |
| redis3 | 192.168.220.30 |
| redis4 | 192.168.220.40 |
| redis5 | 192.168.220.50 |
| redis6 | 192.168.220.60 |
| redis7 | 192.168.220.70 |
| redis8 | 192.168.220.80 |

注意:设备开启前要关闭防火墙和seliux,以免影响实验

二.Redis安装部署(每台主机都要)

下载依赖性

cpp 复制代码
[root@redis1 ~]# dnf install make gcc initscripts -y

源码编译redis

redis压缩包下载地址 : https://download.redis.io/releases/redis-7.4.8.tar.gz

cpp 复制代码
[root@redis1 ~]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port           : 6379
Config file    : /etc/redis/redis.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!


#更新重启文件
[root@redis1 ~]# systemctl status redis_6379.service

[root@redis1 ~]# systemctl daemon-reload
[root@redis1 ~]# systemctl status redis_6379.service
○ redis_6379.service - LSB: start and stop redis_6379
     Loaded: loaded (/etc/rc.d/init.d/redis_6379; generated)
     Active: inactive (dead)
       Docs: man:systemd-sysv-generator(8)

[root@redis1 ~]# systemctl start redis_6379.service
[root@redis1 ~]# systemctl status redis_6379.service
● redis_6379.service - LSB: start and stop redis_6379
     Loaded: loaded (/etc/rc.d/init.d/redis_6379; generated)
     Active: active (exited) since Sun 2026-03-08 15:24:18 CST; 8s ago
       Docs: man:systemd-sysv-generator(8)
    Process: 35637 ExecStart=/etc/rc.d/init.d/redis_6379 start (code=exited, status=0/SUCCESS)
        CPU: 1ms

修改所有节点配置文件(每台主机都要)

cpp 复制代码
[root@redis1 ~]# vim /etc/redis/6379.conf
protected-mode no
masterauth "123456"					#集群主从认证
cluster-enabled yes					#开启cluster集群功能
cluster-config-file nodes-6379.conf	#指定集群配置文件
cluster-node-timeout 15000			#节点加入集群的超时时间单位是ms

[root@redis1 ~]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

三.启动集群

cpp 复制代码
[root@redis1 ~]# redis-cli  --cluster create 192.168.220.10:6379 192.168.220.20:6379 192.168.220.30:6379 192.168.220.40:6379 192.168.220.50:6379 192.168.220.60:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.220.50:6379 to 192.168.220.10:6379
Adding replica 192.168.220.60:6379 to 192.168.220.20:6379
Adding replica 192.168.220.40:6379 to 192.168.220.30:6379
M: 95826ef97951c091da72045929ffe25e7f23e338 192.168.220.10:6379
   slots:[0-5460] (5461 slots) master
M: 54a7e06bb23a6f5c40ec54ed27e12e0e91713edb 192.168.220.20:6379
   slots:[5461-10922] (5462 slots) master
M: b4c860b38c4c1e2c7f936634e4decfa3ffb7b582 192.168.220.30:6379
   slots:[10923-16383] (5461 slots) master
S: 2b12388e31783b2d41d70398a1a29f3a3675211d 192.168.220.40:6379
   replicates b4c860b38c4c1e2c7f936634e4decfa3ffb7b582
S: 17eede28eb72acf71fbdc2ac9f1fe4f4a4e1aa7d 192.168.220.50:6379
   replicates 95826ef97951c091da72045929ffe25e7f23e338
S: 2ea279582d95aac31a1be2816806913dfbaa8c5b 192.168.220.60:6379
   replicates 54a7e06bb23a6f5c40ec54ed27e12e0e91713edb
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.220.10:6379)
M: 95826ef97951c091da72045929ffe25e7f23e338 192.168.220.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 54a7e06bb23a6f5c40ec54ed27e12e0e91713edb 192.168.220.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: b4c860b38c4c1e2c7f936634e4decfa3ffb7b582 192.168.220.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 2ea279582d95aac31a1be2816806913dfbaa8c5b 192.168.220.60:6379
   slots: (0 slots) slave
   replicates 54a7e06bb23a6f5c40ec54ed27e12e0e91713edb
S: 17eede28eb72acf71fbdc2ac9f1fe4f4a4e1aa7d 192.168.220.50:6379
   slots: (0 slots) slave
   replicates 95826ef97951c091da72045929ffe25e7f23e338
S: 2b12388e31783b2d41d70398a1a29f3a3675211d 192.168.220.40:6379
   slots: (0 slots) slave
   replicates b4c860b38c4c1e2c7f936634e4decfa3ffb7b582
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...

查看集群状态

cpp 复制代码
[root@redis1 ~]# redis-cli  --cluster info 192.168.220.10:6379
192.168.220.10:6379 (95826ef9...) -> 0 keys | 5461 slots | 1 slaves.
192.168.220.20:6379 (54a7e06b...) -> 0 keys | 5462 slots | 1 slaves.
192.168.220.30:6379 (b4c860b3...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

查看集群信息

cpp 复制代码
[root@redis1 ~]# redis-cli  --cluster info 192.168.220.10:6379
192.168.220.10:6379 (95826ef9...) -> 0 keys | 5461 slots | 1 slaves.
192.168.220.20:6379 (54a7e06b...) -> 0 keys | 5462 slots | 1 slaves.
192.168.220.30:6379 (b4c860b3...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
[root@redis1 ~]# redis-cli cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:56
cluster_stats_messages_pong_sent:65
cluster_stats_messages_sent:121
cluster_stats_messages_ping_received:60
cluster_stats_messages_pong_received:56
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:121
total_cluster_links_buffer_limit_exceeded:0

检测当前集群

cpp 复制代码
total_cluster_links_buffer_limit_exceeded:0
[root@redis1 ~]# redis-cli --cluster check 192.168.220.10:6379
192.168.220.10:6379 (95826ef9...) -> 0 keys | 5461 slots | 1 slaves.
192.168.220.20:6379 (54a7e06b...) -> 0 keys | 5462 slots | 1 slaves.
192.168.220.30:6379 (b4c860b3...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.220.10:6379)
M: 95826ef97951c091da72045929ffe25e7f23e338 192.168.220.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 54a7e06bb23a6f5c40ec54ed27e12e0e91713edb 192.168.220.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: b4c860b38c4c1e2c7f936634e4decfa3ffb7b582 192.168.220.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 2ea279582d95aac31a1be2816806913dfbaa8c5b 192.168.220.60:6379
   slots: (0 slots) slave
   replicates 54a7e06bb23a6f5c40ec54ed27e12e0e91713edb
S: 17eede28eb72acf71fbdc2ac9f1fe4f4a4e1aa7d 192.168.220.50:6379
   slots: (0 slots) slave
   replicates 95826ef97951c091da72045929ffe25e7f23e338
S: 2b12388e31783b2d41d70398a1a29f3a3675211d 192.168.220.40:6379
   slots: (0 slots) slave
   replicates b4c860b38c4c1e2c7f936634e4decfa3ffb7b582
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

四.集群扩容

添加master

cpp 复制代码
[root@redis1 ~]# redis-cli --cluster  add-node 192.168.220.70:6379 192.168.220.10:6379

[root@redis1 ~]# redis-cli  --cluster  check 192.168.220.10:6379
192.168.220.10:6379 (8db833f3...) -> 0 keys | 5461 slots | 1 slaves.
192.168.220.70:6379 (dfabfe07...) -> 0 keys | 0 slots | 0 slaves.
192.168.220.30:6379 (d9300173...) -> 0 keys | 5461 slots | 1 slaves.
192.168.220.20:6379 (ca599940...) -> 1 keys | 5462 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.220.10:6379)
M: 8db833f3c3bc6b8f93e87111f13f56d366f833a0 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: dfabfe07170ac9b5d20a5a7a70c836877bd64504 192.168.220.70:6379
   slots: (0 slots) master
S: c939a04358edc1ce7a1c1a44561d77fb402025fd 192.168.220.60:6379
   slots: (0 slots) slave
   replicates ca599940209f55c07d06951480703bb0a5d8873a
M: d9300173b75149d3056f0ee3edec063f8ec66e9a 192.168.220.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: ca599940209f55c07d06951480703bb0a5d8873a 192.168.220.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: ba6ef067c63d30c213493eb48d43427015018898 192.168.220.50:6379
   slots: (0 slots) slave
   replicates 8db833f3c3bc6b8f93e87111f13f56d366f833a0
S: 32d797eb30094b77edb896abcc0b0fc91ccdb4fd 192.168.220.40:6379
   slots: (0 slots) slave
   replicates d9300173b75149d3056f0ee3edec063f8ec66e9a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered

分配solt给新加入的主机

cpp 复制代码
>>> Performing Cluster Check (using node 192.168.220.10:6379)
M: 8db833f3c3bc6b8f93e87111f13f56d366f833a0 192.168.220.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: dfabfe07170ac9b5d20a5a7a70c836877bd64504 192.168.220.70:6379
   slots: (0 slots) master
S: c939a04358edc1ce7a1c1a44561d77fb402025fd 192.168.220.60:6379
   slots: (0 slots) slave
   replicates ca599940209f55c07d06951480703bb0a5d8873a
M: d9300173b75149d3056f0ee3edec063f8ec66e9a 192.168.220.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: ca599940209f55c07d06951480703bb0a5d8873a 192.168.220.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: ba6ef067c63d30c213493eb48d43427015018898 192.168.220.50:6379
   slots: (0 slots) slave
   replicates 8db833f3c3bc6b8f93e87111f13f56d366f833a0
S: 32d797eb30094b77edb896abcc0b0fc91ccdb4fd 192.168.220.40:6379
   slots: (0 slots) slave
   replicates d9300173b75149d3056f0ee3edec063f8ec66e9a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096			#分配solt的数量
What is the receiving node ID? dfabfe07170ac9b5d20a5a7a70c836877bd64504
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: all			#solt来源

Ready to move 4096 slots.

给新主机添加slave

cpp 复制代码
[root@redis1 ~]# redis-cli  --cluster  add-node 192.168.220.80:6379 192.168.220.10:6379 --cluster-slave --cluster-master-id dfabfe07170ac9b5d20a5a7a70c836877bd64504 #这个materid是你新加入进去主机ip地址192.168.220.70主机的

#查看集群状态
[root@redis1 ~]# redis-cli  --cluster  check  192.168.220.10:6379                                              192.168.220.10:6379 (8db833f3...) -> 0 keys | 4096 slots | 1 slaves.
192.168.220.70:6379 (dfabfe07...) -> 1 keys | 4096 slots | 1 slaves.
192.168.220.30:6379 (d9300173...) -> 0 keys | 4096 slots | 1 slaves.
192.168.220.20:6379 (ca599940...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.220.10:6379)
M: 8db833f3c3bc6b8f93e87111f13f56d366f833a0 192.168.220.10:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: 1176ee294e6b5071ca57e93374d04ac22028daed 192.168.220.80:6379
   slots: (0 slots) slave
   replicates dfabfe07170ac9b5d20a5a7a70c836877bd64504
M: dfabfe07170ac9b5d20a5a7a70c836877bd64504 192.168.220.70:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
S: c939a04358edc1ce7a1c1a44561d77fb402025fd 192.168.220.60:6379
   slots: (0 slots) slave
   replicates ca599940209f55c07d06951480703bb0a5d8873a
M: d9300173b75149d3056f0ee3edec063f8ec66e9a 192.168.220.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: ca599940209f55c07d06951480703bb0a5d8873a 192.168.220.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: ba6ef067c63d30c213493eb48d43427015018898 192.168.220.50:6379
   slots: (0 slots) slave
   replicates 8db833f3c3bc6b8f93e87111f13f56d366f833a0
S: 32d797eb30094b77edb896abcc0b0fc91ccdb4fd 192.168.220.40:6379
   slots: (0 slots) slave
   replicates d9300173b75149d3056f0ee3edec063f8ec66e9a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

集群缩容

#集群槽位回收到10主机中

cpp 复制代码
[root@redis1 ~]# redis-cli  --cluster reshard 192.168.220.10:6379
>>> Performing Cluster Check (using node 192.168.220.10:6379)
M: 8db833f3c3bc6b8f93e87111f13f56d366f833a0 192.168.220.10:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: 1176ee294e6b5071ca57e93374d04ac22028daed 192.168.220.80:6379
   slots: (0 slots) slave
   replicates dfabfe07170ac9b5d20a5a7a70c836877bd64504
M: dfabfe07170ac9b5d20a5a7a70c836877bd64504 192.168.220.70:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
S: c939a04358edc1ce7a1c1a44561d77fb402025fd 192.168.220.60:6379
   slots: (0 slots) slave
   replicates ca599940209f55c07d06951480703bb0a5d8873a
M: d9300173b75149d3056f0ee3edec063f8ec66e9a 192.168.220.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: ca599940209f55c07d06951480703bb0a5d8873a 192.168.220.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: ba6ef067c63d30c213493eb48d43427015018898 192.168.220.50:6379
   slots: (0 slots) slave
   replicates 8db833f3c3bc6b8f93e87111f13f56d366f833a0
S: 32d797eb30094b77edb896abcc0b0fc91ccdb4fd 192.168.220.40:6379
   slots: (0 slots) slave
   replicates d9300173b75149d3056f0ee3edec063f8ec66e9a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? 8db833f3c3bc6b8f93e87111f13f56d366f833a0  #10id
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: dfabfe07170ac9b5d20a5a7a70c836877bd64504		#70id
Source node #2: done

删除70和80节点

cpp 复制代码
[root@redis1 ~]# redis-cli --cluster del-node  192.168.220.10:6379 dfabfe07170ac9b5d20a5a7a70c836877bd64504   #dfabfe07170ac9b5d20a5a7a70c836877bd64504这个是masterid70的

>>> Removing node dfabfe07170ac9b5d20a5a7a70c836877bd64504 from cluster 192.168.220.10:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

[root@redis-node1 ~]# redis-cli --cluster del-node  192.168.220.10:6379 1176ee294e6b5071ca57e93374d04ac22028daed  #1176ee294e6b5071ca57e93374d04ac22028daed这个是masterid80的

>>> Removing node 1176ee294e6b5071ca57e93374d04ac22028daed from cluster 192.168.220.10:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

#查看集群状态
[root@redis-node1 ~]# redis-cli  --cluster  check  192.168.220.10:6379
192.168.220.10:6379 (8db833f3...) -> 1 keys | 8192 slots | 1 slaves.
192.168.220.30:6379 (d9300173...) -> 0 keys | 4096 slots | 1 slaves.
192.168.220.20:6379 (ca599940...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.220.10:6379)
M: 8db833f3c3bc6b8f93e87111f13f56d366f833a0 172.25.254.10:6379
   slots:[0-6826],[10923-12287] (8192 slots) master
   1 additional replica(s)
S: c939a04358edc1ce7a1c1a44561d77fb402025fd 192.168.220.60:6379
   slots: (0 slots) slave
   replicates ca599940209f55c07d06951480703bb0a5d8873a
M: d9300173b75149d3056f0ee3edec063f8ec66e9a 192.168.220.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: ca599940209f55c07d06951480703bb0a5d8873a 192.168.220.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: ba6ef067c63d30c213493eb48d43427015018898 192.168.220.50:6379
   slots: (0 slots) slave
   replicates 8db833f3c3bc6b8f93e87111f13f56d366f833a0
S: 32d797eb30094b77edb896abcc0b0fc91ccdb4fd 192.168.220.40:6379
   slots: (0 slots) slave
   replicates d9300173b75149d3056f0ee3edec063f8ec66e9a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
相关推荐
夜晚打字声1 天前
9(九)Jmeter如何连接数据库
数据库·jmeter·oracle
Chasing__Dreams1 天前
Mysql--基础知识点--95--为什么避免使用长事务
数据库·mysql
风吹迎面入袖凉1 天前
【Redis】Redis的五种核心数据类型详解
java·redis
NineData1 天前
NineData 智能数据管理平台新功能发布|2026 年 3 月
数据库·oracle·架构·dba·ninedata·数据复制·数据迁移工具
小陈工1 天前
2026年4月7日技术资讯洞察:下一代数据库融合、AI基础设施竞赛与异步编程实战
开发语言·前端·数据库·人工智能·python
❀͜͡傀儡师1 天前
k8s部署的Nexus 3 数据库损坏恢复指南:从删除损坏数据库到完整数据重建
数据库·kubernetes·nexus3
StackNoOverflow1 天前
Spring Security权限控制框架详解
java·数据库·sql
不愿透露姓名的大鹏1 天前
Oracle归档日志爆满急救指南
linux·数据库·oracle·dba
a里啊里啊1 天前
Redis面试题记录
数据库·redis·缓存
数据知道1 天前
claw-code 源码分析:OmX `$team` / `$ralph`——把 AI 辅助开发从偶发灵感变成可重复流水线
数据库·人工智能·mysql·ai·claude code·claw code