上一篇我们讲述了《Docker 快速搭建 Redis 集群》,本篇我们将继续讲述 Redis 集群添加节点和删除节点。

之前搭建集群的时候,已经创建了6个 Redis 实例,在原基础上再加2个实例,配置文件 docker-compose.yml 如下:

version: "2"


services:
  redis1:
    image: redis:6
    container_name: redis1
    volumes:
      - ./conf/redis.conf:/etc/redis.conf
      - ./data/data1:/data
    networks:
      backends-redis:
        ipv4_address: 172.10.1.1
    command: redis-server /etc/redis.conf

  redis2:
    image: redis:6
    container_name: redis2
    volumes:
      - ./conf/redis.conf:/etc/redis.conf
      - ./data/data2:/data
    networks:
      backends-redis:
        ipv4_address: 172.10.1.2
    command: redis-server /etc/redis.conf

  redis3:
    image: redis:6
    container_name: redis3
    volumes:
      - ./conf/redis.conf:/etc/redis.conf
      - ./data/data3:/data
    networks:
      backends-redis:
        ipv4_address: 172.10.1.3
    command: redis-server /etc/redis.conf

  redis4:
    image: redis:6
    container_name: redis4
    volumes:
      - ./conf/redis.conf:/etc/redis.conf
      - ./data/data4:/data
    networks:
      backends-redis:
        ipv4_address: 172.10.1.4
    command: redis-server /etc/redis.conf

  redis5:
    image: redis:6
    container_name: redis5
    volumes:
      - ./conf/redis.conf:/etc/redis.conf
      - ./data/data5:/data
    networks:
      backends-redis:
        ipv4_address: 172.10.1.5
    command: redis-server /etc/redis.conf

  redis6:
    image: redis:6
    container_name: redis6
    volumes:
      - ./conf/redis.conf:/etc/redis.conf
      - ./data/data6:/data
    networks:
      backends-redis:
        ipv4_address: 172.10.1.6
    command: redis-server /etc/redis.conf

  redis7:
    image: redis:6
    container_name: redis7
    volumes:
      - ./conf/redis.conf:/etc/redis.conf
      - ./data/data7:/data
    networks:
      backends-redis:
        ipv4_address: 172.10.1.7
    command: redis-server /etc/redis.conf

  redis8:
    image: redis:6
    container_name: redis8
    volumes:
      - ./conf/redis.conf:/etc/redis.conf
      - ./data/data8:/data
    networks:
      backends-redis:
        ipv4_address: 172.10.1.8
    command: redis-server /etc/redis.conf


networks:
  backends-redis:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.10.0.0/16
          gateway: 172.10.0.1

新增的容器为 redis7、redis8,它们的 IP 地址分别是 172.10.1.7、172.10.1.8。执行 docker-compose up,将容器启动起来。

集群添加新节点的指令格式为 redis-cli --cluster add-node <新增节点ip:port> <现存节点ip:port>。其中 新增节点ip:port 我们填 172.10.1.7:6379,而 现存节点ip:port 可以取当前集群中任一节点的 IP 和端口号,如 172.10.1.1:6379

执行 redis-cli --cluster add-node 172.10.1.7:6379 172.10.1.1:6379 输出如下:

$ redis-cli --cluster add-node 172.10.1.7:6379 172.10.1.1:6379
>>> Adding node 172.10.1.7:6379 to cluster 172.10.1.1:6379
>>> Performing Cluster Check (using node 172.10.1.1:6379)
M: 6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: d0afdd6513bd8be9382759dfa635f1566f26ca99 172.10.1.4:6379
   slots: (0 slots) slave
   replicates 775b9f8ae557c4a7543680a25c46910d37a62b2d
M: 775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 1c79fb6e06b78d1a629dbfc0d64e2faf9ac33856 172.10.1.5:6379
   slots: (0 slots) slave
   replicates 6728a6eb85d867689f8822f29e1f34bf983ffc9e
M: 2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: c2dea8329847238f3f450500153ecef1eefb1dae 172.10.1.2:6379
   slots: (0 slots) slave
   replicates 2d67bd9cb3952bec942f468fe9214afdcf4419f4
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.10.1.7:6379 to make it join the cluster.
[OK] New node added correctly.

