RedisCluster 添加/删除节点

一,redis cluster命令行

  1. //集群(cluster)  
  2. CLUSTER INFO 打印集群的信息  
  3. CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。   
  4.   
  5. //节点(node)  
  6. CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。  
  7. CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。  
  8. CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。  
  9. CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。   
  10.   
  11. //槽(slot)  
  12. CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。  
  13. CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。  
  14. CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。  
  15. CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。  
  16. CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。  
  17. CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。  
  18. CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。   
  19.   
  20. //键 (key)  
  21. CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。  
  22. CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。  
  23. CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。  

这些命令是集群所独有的。执行上述命令要先登录

  1. [root@manage redis]# redis-cli -c -p 6382 -h 192.168.10.220    //登录  
  2. 192.168.10.220:6382> cluster info   //查看集群情况  
  3. cluster_state:ok  
  4. cluster_slots_assigned:16384  
  5. cluster_slots_ok:16384  
  6. cluster_slots_pfail:0  
  7. cluster_slots_fail:0  
  8. cluster_known_nodes:6  
  9. cluster_size:3  
  10. cluster_current_epoch:8  
  11. cluster_my_epoch:4  
  12. cluster_stats_messages_sent:82753  
  13. cluster_stats_messages_received:82754  

二,添加节点

1,新配置二个测试节点

  1. # cd /etc/redis  
  2.   
  3. //新增配置  
  4. # cp redis-6379.conf redis-6378.conf && sed -i "s/6379/6378/g" redis-6378.conf  
  5. # cp redis-6382.conf redis-6385.conf && sed -i "s/6382/6385/g" redis-6385.conf  
  6.   
  7. //启动  
  8. # redis-server /etc/redis/redis-6385.conf > /var/log/redis/redis-6385.log 2>&1 &  
  9. # redis-server /etc/redis/redis-6378.conf > /var/log/redis/redis-6378.log 2>&1 &  

2,添加主节点

  1. # redis-trib.rb add-node 192.168.10.219:6378 192.168.10.219:6379  

注释:

192.168.10.219:6378是新增的节点

192.168.10.219:6379集群任一个旧节点

3,添加从节点

  1. # redis-trib.rb add-node --slave --master-id 03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.220:6385 192.168.10.219:6379  

注释:

--slave,表示添加的是从节点

--master-id 03ccad2ba5dd1e062464bc7590400441fafb63f2,主节点的node id,在这里是前面新添加的6378的node id

192.168.10.220:6385,新节点

192.168.10.219:6379集群任一个旧节点

4,重新分配slot

  1. # redis-trib.rb reshard 192.168.10.219:6378 //下面是主要过程  
  2.   
  3. How many slots do you want to move (from 1 to 16384)? 1000 //设置slot数1000  
  4. What is the receiving node ID? 03ccad2ba5dd1e062464bc7590400441fafb63f2 //新节点node id  
  5. Please enter all the source node IDs.  
  6.  Type 'all' to use all the nodes as source nodes for the hash slots.  
  7.  Type 'done' once you entered all the source nodes IDs.  
  8. Source node #1:all //表示全部节点重新洗牌  
  9. Do you want to proceed with the proposed reshard plan (yes/no)? yes //确认重新分  

新增加的主节点,是没有slots的,

M: 03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.219:6378
slots:0-332,5461-5794,10923-11255 (0 slots) master

主节点如果没有slots的话,存取数据就都不会被选中。

可以把分配的过程理解成打扑克牌,all表示大家重新洗牌;输入某个主节点的node id,然后在输入done的话,就好比从某个节点,抽牌。

