Redis集群管理(二)

3> 添加slave节点

下面为201:6382 master节点添加一个slave节点201:6383

shell> redis-trib.rb add-node --slave --master-id 901cade1bc2703ef2bb4ff49466b38762346755d 192.168.0.201:6383 192.168.0.201:6379

参数说明:
1> –slave:表示添加从节点
2> –master-id:master节点ID,这里用上边新添加的主节点ID(201:6382)
3> 192.168.0.201:6383:新slave节点的IP和端口
4> 192.168.0.201:6379:集群中任意旧节点,只要能连上就可以。这里以201:6379为例

添加slave节点成功后集群中的节点状态:

[root@localhost redis-3.2.0]# redis-cli cluster nodes
901cade1bc2703ef2bb4ff49466b38762346755d 192.168.0.201:6382 master - 0 1468333842665 9 connected 0-332 5461-5794 10923-11255
459dbcccc1e5be021f19efde794b9e84474c0c02 192.168.0.202:16379 slave b6814c717080de33907e6f769e1fd8dc4fe53c38 0 1468333841661 7 connected
9080c5049615730c66731b54cb084999009eeb52 192.168.0.201:6383 slave 901cade1bc2703ef2bb4ff49466b38762346755d 0 1468333843669 9 connected
06023f8283389ad5580c782797f49a5996c108b3 192.168.0.201:6379 myself,master - 0 0 1 connected 333-5460
91179b96d9b8abce85fa7a5748aa9b4114f0b260 192.168.0.202:16380 slave 06023f8283389ad5580c782797f49a5996c108b3 0 1468333844674 5 connected
ef8a49e5242c791551f1b317d5039144e38acde6 192.168.0.201:6380 slave 13567f6f8787d3aa3f4ef3abd15c2cc500a1469b 0 1468333842665 8 connected
b6814c717080de33907e6f769e1fd8dc4fe53c38 192.168.0.201:6381 master - 0 1468333845678 7 connected 5795-10922
13567f6f8787d3aa3f4ef3abd15c2cc500a1469b 192.168.0.202:16381 master - 0 1468333846685 8 connected 11256-16383

very good~ 可以看出,201:6383以成功添加为一个slave节点,并将201:6382作为master节点。

cluster nodes命令列表的节点状态信息说明:

459dbcccc1e5be021f19efde794b9e84474c0c02 192.168.0.202:16379 slave b6814c717080de33907e6f769e1fd8dc4fe53c38 0 1468333841661 7 connected

1> 459dbcccc1e5be021f19efde794b9e84474c0c02 :节点ID
2> 192.168.0.202:16379:节点IP和端口
3> slave:节点类型,master、slave或myself(master/slave)
4> b6814c717080de33907e6f769e1fd8dc4fe53c38 :master节点ID,如果自己是master节点,这一列的值为0
5> 0:集群最近一次向节点发送 PING 命令之后, 过去了多长时间还没接到回复。
6> 1468333841661:节点最近一次返回 PONG 回复的时间
7> 7:节点的配置纪元
8> connected:节点网络连接状态
9> 如果是主节点,节点状态后边显示slot分配的范围

3、改变slave节点的master

将上边新加的slave节点(201:6383)的master,修改为201:6379(06023f8283389ad5580c782797f49a5996c108b3)

shell> redis-cli -h 192.168.0.201 -p 6383 cluster nodes | grep myself
9080c5049615730c66731b54cb084999009eeb52 192.168.0.201:6383 myself,slave 901cade1bc2703ef2bb4ff49466b38762346755d 0 0 0 connected

当前slave节点201:6383的master节点是901cade1bc2703ef2bb4ff49466b38762346755d

shell> redis-cli -h 192.168.0.201 -p 6383
192.168.0.201:6383> cluster replicate 06023f8283389ad5580c782797f49a5996c108b3

06023f8283389ad5580c782797f49a5996c108b3:新master节点的ID

192.168.0.201:6383> CLUSTER SLAVES 06023f8283389ad5580c782797f49a5996c108b3
1) "91179b96d9b8abce85fa7a5748aa9b4114f0b260 192.168.0.202:16380 slave 06023f8283389ad5580c782797f49a5996c108b3 0 1468336124600 1 connected"
2) "9080c5049615730c66731b54cb084999009eeb52 192.168.0.201:6383 myself,slave 06023f8283389ad5580c782797f49a5996c108b3 0 0 0 connected"

可以看出201:6383 slave节点已经是201:6379 master节点的子节点了。

4、删除节点

1> 删除slave节点

[root@localhost redis-3.2.0]# redis-trib.rb del-node 192.168.0.201:6383 9080c5049615730c66731b54cb084999009eeb52
>>> Removing node 9080c5049615730c66731b54cb084999009eeb52 from cluster 192.168.0.201:6383
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

参数说明:
1> del-node:删除节点参数
2> 192.168.0.201:6383:节点IP和端口
3> 9080c5049615730c66731b54cb084999009eeb52:节点ID
节点成功删除后,会同时将进程杀死

从节点重新恢复后,先cluster meet ip port将从节点加入集群,然后设置自己的mastercluster replication master_node_id

2、删除master节点

注意:
1> 如果master节点有slot,必须先将所有slot分配给其它master节点。否则会提示你先将slot移除再尝试删除,如下所示:

shell> redis-trib.rb del-node 192.168.0.201:6382 901cade1bc2703ef2bb4ff49466b38762346755d
>>> Removing node 901cade1bc2703ef2bb4ff49466b38762346755d from cluster 192.168.0.201:6382
[ERR] Node 192.168.0.201:6382 is not empty! Reshard data away and try again.

2> 如果master节点有slave节点,先将slave节点移到其它master下

  • 移除201:6382 master节点的slot
  • [root@localhost redis-3.2.0]# redis-trib.rb reshard 192.168.0.201:6382
    >>> Performing Cluster Check (using node 192.168.0.201:6382)
    M: 901cade1bc2703ef2bb4ff49466b38762346755d 192.168.0.201:6382
       slots:0-332,5461-5794,10923-11255 (1000 slots) master
       1 additional replica(s)
    M: 06023f8283389ad5580c782797f49a5996c108b3 192.168.0.201:6379
       slots:333-5460 (5128 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)? 1000 # 201:6382一共1000个slot
    What is the receiving node ID? 06023f8283389ad5580c782797f49a5996c108b3 # 接收这1000个slot的节点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:901cade1bc2703ef2bb4ff49466b38762346755d # 移除这1000个slot的master节点ID(这里同样是201:6382)
    Source node #2:done
    ....
    Do you want to proceed with the proposed reshard plan (yes/no)? yes
  • 新增master节点后,也进行了这一步操作,当时是分配,现在是去掉,反着的。

    • 删除master节点
    • shell> redis-trib.rb del-node 192.168.0.201:6382 901cade1bc2703ef2bb4ff49466b38762346755d
      >>> Removing node 901cade1bc2703ef2bb4ff49466b38762346755dfrom cluster 192.168.0.201:6382
      >>> Sending CLUSTER FORGET messages to the cluster...
      >>> SHUTDOWN the node.g
    • 此时新的master节点也被删除了,回到了添加节点之前的状态。

文章链接: https://www.mfisp.com/13634.html

文章标题:Redis集群管理(二)

文章版权:梦飞科技所发布的内容,部分为原创文章,转载请注明来源,网络转载文章如有侵权请联系我们!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
建站教程投稿分享

Redis集群管理(一)

2022-12-2 0:19:36

建站教程投稿分享

Redis集群技术架构

2022-12-2 0:26:53

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索

梦飞科技 - 最新云主机促销服务器租用优惠