查看集群节点信息:

$ redis-cli -c -h 172.10.1.1
172.10.1.1:6379> cluster nodes
d0afdd6513bd8be9382759dfa635f1566f26ca99 172.10.1.4:6379@16379 slave 775b9f8ae557c4a7543680a25c46910d37a62b2d 0 1604468722000 3 connected
775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379@16379 master - 0 1604468720000 3 connected 10923-16383
1c79fb6e06b78d1a629dbfc0d64e2faf9ac33856 172.10.1.5:6379@16379 slave 6728a6eb85d867689f8822f29e1f34bf983ffc9e 0 1604468723336 1 connected
2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379@16379 master - 0 1604468721000 7 connected 5461-10922
3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379@16379 master - 0 1604468722000 0 connected
c2dea8329847238f3f450500153ecef1eefb1dae 172.10.1.2:6379@16379 slave 2d67bd9cb3952bec942f468fe9214afdcf4419f4 0 1604468722331 7 connected
6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379@16379 myself,master - 0 1604468721000 1 connected 0-5460
172.10.1.1:6379>

新增的节点 ID 是 3b0dd4954a4c5841fca000b7737129182442f07a,该节点还未分配 slots(哈希槽),所以暂时无法存储数据。

给新节点分配哈希槽,执行指令 redis-cli --cluster reshard 172.10.1.1:6379 输出如下(注意看中文注释):

root@44c4b584f4f0:/data# redis-cli --cluster reshard 172.10.1.1:6379
>>> Performing Cluster Check (using node 172.10.1.1:6379)
M: 3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379
   slots: (0 slots) master
S: d0afdd6513bd8be9382759dfa635f1566f26ca99 172.10.1.4:6379
   slots: (0 slots) slave
   replicates 775b9f8ae557c4a7543680a25c46910d37a62b2d
M: 6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: c2dea8329847238f3f450500153ecef1eefb1dae 172.10.1.2:6379
   slots: (0 slots) slave
   replicates 2d67bd9cb3952bec942f468fe9214afdcf4419f4
S: 1c79fb6e06b78d1a629dbfc0d64e2faf9ac33856 172.10.1.5:6379
   slots: (0 slots) slave
   replicates 6728a6eb85d867689f8822f29e1f34bf983ffc9e
M: 2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379
   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.
## 询问一:你希望移动多少个哈希槽到新节点?我们输入4000
How many slots do you want to move (from 1 to 16384)? 4000
## 询问二:哪个节点用于接收移动的哈希槽?我们输入新节点的ID
What is the receiving node ID? 3b0dd4954a4c5841fca000b7737129182442f07a
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.
## 输入all表示从所有现存节点平均抽取哈希槽
Source node #1: all

Ready to move 4000 slots.
  Source nodes:
    M: 6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    M: 775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    M: 2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
  Destination node:
    M: 3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379
       slots: (0 slots) master
  Resharding plan:
    Moving slot 5461 from 2d67bd9cb3952bec942f468fe9214afdcf4419f4
    Moving slot 5462 from 2d67bd9cb3952bec942f468fe9214afdcf4419f4
    ...
    Moving slot 12255 from 775b9f8ae557c4a7543680a25c46910d37a62b2d
## 询问三:您是否要继续执行建议的分片计划?输入yes
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 5461 from 172.10.1.6:6379 to 172.10.1.7:6379:
Moving slot 5462 from 172.10.1.6:6379 to 172.10.1.7:6379:
...
Moving slot 12255 from 172.10.1.3:6379 to 172.10.1.7:6379:

执行完分片计划后,重新查看节点信息:

