8.集群伸缩(自动扩缩容)02
这里说一个公式:
公式:slot = CRC16(key)mod 16384
这个公式是根据redis的键计算槽位,然后再把这个键值对放进这个槽位中。
在上一篇文章中介绍了redis cluster的手工扩容,扩容了一个节点6385主节点和一个节点6386节点,其中这两个扩容的核心就是槽位的移动,比如上上一篇中写的将原主节点上的槽4396移动到6385中,主要这里是仅仅移动了一个槽位而已,但是,在实际的过程中,我们扩容节点可能有很多,因此移动的槽位也会有很多,所以这里将介绍批量移动槽位的过程。
[root@redis ~]# redis-cli -c -p 6379 127.0.0.1:6379> cluster nodes 0c35e00aa5b8c0591c0519d3fcc0153f90d5f9c6 127.0.0.1:6385@16385 master - 0 1648562593000 7 connected 4096 39276f17bb64869a1b4c03433bde1f688a2065e9 127.0.0.1:6381@16381 master - 0 1648562596000 3 connected 10923-16383 889af758c59d392240d2d2d38e37c9df7be053ab 127.0.0.1:6384@16384 slave f87d0bac225f13d1d16e6f5cffecff3b83998484 0 1648562596000 1 connected 9b9c5a435fb8fa59d978a98be0e0aeaedb5027d5 127.0.0.1:6382@16382 slave 78f13905a498d20450033f04014f905193b251c0 0 1648562596571 2 connected da69fbf33f9395f4d9ed3016e69855b7fbb28f62 127.0.0.1:6383@16383 slave 39276f17bb64869a1b4c03433bde1f688a2065e9 0 1648562598592 3 connected 78f13905a498d20450033f04014f905193b251c0 127.0.0.1:6380@16380 master - 0 1648562597583 2 connected 5461-10922 24f0ba67684e8ea579af52245a315d01da217596 127.0.0.1:6386@16386 slave 0c35e00aa5b8c0591c0519d3fcc0153f90d5f9c6 0 1648562597000 7 connected f87d0bac225f13d1d16e6f5cffecff3b83998484 127.0.0.1:6379@16379 myself,master - 0 1648562598000 1 connected 0-4095 4097-5460
这里是之前只移动一个槽位所显示的各个节点的信息。
这里用Redis-cli reshard命令批量移动槽位
[root@redis ~]# redis-cli --cluster reshard 127.0.0.1:6379 ##这里可以指定原集群中任何一个节点 >>> Performing Cluster Check (using node 127.0.0.1:6379) M: f87d0bac225f13d1d16e6f5cffecff3b83998484 127.0.0.1:6379 slots:[0-4095],[4097-5460] (5460 slots) master 1 additional replica(s) M: 0c35e00aa5b8c0591c0519d3fcc0153f90d5f9c6 127.0.0.1:6385 slots:[4096] (1 slots) master 1 additional replica(s) M: 39276f17bb64869a1b4c03433bde1f688a2065e9 127.0.0.1:6381 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 889af758c59d392240d2d2d38e37c9df7be053ab 127.0.0.1:6384 slots: (0 slots) slave replicates f87d0bac225f13d1d16e6f5cffecff3b83998484 S: 9b9c5a435fb8fa59d978a98be0e0aeaedb5027d5 127.0.0.1:6382 slots: (0 slots) slave replicates 78f13905a498d20450033f04014f905193b251c0 S: da69fbf33f9395f4d9ed3016e69855b7fbb28f62 127.0.0.1:6383 slots: (0 slots) slave replicates 39276f17bb64869a1b4c03433bde1f688a2065e9 M: 78f13905a498d20450033f04014f905193b251c0 127.0.0.1:6380 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 24f0ba67684e8ea579af52245a315d01da217596 127.0.0.1:6386 slots: (0 slots) slave replicates 0c35e00aa5b8c0591c0519d3fcc0153f90d5f9c6 [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)? 4096 ##这里需要确认迁移槽的数量,我的理解是迁移到新节点上槽位的数量 What is the receiving node ID? 0c35e00aa5b8c0591c0519d3fcc0153f90d5f9c6 ##这里是目标节点的id,只能写一个,我的目标节点是6385 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: f87d0bac225f13d1d16e6f5cffecff3b83998484 ##源节点run_id 6379 Source node #2: 39276f17bb64869a1b4c03433bde1f688a2065e9 ##源节点run_id 6380 Source node #3: 78f13905a498d20450033f04014f905193b251c0 ##源节点run_id 6381 Source node #4: done ##这里是结束
注意: 1.填写的源节点的run_id必须都是源节点的master节点
2.填写三个原节点意思就是将由这三个原节点组成的集群的槽位(16384个)分4096个槽位给6385节点
执行流程展示结果:
Ready to move 4096 slots. Source nodes: M: f87d0bac225f13d1d16e6f5cffecff3b83998484 127.0.0.1:6379 slots:[0-4095],[4097-5460] (5460 slots) master 1 additional replica(s) M: 39276f17bb64869a1b4c03433bde1f688a2065e9 127.0.0.1:6381 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: 78f13905a498d20450033f04014f905193b251c0 127.0.0.1:6380 slots:[5461-10922] (5462 slots) master 1 additional replica(s) Destination node: M: 0c35e00aa5b8c0591c0519d3fcc0153f90d5f9c6 127.0.0.1:6385 slots:[4096] (1 slots) master 1 additional replica(s) Resharding plan: Moving slot 5461 from 78f13905a498d20450033f04014f905193b251c0 Moving slot 5462 from 78f13905a498d20450033f04014f905193b251c0 Moving slot 5463 from 78f13905a498d20450033f04014f905193b251c0 ...... ...... ......
最后在来看一下集群节点信息
[root@redis ~]# redis-cli -p 6379 127.0.0.1:6379> 127.0.0.1:6379> cluster nodes 0c35e00aa5b8c0591c0519d3fcc0153f90d5f9c6 127.0.0.1:6385@16385 master - 0 1648564087000 7 connected 0-1364 4096 5461-6826 10923-12287 39276f17bb64869a1b4c03433bde1f688a2065e9 127.0.0.1:6381@16381 master - 0 1648564084000 3 connected 12288-16383 889af758c59d392240d2d2d38e37c9df7be053ab 127.0.0.1:6384@16384 slave f87d0bac225f13d1d16e6f5cffecff3b83998484 0 1648564086052 1 connected 9b9c5a435fb8fa59d978a98be0e0aeaedb5027d5 127.0.0.1:6382@16382 slave 78f13905a498d20450033f04014f905193b251c0 0 1648564085000 2 connected da69fbf33f9395f4d9ed3016e69855b7fbb28f62 127.0.0.1:6383@16383 slave 39276f17bb64869a1b4c03433bde1f688a2065e9 0 1648564088070 3 connected 78f13905a498d20450033f04014f905193b251c0 127.0.0.1:6380@16380 master - 0 1648564088000 2 connected 6827-10922 24f0ba67684e8ea579af52245a315d01da217596 127.0.0.1:6386@16386 slave 0c35e00aa5b8c0591c0519d3fcc0153f90d5f9c6 0 1648564087060 7 connected f87d0bac225f13d1d16e6f5cffecff3b83998484 127.0.0.1:6379@16379 myself,master - 0 1648564085000 1 connected 1365-4095 4097-5460
从这里可以看出6385节点已经被分配了0-1364 4096 5461-6826 10923-12287 共1365+1+1366+1364=4096 个槽位 。。。很nice。。。
由于槽用 于hash运算本身顺序没有意义,因此无须强制要求节点负责槽的顺序性,因此,这里使用命令rebalance命令检查节点之间槽的均衡性。
[root@redis ~]# redis-cli --cluster rebalance 127.0.0.1:6379 >>> Performing Cluster Check (using node 127.0.0.1:6379) [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%左右。因此不用重新再平衡了。。。
因此。以上过程扩容完毕。。。