Redis学习笔记(9)- Redis集群常用操作

1、简介

  在《Redis集群搭建》这篇内容中,已经尝试了Redis集群的环境搭建,现在我们继续学习在集群中常用的操作。

2、集群命令

通过下面命令,可以查看集群中常用的命令:

redis-cli --cluster help

在这里插入图片描述

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

3、create

  创建集群。在上一篇中已经用到了该命令。语法如下:

redis-cli --cluster create host1:port1 ... hostN:portN --cluster-replicas <arg>

其中,host1:port1…hostN:portN到是需要创建集群的实例的ip+port,–cluster-replicas 表示从节点个数,如果不带–cluster-replicas 参数,表示只创建主节点。
在这里插入图片描述

4、check

  任意连接一个集群节点,进行集群状态检查。语法如下:

redis-cli --cluster check host:port --cluster-search-multiple-owners

其中,host:port是集群中一个实例的ip+port,–cluster-search-multiple-owners表示是否有槽同时被分配给了多个节点。
在这里插入图片描述

5、info

  查看集群状态。语法如下:

redis-cli --cluster info host:port

其中,host:port是集群中任意一个实例的ip+port。
在这里插入图片描述

5、info

  查看集群状态。语法如下:

redis-cli --cluster info host:port

其中,host:port是集群中任意一个实例的ip+port。
在这里插入图片描述

6、fix

  查看集群状态。语法如下:

redis-cli --cluster fix host:port --cluster-search-multiple-owners

其中,host:port是集群中任意一个实例的ip+port;–cluster-search-multiple-owners 参数表示修复槽的重复分配问题。
在这里插入图片描述

7、 reshard

  指定集群的任意一节点进行迁移slot,重新分slots。语法如下:

redis-cli --cluster reshard  host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace

其中,

  • host:port是集群中任意一节点的ip+port;
  • –cluster-from 表示需要从哪些源节点上迁移slot,可从多个源节点完成迁移,以逗号隔开,传递的是节点的node id,还可以直接传递–from all,这样源节点就是集群的所有节点,不传递该参数的话,则会在迁移过程中提示用户输入
  • –cluster-to 表示slot需要迁移的目的节点的node id,目的节点只能填写一个,不传递该参数的话,则会在迁移过程中提示用户输入
  • –cluster-slots 表示需要迁移的slot数量,不传递该参数的话,则会在迁移过程中提示用户输入。
  • –cluster-yes 表示指定迁移时的确认输入
  • –cluster-timeout 表示设置migrate命令的超时时间
  • –cluster-pipeline 表示定义cluster getkeysinslot命令一次取出的key数量,不传的话使用默认值为10
  • –cluster-replace 表示是否直接replace到目标节点

在执行过程中,输入参数,如下所示:

[root@ecs-4f60-0002 redis]#  redis-cli --cluster reshard 192.168.0.182:7000
>>> Performing Cluster Check (using node 192.168.0.182:7000)
M: e26d23cb8fbffeee91cf05ad30505af4f898de95 192.168.0.182:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2b71fb0cb6a20912536c8d09436ec0c84da7b298 192.168.0.182:7005
   slots: (0 slots) slave
   replicates e26d23cb8fbffeee91cf05ad30505af4f898de95
M: c10ba6ce6a66d4800c95b33571761c8e35119943 192.168.0.182:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: bcd8dabaff4c24705b64e1495762341b3afaed28 192.168.0.182:7003
   slots: (0 slots) slave
   replicates efae6cd04378be656b028157561ec4e7bf6acb28
S: 4f2f968f35c0e755ce440a054384efc940e1c9a2 192.168.0.182:7004
   slots: (0 slots) slave
   replicates c10ba6ce6a66d4800c95b33571761c8e35119943
M: efae6cd04378be656b028157561ec4e7bf6acb28 192.168.0.182:7001
   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.
How many slots do you want to move (from 1 to 16384)? 2
What is the receiving node ID? e26d23cb8fbffeee91cf05ad30505af4f898de95
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: c10ba6ce6a66d4800c95b33571761c8e35119943
Source node #2: done

Ready to move 2 slots.
  Source nodes:
    M: c10ba6ce6a66d4800c95b33571761c8e35119943 192.168.0.182:7002
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
  Destination node:
    M: e26d23cb8fbffeee91cf05ad30505af4f898de95 192.168.0.182:7000
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 10923 from c10ba6ce6a66d4800c95b33571761c8e35119943
    Moving slot 10924 from c10ba6ce6a66d4800c95b33571761c8e35119943
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 10923 from 192.168.0.182:7002 to 192.168.0.182:7000: 
Moving slot 10924 from 192.168.0.182:7002 to 192.168.0.182:7000: 

通过命令,直接执行迁移操作,结果如下:

[root@ecs-4f60-0002 redis]#  redis-cli --cluster reshard 192.168.0.182:7000 --cluster-from c10ba6ce6a66d4800c95b33571761c8e35119943 --cluster-to efae6cd04378be656b028157561ec4e7bf6acb28 --cluster-slots 5
>>> Performing Cluster Check (using node 192.168.0.182:7000)
M: e26d23cb8fbffeee91cf05ad30505af4f898de95 192.168.0.182:7000
   slots:[0-5460],[10923-10924] (5463 slots) master
   1 additional replica(s)