$ redis-cli -c -h 172.10.1.1
172.10.1.1:6379> cluster nodes
d0afdd6513bd8be9382759dfa635f1566f26ca99 172.10.1.4:6379@16379 slave 775b9f8ae557c4a7543680a25c46910d37a62b2d 0 1604471336255 3 connected
775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379@16379 master - 0 1604471334000 3 connected 12256-16383
1c79fb6e06b78d1a629dbfc0d64e2faf9ac33856 172.10.1.5:6379@16379 slave 6728a6eb85d867689f8822f29e1f34bf983ffc9e 0 1604471335000 1 connected
2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379@16379 master - 0 1604471336000 7 connected 6795-10922
3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379@16379 master - 0 1604471337257 8 connected 0-1332 5461-6794 10923-12255
c2dea8329847238f3f450500153ecef1eefb1dae 172.10.1.2:6379@16379 slave 2d67bd9cb3952bec942f468fe9214afdcf4419f4 0 1604471334000 7 connected
6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379@16379 myself,master - 0 1604471334000 1 connected 1333-5460
172.10.1.1:6379>

新节点的哈希槽区间为0-1332、5461-6794、10923-12255,分别抽取自节点1、节点6、节点3。

现在,实例7作为主节点已经可以提供服务了,但是为了增强集群的可用性,我们还需要给实例7增加1个从节点,也就是把实例8作为实例7的从节点。

增加从节点的指令格式为 redis-cli --cluster add-node <新增从节点ip:port> <现存节点ip:port> --cluster-slave --cluster-master-id <主节点ID>。其中,<新增从节点ip:port> 填 172.10.1.8:6379现存节点ip:port 可以取当前集群中任一节点的 IP 和端口号,如 172.10.1.1:6379主节点ID 填实例7的节点 ID 3b0dd4954a4c5841fca000b7737129182442f07a

执行指令 redis-cli --cluster add-node 172.10.1.8:6379 172.10.1.1:6379 --cluster-slave --cluster-master-id 3b0dd4954a4c5841fca000b7737129182442f07a 输出如下:

$ redis-cli --cluster add-node 172.10.1.8:6379 172.10.1.1:6379 --cluster-slave --cluster-master-id 3b0dd4954a4c5841fca000b7737129182442f07a
>>> Adding node 172.10.1.8:6379 to cluster 172.10.1.1:6379
>>> Performing Cluster Check (using node 172.10.1.1:6379)
M: 6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379
   slots:[1333-5460] (4128 slots) master
   1 additional replica(s)
S: d0afdd6513bd8be9382759dfa635f1566f26ca99 172.10.1.4:6379
   slots: (0 slots) slave
   replicates 775b9f8ae557c4a7543680a25c46910d37a62b2d
M: 775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379
   slots:[12256-16383] (4128 slots) master
   1 additional replica(s)
S: 1c79fb6e06b78d1a629dbfc0d64e2faf9ac33856 172.10.1.5:6379
   slots: (0 slots) slave
   replicates 6728a6eb85d867689f8822f29e1f34bf983ffc9e
M: 2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379
   slots:[6795-10922] (4128 slots) master
   1 additional replica(s)
M: 3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379
   slots:[0-1332],[5461-6794],[10923-12255] (4000 slots) master
S: c2dea8329847238f3f450500153ecef1eefb1dae 172.10.1.2:6379
   slots: (0 slots) slave
   replicates 2d67bd9cb3952bec942f468fe9214afdcf4419f4
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.10.1.8:6379 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 172.10.1.7:6379.
[OK] New node added correctly.

重新查看节点信息:

$ redis-cli -c -h 172.10.1.1
172.10.1.1:6379> cluster nodes
d0afdd6513bd8be9382759dfa635f1566f26ca99 172.10.1.4:6379@16379 slave 775b9f8ae557c4a7543680a25c46910d37a62b2d 0 1604472654000 3 connected
775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379@16379 master - 0 1604472656000 3 connected 12256-16383
1c79fb6e06b78d1a629dbfc0d64e2faf9ac33856 172.10.1.5:6379@16379 slave 6728a6eb85d867689f8822f29e1f34bf983ffc9e 0 1604472655042 1 connected
2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379@16379 master - 0 1604472658049 7 connected 6795-10922
3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379@16379 master - 0 1604472656045 8 connected 0-1332 5461-6794 10923-12255
c2dea8329847238f3f450500153ecef1eefb1dae 172.10.1.2:6379@16379 slave 2d67bd9cb3952bec942f468fe9214afdcf4419f4 0 1604472654036 7 connected
6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379@16379 myself,master - 0 1604472651000 1 connected 1333-5460
43d539d12d2b1dde5f41718bd2fe2c8144f9d73b 172.10.1.8:6379@16379 slave 3b0dd4954a4c5841fca000b7737129182442f07a 0 1604472657048 8 connected
172.10.1.1:6379>

