【Redis学习笔记】redis-trib.rb命令详解

小咪咪 2024-04-08 11:30 140阅读 0赞

文章目录

  • 1、redis-trib.rb环境配置
  • 2、创建集群
  • 3、查看集群信息
  • 4、 检查集群状态
  • 5、在线迁移slot
  • 6、平衡集群节点slot数量
  • 7、添加新节点
  • 8、删除节点
  • 9、修复集群
  • 10、设置节点超时时间
  • 11、在集群所有节点上执行命令
  • 12、将外部redis数据导入集群

redis-trib.rb是官方提供的Redis Cluster的管理工具,在src目录下,但该工具是用ruby开发的,所以需要准备相关的依赖环境。

1、redis-trib.rb环境配置

  1. 准备redis-trib.rb的运行环境
  2. wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.gz
  3. yum -y install zlib-devel
  4. tar xvf ruby-2.5.1.tar.gz
  5. cd ruby-2.5.1/
  6. ./configure -prefix=/usr/local/ruby
  7. make
  8. make install
  9. cd /usr/local/ruby/
  10. cp bin/ruby /usr/local/bin
  11. cp bin/gem /usr/local/bin
  12. -------------------------------------
  13. 有的Linux版本,直接yum install ruby可一步到位
  14. 安装rubygem redis依赖
  15. wget http://rubygems.org/downloads/redis-3.3.0.gem
  16. gem install -l redis-3.3.0.gem

2、创建集群

指令:

redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384

注:
–replicas参数指定集群中每个主节点配备几个从节点,这里设置为1,即三主三从

结果:

  1. >>> Creating cluster
  2. /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
  3. >>> Performing hash slots allocation on 6 nodes...
  4. Using 3 masters:
  5. 127.0.0.1:6379
  6. 127.0.0.1:6380
  7. 127.0.0.1:6381
  8. Adding replica 127.0.0.1:6383 to 127.0.0.1:6379
  9. Adding replica 127.0.0.1:6384 to 127.0.0.1:6380
  10. Adding replica 127.0.0.1:6382 to 127.0.0.1:6381
  11. >>> Trying to optimize slaves allocation for anti-affinity
  12. [WARNING] Some slaves are in the same host as their master
  13. M: bc775f9c4dea40820b82c9451778b1fcd42f92bc 127.0.0.1:6379
  14. slots:0-5460 (5461 slots) master
  15. M: 3b27d00d13706a032a92ff6b0a914af272dcaaf2 127.0.0.1:6380
  16. slots:5461-10922 (5462 slots) master
  17. M: d874f003257f1fb036bbd856ca605172a1741232 127.0.0.1:6381
  18. slots:10923-16383 (5461 slots) master
  19. S: 648eb314863b82aaa676380be7db2ec307f5547d 127.0.0.1:6382
  20. replicates bc775f9c4dea40820b82c9451778b1fcd42f92bc
  21. S: 65a6efb441ac44c348f7da8c62e26b888cda7c48 127.0.0.1:6383
  22. replicates 3b27d00d13706a032a92ff6b0a914af272dcaaf2
  23. S: 57bda956485109552547aef6c77fba43d2124abf 127.0.0.1:6384
  24. replicates d874f003257f1fb036bbd856ca605172a1741232
  25. Can I set the above configuration? (type 'yes' to accept): yes
  26. >>> Nodes configuration updated
  27. >>> Assign a different config epoch to each node
  28. >>> Sending CLUSTER MEET messages to join the cluster
  29. Waiting for the cluster to join...
  30. >>> Performing Cluster Check (using node 127.0.0.1:6379)
  31. M: bc775f9c4dea40820b82c9451778b1fcd42f92bc 127.0.0.1:6379
  32. slots:0-5460 (5461 slots) master
  33. 1 additional replica(s)
  34. S: 648eb314863b82aaa676380be7db2ec307f5547d 127.0.0.1:6382
  35. slots: (0 slots) slave
  36. replicates bc775f9c4dea40820b82c9451778b1fcd42f92bc
  37. M: 3b27d00d13706a032a92ff6b0a914af272dcaaf2 127.0.0.1:6380
  38. slots:5461-10922 (5462 slots) master
  39. 1 additional replica(s)
  40. S: 57bda956485109552547aef6c77fba43d2124abf 127.0.0.1:6384
  41. slots: (0 slots) slave
  42. replicates d874f003257f1fb036bbd856ca605172a1741232
  43. S: 65a6efb441ac44c348f7da8c62e26b888cda7c48 127.0.0.1:6383
  44. slots: (0 slots) slave
  45. replicates 3b27d00d13706a032a92ff6b0a914af272dcaaf2
  46. M: d874f003257f1fb036bbd856ca605172a1741232 127.0.0.1:6381
  47. slots:10923-16383 (5461 slots) master
  48. 1 additional replica(s)
  49. [OK] All nodes agree about slots configuration.
  50. >>> Check for open slots...
  51. >>> Check slots coverage...
  52. [OK] All 16384 slots covered.

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

