温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

redis-trib.rb命令详解

发布时间:2020-05-26 01:56:57 来源:网络 阅读:1875 作者:898009427 栏目:关系型数据库

redis-trib.rb命令详解
redis-trib.rb是官方提供的Redis Cluster的管理工具,无需额外下载,默认位于源码包的src目录下,但因该工具是用ruby开发的,所以需要准备相关的依赖环境。

官网:https://redis.io/documentation
中文官网:http://www.redis.cn/documentation.html

1 redis-trib.rb支持的操作

[root@node1 src]# ./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.
[root@node1 src]#

支持的操作如下:

  • create:创建集群
  • check:检查集群
  • info:查看集群信息
  • fix:修复集群
  • reshard:在线迁移slot
  • rebalance:平衡集群节点slot数量
  • add-node:将新节点加入集群
  • del-node:从集群中删除节点
  • set-timeout:设置集群节点间心跳连接的超时时间
  • call:在集群全部节点上执行命令
  • import:将外部redis数据导入集群

对于check,fix,reshard,del-node,set-timeout,您可以指定集群中任何工作节点的主机和端口。

2 创建集群

用户无需指定哪台节点为master,哪台节点为slave,因为redis内部算法帮我们实现了,也可以先创建主节点,然后再指定从节点;

  • create
  • --replicas # 可选参数,replicas表示每个master需要有几个slave。

  • 只有master节点的创建方式
./redis-trib.rb create 192.168.1.101:7000 192.168.1.102:7000 192.168.1.103:7000 192.168.1.104:7000 192.168.1.105:7000 192.168.1.106:7000
  • 使用 --replicas 1 创建 每个master带一个 slave 指令
./redis-trib.rb create --replicas 1 192.168.1.101:7000 192.168.1.102:7000 192.168.1.103:7000 192.168.1.104:7000 192.168.1.105:7000 192.168.1.106:7000

16384个槽全部被分配,集群创建成功。注意:给redis-trib.rb的节点地址必须是不包含任何槽/数据的节点,否则会拒绝创建集群。

关于主从节点的选择及槽的分配,其算法如下:

把节点按照host分类,这样保证master节点能分配到更多的主机中。
遍历host列表,从每个host列表中弹出一个节点,放入interleaved数组。直到所有的节点都弹出为止。
将interleaved数组中前master个数量的节点保存到masters数组中。
计算每个master节点负责的slot数量,16384除以master数量取整,这里记为N。
遍历masters数组,每个master分配N个slot,最后一个master,分配剩下的slot。
接下来为master分配slave,分配算法会尽量保证master和slave节点不在同一台主机上。对于分配完指定slave数量的节点,还有多余的节点,也会为这些节点寻找master。分配算法会遍历两次masters数组。
第一次遍历master数组,在余下的节点列表找到replicas数量个slave。每个slave为第一个和master节点host不一样的节点,如果没有不一样的节点,则直接取出余下列表的第一个节点。
第二次遍历是分配节点数除以replicas不为整数而多出的一部分节点。

3 检查集群情况:check

指定任意一个节点即可。

./redis-trib.rb check 192.168.1.101:7000

显示:

[root@NUC-2 src]# ./redis-trib.rb check 192.168.1.101:7000
>>> Performing Cluster Check (using node 192.168.1.101:7000)
S: afaa82815a7fc2d0e19ffa664677dde03aa8ab36 192.168.1.101:7000
   slots: (0 slots) slave
   replicates 845674c71b1f43f9297501903e616140b2a0a1f6
M: b7cada75939d960f8ca98aad875b8f2e49020b19 192.168.1.102:7000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: f1946135289c8c76bb9817213cbb1730d9a0b052 192.168.1.103:7000
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 9f9a36f2da46bd18f9df785900b5a8e3c53b5ca3 192.168.1.106:7000
   slots: (0 slots) slave
   replicates b7cada75939d960f8ca98aad875b8f2e49020b19
M: 845674c71b1f43f9297501903e616140b2a0a1f6 192.168.1.105:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 15fc1bd754a2e02ec571e23c7d81a80c7322a100 192.168.1.104:7000
   slots: (0 slots) slave
   replicates f1946135289c8c76bb9817213cbb1730d9a0b052
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@NUC-2 src]#

4 查看集群信息:info

[root@NUC-2 src]# ./redis-trib.rb info 192.168.20.61:7000
192.168.20.62:7000 (b7cada75...) -> 2 keys | 5462 slots | 1 slaves.
192.168.20.63:7000 (f1946135...) -> 2 keys | 5461 slots | 1 slaves.
192.168.20.65:7000 (845674c7...) -> 4 keys | 5461 slots | 1 slaves.
[OK] 8 keys in 3 masters.
0.00 keys per slot on average.
[root@NUC-2 src]#

5 修复集群