至此,集群添加主从新节点的操作已经完成。

删除集群节点,指令格式为 redis-cli --cluster del-node <任一节点ip:port> <被删除节点ID>

删除从节点8,并查看删除后的集群节点信息:

$ redis-cli --cluster del-node 172.10.1.1:6379 43d539d12d2b1dde5f41718bd2fe2c8144f9d73b
>>> Removing node 43d539d12d2b1dde5f41718bd2fe2c8144f9d73b from cluster 172.10.1.1:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
$ redis-cli -c -h 172.10.1.1
172.10.1.1:6379> cluster nodes
1c79fb6e06b78d1a629dbfc0d64e2faf9ac33856 172.10.1.5:6379@16379 slave 6728a6eb85d867689f8822f29e1f34bf983ffc9e 0 1604475368714 1 connected
d0afdd6513bd8be9382759dfa635f1566f26ca99 172.10.1.4:6379@16379 slave 775b9f8ae557c4a7543680a25c46910d37a62b2d 0 1604475369000 3 connected
775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379@16379 master - 0 1604475369717 3 connected 12256-16383
c2dea8329847238f3f450500153ecef1eefb1dae 172.10.1.2:6379@16379 slave 2d67bd9cb3952bec942f468fe9214afdcf4419f4 0 1604475367000 7 connected
2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379@16379 master - 0 1604475370722 7 connected 6795-10922
3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379@16379 master - 0 1604475371726 8 connected 0-1332 5461-6794 10923-12255
6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379@16379 myself,master - 0 1604475367000 1 connected 1333-5460
172.10.1.1:6379>

节点8已经从集群中移除。

删除主节点7,就没那么容易了。必须先将节点7上的哈希槽移动到其他主节点上,才可以删除它。

前面给节点7分配哈希槽时,我们已知,节点7的哈希槽0-1332号抽取自节点1,哈希槽5461-6794号((1334个))抽取自节点6,哈希槽10923-12255号(1333个)抽取自节点3,现在我们将这些哈希槽从节点7移回到原来的节点下(想偷懒的话就把所有哈希槽单独移到一个节点)。

将哈希槽0-1332号(1333个)移回节点1(注意看中文注释):

$ redis-cli --cluster reshard 172.10.1.1:6379
>>> Performing Cluster Check (using node 172.10.1.1:6379)
M: 6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379
   slots:[1333-5460] (4128 slots) master
   1 additional replica(s)
S: 1c79fb6e06b78d1a629dbfc0d64e2faf9ac33856 172.10.1.5:6379
   slots: (0 slots) slave
   replicates 6728a6eb85d867689f8822f29e1f34bf983ffc9e
S: d0afdd6513bd8be9382759dfa635f1566f26ca99 172.10.1.4:6379
   slots: (0 slots) slave
   replicates 775b9f8ae557c4a7543680a25c46910d37a62b2d
M: 775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379
   slots:[12256-16383] (4128 slots) master
   1 additional replica(s)
S: c2dea8329847238f3f450500153ecef1eefb1dae 172.10.1.2:6379
   slots: (0 slots) slave
   replicates 2d67bd9cb3952bec942f468fe9214afdcf4419f4
M: 2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379
   slots:[6795-10922] (4128 slots) master
   1 additional replica(s)
