堕落不振功业废,勤耕不辍日月新

redis学习(10)—redis哈希槽扩容与缩小

综合 hailen 14℃

1、将节点重新加入集群
6387为主节点,6388为从节点
步骤看上一篇

2、重新分配槽号
redis-cli --cluster reshard 192.168.1.138:6381
说明:
ip:port:为集群中任意一个节点就行
步骤一:

>>> Performing Cluster Check (using node 192.168.1.138:6381)
M: c790b5f9584777ba8d9f96be5fe42b2efe646635 192.168.1.138:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 7599539245821b1a2e185439f3170b896b287106 192.168.1.138:6388
   slots: (0 slots) slave
   replicates ca77bc2a223c64a7af727b24172ae8f43d3e9b5a
S: b264b25c716863096d0c2fe39483130cbfeba894 192.168.1.138:6385
   slots: (0 slots) slave
   replicates f9200d36046d8325817c8014155e8f9950760da4
S: 33add5fdf6d9cc3eb8656c865403a34b237b4b73 192.168.1.138:6384
   slots: (0 slots) slave
   replicates 691e5855e8f274a6ea61dc95dbed89da87ed6053
S: d7bbc7bb0b501b421d527f6c302a8abe00aa535b 192.168.1.138:6386
   slots: (0 slots) slave
   replicates c790b5f9584777ba8d9f96be5fe42b2efe646635
M: f9200d36046d8325817c8014155e8f9950760da4 192.168.1.138:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: ca77bc2a223c64a7af727b24172ae8f43d3e9b5a 192.168.1.138:6387
   slots: (0 slots) master
   1 additional replica(s)
