redis4.x内置集群扩容与缩容

目录

目录

1 redis集群的缩容

1.1 删除一个主节点

1.2 删除一个从节点

2 redis集群的扩容

2.1 添加一个主节点

2.2 添加一个从节点


1 redis集群的缩容

前面介绍到redis的内置集群方式,三主三从的搭建。现在我们看一下redis集群的扩容和缩容。

1.1 删除一个主节点

 因为主节点含有槽数,所以首先要把节点中的哈希槽转移到其他节点中,先查看集群nodes的信息

[root@wyl01 redis]# redis-cli  -h 192.168.52.128 -p 7000 -c
192.168.52.128:7000> CLUSTER nodes
c7bc97babd7cf248eeff0434d2c9e0321042ecbd 192.168.52.128:7001@17001 slave e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 0 1562556408000 7 connected
4d7ad9aded90ecfbf5caebf588d57354a3a73794 192.168.52.129:7001@17001 slave e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 0 1562556409091 7 connected
b0584ed82033ee4112cee98e4bb5f9b4a8a2095f 192.168.52.130:7001@17001 slave 7bd6fe68bf877f65137ad37de7a0d4b39fd0cc2f 0 1562556409000 6 connected
7bd6fe68bf877f65137ad37de7a0d4b39fd0cc2f 192.168.52.129:7000@17000 master - 0 1562556407782 2 connected 5461-10922
075fa701c9d872a5a822858630eff6482a467ead 192.168.52.130:7000@17000 master - 0 1562556409797 3 connected
e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 192.168.52.128:7000@17000 myself,master - 0 1562556408000 7 connected 0-5460 10923-16383

移动solts到其他的节点上,命令如下

[root@wyl01 redis]# ./bin/redis-trib.rb  reshard 192.168.52.128:7000
>>> Performing Cluster Check (using node 192.168.52.128:7000)
M: e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 192.168.52.128:7000
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: c7bc97babd7cf248eeff0434d2c9e0321042ecbd 192.168.52.128:7001
   slots: (0 slots) slave
   replicates e31c8d7b51f23e6bb1119dcea1c9e61087efbe28
S: 4d7ad9aded90ecfbf5caebf588d57354a3a73794 192.168.52.129:7001
   slots: (0 slots) slave
   replicates d16673d5ea9e355112cdece40aa0f2283ccf1b52
M: d16673d5ea9e355112cdece40aa0f2283ccf1b52 192.168.52.130:7000
   slots:0-5460 (5461 slots) master
   2 additional replica(s)
S: 4fffa4368ff79be384511a9d5f0e6cdbc7551818 192.168.52.130:7001
   slots: (0 slots) slave
   replicates d16673d5ea9e355112cdece40aa0f2283ccf1b52
M: 7bd6fe68bf877f65137ad37de7a0d4b39fd0cc2f 192.168.52.129:7000
   slots:5461-10922 (5462 slots) master
   0 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)? 5461         # 需要转移槽的数量
What is the receiving node ID? e31c8d7b51f23e6bb1119dcea1c9e61087efbe28  # 接收这些槽的master节点node 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:7bd6fe68bf877f65137ad37de7a0d4b39fd0cc2f    # 转移的master节点node id
Source node #2:done   # 输入完毕, 开始转移

转移成功后,我们就可以删除该节点,命令如下: 

# ip:port是集群中任一主节点,后面的node_id为要删除主节点的ID,也就是130机器端口为7000的redis实例
[root@wyl01 redis]# ./bin/redis-trib.rb del-node 192.168.52.128:7000 075fa701c9d872a5a822858630eff6482a467ead
>>> Removing node 075fa701c9d872a5a822858630eff6482a467ead from cluster 192.168.52.128:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

