redis实战第九篇 集群扩容自动迁移槽(redis-cli)

上文讲解过自动迁移槽实现集群扩容(传送门)
1.准备新节点
安装redis,参考传送门
节点配置,参考传送门

2.将节点加入集群
redis-cli --cluster add-node {new host}:{new port} {exist host}:{exist port} 加入集群

redis-cli --cluster add-node 192.168.0.207:6380 192.168.0.31:6380
redis-cli --cluster add-node 192.168.0.208:6380 192.168.0.31:6380

确认集群状态

[root@kafka31 data]# redis-cli -p 6380
127.0.0.1:6380> cluster nodes
3c1b16407c984f33b7c0ff06b8a7bbcc59c1d91b 192.168.0.208:6380@16380 master - 0 1550229198271 11 connected
ed93d5ea74751d7124a2d5830ce0806a0c962d43 192.168.0.32:6380@16380 master - 0 1550229196000 2 connected 5461-10922
3ba3e8323b7b637c958977335bf7f7213c009929 192.168.0.35:6380@16380 slave ed93d5ea74751d7124a2d5830ce0806a0c962d43 0 1550229197267 5 connected
7fbf45fdc4d0780074f8fe324aac28ae686eebaa 192.168.0.34:6380@16380 slave 92fd7c2a7b7b8933d1019e72a852f621f6b4faff 0 1550229196264 10 connected
b94828e8816574d66b413c6bfa6de130eb14ee66 192.168.0.207:6380@16380 master - 0 1550229194000 0 connected
3641ec8359d5400e5540d77600a2360de8ca367e 192.168.0.33:6380@16380 master - 0 1550229194258 3 connected 10923-16383
a5ac7dfedd81f2ecd9dd101ef8d9d4f70f895bbf 192.168.0.36:6380@16380 slave 3641ec8359d5400e5540d77600a2360de8ca367e 0 1550229193256 6 connected
92fd7c2a7b7b8933d1019e72a852f621f6b4faff 192.168.0.31:6380@16380 myself,master - 0 1550229194000 10 connected 0-5460

3.使用redis-cli自动迁移槽
前面有介绍过手动迁移槽的过程,但是过程繁琐,容易出错,这里使用redis-cli自动迁移槽。

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

参数说明:
host:port:必传参数,集群内任意节点地址,用来获取整个集群信息。
--from:制定源节点的id,如果有多个源节点,使用逗号分隔,如果是all源节点变为集群内所有主节点,在迁移过程中提示用户输入。
--to:需要迁移的目标节点的id,目标节点只能填写一个,在迁移过程中提示用户输入。
--slots:需要迁移槽的总数量,在迁移过程中提示用户输入。
--yes:当打印出reshard执行计划时,是否需要用户输入yes确认后再执行reshard。
-timeout:控制每次migrate操作的超时时间,默认为60000毫秒。
--pipeline:控制每次批量迁移键的数量,默认为10。

使用redis-cli迁移4096个槽到207

redis-cli --cluster reshard 192.168.0.31:6380
...
How many slots do you want to move (from 1 to 16384)? 4096  #要迁移多少个槽
What is the receiving node ID? b94828e8816574d66b413c6bfa6de130eb14ee66  #迁移到哪个节点
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.
#要求输入源节点的id,这里输入31,32,33三个节点,输入done表示结束
Source node #1: 92fd7c2a7b7b8933d1019e72a852f621f6b4faff 
Source node #2: ed93d5ea74751d7124a2d5830ce0806a0c962d43
Source node #3: 3641ec8359d5400e5540d77600a2360de8ca367e
Source node #4: done
最后会有一个迁移方案,输入yes表示同意,迁移开始。输入no表示不同意,重新设置迁移方案。

迁移完成后,确认集群状态,0-1364 5461-6826 10923-12287这些槽分配给了207节点

[root@kafka31 data]# redis-cli -p 6380
127.0.0.1:6380> cluster nodes
3c1b16407c984f33b7c0ff06b8a7bbcc59c1d91b 192.168.0.208:6380@16380 master - 0 1550230101738 11 connected
ed93d5ea74751d7124a2d5830ce0806a0c962d43 192.168.0.32:6380@16380 master - 0 1550230098728 2 connected 6827-10922
3ba3e8323b7b637c958977335bf7f7213c009929 192.168.0.35:6380@16380 slave ed93d5ea74751d7124a2d5830ce0806a0c962d43 0 1550230100000 5 connected
7fbf45fdc4d0780074f8fe324aac28ae686eebaa 192.168.0.34:6380@16380 slave 92fd7c2a7b7b8933d1019e72a852f621f6b4faff 0 1550230100734 10 connected
b94828e8816574d66b413c6bfa6de130eb14ee66 192.168.0.207:6380@16380 master - 0 1550230098000 12 connected 0-1364 5461-6826 10923-12287
3641ec8359d5400e5540d77600a2360de8ca367e 192.168.0.33:6380@16380 master - 0 1550230100000 3 connected 12288-16383
a5ac7dfedd81f2ecd9dd101ef8d9d4f70f895bbf 192.168.0.36:6380@16380 slave 3641ec8359d5400e5540d77600a2360de8ca367e 0 1550230099000 6 connected
92fd7c2a7b7b8933d1019e72a852f621f6b4faff 192.168.0.31:6380@16380 myself,master - 0 1550230097000 10 connected 1365-5460

4.检测节点之间槽的均衡性
使用redis-cli --cluster rebalance检测节点间槽的均衡性

[root@kafka31 data]# redis-cli --cluster rebalance 192.168.0.31:6380
>>> Performing Cluster Check (using node 192.168.0.31:6380)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 2.00% threshold.

可以看出,节点负责的槽数据差异在2%以内,因此槽分配均衡。

5.添加从节点
将208添加为207的从节点

127.0.0.1:6380> cluster replicate b94828e8816574d66b413c6bfa6de130eb14ee66
OK

确认集群状态,208为207的从节点,207负责0-1364 5461-6826 10923-12287这4096个槽,至此使用redis-cli自动迁移槽完成。

127.0.0.1:6380> cluster nodes
3c1b16407c984f33b7c0ff06b8a7bbcc59c1d91b 192.168.0.208:6380@16380 myself,slave b94828e8816574d66b413c6bfa6de130eb14ee66 0 1550230723000 11 connected
...
b94828e8816574d66b413c6bfa6de130eb14ee66 192.168.0.207:6380@16380 master - 0 1550230720727 12 connected 0-1364 5461-6826 10923-12287 

你可能感兴趣的:(redis,redis,实战)