zoukankan      html  css  js  c++  java
  • Redis cluster 日常操作命令

    本文主要讲解cluster添加节点,删除节点的操作

     

    一、以下命令是Redis Cluster集群所独有的,执行下面命令需要先登录redis:

    [root@ceshi ~]# redis-cli -h 192.168.110.66 -c -p 6379

    //集群(cluster) 

    CLUSTER INFO 打印集群的信息 

    CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。  

       

    //节点(node) 

    CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。 

    CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。 

    CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。 

    CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。  

       

    //槽(slot) 

    CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。 

    CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。 

    CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。 

    CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。 

    CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。 

    CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。 

    CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。  

       

    //键 (key) 

    CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。 

    CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。 

    CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。

    二、添加节点

    1、新配置二个测试节点

    [root@localhost ~]# cd /etc/redis/

    //新增配置 

    [root@localhost redis]# cp redis_7007.conf redis_7008.conf &&  sed -i "s/7007/7008/g" redis_7008.conf

    [root@localhost redis]# cp redis_7007.conf redis_7009.conf &&  sed -i "s/7007/7009/g" redis_7009.conf

       

    //启动 

    [root@localhost redis]# /usr/bin/redis-server /etc/redis/redis_7008.conf

    [root@localhost redis]# /usr/bin/redis-server /etc/redis/redis_7009.conf

     2、添加主节点

    [root@localhost ~]# redis-trib.rb add-node 192.168.110.63:7008 192.168.110.63:7007

    >>> Adding node 192.168.110.63:7008 to cluster 192.168.110.63:7007

    >>> Performing Cluster Check (using node 192.168.110.63:7007)

    M: 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007

       slots:8192-12287 (4096 slots) master

       1 additional replica(s)

    S: 8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003

       slots: (0 slots) slave

       replicates 25fe0699abf9b5d713fde30af06889a3000bc45a

    S: 949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001

       slots: (0 slots) slave

       replicates 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd

    M: 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004

       slots:4096-8191 (4096 slots) master

       1 additional replica(s)

    M: 72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006

       slots:0-4095 (4096 slots) master

       0 additional replica(s)

    M: 25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005

       slots:12288-16383 (4096 slots) master

       1 additional replica(s)

    S: d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002

       slots: (0 slots) slave

       replicates 153dca4555605c8a9ccbc5bdf968727c43d3e5a6

    [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.110.63:7008 to make it join the cluster.

    [OK] New node added correctly.

    [root@localhost ~]#

    注释:

    192.168.110.63:7007是集群中任意一个旧节点

    192.168.110.63:7008是新增的主节点

    3、给192.168.110.63:7008添加从节点

    [root@localhost ~]# redis-trib.rb add-node --slave --master-id 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7009 192.168.110.63:7007

    >>> Adding node 192.168.110.63:7009 to cluster 192.168.110.63:7007

    >>> Performing Cluster Check (using node 192.168.110.63:7007)

    M: 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007

       slots:8192-12287 (4096 slots) master

       1 additional replica(s)

    S: 8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003

       slots: (0 slots) slave

       replicates 25fe0699abf9b5d713fde30af06889a3000bc45a

    M: 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004

       slots:4096-8191 (4096 slots) master

       1 additional replica(s)

    M: 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7008

       slots: (0 slots) master

       0 additional replica(s)

    M: 25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005

       slots:12288-16383 (4096 slots) master

       1 additional replica(s)

    S: 949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001

       slots: (0 slots) slave

       replicates 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd

    M: 72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006

       slots:0-4095 (4096 slots) master

       0 additional replica(s)

    S: d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002

       slots: (0 slots) slave

       replicates 153dca4555605c8a9ccbc5bdf968727c43d3e5a6

    [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.110.63:7009 to make it join the cluster.

    Waiting for the cluster to join.

    >>> Configure node as replica of 192.168.110.63:7008.

    [OK] New node added correctly.

    [root@localhost ~]#

    注释:

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

    --master-id 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0,主节点的node id,在这里是前面新添加的7008的node id

    192.168.110.63:7009,新节点

    192.168.110.63:7007,集群任一个旧节点

     

    4、给新加的主节点重新分配slot

    [root@localhost ~]# # redis-trib.rb reshard 192.168.110.63:7008               //下面是主要过程 

    How many slots do you want to move (from 1 to 16384)? 1000 //设置slot数1000 

    What is the receiving node ID? 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 //新节点7008的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:all                                      //表示全部节点重新洗牌 

    Do you want to proceed with the proposed reshard plan (yes/no)? yes //确认重新分 

    注释: 

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

    M: 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7008

       slots: (0 slots) master

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

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

    5、查看一下,集群情况

    [root@localhost ~]# redis-trib.rb check 192.168.110.63:7007

    >>> Performing Cluster Check (using node 192.168.110.63:7007)

    M: 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007

       slots:8442-12287 (3846 slots) master

       1 additional replica(s)

    S: 8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003

       slots: (0 slots) slave

       replicates 25fe0699abf9b5d713fde30af06889a3000bc45a

    M: 54c94223ef55a24082740ce3659215c619120b82 192.168.110.56:7000

       slots: (0 slots) master

       0 additional replica(s)

    M: 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004

       slots:4346-8191 (3846 slots) master

       1 additional replica(s)

    M: 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7008

       slots:0-249,4096-4345,8192-8441,12288-12537 (1000 slots) master

       1 additional replica(s)

    M: 25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005

       slots:12538-16383 (3846 slots) master

       1 additional replica(s)

    S: 949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001

       slots: (0 slots) slave

       replicates 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd

    M: 72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006

       slots:250-4095 (3846 slots) master

       0 additional replica(s)

    S: 5653e8301abb5356a3a32d56da8512becb6d4409 192.168.110.63:7009

       slots: (0 slots) slave

       replicates 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0

    S: d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002

       slots: (0 slots) slave

       replicates 153dca4555605c8a9ccbc5bdf968727c43d3e5a6

    [OK] All nodes agree about slots configuration.

    >>> Check for open slots...

    >>> Check slots coverage...

    [OK] All 16384 slots covered.

    [root@localhost ~]#

    三、改变从节点的master

    1、查看下7008的从节点

    [root@ceshi ~]# redis-cli -h 192.168.110.63 -p 7008 cluster nodes|grep slave

    5653e8301abb5356a3a32d56da8512becb6d4409 192.168.110.63:7009 slave 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 0 1521105048702 13 connected

    2、将7009加入新的master 7007

    [root@ceshi ~]# redis-cli -c -p 7009 -h 192.168.110.63

    192.168.110.63:7009> CLUSTER REPLICATE 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd

    OK

    192.168.110.63:7009> exit

    [root@ceshi ~]#

    3、查看7007matser的slave

    [root@ceshi ~]#  redis-cli -h 192.168.110.63 -p 7007 cluster nodes|grep slave

    5653e8301abb5356a3a32d56da8512becb6d4409 192.168.110.63:7009 slave 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 0 1521105048702 13 connected

     

    四、删除节点

    1、删除从节点

    (1)查看目前存在的所有节点

    [root@localhost ~]# redis-cli -h 192.168.110.63 -p 7007 cluster nodes

    8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003 slave 25fe0699abf9b5d713fde30af06889a3000bc45a 0 1521106589269 6 connected

    54c94223ef55a24082740ce3659215c619120b82 192.168.110.56:7000 master - 0 1521106585742 12 connected

    153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004 master - 0 1521106584233 5 connected 4346-8191

    777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7008 master - 0 1521106588262 13 connected 0-249 4096-4345 8192-8441 12288-12537

    25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005 master - 0 1521106587759 6 connected 12538-16383

    740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007 myself,master - 0 0 9 connected 8442-12287

    33571cb37ef6b868b8413d570c2c212572694204 :0 slave,fail,noaddr 72afb98122d923d3c4e16027947ec34c33df80b3 1521093508336 1521093508235 10 disconnected

    949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001 slave 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 0 1521106585240 9 connected

    72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006 master - 0 1521106587254 10 connected 250-4095

    5653e8301abb5356a3a32d56da8512becb6d4409 192.168.110.63:7009 slave 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 0 1521106583729 13 connected

    d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002 slave 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 0 1521106590274 5 connected

    [root@localhost ~]#

    (2)删除刚才创建的7009的从节点

    [root@ceshi ~]# redis-trib.rb del-node 192.168.110.63:7009 '5653e8301abb5356a3a32d56da8512becb6d4409'

    >>> Removing node 5653e8301abb5356a3a32d56da8512becb6d4409 from cluster 192.168.110.63:7009

    >>> Sending CLUSTER FORGET messages to the cluster...

    >>> SHUTDOWN the node.

    [root@localhost ~]#

    (3)重新查看发现7009不存在了

    [root@localhost ~]# redis-cli -h 192.168.110.63 -p 7007 cluster nodes

    8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003 slave 25fe0699abf9b5d713fde30af06889a3000bc45a 0 1521106810771 6 connected

    54c94223ef55a24082740ce3659215c619120b82 192.168.110.56:7000 master - 0 1521106806741 12 connected

    153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004 master - 0 1521106812788 5 connected 4346-8191

    777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7008 master - 0 1521106809763 13 connected 0-249 4096-4345 8192-8441 12288-12537

    25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005 master - 0 1521106809763 6 connected 12538-16383

    740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007 myself,master - 0 0 9 connected 8442-12287

    33571cb37ef6b868b8413d570c2c212572694204 :0 slave,fail,noaddr 72afb98122d923d3c4e16027947ec34c33df80b3 1521093508336 1521093508235 10 disconnected

    949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001 slave 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 0 1521106806239 9 connected

    72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006 master - 0 1521106811779 10 connected 250-4095

    d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002 slave 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 0 1521106808756 5 connected

    (4)删除从节点成功

    2、删除主节点

    (1)查看当前存在的所有节点

    [root@localhost ~]# redis-cli -h 192.168.110.63 -p 7007 cluster nodes

    8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003 slave 25fe0699abf9b5d713fde30af06889a3000bc45a 0 1521106810771 6 connected

    54c94223ef55a24082740ce3659215c619120b82 192.168.110.56:7000 master - 0 1521106806741 12 connected

    153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004 master - 0 1521106812788 5 connected 4346-8191

    777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7008 master - 0 1521106809763 13 connected 0-249 4096-4345 8192-8441 12288-12537

    25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005 master - 0 1521106809763 6 connected 12538-16383

    740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007 myself,master - 0 0 9 connected 8442-12287

    33571cb37ef6b868b8413d570c2c212572694204 :0 slave,fail,noaddr 72afb98122d923d3c4e16027947ec34c33df80b3 1521093508336 1521093508235 10 disconnected

    949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001 slave 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 0 1521106806239 9 connected

    72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006 master - 0 1521106811779 10 connected 250-4095

    d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002 slave 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 0 1521106808756 5 connected

    (2)删除刚才创建的主节点7008

    注意:

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

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

     

    a:去掉slot

    [root@localhost ~]# redis-trib.rb reshard 192.168.110.63:7008                                       //取消分配的slot,下面是主要过程 

    How many slots do you want to move (from 1 to 16384)? 1000          //被删除master7008的所有slot数量

    What is the receiving node ID? 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd     //接收7008节点slot的master,我这里使用的是7007

    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:777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0              //被删除master的node-id,我这里7008

    Source node #2:done  

    Do you want to proceed with the proposed reshard plan (yes/no)? yes

     

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

    b:删除主节点

    [root@localhost ~]# redis-trib.rb del-node 192.168.110.63:7008 '777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0'

    >>> Removing node 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 from cluster 192.168.110.63:7008

    >>> Sending CLUSTER FORGET messages to the cluster...

    >>> SHUTDOWN the node.

    [root@ceshi ~]#

    c:查看当前存在的节点

    [root@localhost ~]# redis-cli -h 192.168.110.63 -p 7007 cluster nodes

    8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003 slave 25fe0699abf9b5d713fde30af06889a3000bc45a 0 1521108471111 6 connected

    54c94223ef55a24082740ce3659215c619120b82 192.168.110.56:7000 master - 0 1521108475138 12 connected

    153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004 master - 0 1521108476142 5 connected 4346-8191

    25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005 master - 0 1521108474636 6 connected 12538-16383

    740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007 myself,master - 0 0 14 connected 0-249 4096-4345 8192-12537

    33571cb37ef6b868b8413d570c2c212572694204 :0 slave,fail,noaddr 72afb98122d923d3c4e16027947ec34c33df80b3 1521093508336 1521093508235 10 disconnected

    949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001 slave 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 0 1521108474133 14 connected

    72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006 master - 0 1521108473126 10 connected 250-4095

    d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002 slave 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 0 1521108472120 5 connected

    [root@localhost ~]#

    d:至此节点删除完毕

  • 相关阅读:
    第1次作业
    第0次作业
    总结报告
    第14、15周作业
    第七周作业
    第六周作业
    第四周作业
    第四次作业
    第三次作业
    2018第二次作业
  • 原文地址:https://www.cnblogs.com/bazingafraser/p/8579522.html
Copyright © 2011-2022 走看看