redis trib.rb管理cluster集群 - 1990bingliu/redis GitHub Wiki
配置好redis.conf后,使用下命令创建新的一个3个节点集群
$ redis-trib.rb create <host_master-1>:<port-1> <host_master-2>:<port-2> <host_master-3>:<port-3>
如需要指定分配slave机,则使用replicas参数
$ redis-trib.rb create <host_master-1>:<port-1> <host_master-2>:<port-2> <host_master-3>:<port-3> <host_slave-4>:<port-4> <host_slave-5>:<port-5> <host_slave-6>:<port-6>
指定任意一台cluster节点中的redis服务器即可,主从都可以
$ redis-trib.rb check <host>:<port>
>>> Performing Cluster Check (using node 10.0.2.8:7001)
S: 5f54805f02b6666589ed7f014b454555fba42d62 10.0.2.8:7001
slots: (0 slots) slave
replicates 1f758f3dcca92e3694f9b4dfd35733eb2109f9f0
S: c41e95ad470d5377cdeac57079d9b50fb070aba9 10.0.2.8:8002
slots: (0 slots) slave
replicates bfb4b4d0b909e13fd3171cc38afac715dc952078
M: bfb4b4d0b909e13fd3171cc38afac715dc952078 10.0.2.8:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 1f758f3dcca92e3694f9b4dfd35733eb2109f9f0 10.0.2.8:8001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 3349f8e30c1267c99e4e64377060fb84605b4a3c 10.0.2.8:8003
slots: (0 slots) slave
replicates e851101f2f61d5fec8550cecc7e5708683727d7f
M: e851101f2f61d5fec8550cecc7e5708683727d7f 10.0.2.8:7003
slots:10923-16383 (5461 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-trb.rb info <host>:<port>
10.0.2.8:7002 (bfb4b4d0...) -> 1 keys | 5462 slots | 1 slaves.
10.0.2.8:8001 (1f758f3d...) -> 7 keys | 5461 slots | 1 slaves.
10.0.2.8:7003 (e851101f...) -> 5 keys | 5461 slots | 1 slaves.
[OK] 13 keys in 3 masters.
0.00 keys per slot on average.
reshard host:port 必须的,可以集群中任意节点
--from <arg> 从哪些节点中迁移solt, 可以使用--form all
--to <arg> 目标节点node id(不是ip哦),只能写一个
--slots <arg> 迁移的slots
--yes 加了这个参数,会提示输入yes再继续执行
--timeout <arg> migrate命令超时时间
--pipeline <arg> cluster getkeysinslot命令一次取出的key数量,不传的话使用默认值为10
如:redis-trib.rb reshard --from all --to 80b661ecca260c89e3d8ea9b98f77edaeef43dcd --slots 11 192.168.10.199:6379
可以增加master,也可增加master的slave
add-node <new_host>:<new_port> <existing_host>:<existing_port>
--slave
--master-id <arg>
只能删除没有被分配slots的master, 如有,则应新迁移后,再删除
del-node
<host>:<port> 从该节点获取集群信息。
node_id 需要删除的节点id。
rebalance命令可以根据用户传入的参数平衡集群节点的slot数量,rebalance功能非常强大,可以传入的参数很多,以下是rebalance的参数列表和命令示例。
rebalance host:port
--weight <arg>
--auto-weights
--threshold <arg>
--use-empty-masters
--timeout <arg>
--simulate
--pipeline <arg>
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 --threshold 1 --weight b31e3a2e=5 --weight 60b8e3a1=5 --use-empty-masters --simulate 192.168.10.199:6379
set-timeout用来设置集群节点间心跳连接的超时时间,单位是毫秒,不得小于100毫秒,因为100毫秒对于心跳时间来说太短了。该命令修改是节点配置参数cluster-node-timeout,默认是15000毫秒。通过该命令,可以给每个节点设置超时时间,设置的方式使用config set命令动态设置,然后执行config rewrite命令将配置持久化保存到硬盘。以下是示例:
redis-trib.rb set-timeout 192.168.10.199:6379 30000