Redis集群分布式存储

哈希取余分区
一致性哈希算法分区
哈希槽分区(标配,16384个槽位)

3主3从redis集群扩缩容配置案例构架说明

1.关闭防火墙+启动docker后台服务

systemctl start docker

2.新建6个docker容器redis实例

docker run -d --name redis-node-1 --net host --privileged=true -v /data/redis/share/redis-node-1:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381
docker run -d --name redis-node-2 --net host --privileged=true -v /data/redis/share/redis-node-2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382
docker run -d --name redis-node-3 --net host --privileged=true -v /data/redis/share/redis-node-3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383
docker run -d --name redis-node-4 --net host --privileged=true -v /data/redis/share/redis-node-4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384
docker run -d --name redis-node-5 --net host --privileged=true -v /data/redis/share/redis-node-5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385
docker run -d --name redis-node-6 --net host --privileged=true -v /data/redis/share/redis-node-6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386
[root@test102 ~]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED             STATUS             PORTS     NAMES
21c6f2264a0d   redis:6.0.8   "docker-entrypoint.s…"   About an hour ago   Up About an hour             redis-node-6
6fd8a48dbfa9   redis:6.0.8   "docker-entrypoint.s…"   About an hour ago   Up About an hour             redis-node-5
bb335da6c5fe   redis:6.0.8   "docker-entrypoint.s…"   About an hour ago   Up 5 minutes                 redis-node-4
ef74ff78c0c4   redis:6.0.8   "docker-entrypoint.s…"   About an hour ago   Up About an hour             redis-node-3
dc094b4ea333   redis:6.0.8   "docker-entrypoint.s…"   About an hour ago   Up About an hour             redis-node-2
0f53c25bd4c1   redis:6.0.8   "docker-entrypoint.s…"   About an hour ago   Up 9 minutes                 redis-node-1
#命令分步解释
docker run    #创建并运行docker容器实例
--name redis-node-6    #容器名字
--net host    #使用宿主机的IP和端口,默认
--privileged=true    #获取宿主机root用户权限
-v /data/redis/share/redis-node-6:/data    #容器卷,宿主机地址:docker内部地址
redis:6.0.8    #redis镜像和版本号
--cluster-enabled yes    #开启redis集群
--appendonly yes    #开启持久化
--port 6386    #redis端口号

3.进入容器redis-node-1并为6台机器构建集群关系

#进入容器
docker exec -it redis-node-1 /bin/bash
#构建主从关系
//注意,进入docker容器后才能执行一下命令,且注意自己的真实IP地址
redis-cli --cluster create 192.168.238.102:6381 192.168.238.102:6382 192.168.238.102:6383 192.168.238.102:6384 192.168.238.102:6385 192.168.238.102:6386 --cluster-replicas 1
 
--cluster-replicas 1 表示为每个master创建一个slave节点
root@test102:/data# redis-cli --cluster create 192.168.238.102:6381 192.168.238.102:6382 192.168.238.102:6383 192.168.238.102:6384 192.168.238.102:6385 192.168.238.102:6386 --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.238.102:6385 to 192.168.238.102:6381
Adding replica 192.168.238.102:6386 to 192.168.238.102:6382
Adding replica 192.168.238.102:6384 to 192.168.238.102:6383
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 7c0f119c12f00a524f10698095f633ce6c71bc26 192.168.238.102:6381
   slots:[0-5460] (5461 slots) master
M: 2a79d5d2975e286d8a27677a29d54999cee77314 192.168.238.102:6382
   slots:[5461-10922] (5462 slots) master
M: bf64462608ff216e0dd6beba8194104a0fa31166 192.168.238.102:6383
   slots:[10923-16383] (5461 slots) master
S: 882d478689f8c0358f1206d8f3a0cf744e215c69 192.168.238.102:6384
   replicates 7c0f119c12f00a524f10698095f633ce6c71bc26
S: 2e785665b5c44d46aed52448b7e1054557d24122 192.168.238.102:6385
   replicates 2a79d5d2975e286d8a27677a29d54999cee77314