S: 2b71fb0cb6a20912536c8d09436ec0c84da7b298 192.168.0.182:7005
   slots: (0 slots) slave
   replicates e26d23cb8fbffeee91cf05ad30505af4f898de95
M: c10ba6ce6a66d4800c95b33571761c8e35119943 192.168.0.182:7002
   slots:[10925-16383] (5459 slots) master
   1 additional replica(s)
S: bcd8dabaff4c24705b64e1495762341b3afaed28 192.168.0.182:7003
   slots: (0 slots) slave
   replicates efae6cd04378be656b028157561ec4e7bf6acb28
S: 4f2f968f35c0e755ce440a054384efc940e1c9a2 192.168.0.182:7004
   slots: (0 slots) slave
   replicates c10ba6ce6a66d4800c95b33571761c8e35119943
M: efae6cd04378be656b028157561ec4e7bf6acb28 192.168.0.182:7001
   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.

Ready to move 5 slots.
  Source nodes:
    M: c10ba6ce6a66d4800c95b33571761c8e35119943 192.168.0.182:7002
       slots:[10925-16383] (5459 slots) master
       1 additional replica(s)
  Destination node:
    M: efae6cd04378be656b028157561ec4e7bf6acb28 192.168.0.182:7001
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 10925 from c10ba6ce6a66d4800c95b33571761c8e35119943
    Moving slot 10926 from c10ba6ce6a66d4800c95b33571761c8e35119943
    Moving slot 10927 from c10ba6ce6a66d4800c95b33571761c8e35119943
    Moving slot 10928 from c10ba6ce6a66d4800c95b33571761c8e35119943
    Moving slot 10929 from c10ba6ce6a66d4800c95b33571761c8e35119943
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 10925 from 192.168.0.182:7002 to 192.168.0.182:7001: 
Moving slot 10926 from 192.168.0.182:7002 to 192.168.0.182:7001: 
Moving slot 10927 from 192.168.0.182:7002 to 192.168.0.182:7001: 
Moving slot 10928 from 192.168.0.182:7002 to 192.168.0.182:7001: 
Moving slot 10929 from 192.168.0.182:7002 to 192.168.0.182:7001: 
8、 rebalance

  指定集群的任意一节点进行平衡集群节点slot数量,语法如下:

redis-cli --cluster rebalance host:port
                --cluster-weight <node1=w1...nodeN=wN>
                --cluster-use-empty-masters
                --cluster-timeout <arg>
                --cluster-simulate
                --cluster-pipeline <arg>
                --cluster-threshold <arg>
                --cluster-replace

其中,

  • host:port是集群中任意一节点的ip+port;
  • –cluster-weight <node1=w1…nodeN=wN> 指定集群节点的权重
  • –cluster-use-empty-masters 设置可以让没有分配slot的主节点参与,默认不允许
  • –cluster-timeout 设置migrate命令的超时时间
  • –cluster-simulate 模拟rebalance操作,不会真正执行迁移操作
  • –cluster-pipeline 定义cluster getkeysinslot命令一次取出的key数量,默认值为10
  • –cluster-threshold 迁移的slot阈值超过threshold,执行rebalance操作
  • –cluster-replace 是否直接replace到目标节点

当slot分布比较平衡时,不执行任何操作。如下所示:
在这里插入图片描述在这里插入图片描述

通过上面的reshard 命令,先重新分配slot,然后再执行rebalance命令如下:
在这里插入图片描述
在这里插入图片描述
再通过check命令查看,如下所示:
在这里插入图片描述

9、 add-node

  添加节点,把新节点加入到指定的集群,默认添加主节点。语法如下:

redis-cli --cluster add-node new_host:new_port existing_host:existing_port
            				--cluster-slave
                			--cluster-master-id <arg>

其中,

  • host:port是集群中任意一节点的ip+port;
  • –cluster-slave 新节点作为从节点,默认随机一个主节点
  • –cluster-master-id 给新节点指定主节点

在这里插入图片描述
在这里插入图片描述

10、del-node

  删除节点,删除指定的节点。语法如下:

redis-cli --cluster del-node host:port node_id

其中,

  • host:port是集群中任意一节点的ip+port;
  • node_id 要删除节点的id

当节点有slot时,删除失败,如下所示:
在这里插入图片描述
通过reshard命令把slot分配到其他节点,然后再执行删除命令如下:
在这里插入图片描述
在这里插入图片描述

11、call

  在集群的所有节点执行相关命令,即连接到集群的任意一节点来对整个集群的所有节点进行设置。语法如下:

redis-cli --cluster call host:port command arg arg .. arg
12、set-timeout

  设置cluster-node-timeout。语法如下:

redis-cli --cluster set-timeout host:port milliseconds
13、import

  将外部redis数据导入集群。语法如下:

redis-cli --cluster import host:port
                		 --cluster-from <arg>
                		 --cluster-copy
                		 --cluster-replace

其中,

  • host:port是集群中任意一节点的ip+port;
  • –cluster-from 将指定实例的数据导入到集群
  • –cluster-copy migrate时指定copy
  • –cluster-replace migrate时指定replace
发布了71 篇原创文章 · 获赞 3 · 访问量 5267

猜你喜欢

转载自blog.csdn.net/hou_ge/article/details/104570592