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.
相关推荐
阿贵---2 小时前
构建一个基于命令行的待办事项应用
jvm·数据库·python
2301_815357702 小时前
Spring 声明式事务:删除操作的事务管理全解与最佳实践
数据库
小李来了!2 小时前
Oracle、MySQL、SQL server介绍及有何区别
数据库·mysql·oracle·sqlserver
mcooiedo2 小时前
Mysql ONLY_FULL_GROUP_BY模式详解、group by非查询字段报错
数据库·mysql
柒.梧.2 小时前
新手入门:NoSQL与Redis核心基础解析
数据库·redis·nosql
qq_416018722 小时前
持续集成/持续部署(CI/CD) for Python
jvm·数据库·python
执笔画情ora2 小时前
PG/mysql/oracle--- 长事务对后续事务影响分析
数据库·mysql·oracle
qq_283720052 小时前
nestjs实战(六):诺依Nest.js + MySQL 项目改造为兼容达梦8数据库详细教程
javascript·数据库·mysql·达梦·nest.js·诺依
qq_416018722 小时前
使用Python处理计算机图形学(PIL/Pillow)
jvm·数据库·python