3、查看集群信息

指令:

redis-trib.rb info 127.0.0.1:6383

结果:

  1. /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
  2. 127.0.0.1:6380 (3b27d00d...) -> 0 keys | 5462 slots | 1 slaves.
  3. 127.0.0.1:6381 (d874f003...) -> 1 keys | 5461 slots | 1 slaves.
  4. 127.0.0.1:6379 (bc775f9c...) -> 0 keys | 5461 slots | 1 slaves.
  5. [OK] 1 keys in 3 masters.
  6. 0.00 keys per slot on average.

4、 检查集群状态

指令:

redis-trib.rb check 127.0.0.1:6379

结果:

  1. /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
  2. >>> Performing Cluster Check (using node 127.0.0.1:6379)
  3. M: bc775f9c4dea40820b82c9451778b1fcd42f92bc 127.0.0.1:6379
  4. slots:0-5460 (5461 slots) master
  5. 1 additional replica(s)
  6. S: 648eb314863b82aaa676380be7db2ec307f5547d 127.0.0.1:6382
  7. slots: (0 slots) slave
  8. replicates bc775f9c4dea40820b82c9451778b1fcd42f92bc
  9. M: 3b27d00d13706a032a92ff6b0a914af272dcaaf2 127.0.0.1:6380
  10. slots:5461-10922 (5462 slots) master
  11. 1 additional replica(s)
  12. S: 57bda956485109552547aef6c77fba43d2124abf 127.0.0.1:6384
  13. slots: (0 slots) slave
  14. replicates d874f003257f1fb036bbd856ca605172a1741232
  15. S: 65a6efb441ac44c348f7da8c62e26b888cda7c48 127.0.0.1:6383
  16. slots: (0 slots) slave
  17. replicates 3b27d00d13706a032a92ff6b0a914af272dcaaf2
  18. M: d874f003257f1fb036bbd856ca605172a1741232 127.0.0.1:6381
  19. slots:10923-16383 (5461 slots) master
  20. 1 additional replica(s)
  21. [OK] All nodes agree about slots configuration.
  22. >>> Check for open slots...
  23. >>> Check slots coverage...
  24. [OK] All 16384 slots covered.

5、在线迁移slot

指令:

redis-trib.rb reshard 127.0.0.1:6379

结果:

  1. ...
  2. How many slots do you want to move (from 1 to 16384)? 200
  3. What is the receiving node ID? 3b27d00d13706a032a92ff6b0a914af272dcaaf2
  4. Please enter all the source node IDs.
  5. Type 'all' to use all the nodes as source nodes for the hash slots.
  6. Type 'done' once you entered all the source nodes IDs.
  7. Source node #1:bc775f9c4dea40820b82c9451778b1fcd42f92bc
  8. Source node #2:done
  9. Ready to move 200 slots.
  10. ...
  11. Do you want to proceed with the proposed reshard plan (yes/no)? yes
  12. Moving slot 3225 from 127.0.0.1:6379 to 127.0.0.1:6380: .
  13. Moving slot 3226 from 127.0.0.1:6379 to 127.0.0.1:6380:

注意以上的交互:

  • 首先会提示需要迁移多个槽,我这里写的是200
  • 接着提示需要将槽迁移到哪个节点,这里必须写节点ID
  • 提示槽从哪些节点中迁出,指定为all,则待迁移的槽在剩余节点中平均分配;指定节点中迁出时,必须指定源节点的节点ID,最后以done结束

    也可无交互迁移:

    redis-trib.rb reshard host:port —from —to —slots —yes —timeout —pipeline

6、平衡集群节点slot数量

指令:

redis-trib.rb rebalance host:port —weight —auto-weights ……

结果:

  1. redis-trib.rb rebalance --weight a8xx=3 --weight f41xx=2 --use-empty-masters 127.0.0.1:6379
  2. /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
  3. >>> Performing Cluster Check (using node 127.0.0.1:6379)
  4. [OK] All nodes agree about slots configuration.
  5. >>> Check for open slots...
  6. >>> Check slots coverage...
  7. [OK] All 16384 slots covered.
  8. >>> Rebalancing across 2 nodes. Total weight = 5.0
  9. Moving 3824 slots from 127.0.0.1:6380 to 127.0.0.1:6381

7、添加新节点

指令:

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