M: 3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379
   slots:[0-1332],[5461-6794],[10923-12255] (4000 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
## 你希望移动多少个哈希槽到新节点?我们输入1333
How many slots do you want to move (from 1 to 16384)? 1333
## 哪个节点用于接收移动的哈希槽?我们输入节点1的ID
What is the receiving node ID? 6728a6eb85d867689f8822f29e1f34bf983ffc9e
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.
## 输入哈希槽源节点7的ID
Source node #1: 3b0dd4954a4c5841fca000b7737129182442f07a
## 键入“done”完成输入
Source node #2: done

Ready to move 1333 slots.
  Source nodes:
    M: 3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379
       slots:[0-1332],[5461-6794],[10923-12255] (4000 slots) master
  Destination node:
    M: 6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379
       slots:[1333-5460] (4128 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 0 from 3b0dd4954a4c5841fca000b7737129182442f07a
    Moving slot 1 from 3b0dd4954a4c5841fca000b7737129182442f07a
    ...
    Moving slot 1332 from 3b0dd4954a4c5841fca000b7737129182442f07a
## 输入“yes”确认执行分片计划
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 0 from 172.10.1.7:6379 to 172.10.1.1:6379:
Moving slot 1 from 172.10.1.7:6379 to 172.10.1.1:6379:
...
Moving slot 1332 from 172.10.1.7:6379 to 172.10.1.1:6379:

将哈希槽5461-6794号(1334个)移回节点6(注意看中文注释):

$ redis-cli --cluster reshard 172.10.1.1:6379
>>> Performing Cluster Check (using node 172.10.1.1:6379)
M: 6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 1c79fb6e06b78d1a629dbfc0d64e2faf9ac33856 172.10.1.5:6379
   slots: (0 slots) slave
   replicates 6728a6eb85d867689f8822f29e1f34bf983ffc9e
S: d0afdd6513bd8be9382759dfa635f1566f26ca99 172.10.1.4:6379
   slots: (0 slots) slave
   replicates 775b9f8ae557c4a7543680a25c46910d37a62b2d
M: 775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379
   slots:[12256-16383] (4128 slots) master
   1 additional replica(s)
S: c2dea8329847238f3f450500153ecef1eefb1dae 172.10.1.2:6379
   slots: (0 slots) slave
   replicates 2d67bd9cb3952bec942f468fe9214afdcf4419f4
M: 2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379
   slots:[6795-10922] (4128 slots) master
   1 additional replica(s)
M: 3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379
   slots:[5461-6794],[10923-12255] (2667 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
## 你希望移动多少个哈希槽到新节点?我们输入1334
How many slots do you want to move (from 1 to 16384)? 1334
## 哪个节点用于接收移动的哈希槽?我们输入节点6的ID
What is the receiving node ID? 2d67bd9cb3952bec942f468fe9214afdcf4419f4
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.
## 输入哈希槽源节点7的ID
Source node #1: 3b0dd4954a4c5841fca000b7737129182442f07a
## 键入“done”完成输入
Source node #2: done

Ready to move 1334 slots.
  Source nodes:
    M: 3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379
       slots:[5461-6794],[10923-12255] (2667 slots) master
  Destination node:
    M: 2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379
       slots:[6795-10922] (4128 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 5461 from 3b0dd4954a4c5841fca000b7737129182442f07a
    Moving slot 5462 from 3b0dd4954a4c5841fca000b7737129182442f07a
    ...
    Moving slot 6794 from 3b0dd4954a4c5841fca000b7737129182442f07a
## 输入“yes”确认执行分片计划
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 5461 from 172.10.1.7:6379 to 172.10.1.6:6379:
Moving slot 5462 from 172.10.1.7:6379 to 172.10.1.6:6379:
...
Moving slot 6794 from 172.10.1.7:6379 to 172.10.1.6:6379:

将哈希槽10923-12255号(1333个)移回节点3(注意看中文注释):

# redis-cli --cluster reshard 172.10.1.1:6379
>>> Performing Cluster Check (using node 172.10.1.1:6379)
M: 6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 1c79fb6e06b78d1a629dbfc0d64e2faf9ac33856 172.10.1.5:6379
   slots: (0 slots) slave
   replicates 6728a6eb85d867689f8822f29e1f34bf983ffc9e
S: d0afdd6513bd8be9382759dfa635f1566f26ca99 172.10.1.4:6379
   slots: (0 slots) slave
   replicates 775b9f8ae557c4a7543680a25c46910d37a62b2d
M: 775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379
   slots:[12256-16383] (4128 slots) master
   1 additional replica(s)
S: c2dea8329847238f3f450500153ecef1eefb1dae 172.10.1.2:6379
   slots: (0 slots) slave
   replicates 2d67bd9cb3952bec942f468fe9214afdcf4419f4
M: 2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379
   slots:[10923-12255] (1333 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
## 你希望移动多少个哈希槽到新节点?我们输入1333
How many slots do you want to move (from 1 to 16384)? 1333
## 哪个节点用于接收移动的哈希槽?我们输入节点3的ID
What is the receiving node ID? 775b9f8ae557c4a7543680a25c46910d37a62b2d
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.
## 输入哈希槽源节点7的ID
Source node #1: 3b0dd4954a4c5841fca000b7737129182442f07a
## 键入“done”完成输入
Source node #2: done

Ready to move 1333 slots.
  Source nodes:
    M: 3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379
       slots:[10923-12255] (1333 slots) master
  Destination node:
    M: 775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379
       slots:[12256-16383] (4128 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 10923 from 3b0dd4954a4c5841fca000b7737129182442f07a
    Moving slot 10924 from 3b0dd4954a4c5841fca000b7737129182442f07a
    ...
    Moving slot 12255 from 3b0dd4954a4c5841fca000b7737129182442f07a
## 输入“yes”确认执行分片计划
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 10923 from 172.10.1.7:6379 to 172.10.1.3:6379:
Moving slot 10924 from 172.10.1.7:6379 to 172.10.1.3:6379:
...
Moving slot 12255 from 172.10.1.7:6379 to 172.10.1.3:6379:

查看集群节点信息:

$ redis-cli -c -h 172.10.1.1
172.10.1.1:6379> cluster nodes
1c79fb6e06b78d1a629dbfc0d64e2faf9ac33856 172.10.1.5:6379@16379 slave 6728a6eb85d867689f8822f29e1f34bf983ffc9e 0 1604481329000 9 connected
d0afdd6513bd8be9382759dfa635f1566f26ca99 172.10.1.4:6379@16379 slave 775b9f8ae557c4a7543680a25c46910d37a62b2d 0 1604481329168 11 connected
775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379@16379 master - 0 1604481330173 11 connected 10923-16383
c2dea8329847238f3f450500153ecef1eefb1dae 172.10.1.2:6379@16379 slave 2d67bd9cb3952bec942f468fe9214afdcf4419f4 0 1604481331177 10 connected
2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379@16379 master - 0 1604481328165 10 connected 5461-10922
3b0dd4954a4c5841fca000b7737129182442f07a 172.10.1.7:6379@16379 master - 0 1604481327165 8 connected
6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379@16379 myself,master - 0 1604481329000 9 connected 0-5460
172.10.1.1:6379>

至此,节点7的所有哈希槽已经移走完毕,我们可以将节点7从集群中移除了:

$ redis-cli --cluster del-node 172.10.1.1:6379 3b0dd4954a4c5841fca000b7737129182442f07a
>>> Removing node 3b0dd4954a4c5841fca000b7737129182442f07a from cluster 172.10.1.1:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
$ redis-cli -c -h 172.10.1.1
172.10.1.1:6379> cluster nodes
1c79fb6e06b78d1a629dbfc0d64e2faf9ac33856 172.10.1.5:6379@16379 slave 6728a6eb85d867689f8822f29e1f34bf983ffc9e 0 1604481501000 9 connected
d0afdd6513bd8be9382759dfa635f1566f26ca99 172.10.1.4:6379@16379 slave 775b9f8ae557c4a7543680a25c46910d37a62b2d 0 1604481499000 11 connected
775b9f8ae557c4a7543680a25c46910d37a62b2d 172.10.1.3:6379@16379 master - 0 1604481500000 11 connected 10923-16383
c2dea8329847238f3f450500153ecef1eefb1dae 172.10.1.2:6379@16379 slave 2d67bd9cb3952bec942f468fe9214afdcf4419f4 0 1604481499734 10 connected
2d67bd9cb3952bec942f468fe9214afdcf4419f4 172.10.1.6:6379@16379 master - 0 1604481501743 10 connected 5461-10922
6728a6eb85d867689f8822f29e1f34bf983ffc9e 172.10.1.1:6379@16379 myself,master - 0 1604481498000 9 connected 0-5460
172.10.1.1:6379>

本文参考了:https://blog.csdn.net/HANLIPENGHANLIPENG/article/details/78951861
Redis官网集群教程:https://redis.io/topics/cluster-tutorial