S: fe649fea769383319ace5240af5147d9b6bf4e3e 192.168.238.102:6386
   replicates bf64462608ff216e0dd6beba8194104a0fa31166
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.238.102:6381)
M: 7c0f119c12f00a524f10698095f633ce6c71bc26 192.168.238.102:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 882d478689f8c0358f1206d8f3a0cf744e215c69 192.168.238.102:6384
   slots: (0 slots) slave
   replicates 7c0f119c12f00a524f10698095f633ce6c71bc26
S: fe649fea769383319ace5240af5147d9b6bf4e3e 192.168.238.102:6386
   slots: (0 slots) slave
   replicates bf64462608ff216e0dd6beba8194104a0fa31166
S: 2e785665b5c44d46aed52448b7e1054557d24122 192.168.238.102:6385
   slots: (0 slots) slave
   replicates 2a79d5d2975e286d8a27677a29d54999cee77314
M: bf64462608ff216e0dd6beba8194104a0fa31166 192.168.238.102:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 2a79d5d2975e286d8a27677a29d54999cee77314 192.168.238.102:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

一切OK的话,3主3从搞定

4.链接进入6381作为切入点,查看集群状态

# 链接进入6381作为切入点,查看节点状态
cluster info
cluster nodes

root@test102:/data# redis-cli -p 6381
127.0.0.1:6381> 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:259
cluster_stats_messages_pong_sent:261
cluster_stats_messages_sent:520
cluster_stats_messages_ping_received:256
cluster_stats_messages_pong_received:259
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:520
127.0.0.1:6381> cluster nodes
882d478689f8c0358f1206d8f3a0cf744e215c69 192.168.238.102:6384@16384 slave 7c0f119c12f00a524f10698095f633ce6c71bc26 0 1669623053076 1 connected
fe649fea769383319ace5240af5147d9b6bf4e3e 192.168.238.102:6386@16386 slave bf64462608ff216e0dd6beba8194104a0fa31166 0 1669623054097 3 connected
2e785665b5c44d46aed52448b7e1054557d24122 192.168.238.102:6385@16385 slave 2a79d5d2975e286d8a27677a29d54999cee77314 0 1669623051032 2 connected
bf64462608ff216e0dd6beba8194104a0fa31166 192.168.238.102:6383@16383 master - 0 1669623052000 3 connected 10923-16383
2a79d5d2975e286d8a27677a29d54999cee77314 192.168.238.102:6382@16382 master - 0 1669623052049 2 connected 5461-10922
7c0f119c12f00a524f10698095f633ce6c71bc26 192.168.238.102:6381@16381 myself,master - 0 1669623052000 1 connected 0-5460

从上面可以分析

Master Slave
1 4
2 5
3 6

5.主从数据测试

数据读写存储
启动6机构成的集群并通过exec进入
对6381新增两个key
防止路由失效加参数-c并新增两个key

root@test102:/data# redis-cli -p 6381 -c
127.0.0.1:6381> 
127.0.0.1:6381> 
127.0.0.1:6381> 
127.0.0.1:6381> 
127.0.0.1:6381> set k1 v1
-> Redirected to slot [12706] located at 192.168.238.102:6383
OK
192.168.238.102:6383> set k2 v2
-> Redirected to slot [449] located at 192.168.238.102:6381
OK
192.168.238.102:6381> set k3 v3
OK
192.168.238.102:6381> 
# 查看集群信息
root@test102:/data# redis-cli --cluster check 192.168.238.102:6381
192.168.238.102:6381 (7c0f119c...) -> 2 keys | 5461 slots | 1 slaves.
192.168.238.102:6383 (bf644626...) -> 1 keys | 5461 slots | 1 slaves.
192.168.238.102:6382 (2a79d5d2...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 3 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.238.102:6381)
M: 7c0f119c12f00a524f10698095f633ce6c71bc26 192.168.238.102:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 882d478689f8c0358f1206d8f3a0cf744e215c69 192.168.238.102:6384
   slots: (0 slots) slave
   replicates 7c0f119c12f00a524f10698095f633ce6c71bc26
