• 【Redis】Redis Cluster集群之水平扩展


    一、环境 与 Redis集群查看

      在同一台机器上存在6个节点的Redis集群,6个分别是 16379(主节点)、16380(主节点)、16381(主节点)、16382(从节点)、16383(从节点)、16384(从节点)

      

    1、查看redis集群的命令帮助

      [root@H__D soft]# redis-5.0.5/src/redis-cli --cluster help

    2、查看集群情况

      127.0.0.1:16379> cluster nodes

    二、扩展节点  

      现在需要扩展2个节点,一个16385(主节点),一个16386(从节点),节点16386是主节点16385的从节点,且分配500个hash槽给16385(主节点)

    1、使用add-node命令新增一个主节点16385(master)

      启动新增16385节点

      [root@H__D redis-cluster]# redis-5.0.5/src/redis-server config/redis-16385.conf

    2、将主节点16385添加到集群

      前面的ip:port为新增节点,后面的ip:port为已知存在节点,看到日志最后有"[OK] New node added correctly"提示代表新节点加入成功  

      [root@H__D redis-cluster]# redis-5.0.5/src/redis-cli -a 123456 --cluster add-node 127.0.0.1:16385 127.0.0.1:16379

      

      注意:当添加节点成功以后,新增的节点就是主节点,新增的节点不会有任何数据,因为它还没有分配任何的slot(hash槽),我们需要为新节点手工分配hash槽

      查看集群情况

      

    3、给新主节点16385分配hash槽

      使用redis-cli命令为新主节点16385分配hash槽,找到集群中的任意一个主节点,对其进行重新分片工作。

      [root@H__D redis-cluster]# redis-5.0.5/src/redis-cli -a 123456 --cluster reshard 127.0.0.1:16379

    [root@H__D redis-cluster]# redis-5.0.5/src/redis-cli -a 123456 --cluster reshard 127.0.0.1:16379
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    ...... 
    [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)? 500
    (ps:需要多少个槽移动到新的节点上,自己设置,比如500个hash槽)
    
    What is the receiving node ID? df7e73487a2c1c93edddd1e774e662a85a1a5494
    (ps:把这500个hash槽移动到哪个节点上去,需要指定节点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: all
    (ps:输入all为从所有主节点(16379,16380,16381)中分别抽取相应的槽数指定到新节点中,抽取的总槽数为500个)
    
    Ready to move 500 slots.
      Source nodes:
        M: 483d1656eaba6ee84ed0b6ad9cf3aea7a287305b 127.0.0.1:16379
           slots:[0-5460] (5461 slots) master
           1 additional replica(s)
        M: 679baec5d21f9a1739b60a110215f8a4cabacdd4 127.0.0.1:16381
           slots:[10923-16383] (5461 slots) master
           1 additional replica(s)
        M: 1b4d1ca232f637f7c426e3912eed6c5f5a7eab72 127.0.0.1:16380
           slots:[5461-10922] (5462 slots) master
           1 additional replica(s)
      Destination node:
        M: df7e73487a2c1c93edddd1e774e662a85a1a5494 127.0.0.1:16385
           slots: (0 slots) master
      Resharding plan:
        Moving slot 5461 from 1b4d1ca232f637f7c426e3912eed6c5f5a7eab72
        ......
        Moving slot 11088 from 679baec5d21f9a1739b60a110215f8a4cabacdd4
    Do you want to proceed with the proposed reshard plan (yes/no)? yes
    (ps:输入yes确认开始执行分片任务)
    
    Moving slot 5461 from 127.0.0.1:16380 to 127.0.0.1:16385: 
    ......
    Moving slot 11088 from 127.0.0.1:16381 to 127.0.0.1:16385: 
    [root@H__D redis-cluster]# 
    [root@H__D redis-cluster]# 

      查看集群情况

      

    4、为主节点16385配置从节点16386

      启动新增16386节点
      [root@H__D redis-cluster]# redis-5.0.5/src/redis-server config/redis-16386.conf

      将节点16386添加到集群
      [root@H__D redis-cluster]# redis-5.0.5/src/redis-cli -a 123456 --cluster add-node 127.0.0.1:16386 127.0.0.1:16379

      redis客户端连接节点16386,设置从节点

      [root@H__D redis-cluster]# redis-5.0.5/src/redis-cli -h 127.0.0.1 -p 16386 -a 123456

      #后面这串id为主节点16385的id

      127.0.0.1:16379> cluster replicate df7e73487a2c1c93edddd1e774e662a85a1a5494

      查看集群情况

      

    三、移除节点 

      现在需要移除2个节点,一个16385(主节点),一个16386(从节点)

    1、用del-node删除16386(从节点),指定删除节点ip和端口,以及节点id(删除节点id)  

      [root@H__D redis-cluster]# redis-5.0.5/src/redis-cli -a 123456 --cluster del-node 127.0.0.1:16386 09fd99b620d6141fc0eaff3a055c52b73e418556

       

      查看集群情况

      

    2、将16385(主节点)的hash槽移除

      这个步骤相对比较麻烦一些,因为主节点的里面是有分配了hash槽的,所以这里必须 先把16385里的hash槽放入到其他的可用主节点中去,然后再进行移除节点操作,不然会出现数据丢失问题(目前只能把master的数据迁移 到一个节点上,暂时做不了平均分配功能),执行命令如下:

      找到集群中的任意一个主节点,对其进行重新分片工作。

      [root@H__D redis-cluster]# redis-5.0.5/src/redis-cli -a 123456 --cluster reshard 127.0.0.1:16379

    [root@H__D redis-cluster]# redis-5.0.5/src/redis-cli -a 123456 --cluster reshard 127.0.0.1:16379
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    >>> Performing Cluster Check (using node 127.0.0.1:16379)
    ......
       replicates 679baec5d21f9a1739b60a110215f8a4cabacdd4
    [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)? 500
    (ps:这里是需要把数据移动到哪?16379的主节点id)
    
    What is the receiving node ID? 483d1656eaba6ee84ed0b6ad9cf3aea7a287305b
    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: df7e73487a2c1c93edddd1e774e662a85a1a5494
    (ps:这里是需要数据源,也就是我们的16385节点id)
    
    Source node #2: done
    (ps:这里直接输入done 开始生成迁移计划)
    
    Ready to move 500 slots.
      Source nodes:
        M: df7e73487a2c1c93edddd1e774e662a85a1a5494 127.0.0.1:16385
           slots:[0-165],[5461-5627],[10923-11088] (499 slots) master
      Destination node:
        M: 483d1656eaba6ee84ed0b6ad9cf3aea7a287305b 127.0.0.1:16379
           slots:[166-5460] (5295 slots) master
           1 additional replica(s)
      Resharding plan:
        Moving slot 0 from df7e73487a2c1c93edddd1e774e662a85a1a5494
        ......
        Moving slot 11088 from df7e73487a2c1c93edddd1e774e662a85a1a5494
    Do you want to proceed with the proposed reshard plan (yes/no)? yes
    (ps:这里输入yes开始迁移)
    
    Moving slot 0 from 127.0.0.1:16385 to 127.0.0.1:16379: 
    ...... 
    Moving slot 11088 from 127.0.0.1:16385 to 127.0.0.1:16379: 
    [root@H__D redis-cluster]# 

      查看集群情况

      

    3、最后直接使用del-node命令删除16385(主节点)即可

      用del-node删除16386(从节点),指定删除节点ip和端口,以及节点id(删除节点id)  

      [root@H__D redis-cluster]# redis-5.0.5/src/redis-cli -a 123456 --cluster del-node 127.0.0.1:16385 df7e73487a2c1c93edddd1e774e662a85a1a5494

      查看集群情况 

      

    完成

  • 相关阅读:
    分布式事务框架seata入门
    生活==不易
    LR Socket接收超时TPS上不去解决方法
    Loadrunner拼装(并发用户)唯一值方法
    shell/Python复制文件和文件夹
    redis可以设置过期key回调实现延时队列
    计算机USB读写权限统一管理(在域环境中)
    微软Win10最新BUG_2021年9月15日_KB5005565更新之后打印机突然连接不上 或 不能打印0x0000011b(附解决方案)!
    SQLServer中update没带where的回滚操作(Update或者Delete误操作恢复方法)
    107.如何用Windows Defender全盘扫描
  • 原文地址:https://www.cnblogs.com/h--d/p/14770630.html
Copyright © 2020-2023  润新知