美文网首页
Centos7 Redis 集群--之三 管理集群-主从关系调整

Centos7 Redis 集群--之三 管理集群-主从关系调整

作者: 轻飘飘D | 来源:发表于2020-12-02 18:18 被阅读0次

    redis-trib.rb支持的操作

    # redis-trib.rb help
    Usage: redis-trib <command> <options> <arguments ...>
    
      create          host1:port1 ... hostN:portN
                      --replicas <arg>
      check           host:port
      info            host:port
      fix             host:port
                      --timeout <arg>
      reshard         host:port
                      --from <arg>
                      --to <arg>
                      --slots <arg>
                      --yes
                      --timeout <arg>
                      --pipeline <arg>
      rebalance       host:port
                      --weight <arg>
                      --auto-weights
                      --use-empty-masters
                      --timeout <arg>
                      --simulate
                      --pipeline <arg>
                      --threshold <arg>
      add-node        new_host:new_port existing_host:existing_port
                      --slave
                      --master-id <arg>
      del-node        host:port node_id
      set-timeout     host:port milliseconds
      call            host:port command arg arg .. arg
      import          host:port
                      --from <arg>
                      --copy
                      --replace
      help            (show this help)
    
    For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
    
    支持的操作如下:
    
    1. create:创建集群
    2. check:检查集群
    3. info:查看集群信息
    4. fix:修复集群
    5. reshard:在线迁移slot
    6. rebalance:平衡集群节点slot数量
    7. add-node:添加新节点
    8. del-node:删除节点
    9. set-timeout:设置节点的超时时间
    10. call:在集群所有节点上执行命令
    11. import:将外部redis数据导入集群
    

    一、調整從節點指向 新主節點
    1.集群現狀

    序 號 主集群IP + PORT 主集群ID 從集群IP+端口 從集群ID
    1 10.0.30.14:7000 77d...b3d 10.0.30.15:7000 edb5f035347d1f27f7b321694963f432242f590f
    2 10.0.30.183:7000 833...0c1 10.0.30.184:7000 9136c0a49eb84b4d0937f77924e1a1d9f44bc7b6
    3 10.0.30.220:7000 a0a...990 10.0.30.221:7000 1576e6d68350ee8e231126b988139fb74bb8920c
    10.0.30.183:7000> CLUSTER nodes
    a0ac90e2d92df740da3a6e67d6650637a858e990 10.0.30.220:7000 master - 0 1606896594264 8 connected 10923-16383
    1576e6d68350ee8e231126b988139fb74bb8920c 10.0.30.221:7000 slave a0ac90e2d92df740da3a6e67d6650637a858e990 0 1606896591215 8 connected
    edb5f035347d1f27f7b321694963f432242f590f 10.0.30.15:7000 slave 77d2953e160c6b44cc88d0a3c4595e6520817b3d 0 1606896596297 4 connected
    833afe994d8d4b27306b8614d2b27ea9a8e240c1 10.0.30.183:7000 myself,master - 0 0 2 connected 5461-10922
    9136c0a49eb84b4d0937f77924e1a1d9f44bc7b6 10.0.30.184:7000 slave 833afe994d8d4b27306b8614d2b27ea9a8e240c1 0 1606896595281 5 connected
    77d2953e160c6b44cc88d0a3c4595e6520817b3d 10.0.30.14:7000 master - 0 1606896593247 1 connected 0-5460
    
    

    2.計劃調整 從節點為如下

    序 號 主集群IP + PORT 主集群ID 從集群IP+端口 從集群ID
    1 10.0.30.14:7000 77d...b3d 10.0.30.15:7000 edb5f035347d1f27f7b321694963f432242f590f
    2 10.0.30.183:7000 833...0c1 10.0.30.221:7000 1576e6d68350ee8e231126b988139fb74bb8920c
    3 10.0.30.220:7000 a0a...990 10.0.30.184:7000 9136c0a49eb84b4d0937f77924e1a1d9f44bc7b6

    3.调整方法描述
    使用redis-trib.rb脚本将待调整的从节点从redis集群中删除。(执行此步操作时,从节点会从redis集群中去除。同时,此从节点进程会被杀掉。)

    命令格式:
    redis-trib.rb del-node 集群中某节点IP:PORT 待调整的从节点ID
    
    [root@host1 gem]#  /usr/local/bin/redis-trib.rb del-node 10.0.30.14:7000 9136c0a49eb84b4d0937f77924e1a1d9f44bc7b6
    >>> Removing node 9136c0a49eb84b4d0937f77924e1a1d9f44bc7b6 from cluster 10.0.30.14:7000
    >>> Sending CLUSTER FORGET messages to the cluster...
    >>> SHUTDOWN the node.
    
    [root@host1 gem]# /usr/local/bin/redis-trib.rb del-node 10.0.30.14:7000 1576e6d68350ee8e231126b988139fb74bb8920c
    >>> Removing node 1576e6d68350ee8e231126b988139fb74bb8920c from cluster 10.0.30.14:7000
    >>> Sending CLUSTER FORGET messages to the cluster...
    >>> SHUTDOWN the node.
    
    [root@host1 gem]# /usr/local/bin/redis-trib.rb info 10.0.30.14:7000
    10.0.30.14:7000 (77d2953e...) -> 2 keys | 5461 slots | 1 slaves.
    10.0.30.220:7000 (a0ac90e2...) -> 1 keys | 5461 slots | 0 slaves.
    10.0.30.183:7000 (833afe99...) -> 1 keys | 5462 slots | 0 slaves.
    [OK] 4 keys in 3 masters.
    
    開始 删除两个从节点(184,221)data子目录下的所有文件
    #221
    [root@dbvm2 conf]# ip addr
    inet 10.0.30.221/16
    
    [root@dbvm2 conf]# ll /usr/local/redis/data/7000
    总用量 8
    -rw-r--r--. 1 root root  94 12月  2 17:29 dump.rdb
    -rw-r--r--. 1 root root 610 12月  2 17:29 nodes_7000.conf
    
    [root@dbvm2 conf]# netstat -tunpl | grep 7000
    
    [root@dbvm2 conf]# rm /usr/local/redis/data/7000/*.*
    rm:是否删除普通文件 "/usr/local/redis/data/7000/dump.rdb"?yes
    rm:是否删除普通文件 "/usr/local/redis/data/7000/nodes_7000.conf"?yes
    
    [root@dbvm2 conf]# ll /usr/local/redis/data/7000
    总用量 0
    
    #184
    [root@cs184 conf]# ip addr
    inet 10.0.30.184/16 
    [root@cs184 conf]# ll /usr/local/redis/data/7000
    total 8
    -rw-r--r-- 1 root root  94 Dec  2 17:29 dump.rdb
    -rw-r--r-- 1 root root 743 Dec  2 16:09 nodes_7000.conf
    
    [root@cs184 conf]# netstat -tunpl | grep 7000
    
    [root@cs184 conf]# rm /usr/local/redis/data/7000/*.*
    rm: remove regular file `/usr/local/redis/data/7000/dump.rdb'? yes
    rm: remove regular file `/usr/local/redis/data/7000/nodes_7000.conf'? yes
    

    4.重新启动删掉的两个从节点(184,221)

    [root@cs184 conf]# netstat -tunpl | grep 7000
    tcp        0      0 10.0.30.184:7000            0.0.0.0:*                   LISTEN      6635/redis-server 1 
    tcp        0      0 10.0.30.184:17000           0.0.0.0:*                   LISTEN      6635/redis-server 1 
    

    5.将删掉的两个从节点加入redis集群并建立新的主从关系,在任意一台服务器上执行下面2条命令:

    命令格式:
    redis-trib add-node --slave --master-id <arg> new_host:new_port existing_host:existing_port
    new_host:new_port:待添加的节点,必须确保其为空或不在其它集群中
    existing_host:existing_port:集群中任意一个节点的地址
    --master-id <arg> 將從節點加到此主節點下
    
    [root@host1 gem]#  
    /usr/local/bin/redis-trib.rb add-node --slave --master-id 833afe994d8d4b27306b8614d2b27ea9a8e240c1 10.0.30.221:7000 10.0.30.14:7000
    
    [root@host1 gem]#  
    /usr/local/bin/redis-trib.rb add-node --slave --master-id a0ac90e2d92df740da3a6e67d6650637a858e990 10.0.30.184:7000 10.0.30.14:7000
    
    詳細過程如下:
    [root@host1 gem]# /usr/local/bin/redis-trib.rb add-node --slave --master-id 833afe994d8d4b27306b8614d2b27ea9a8e240c1 10.0.30.221:7000 10.0.30.14:7000
    >>> Adding node 10.0.30.221:7000 to cluster 10.0.30.14:7000
    >>> Performing Cluster Check (using node 10.0.30.14:7000)
    M: 77d2953e160c6b44cc88d0a3c4595e6520817b3d 10.0.30.14:7000
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    S: edb5f035347d1f27f7b321694963f432242f590f 10.0.30.15:7000
       slots: (0 slots) slave
       replicates 77d2953e160c6b44cc88d0a3c4595e6520817b3d
    M: a0ac90e2d92df740da3a6e67d6650637a858e990 10.0.30.220:7000
       slots:10923-16383 (5461 slots) master
       0 additional replica(s)
    M: 833afe994d8d4b27306b8614d2b27ea9a8e240c1 10.0.30.183: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 10.0.30.221:7000 to make it join the cluster.
    Waiting for the cluster to join.
    >>> Configure node as replica of 10.0.30.183:7000.
    [OK] New node added correctly.
    
    [root@host1 gem]# /usr/local/bin/redis-trib.rb info 10.0.30.14:7000
    10.0.30.14:7000 (77d2953e...) -> 2 keys | 5461 slots | 1 slaves.
    10.0.30.220:7000 (a0ac90e2...) -> 1 keys | 5461 slots | 0 slaves.
    10.0.30.183:7000 (833afe99...) -> 1 keys | 5462 slots | 1 slaves.
    [OK] 4 keys in 3 masters.
    0.00 keys per slot on average.
    
    [root@host1 gem]# /usr/local/bin/redis-trib.rb add-node --slave --master-id a0ac90e2d92df740da3a6e67d6650637a858e990 10.0.30.184:7000 10.0.30.14:7000
    >>> Adding node 10.0.30.184:7000 to cluster 10.0.30.14:7000
    >>> Performing Cluster Check (using node 10.0.30.14:7000)
    M: 77d2953e160c6b44cc88d0a3c4595e6520817b3d 10.0.30.14:7000
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    S: edb5f035347d1f27f7b321694963f432242f590f 10.0.30.15:7000
       slots: (0 slots) slave
       replicates 77d2953e160c6b44cc88d0a3c4595e6520817b3d
    S: 76c780e52b115a5127a467e9d75effaa99ef7f51 10.0.30.221:7000
       slots: (0 slots) slave
       replicates 833afe994d8d4b27306b8614d2b27ea9a8e240c1
    M: a0ac90e2d92df740da3a6e67d6650637a858e990 10.0.30.220:7000
       slots:10923-16383 (5461 slots) master
       0 additional replica(s)
    M: 833afe994d8d4b27306b8614d2b27ea9a8e240c1 10.0.30.183:7000
       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.
    >>> Send CLUSTER MEET to node 10.0.30.184:7000 to make it join the cluster.
    Waiting for the cluster to join.
    >>> Configure node as replica of 10.0.30.220:7000.
    [OK] New node added correctly.
    
    [root@host1 gem]# /usr/local/bin/redis-trib.rb info 10.0.30.14:7000
    10.0.30.14:7000 (77d2953e...) -> 2 keys | 5461 slots | 1 slaves.
    10.0.30.220:7000 (a0ac90e2...) -> 1 keys | 5461 slots | 1 slaves.
    10.0.30.183:7000 (833afe99...) -> 1 keys | 5462 slots | 1 slaves.
    [OK] 4 keys in 3 masters.
    0.00 keys per slot on average.
    
    

    6.检查集群状态

    [root@host1 gem]# /usr/local/bin/redis-trib.rb check 10.0.30.14:7000
    >>> Performing Cluster Check (using node 10.0.30.14:7000)
    M: 77d2953e160c6b44cc88d0a3c4595e6520817b3d 10.0.30.14:7000
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    S: edb5f035347d1f27f7b321694963f432242f590f 10.0.30.15:7000
       slots: (0 slots) slave
       replicates 77d2953e160c6b44cc88d0a3c4595e6520817b3d
    S: 76c780e52b115a5127a467e9d75effaa99ef7f51 10.0.30.221:7000
       slots: (0 slots) slave
       replicates 833afe994d8d4b27306b8614d2b27ea9a8e240c1
    S: 89a0c8445e81340e221f88e14c33053d10bf8a1b 10.0.30.184:7000
       slots: (0 slots) slave
       replicates a0ac90e2d92df740da3a6e67d6650637a858e990
    M: a0ac90e2d92df740da3a6e67d6650637a858e990 10.0.30.220:7000
       slots:10923-16383 (5461 slots) master
       1 additional replica(s)
    M: 833afe994d8d4b27306b8614d2b27ea9a8e240c1 10.0.30.183:7000
       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.
    

    二、在集群所有节点上执行命令
    命令格式: redis-trib.rb call host:port command arg arg .. arg

    1. set 命令
    [root@host1 gem]# /usr/local/bin/redis-trib.rb call 10.0.30.14:7000 set key5 value5
    >>> Calling SET key5 value5
    10.0.30.14:7000: MOVED 9057 10.0.30.183:7000
    10.0.30.15:7000: MOVED 9057 10.0.30.183:7000
    10.0.30.221:7000: MOVED 9057 10.0.30.183:7000
    10.0.30.184:7000: MOVED 9057 10.0.30.183:7000
    10.0.30.220:7000: MOVED 9057 10.0.30.183:7000
    10.0.30.183:7000: OK
    
    1. get 命令
    [root@host1 gem]# /usr/local/bin/redis-trib.rb call 10.0.30.14:7000 get key5 
    >>> Calling GET key5
    10.0.30.14:7000: MOVED 9057 10.0.30.183:7000
    10.0.30.15:7000: MOVED 9057 10.0.30.183:7000
    10.0.30.221:7000: MOVED 9057 10.0.30.183:7000
    10.0.30.184:7000: MOVED 9057 10.0.30.183:7000
    10.0.30.220:7000: MOVED 9057 10.0.30.183:7000
    10.0.30.183:7000: value5
    

    相关文章

      网友评论

          本文标题:Centos7 Redis 集群--之三 管理集群-主从关系调整

          本文链接:https://www.haomeiwen.com/subject/emduwktx.html