M: 691e5855e8f274a6ea61dc95dbed89da87ed6053 192.168.1.138:6382
   slots:[5461-10922] (5462 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.
How many slots do you want to move (from 1 to 16384)? 4096

填4096,总共有16384个槽号,你想移动多少个槽号,由于我们是4台主,所以平均分配16384/4=4096
步骤二:
What is the receiving node ID? ca77bc2a223c64a7af727b24172ae8f43d3e9b5a
要把这些槽号,移到哪个节点上,填节点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.
Source node #1: all

这里填all,问你从哪个节点中取出4096个哈希槽,还是从全部节点提取4096个哈希槽, 并将这些槽移动到6387节点上面
步骤四:
……
Moving slot 12284 from f9200d36046d8325817c8014155e8f9950760da4
Moving slot 12285 from f9200d36046d8325817c8014155e8f9950760da4
Moving slot 12286 from f9200d36046d8325817c8014155e8f9950760da4
Moving slot 12287 from f9200d36046d8325817c8014155e8f9950760da4
Do you want to proceed with the proposed reshard plan (yes/no)? yes

3、查看集群信息
redis-cli --cluster check 192.168.1.138:6381

192.168.1.138:6381 (c790b5f9...) -> 0 keys | 4096 slots | 1 slaves.
192.168.1.138:6383 (f9200d36...) -> 1 keys | 4096 slots | 1 slaves.
192.168.1.138:6387 (ca77bc2a...) -> 0 keys | 4096 slots | 1 slaves.
192.168.1.138:6382 (691e5855...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 2 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.138:6381)
M: c790b5f9584777ba8d9f96be5fe42b2efe646635 192.168.1.138:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: 7599539245821b1a2e185439f3170b896b287106 192.168.1.138:6388
   slots: (0 slots) slave
   replicates ca77bc2a223c64a7af727b24172ae8f43d3e9b5a
S: b264b25c716863096d0c2fe39483130cbfeba894 192.168.1.138:6385
   slots: (0 slots) slave
   replicates f9200d36046d8325817c8014155e8f9950760da4
S: 33add5fdf6d9cc3eb8656c865403a34b237b4b73 192.168.1.138:6384
   slots: (0 slots) slave
   replicates 691e5855e8f274a6ea61dc95dbed89da87ed6053
S: d7bbc7bb0b501b421d527f6c302a8abe00aa535b 192.168.1.138:6386
   slots: (0 slots) slave
   replicates c790b5f9584777ba8d9f96be5fe42b2efe646635
M: f9200d36046d8325817c8014155e8f9950760da4 192.168.1.138:6383
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: ca77bc2a223c64a7af727b24172ae8f43d3e9b5a 192.168.1.138:6387
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
M: 691e5855e8f274a6ea61dc95dbed89da87ed6053 192.168.1.138:6382
   slots:[6827-10922] (4096 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.

192.168.1.138:6387这个master变成了很多个槽号,槽号是不连续的

4、为什么新加入的节点是3个区间,其他节点是单区间
因为对于新加入的节点,要重新分配槽号成本太大,故采用了挪移的方式

二、哈希槽缩小

下线6387(master)和6388(slave)这两个节点

1、删除master对应的slave
redis-cli --cluster check 192.168.1.138:6381
获取节点node ID
把6387master对应的6388slave删除
redis-cli --cluster del-node 192.168.1.138:6388 7599539245821b1a2e185439f3170b896b287106

2、把master 6387的所有槽号迁移给其他master
redis-cli --cluster reshard 192.168.1.138:6381
步骤一:

>>> Performing Cluster Check (using node 192.168.1.138:6381)
M: c790b5f9584777ba8d9f96be5fe42b2efe646635 192.168.1.138:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: b264b25c716863096d0c2fe39483130cbfeba894 192.168.1.138:6385
   slots: (0 slots) slave
   replicates f9200d36046d8325817c8014155e8f9950760da4
S: 33add5fdf6d9cc3eb8656c865403a34b237b4b73 192.168.1.138:6384
   slots: (0 slots) slave
   replicates 691e5855e8f274a6ea61dc95dbed89da87ed6053
S: d7bbc7bb0b501b421d527f6c302a8abe00aa535b 192.168.1.138:6386
   slots: (0 slots) slave
   replicates c790b5f9584777ba8d9f96be5fe42b2efe646635
M: f9200d36046d8325817c8014155e8f9950760da4 192.168.1.138:6383
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: ca77bc2a223c64a7af727b24172ae8f43d3e9b5a 192.168.1.138:6387
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: 691e5855e8f274a6ea61dc95dbed89da87ed6053 192.168.1.138:6382
   slots:[6827-10922] (4096 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.
How many slots do you want to move (from 1 to 16384)? 4096

要移动多少个槽号
步骤二:
What is the receiving node ID? c790b5f9584777ba8d9f96be5fe42b2efe646635
谁来接收这些槽号,指定6381节点
步骤三:
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: ca77bc2a223c64a7af727b24172ae8f43d3e9b5a
Source node #2: done

从哪个节点中取出这些槽号,我们从6387节点中取出,当node ID输入完后,输入done结束
步骤四:
……
Moving slot 12284 from ca77bc2a223c64a7af727b24172ae8f43d3e9b5a
Moving slot 12285 from ca77bc2a223c64a7af727b24172ae8f43d3e9b5a
Moving slot 12286 from ca77bc2a223c64a7af727b24172ae8f43d3e9b5a
Moving slot 12287 from ca77bc2a223c64a7af727b24172ae8f43d3e9b5a
Do you want to proceed with the proposed reshard plan (yes/no)? yes

3、查看集群信息
redis-cli --cluster check 192.168.1.138:6381

192.168.1.138:6381 (c790b5f9...) -> 0 keys | 8192 slots | 1 slaves.
192.168.1.138:6383 (f9200d36...) -> 1 keys | 4096 slots | 1 slaves.
192.168.1.138:6387 (ca77bc2a...) -> 0 keys | 0 slots | 0 slaves.
192.168.1.138:6382 (691e5855...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 2 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.138:6381)
M: c790b5f9584777ba8d9f96be5fe42b2efe646635 192.168.1.138:6381
   slots:[0-6826],[10923-12287] (8192 slots) master
   1 additional replica(s)
S: b264b25c716863096d0c2fe39483130cbfeba894 192.168.1.138:6385
   slots: (0 slots) slave
   replicates f9200d36046d8325817c8014155e8f9950760da4
S: 33add5fdf6d9cc3eb8656c865403a34b237b4b73 192.168.1.138:6384
   slots: (0 slots) slave
   replicates 691e5855e8f274a6ea61dc95dbed89da87ed6053
S: d7bbc7bb0b501b421d527f6c302a8abe00aa535b 192.168.1.138:6386
   slots: (0 slots) slave
   replicates c790b5f9584777ba8d9f96be5fe42b2efe646635
M: f9200d36046d8325817c8014155e8f9950760da4 192.168.1.138:6383
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: ca77bc2a223c64a7af727b24172ae8f43d3e9b5a 192.168.1.138:6387
   slots: (0 slots) master
M: 691e5855e8f274a6ea61dc95dbed89da87ed6053 192.168.1.138:6382
   slots:[6827-10922] (4096 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.

4、为什么要把master 6387所有槽都给master 6381
因为这里只输入了一个ID,所以我们偷懒给了6381,6381有8192个哈希槽
如果要均匀分配,需要输入3个ID

5、删除master 6387
redis-cli --cluster del-node 192.168.1.138:6387 ca77bc2a223c64a7af727b24172ae8f43d3e9b5a

转载请注明:我是IT » redis学习(10)—redis哈希槽扩容与缩小

喜欢 (0)or分享 (0)