[root@wyl01 redis]# redis-cli  -h 192.168.52.128 -p 7000 -c
# 看到只剩下5个节点了
192.168.52.128:7000> CLUSTER nodes
c7bc97babd7cf248eeff0434d2c9e0321042ecbd 192.168.52.128:7001@17001 slave e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 0 1562556462197 7 connected
4d7ad9aded90ecfbf5caebf588d57354a3a73794 192.168.52.129:7001@17001 slave e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 0 1562556462000 7 connected
b0584ed82033ee4112cee98e4bb5f9b4a8a2095f 192.168.52.130:7001@17001 slave 7bd6fe68bf877f65137ad37de7a0d4b39fd0cc2f 0 1562556461594 6 connected
7bd6fe68bf877f65137ad37de7a0d4b39fd0cc2f 192.168.52.129:7000@17000 master - 0 1562556463206 2 connected 5461-10922
e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 192.168.52.128:7000@17000 myself,master - 0 1562556461000 7 connected 0-5460 10923-16383
192.168.52.128:7000> 

1.2 删除一个从节点

因为从节点上没有solts,所以可以直接将其删除,指令如下

[root@wyl01 redis]# redis-cli  -h 192.168.52.128 -p 7000 -c
192.168.52.128:7000> CLUSTER nodes
c7bc97babd7cf248eeff0434d2c9e0321042ecbd 192.168.52.128:7001@17001 slave e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 0 1562556462197 7 connected
4d7ad9aded90ecfbf5caebf588d57354a3a73794 192.168.52.129:7001@17001 slave e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 0 1562556462000 7 connected
b0584ed82033ee4112cee98e4bb5f9b4a8a2095f 192.168.52.130:7001@17001 slave 7bd6fe68bf877f65137ad37de7a0d4b39fd0cc2f 0 1562556461594 6 connected
7bd6fe68bf877f65137ad37de7a0d4b39fd0cc2f 192.168.52.129:7000@17000 master - 0 1562556463206 2 connected 5461-10922
e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 192.168.52.128:7000@17000 myself,master - 0 1562556461000 7 connected 0-5460 10923-16383
192.168.52.128:7000> 
# 删除一个从节点,从节点不需要转移solts,直接删除
[root@wyl01 redis]# ./bin/redis-trib.rb del-node 192.168.52.128:7000 b0584ed82033ee4112cee98e4bb5f9b4a8a2095f
>>> Removing node b0584ed82033ee4112cee98e4bb5f9b4a8a2095f from cluster 192.168.52.128:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

2 redis集群的扩容

2.1 添加一个主节点

刚刚上面我们把130上的主从节点实例都给删除了,所以现在集群情况如下:

# 只有4个节点
[root@wyl01 redis]# redis-cli  -h 192.168.52.128 -p 7000 -c
192.168.52.128:7000> CLUSTER nodes
c7bc97babd7cf248eeff0434d2c9e0321042ecbd 192.168.52.128:7001@17001 slave e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 0 1562559527306 7 connected
4d7ad9aded90ecfbf5caebf588d57354a3a73794 192.168.52.129:7001@17001 slave e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 0 1562559528314 7 connected
7bd6fe68bf877f65137ad37de7a0d4b39fd0cc2f 192.168.52.129:7000@17000 master - 0 1562559528000 2 connected 5461-10922
e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 192.168.52.128:7000@17000 myself,master - 0 1562559527000 7 connected 0-5460 10923-16383

到130机器上,启动一个redis实例,相当于添加一台服务器,然后启动一个实例 ,add-node指令加入到集群中,如下所示

备注:192.168.52.130:7000 是新的主节点 , 192.168.52.128:7000 是原存在的任一主节点
[root@wyl01 redis]# ./bin/redis-trib.rb add-node 192.168.52.130:7000  192.168.52.128:7000
>>> Adding node 192.168.52.130:7000 to cluster 192.168.52.128:7000
>>> Performing Cluster Check (using node 192.168.52.128:7000)
M: e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 192.168.52.128:7000
   slots:0-5460,10923-16383 (10922 slots) master
   2 additional replica(s)
S: c7bc97babd7cf248eeff0434d2c9e0321042ecbd 192.168.52.128:7001
   slots: (0 slots) slave
   replicates e31c8d7b51f23e6bb1119dcea1c9e61087efbe28