目前fix命令能修复两种异常:

  1. 节点中存在处于迁移中(importing或migrating状态)的slot。
  2. 节点中存在未分配的slot。
    其它异常不能通过fix命令修复。
[root@NUC-2 src]# ./redis-trib.rb fix 192.168.20.61:7000
>>> Performing Cluster Check (using node 192.168.20.61:7000)
S: afaa82815a7fc2d0e19ffa664677dde03aa8ab36 192.168.20.61:7000
   slots: (0 slots) slave
   replicates 845674c71b1f43f9297501903e616140b2a0a1f6
M: b7cada75939d960f8ca98aad875b8f2e49020b19 192.168.20.62:7000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: f1946135289c8c76bb9817213cbb1730d9a0b052 192.168.20.63:7000
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 9f9a36f2da46bd18f9df785900b5a8e3c53b5ca3 192.168.20.66:7000
   slots: (0 slots) slave
   replicates b7cada75939d960f8ca98aad875b8f2e49020b19
M: 845674c71b1f43f9297501903e616140b2a0a1f6 192.168.20.65:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 15fc1bd754a2e02ec571e23c7d81a80c7322a100 192.168.20.64:7000
   slots: (0 slots) slave
   replicates f1946135289c8c76bb9817213cbb1730d9a0b052
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@NUC-2 src]#

6 在线迁移:slot

格式:

redis-trib.rb reshard host:port --from <arg> --to <arg> --slots <arg> --yes --timeout <arg> --pipeline <arg>

选项:

  • reshard
  • host:port:必传参数,集群内任意节点地址,用来获取整个集群信息。
  • --from <arg>:需要从哪些源节点上迁移slot,可从多个源节点完成迁移,以逗号隔开,传递的是节点的node id,还可以直接传递--from all,这样源节点就是集群的所有节点,不传递该参数的话,则会在迁移过程中提示用户输入。
  • --to <arg>:slot需要迁移的目的节点的node id,目的节点只能填写一个,不传递该参数的话,则会在迁移过程中提示用户输入。
  • --slots <arg>:需要迁移的slot数量,不传递该参数的话,则会在迁移过程中提示用户输入。
  • --yes:设置该参数,可以在打印执行reshard计划的时候,提示用户输入yes确认后再执行reshard。
  • --timeout <arg>:控制每次migrate操作的超时时间,默认为60000毫秒。
  • --pipeline <arg>:定义cluster getkeysinslot命令一次取出的key数量,不传的话使用默认值为10。

例:

./redis-trib.rb reshard --from all --to 845674c71b1f43f9297501903e616140b2a0a1f6 --slots 11 

7 平衡集群节点slot数量

选项:

rebalance       host:port
                  --weight <arg>
                  --auto-weights
                  --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>

说明

  • rebalance
  • host:port:这个是必传参数,用来从一个节点获取整个集群信息,相当于获取集群信息的入口。
  • --weight <arg>:节点的权重,格式为node_id=weight,如果需要为多个节点分配权重的话,需要添加多个--weight <arg>参数,即--weight b31e3a2e=5 --weight 60b8e3a1=5,node_id可为节点名称的前缀,只要保证前缀位数能唯一区分该节点即可。没有传递–weight的节点的权重默认为1。
  • --auto-weights:这个参数在rebalance流程中并未用到。
  • --threshold <arg>:只有节点需要迁移的slot阈值超过threshold,才会执行rebalance操作。具体计算方法可以参考下面的rebalance命令流程的第四步。
  • --use-empty-masters:rebalance是否考虑没有节点的master,默认没有分配slot节点的master是不参与rebalance的,设置--use-empty-masters可以让没有分配slot的节点参与rebalance。
  • --timeout <arg>:设置migrate命令的超时时间。
  • --simulate:设置该参数,可以模拟rebalance操作,提示用户会迁移哪些slots,而不会真正执行迁移操作。
  • --pipeline <arg>:与reshar的pipeline参数一样,定义cluster getkeysinslot命令一次取出的key数量,不传的话使用默认值为10。

例:

redis-trib.rb rebalance --weight 845674c71b1f43f9297501903e616140b2a0a1f6=3 --weight b7cada75939d960f8ca98aad875b8f2e49020b19=2 --use-empty-masters  192.168.20.61:7000

增加一个主节点

./redis-trib.rb add-node 192.168.1.107:7000 192.168.1.108:7000

# 添加成功,但是并没有指定 slot ,所以必须迁移slot节点
./redis-trib.rb reshard 192.168.1.108:7000

# 提示一 :How many slots do you want to move (from 1 to 16384)?
为了平衡每个master管理的slot的个数,所以输入 16384/master  的数量。如这里为4 那么就是 16384/4 = 4096个。
输入 4096

# 提示二:What is the receiving node ID?(接受的node ID是多少)
            890d2c8d989cce50e5fa48e37cd35738887f3f7d # 107的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.
                  (要从哪个节点中获取lost ?)