S: fe649fea769383319ace5240af5147d9b6bf4e3e 192.168.238.102:6386
   slots: (0 slots) slave
   replicates bf64462608ff216e0dd6beba8194104a0fa31166
S: 2e785665b5c44d46aed52448b7e1054557d24122 192.168.238.102:6385
   slots: (0 slots) slave
   replicates 2a79d5d2975e286d8a27677a29d54999cee77314
M: bf64462608ff216e0dd6beba8194104a0fa31166 192.168.238.102:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 2a79d5d2975e286d8a27677a29d54999cee77314 192.168.238.102:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

6.主从容错切换迁移案例

#容错切换迁移
#先停掉redis-node-1
[root@test102 ~]# docker stop redis-node-1
redis-node-1
#再开一个窗口登录
[root@test102 ~]# docker exec -it redis-node-2 bash
root@test102:/data# redis-cli -p 6382 -c
127.0.0.1:6382> cluster nodes
2e785665b5c44d46aed52448b7e1054557d24122 192.168.238.102:6385@16385 slave 2a79d5d2975e286d8a27677a29d54999cee77314 0 1669625298108 2 connected
7c0f119c12f00a524f10698095f633ce6c71bc26 192.168.238.102:6381@16381 master - 0 1669625297086 1 connected 0-5460
fe649fea769383319ace5240af5147d9b6bf4e3e 192.168.238.102:6386@16386 slave bf64462608ff216e0dd6beba8194104a0fa31166 0 1669625296000 3 connected
882d478689f8c0358f1206d8f3a0cf744e215c69 192.168.238.102:6384@16384 slave 7c0f119c12f00a524f10698095f633ce6c71bc26 0 1669625296000 1 connected
2a79d5d2975e286d8a27677a29d54999cee77314 192.168.238.102:6382@16382 myself,master - 0 1669625297000 2 connected 5461-10922
bf64462608ff216e0dd6beba8194104a0fa31166 192.168.238.102:6383@16383 master - 0 1669625296069 3 connected 10923-16383

127.0.0.1:6382> cluster nodes
2e785665b5c44d46aed52448b7e1054557d24122 192.168.238.102:6385@16385 slave 2a79d5d2975e286d8a27677a29d54999cee77314 0 1669625337827 2 connected
7c0f119c12f00a524f10698095f633ce6c71bc26 192.168.238.102:6381@16381 master,fail - 1669625317477 1669625314000 1 disconnected
fe649fea769383319ace5240af5147d9b6bf4e3e 192.168.238.102:6386@16386 slave bf64462608ff216e0dd6beba8194104a0fa31166 0 1669625335000 3 connected
882d478689f8c0358f1206d8f3a0cf744e215c69 192.168.238.102:6384@16384 master - 0 1669625336000 7 connected 0-5460
2a79d5d2975e286d8a27677a29d54999cee77314 192.168.238.102:6382@16382 myself,master - 0 1669625333000 2 connected 5461-10922
bf64462608ff216e0dd6beba8194104a0fa31166 192.168.238.102:6383@16383 master - 0 1669625336811 3 connected 10923-16383

#变化
192.168.238.102:6381@16381 master 变为 192.168.238.102:6381@16381 master,fail
192.168.238.102:6384@16384 master
#检查数据没有丢失
127.0.0.1:6382> get k1
-> Redirected to slot [12706] located at 192.168.238.102:6383
"v1"
192.168.238.102:6383> get k2
-> Redirected to slot [449] located at 192.168.238.102:6384
"v2"
192.168.238.102:6384> get k3
"v3"

#再次启动redis-node-1
[root@test102 ~]# docker start redis-node-1

#在另外一个窗口检查
7c0f119c12f00a524f10698095f633ce6c71bc26 192.168.238.102:6381@16381 slave 882d478689f8c0358f1206d8f3a0cf744e215c69 0 1669625563000 7 connected
redis-node-1变为slave状态

#如何恢复redis-node-1 master状态了
#停掉redis-node-4,在重新启动redis-node-4即可
[root@test102 ~]# docker stop redis-node-4
#中间需要等待一会儿,docker集群重新响应
[root@test102 ~]# docker start redis-node-4

#再次查看集群状态
root@test102:/data# redis-cli --cluster check 192.168.238.102:6381

7.主从扩容案例

新建6387、6388两个节点+新建后启动+查看是否8节点

docker run -d --name redis-node-7 --net host --privileged=true -v /data/redis/share/redis-node-7:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6387
docker run -d --name redis-node-8 --net host --privileged=true -v /data/redis/share/redis-node-8:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6388
[root@test102 ~]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS     NAMES
2cd67f0ceba2   redis:6.0.8   "docker-entrypoint.s…"   3 seconds ago    Up 2 seconds              redis-node-8
f9aac356e875   redis:6.0.8   "docker-entrypoint.s…"   13 seconds ago   Up 12 seconds             redis-node-7
21c6f2264a0d   redis:6.0.8   "docker-entrypoint.s…"   2 hours ago      Up 2 hours                redis-node-6
6fd8a48dbfa9   redis:6.0.8   "docker-entrypoint.s…"   2 hours ago      Up 2 hours                redis-node-5
bb335da6c5fe   redis:6.0.8   "docker-entrypoint.s…"   2 hours ago      Up 38 minutes             redis-node-4
ef74ff78c0c4   redis:6.0.8   "docker-entrypoint.s…"   2 hours ago      Up 2 hours                redis-node-3
dc094b4ea333   redis:6.0.8   "docker-entrypoint.s…"   2 hours ago      Up 2 hours                redis-node-2
0f53c25bd4c1   redis:6.0.8   "docker-entrypoint.s…"   2 hours ago      Up 42 minutes             redis-node-1

进入6387容器实例内部

docker exec -it redis-node-7 /bin/bash

将新增的6387节点(空槽号)作为master节点加入原集群

将新增的6387作为master节点加入集群
redis-cli --cluster add-node 自己实际IP地址:6387 自己实际IP地址:6381
6387 就是将要作为master新增节点
6381 就是原来集群节点里面的领路人,相当于6387拜6381的码头从而找到组织加入集群
root@test102:/data# redis-cli --cluster add-node 192.168.238.102:6387 192.168.238.102:6381
>>> Adding node 192.168.238.102:6387 to cluster 192.168.238.102:6381
>>> Performing Cluster Check (using node 192.168.238.102:6381)
M: 7c0f119c12f00a524f10698095f633ce6c71bc26 192.168.238.102:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2e785665b5c44d46aed52448b7e1054557d24122 192.168.238.102:6385
   slots: (0 slots) slave
   replicates 2a79d5d2975e286d8a27677a29d54999cee77314
S: 882d478689f8c0358f1206d8f3a0cf744e215c69 192.168.238.102:6384
   slots: (0 slots) slave
   replicates 7c0f119c12f00a524f10698095f633ce6c71bc26
S: fe649fea769383319ace5240af5147d9b6bf4e3e 192.168.238.102:6386
   slots: (0 slots) slave
   replicates bf64462608ff216e0dd6beba8194104a0fa31166
M: bf64462608ff216e0dd6beba8194104a0fa31166 192.168.238.102:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 2a79d5d2975e286d8a27677a29d54999cee77314 192.168.238.102:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.238.102:6387 to make it join the cluster.
[OK] New node added correctly.
#检查集群情况,可以看到6387目前没有槽号
root@test102:/data# redis-cli --cluster check 192.168.238.102:6381
192.168.238.102:6381 (7c0f119c...) -> 2 keys | 5461 slots | 1 slaves.
192.168.238.102:6387 (5c1c91c2...) -> 0 keys | 0 slots | 0 slaves.
192.168.238.102:6383 (bf644626...) -> 1 keys | 5461 slots | 1 slaves.
192.168.238.102:6382 (2a79d5d2...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 3 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.238.102:6381)
M: 7c0f119c12f00a524f10698095f633ce6c71bc26 192.168.238.102:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2e785665b5c44d46aed52448b7e1054557d24122 192.168.238.102:6385
   slots: (0 slots) slave
   replicates 2a79d5d2975e286d8a27677a29d54999cee77314
M: 5c1c91c284ecc1cdfd654850a16d8c3ed3322319 192.168.238.102:6387
   slots: (0 slots) master
S: 882d478689f8c0358f1206d8f3a0cf744e215c69 192.168.238.102:6384
   slots: (0 slots) slave
   replicates 7c0f119c12f00a524f10698095f633ce6c71bc26
S: fe649fea769383319ace5240af5147d9b6bf4e3e 192.168.238.102:6386
   slots: (0 slots) slave
   replicates bf64462608ff216e0dd6beba8194104a0fa31166
M: bf64462608ff216e0dd6beba8194104a0fa31166 192.168.238.102:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 2a79d5d2975e286d8a27677a29d54999cee77314 192.168.238.102:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#重新分派槽号
命令:redis-cli --cluster reshard IP地址:端口号
redis-cli --cluster reshard 192.168.238.102:6381
拿16384/4=4096    #4代表有几个master节点,均分
输入4096,然后再输入6387的IDs来接收,如上面5c1c91c284ecc1cdfd654850a16d8c3ed3322319
选择all #代表所有都均点给6387
稍等一时在执行,查看状态
root@test102:/data# redis-cli --cluster check 192.168.238.102:6381
槽号分派说明
为什么6387是3个新的区间,以前的还是连续?
重新分配成本太高,所以前3家各自匀出来一部分,从6381/6382/6383三个旧节点分别匀出1364个坑位给新节点6387

为主节点6387分配从节点6388

命令:redis-cli --cluster add-node ip:新slave端口 ip:新master端口 --cluster-slave --cluster-master-id 新主机节点ID

redis-cli --cluster add-node 192.168.238.102:6388 192.168.238.102:6387 --cluster-slave --cluster-master-id 5c1c91c284ecc1cdfd654850a16d8c3ed3322319-------这个是6387的编号,按照自己实际情况修改
执行如下:
redis-cli --cluster add-node 192.168.238.102:6388 192.168.238.102:6387 --cluster-slave --cluster-master-id 5c1c91c284ecc1cdfd654850a16d8c3ed3322319
#执行成功会返回三个OK
然后再执行
root@test102:/data# redis-cli --cluster check 192.168.238.102:6382

至此主从扩容案例结束。

8.主从缩容案例

目的:6387和6388下线
检查集群情况1获得6388的节点ID,第一次

root@test102:/data# redis-cli --cluster check 192.168.238.102:6382
...
S: 8e717c9da2c779bcba984a19559009ba3f0a9274 192.168.238.102:6388
   slots: (0 slots) slave
   replicates 5c1c91c284ecc1cdfd654850a16d8c3ed3322319
...

将6388删除 从集群中将4号从节点6388删除
命令:redis-cli --cluster del-node ip:从机端口 从机6388节点ID

root@test102:/data# redis-cli --cluster del-node 192.168.238.102:6388 8e717c9da2c779bcba984a19559009ba3f0a9274
>>> Removing node 8e717c9da2c779bcba984a19559009ba3f0a9274 from cluster 192.168.238.102:6388
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
#表示删除成功
#检查只剩3个Slave了
root@test102:/data# redis-cli --cluster check 192.168.238.102:6382
192.168.238.102:6382 (2a79d5d2...) -> 0 keys | 4096 slots | 1 slaves.
192.168.238.102:6387 (5c1c91c2...) -> 1 keys | 4096 slots | 0 slaves.
192.168.238.102:6381 (7c0f119c...) -> 1 keys | 4096 slots | 1 slaves.
192.168.238.102:6383 (bf644626...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 3 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.238.102:6382)
M: 2a79d5d2975e286d8a27677a29d54999cee77314 192.168.238.102:6382
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
M: 5c1c91c284ecc1cdfd654850a16d8c3ed3322319 192.168.238.102:6387
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 2e785665b5c44d46aed52448b7e1054557d24122 192.168.238.102:6385
   slots: (0 slots) slave
   replicates 2a79d5d2975e286d8a27677a29d54999cee77314
M: 7c0f119c12f00a524f10698095f633ce6c71bc26 192.168.238.102:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: fe649fea769383319ace5240af5147d9b6bf4e3e 192.168.238.102:6386
   slots: (0 slots) slave
   replicates bf64462608ff216e0dd6beba8194104a0fa31166
S: 882d478689f8c0358f1206d8f3a0cf744e215c69 192.168.238.102:6384
   slots: (0 slots) slave
   replicates 7c0f119c12f00a524f10698095f633ce6c71bc26
M: bf64462608ff216e0dd6beba8194104a0fa31166 192.168.238.102:6383
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

将6387的槽号清空,重新分配,本例将清出来的槽号都给6381

redis-cli --cluster reshard 192.168.238.102:6381
root@test102:/data# redis-cli --cluster reshard 192.168.238.102:6381
>>> Performing Cluster Check (using node 192.168.238.102:6381)
M: 7c0f119c12f00a524f10698095f633ce6c71bc26 192.168.238.102:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: 2e785665b5c44d46aed52448b7e1054557d24122 192.168.238.102:6385
   slots: (0 slots) slave
   replicates 2a79d5d2975e286d8a27677a29d54999cee77314
M: 5c1c91c284ecc1cdfd654850a16d8c3ed3322319 192.168.238.102:6387
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 882d478689f8c0358f1206d8f3a0cf744e215c69 192.168.238.102:6384
   slots: (0 slots) slave
   replicates 7c0f119c12f00a524f10698095f633ce6c71bc26
S: fe649fea769383319ace5240af5147d9b6bf4e3e 192.168.238.102:6386
   slots: (0 slots) slave
   replicates bf64462608ff216e0dd6beba8194104a0fa31166
M: bf64462608ff216e0dd6beba8194104a0fa31166 192.168.238.102:6383
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: 2a79d5d2975e286d8a27677a29d54999cee77314 192.168.238.102:6382
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
[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                #输入6387所有的槽位号
What is the receiving node ID? 7c0f119c12f00a524f10698095f633ce6c71bc26    #输入要接收槽位号的ID,本次选择6381的ID
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: 5c1c91c284ecc1cdfd654850a16d8c3ed3322319    #输入要提供槽位号的ID,因为要删除6387,所以写此ID
Source node #2: done                                        #输入done,完成执行
....
Do you want to proceed with the proposed reshard plan (yes/no)? yes  #输入yes确认
....
执行结束

检查集群情况第二次

root@test102:/data# redis-cli --cluster check 192.168.238.102:6381
192.168.238.102:6381 (7c0f119c...) -> 2 keys | 8192 slots | 1 slaves.
192.168.238.102:6387 (5c1c91c2...) -> 0 keys | 0 slots | 0 slaves.        #6387的槽位号已空,4096个槽位都指给6381,相当于全部都给6381
192.168.238.102:6383 (bf644626...) -> 1 keys | 4096 slots | 1 slaves.
192.168.238.102:6382 (2a79d5d2...) -> 0 keys | 4096 slots | 1 slaves.

将6387节点删除
命令:redis-cli --cluster del-node ip:端口 6387节点ID

root@test102:/data# redis-cli --cluster del-node 192.168.238.102:6387 5c1c91c284ecc1cdfd654850a16d8c3ed3322319
>>> Removing node 5c1c91c284ecc1cdfd654850a16d8c3ed3322319 from cluster 192.168.238.102:6387
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

检查集群情况第三次

root@test102:/data# redis-cli --cluster check 192.168.238.102:6381
192.168.238.102:6381 (7c0f119c...) -> 2 keys | 8192 slots | 1 slaves.
192.168.238.102:6383 (bf644626...) -> 1 keys | 4096 slots | 1 slaves.
192.168.238.102:6382 (2a79d5d2...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 3 keys in 3 masters.

至此缩容结束。