S: 4d7ad9aded90ecfbf5caebf588d57354a3a73794 192.168.52.129:7001
   slots: (0 slots) slave
   replicates e31c8d7b51f23e6bb1119dcea1c9e61087efbe28
M: 7bd6fe68bf877f65137ad37de7a0d4b39fd0cc2f 192.168.52.129:7000
   slots:5461-10922 (5462 slots) master
   0 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.52.130:7000 to make it join the cluster.
[OK] New node added correctly.

加入后可以看到集群节点情况,有新的节点加入了

新节点目前还没有分配solts,还是不可用的,下面开始分配槽数

[root@wyl01 redis]# ./bin/redis-trib.rb  reshard 192.168.52.128:7000
>>> Performing Cluster Check (using node 192.168.52.128:7000)
M: e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 192.168.52.128:7000
   slots:0-5460,10923-16383 (10922 slots) master
   2 additional replica(s)
S: c7bc97babd7cf248eeff0434d2c9e0321042ecbd 192.168.52.128:7001
   slots: (0 slots) slave
   replicates e31c8d7b51f23e6bb1119dcea1c9e61087efbe28
S: 4d7ad9aded90ecfbf5caebf588d57354a3a73794 192.168.52.129:7001
   slots: (0 slots) slave
   replicates e31c8d7b51f23e6bb1119dcea1c9e61087efbe28
M: d16673d5ea9e355112cdece40aa0f2283ccf1b52 192.168.52.130:7000
   slots: (0 slots) master
   0 additional replica(s)
M: 7bd6fe68bf877f65137ad37de7a0d4b39fd0cc2f 192.168.52.129:7000
   slots:5461-10922 (5462 slots) master
   0 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)? 5461            
What is the receiving node ID? d16673d5ea9e355112cdece40aa0f2283ccf1b52
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:e31c8d7b51f23e6bb1119dcea1c9e61087efbe28
Source node #2:done
    ....
    ....
    Moving slot 5459 from e31c8d7b51f23e6bb1119dcea1c9e61087efbe28
    Moving slot 5460 from e31c8d7b51f23e6bb1119dcea1c9e61087efbe28
Do you want to proceed with the proposed reshard plan (yes/no)? yes

 看到上面的提示输入yes,redis集群就开始分配哈希槽了,然后一个新的主节点就添加完成了,执行命令查看现在的集群中节点的状态,可看到已分配。

2.2 添加一个从节点

对于一个从节点的添加,由于不用分配solts,所以当把从实例启动后,就可以加入到集群中,操作如下:

[root@wyl01 redis]# ./bin/redis-trib.rb add-node --slave --master-id d16673d5ea9e355112cdece40aa0f2283ccf1b52 192.168.52.130:7001 192.168.52.128:7000
>>> Adding node 192.168.52.130:7001 to cluster 192.168.52.128:7000
>>> Performing Cluster Check (using node 192.168.52.128:7000)
M: e31c8d7b51f23e6bb1119dcea1c9e61087efbe28 192.168.52.128:7000
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: c7bc97babd7cf248eeff0434d2c9e0321042ecbd 192.168.52.128:7001
   slots: (0 slots) slave
   replicates e31c8d7b51f23e6bb1119dcea1c9e61087efbe28
S: 4d7ad9aded90ecfbf5caebf588d57354a3a73794 192.168.52.129:7001
   slots: (0 slots) slave
   replicates d16673d5ea9e355112cdece40aa0f2283ccf1b52
M: d16673d5ea9e355112cdece40aa0f2283ccf1b52 192.168.52.130:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 7bd6fe68bf877f65137ad37de7a0d4b39fd0cc2f 192.168.52.129:7000
   slots:5461-10922 (5462 slots) master
   0 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.52.130:7001 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.52.130:7000.
[OK] New node added correctly.

注释:

  •        --slave 表示添加的是从节点
  •        --master-id d16673d5ea9e355112cdece40aa0f2283ccf1b52 是上面加入的130机器端口为7000的主节点的node id
  •       192.168.52.130:7001 是新加的从节点
  •       192.168.52.128:7000 集群任一个旧节点即可  

可以看到从节点添加成功。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值