其中:new_host:new_port是待添加的节点,existing_host:existing_port是集群中任意一个节点的地址

  1. 加入的是主节点:
  2. redis-trib.rb add-node 127.0.0.1:6379 127.0.0.1:6384
  3. 加入的是从节点(可不设置--master-id,此时会随机选择主节点):
  4. redis-trib.rb add-node --slave --master-id f4xxx 127.0.0.1:6379 127.0.0.1:6384

注:

  • 待添加的节点,必须确保其为空或不在其它集群中,否则抛错

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

  • cluster meet命令加入已经存在于其它集群的节点,不做校验,会造成丢失和错乱,不推荐用于生产环境

  • 添加从节点时,–slave和–master-id必须写在前面。否则报错

    redis-trib.rb add-node 127.0.0.1:6379 127.0.0.1:6384 —slave —master-id f41xx
    [ERR] Wrong number of arguments for specified sub command

8、删除节点

指令:

redis-trib.rb del-node host:port node_id

结果:

  1. redis-trib.rb del-node 127.0.0.1:6379 8f7836a9a14fb6638530b42e04f5e58e28de0a6c
  2. >>> Removing node 8f7836a9a14fb6638530b42e04f5e58e28de0a6c from cluster 127.0.0.1:6379
  3. /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
  4. >>> Sending CLUSTER FORGET messages to the cluster...
  5. >>> SHUTDOWN the node.

注:

  • 在删除节点之前,其对应的槽必须为空。(使用redis-trib.rb reshard将其迁移出去)

9、修复集群

指令:

redis-trib.rb fix 127.0.0.1:6379

fix能修复两种异常:

  • 节点中存在处于迁移中(importing或migrating状态)的slot
  • 节点中存在未分配的slot

10、设置节点超时时间

指令:

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

作用:

  • 修改配置文件中的cluster-node-timeout参数

11、在集群所有节点上执行命令

指令:

redis-trib.rb call host:port command arg arg … arg

结果:

  1. redis-trib.rb call 127.0.0.1:6379 set hello world
  2. /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
  3. >>> Calling SET hello world
  4. 127.0.0.1:6379: MOVED 866 127.0.0.1:6381
  5. 127.0.0.1:6383: MOVED 866 127.0.0.1:6381
  6. 127.0.0.1:6381: OK
  7. 127.0.0.1:6382: MOVED 866 127.0.0.1:6381
  8. 127.0.0.1:6384: MOVED 866 127.0.0.1:6381
  9. 127.0.0.1:6380: MOVED 866 127.0.0.1:6381

12、将外部redis数据导入集群

指令:

redis-trib.rb import host:port —from —copy —replace

结果:

  1. redis-trib.rb import --from 127.0.0.1:6378 --replace 127.0.0.1:6379
  2. >>> Importing data from 127.0.0.1:6378 to cluster
  3. /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
  4. >>> Performing Cluster Check (using node 127.0.0.1:6379)
  5. S: d826c5fd98efa8a17a880e9a90a25f06c88e6ae9 127.0.0.1:6379
  6. slots: (0 slots) slave
  7. replicates a8b3d0f9b12d63dab3b7337d602245d96dd55844
  8. S: 55c05d5b0dfea0d52f88548717ddf24975268de6 127.0.0.1:6383
  9. slots: (0 slots) slave
  10. replicates a8b3d0f9b12d63dab3b7337d602245d96dd55844
  11. M: f413fb7e6460308b17cdb71442798e1341b56cbc 127.0.0.1:6381
  12. slots:50-16383 (16334 slots) master
  13. 2 additional replica(s)
  14. S: beba753c5a63607fa66d9ec7427ed9a511ea136e 127.0.0.1:6382
  15. slots: (0 slots) slave
  16. replicates f413fb7e6460308b17cdb71442798e1341b56cbc
  17. S: 83797d518e56c235272402611477f576973e9d34 127.0.0.1:6384
  18. slots: (0 slots) slave
  19. replicates f413fb7e6460308b17cdb71442798e1341b56cbc
  20. M: a8b3d0f9b12d63dab3b7337d602245d96dd55844 127.0.0.1:6380
  21. slots:0-49 (50 slots) master
  22. 2 additional replica(s)
  23. [OK] All nodes agree about slots configuration.
  24. >>> Check for open slots...
  25. >>> Check slots coverage...
  26. [OK] All 16384 slots covered.
  27. >>> Connecting to the source Redis instance
  28. *** Importing 1 keys from DB 0
  29. Migrating key5 to 127.0.0.1:6381: OK

记住动作单词,参数 redis-trib.rb help

参考文档:
https://www.cnblogs.com/ivictor/p/9768010.html

发表评论

表情:
评论列表 (有 0 条评论,140人围观)

还没有评论,来说两句吧...

相关阅读