5,查看一下,集群情况

  1. [root@slave2 redis]# redis-trib.rb check 192.168.10.219:6379  
  2. Connecting to node 192.168.10.219:6379: OK  
  3. Connecting to node 192.168.10.220:6385: OK  
  4. Connecting to node 192.168.10.219:6378: OK  
  5. Connecting to node 192.168.10.220:6382: OK  
  6. Connecting to node 192.168.10.220:6383: OK  
  7. Connecting to node 192.168.10.219:6380: OK  
  8. Connecting to node 192.168.10.219:6381: OK  
  9. Connecting to node 192.168.10.220:6384: OK  
  10. >>> Performing Cluster Check (using node 192.168.10.219:6379)  
  11. M: 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052 192.168.10.219:6379  
  12.  slots:5795-10922 (5128 slots) master  
  13.  1 additional replica(s)  
  14. S: 9c240333476469e8e2c8e80b089c48f389827265 192.168.10.220:6385  
  15.  slots: (0 slots) slave  
  16.  replicates 03ccad2ba5dd1e062464bc7590400441fafb63f2  
  17. M: 03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.219:6378  
  18.  slots:0-332,5461-5794,10923-11255 (1000 slots) master  
  19.  1 additional replica(s)  
  20. M: 19b042c17d2918fade18a4ad2efc75aa81fd2422 192.168.10.220:6382  
  21.  slots:333-5460 (5128 slots) master  
  22.  1 additional replica(s)  
  23. M: b2c50113db7bd685e316a16b423c9b8abc3ba0b7 192.168.10.220:6383  
  24.  slots:11256-16383 (5128 slots) master  
  25.  1 additional replica(s)  
  26. S: 6475e4c8b5e0c0ea27547ff7695d05e9af0c5ccb 192.168.10.219:6380  
  27.  slots: (0 slots) slave  
  28.  replicates 19b042c17d2918fade18a4ad2efc75aa81fd2422  
  29. S: 1ee01fe95bcfb688a50825d54248eea1e6133cdc 192.168.10.219:6381  
  30.  slots: (0 slots) slave  
  31.  replicates b2c50113db7bd685e316a16b423c9b8abc3ba0b7  
  32. S: 9a2a1d75b8eb47e05eee1198f81a9edd88db5aa1 192.168.10.220:6384  
  33.  slots: (0 slots) slave  
  34.  replicates 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052  
  35. [OK] All nodes agree about slots configuration.  
  36. >>> Check for open slots...  
  37. >>> Check slots coverage...  
  38. [OK] All 16384 slots covered.  

三,改变从节点的master

  1. //查看一下6378的从节点  
  2. # redis-cli -p 6378 cluster nodes | grep slave | grep 03ccad2ba5dd1e062464bc7590400441fafb63f2  
  3.   
  4. //将6385加入到新的master  
  5. # redis-cli -c -p 6385 -h 192.168.10.220  
  6. 192.168.10.220:6385> cluster replicate 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052  //新master的node id  
  7. OK  
  8. 192.168.10.220:6385> quit  
  9.   
  10. //查看新master的slave  
  11. # redis-cli -p 6379 cluster nodes | grep slave | grep 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052  

四,删除节点

1,删除从节点

  1. # redis-trib.rb del-node 192.168.10.220:6385 '9c240333476469e8e2c8e80b089c48f389827265'  

2,删除主节点

如果主节点有从节点,将从节点转移到其他主节点

如果主节点有slot,去掉分配的slot,然后在删除主节点

  1. # redis-trib.rb reshard 192.168.10.219:6378 //取消分配的slot,下面是主要过程  
  2.   
  3. How many slots do you want to move (from 1 to 16384)? 1000 //被删除master的所有slot数量  
  4. What is the receiving node ID? 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052 //接收6378节点slot的master  
  5. Please enter all the source node IDs.  
  6.  Type 'all' to use all the nodes as source nodes for the hash slots.  
  7.  Type 'done' once you entered all the source nodes IDs.  
  8. Source node #1:03ccad2ba5dd1e062464bc7590400441fafb63f2 //被删除master的node-id  
  9. Source node #2:done   
  10.   
  11. Do you want to proceed with the proposed reshard plan (yes/no)? yes //取消slot后,reshard  

新增master节点后,也进行了这一步操作,当时是分配,现在去掉。反着的。

  1. # redis-trib.rb del-node 192.168.10.219:6378 '03ccad2ba5dd1e062464bc7590400441fafb63f2'  

新的master节点被删除了,这样就回到了,就是这篇文章开头,还没有添加节点的状态

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

posted @ 2019-05-24 14:56  kuroniko  阅读(3175)  评论(0编辑  收藏  举报