不打算从特定的节点上取出指定数量的哈希槽, 那么可以输入 all
否则输入某个节点的 node ID

# 检查是否成功
./redis-trib.rb check 192.168.1.108:7000

添加新节点

格式:

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

其中,
new_host:new_port:待添加的节点,必须确保其为空或不在其它集群中。否则,会提示以下错误。

[ERR] Node 192.168.1.101:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

所以,线上建议使用redis-trib.rb添加新节点,因为其会对新节点的状态进行检查。如果手动使用cluster meet命令加入已经存在于其它集群的节点,会造成被加入节点的集群合并到现有集群的情况,从而造成数据丢失和错乱,后果非常严重,线上谨慎操作。

existing_host:existing_port:集群中任意一个节点的地址。

如果添加的是主节点,只需指定源节点和目标节点的地址即可。

redis-trib.rb add-node 192.168.1.101:7000 127.0.0.1:6384

如果添加的是从节点,其语法如下,

redis-trib.rb add-node --slave --master-id f413fb7e6460308b17cdb71442798e1341b56cbc 192.168.1.101:7000 127.0.0.1:6384

注意:--slave和--master-id必须写在前面,同样的参数,如果是下面这样写法,会提示错误,

# redis-trib.rb add-node 192.168.1.101:7000 127.0.0.1:6384 --slave --master-id f413fb7e6460308b17cdb71442798e1341b56cbc
[ERR] Wrong number of arguments for specified sub command

添加从节点,可不设置--master-id,此时会随机选择主节点。

  • 从节点会自动匹配主节点
./redis-trib.rb add-node --slave 127.0.0.1:7007 127.0.0.1:7000
  • 增加从节点的时候指定主节点。
./redis-trib.rb add-node --slave --master-id 890d2c8d989cce50e5fa48e37cd35738887f3f7d 192.168.66.3:7008 192.168.66.2:7000

删除节点

格式

redis-trib.rb del-node host:port node_id
参数:
  • del-node:删除节点的指令;
  • host:port:从该节点获取集群信息;
  • node_id:需要删除的节点id。

在删除节点之前,其对应的槽必须为空,所以,在进行节点删除动作之前,必须使用redis-trib.rb reshard将其迁移出去。

需要注意的是,如果某个节点的槽被完全迁移出去,其对应的slave也会随着更新,指向迁移的目标节点。

./redis-trib.rb del-node 192.168.66.2:7000 d5f6d1d17426bd564a6e309f32d0f5b96962fe53

设置节点的超时时间

redis-trib.rb set-timeout host:port milliseconds

其实就是批量修改集群各节点的cluster-node-timeout参数。

# redis-trib.rb set-timeout 192.168.1.101:7000 20000
/usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
>>> Reconfiguring node timeout in every cluster node...
*** New timeout set for 192.168.1.101:7000
*** New timeout set for 192.168.1.102:7000
*** New timeout set for 192.168.1.103:7000
*** New timeout set for 192.168.1.104:7000
*** New timeout set for 192.168.1.105:7000
*** New timeout set for 192.168.1.106:7000
>>> New node timeout set. 6 OK, 0 ERR.

将外部redis数据导入集群

redis-trib.rb import --from 127.0.0.1:6378 127.0.0.1:6379

其内部处理流程如下:

1> 通过load_cluster_info_from_node方法加载集群信息,check_cluster方法检查集群是否健康。

2> 连接外部redis节点,如果外部节点开启了cluster_enabled,则提示错误([ERR] The source node should not be a cluster node.)

3> 通过scan命令遍历外部节点,一次获取1000条数据。

4> 遍历这些key,计算出key对应的slot。

5> 执行migrate命令,源节点是外部节点,目的节点是集群slot对应的节点,如果设置了--copy参数,则传递copy参数,其会保留源节点的key,如果设置了--replace,则传递replace参数。如果目标节点中存在同名key,其值会被覆盖。两个参数可同时指定。

6> 不停执行scan命令,直到遍历完所有key。

7> 迁移完成。

宕机情况

当某个从节点挂掉之后,对于redis集群来说几乎没有什么影响,相当于这个从节点对应的 主节点少了一个备份而已。
当某一个主节点挂掉之后,redis 会从这个 主节点 的 多个从节点 中推选一个出来,担当master的工作,并且把之前依附在
主节点的从节点调整依附到新的master上。如果新任的master也挂掉并且他没有从节点了,那么这个集群也真正的挂掉了。

集群创建时 replicas 参数 指定情况。

使用 --replicas 1 参数时,如果节点数量少于六个。
报错

*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 5 nodes and 1 replicas per node.
*** At least 6 nodes are required.

使用 --replicas 1 参数时,如果节点数量 大于六个,且为单数时。
这样会造成某个master拥有两个salve

参考

https://www.cnblogs.com/ivictor/p/9768010.html

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI