redis cluster-手动扩缩容

一. 垂直扩容

    1)redis-cli客户端连接上服务端

    2)修改配置config set maxmemory 10G

    3)配置持久化config rewrite

二.水平扩容-原生命令

1) 原有集群redis-10001 – – redis-10006对应6个节点,cluster slots获得集群信息如下:

172.24.29.164:10004> cluster slots
1) 1) (integer) 0
   2) (integer) 5460
   3) 1) "172.24.29.164"
      2) (integer) 10001
      3) "d5db6441763d6888551b8038393c33977a15f509"
   4) 1) "172.24.29.164"
      2) (integer) 10005
      3) "844a8cb32cd330891651f23c5876a6615f3127ad"
2) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "172.24.29.164"
      2) (integer) 10002
      3) "bda0d2ab4927f5376cd240b233356c4909ed89fb"
   4) 1) "172.24.29.164"
      2) (integer) 10006
      3) "d027252ac021b215b7ef61eb3d309ff64ceda08d"
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "172.24.29.164"
      2) (integer) 10003
      3) "68fa0c4db651b2c7fefec0e0d676360e6258cb38"
   4) 1) "172.24.29.164"
      2) (integer) 10004
      3) "163489c40568827cf8954682b6d9166aa223ce25"

 

2) 新启动 redis-10007和redis-10008两个redis实例,执行cluster meet命令,和现有集群握手,把redis-10008设为redis-10007的replicate

3)计划从redis-10001迁移槽位5到 redis-10007,通过cluster getkeysinslot 5 10 查询槽位5的keys

172.24.29.164:10001> cluster getkeysinslot 5 10
1) "11b930d0-a008-4436-a9bc-2284e9e998a7"
2) "3e0e5847-1089-407a-b304-e0e95033513c"
3) "be617cb5-a4a4-43b5-8f83-8b608369e47e"
4) "f64aa06f-692b-4689-a4ac-14150b02c4ae"

4)执行迁移分为以下几步1. 原节点准备迁出 2.目标节点准备导入 3.迁移key 4.通知迁移完成,每一步执行如下:

   1. 原节点准备迁出

172.24.29.164:10001> cluster setslot 5 MIGRATING 8eb574c8f01e3d36b969f04aad1afb8179e1f805
OK
172.24.29.164:10001> cluster nodes //为了突出重点,以下结果省略部分信息

d5db6441763d6888551b8038393c33977a15f509 172.24.29.164:10001@20001 myself,master - 0 1592402096000 7 connected 3-5460 [5->-8eb574c8f01e3d36b969f04aad1afb8179e1f805]

2.目标节点准备导入

172.24.29.164:10007> cluster setslot 5 importing d5db6441763d6888551b8038393c33977a15f509
OK
172.24.29.164:10007> cluster nodes  //为了突出重点,以下结果省略部分信息

8eb574c8f01e3d36b969f04aad1afb8179e1f805 172.24.29.164:10007@20007 myself,master - 0 1592402296000 9 connected 0-2 [5-<-d5db6441763d6888551b8038393c33977a15f509]

3.迁移 key

//迁移命令,多个key一起迁移
172.24.29.164:10001> migrate 172.24.29.164 10007 "" 0 1000 keys 11b930d0-a008-4436-a9bc-2284e9e998a7 3e0e5847-1089-407a-b304-e0e95033513c
OK
//迁移后查看2个节点的keys
172.24.29.164:10001> cluster getkeysinslot 5 10
1) "be617cb5-a4a4-43b5-8f83-8b608369e47e"
2) "f64aa06f-692b-4689-a4ac-14150b02c4ae"

172.24.29.164:10007> cluster getkeysinslot 5 10
1) "11b930d0-a008-4436-a9bc-2284e9e998a7"
2) "3e0e5847-1089-407a-b304-e0e95033513c"

//迁移命令,迁移一个key
172.24.29.164:10001> migrate 172.24.29.164 10007 be617cb5-a4a4-43b5-8f83-8b608369e47e 0 1000

 注意:迁移过程中,key的读写问题

读:

在10001上读,有数据则直接返回,无数据则返回ASK,如下

172.24.29.164:10001> get f64aa06f-692b-4689-a4ac-14150b02c4ae
"282101.7450073812"
172.24.29.164:10001> get be617cb5-a4a4-43b5-8f83-8b608369e47e
(error) ASK 5 172.24.29.164:10007

在10007上读,不论是否有,直接MOVE到10001,MOVE之后如上,如果想在10007上读写,需要先执行asking命令

//10007上读10001上有
172.24.29.164:10007> get f64aa06f-692b-4689-a4ac-14150b02c4ae
-> Redirected to slot [5] located at 172.24.29.164:10001
"282101.7450073812"
172.24.29.164:10001> 

//10007上读10001上没有=
172.24.29.164:10007> get 11b930d0-a008-4436-a9bc-2284e9e998a7
-> Redirected to slot [5] located at 172.24.29.164:10001
(error) ASK 5 172.24.29.164:10007
172.24.29.164:10001> 

//如果想在10007上读写,需要先执行asking命令,表示这次不需要MOVE,直接在该节点执行读写命令,如果读写的key在属于这个节点负责,直接执行
172.24.29.164:10007> asking
OK
172.24.29.164:10007> get f64aa06f-692b-4689-a4ac-14150b02c4ae
"111"

关于写,和读类似,10001判断该key是否在自己节点,在则处理,不在则ASK

4.迁移key完成,重新设置slot归属

172.24.29.164:10001> cluster setslot 5 node 8eb574c8f01e3d36b969f04aad1afb8179e1f805
OK
172.24.29.164:10007> cluster setslot 5 node 8eb574c8f01e3d36b969f04aad1afb8179e1f805
OK
//至此完成迁移

5.在10005节点查看slot所属

172.24.29.164:10005> cluster nodes
8eb574c8f01e3d36b969f04aad1afb8179e1f805 172.24.29.164:10007@20007 master - 0 1592446015028 11 connected 0-2 5 7
d5db6441763d6888551b8038393c33977a15f509 172.24.29.164:10001@20001 master - 0 1592446017000 7 connected 3-4 6 8-5460

 

三.水平扩容-redis-cli工具

1.启动新的两个redis实例,端口号10009,10010

2.通过redis-cli命令和集群握手

redis-cli --cluster add-node 172.24.29.164:10009 172.24.29.164:10001 //命令
>>> Adding node 172.24.29.164:10009 to cluster 172.24.29.164:10001 //输出过程及结果
>>> Performing Cluster Check (using node 172.24.29.164:10001)
M: d5db6441763d6888551b8038393c33977a15f509 172.24.29.164:10001
   slots:[3-4],[6],[8-5460] (5456 slots) master
   1 additional replica(s)
S: 8892437702d1896c83b6ad1919c9feb82bbf0c0f 172.24.29.164:10008
   slots: (0 slots) slave
   replicates 8eb574c8f01e3d36b969f04aad1afb8179e1f805
M: bda0d2ab4927f5376cd240b233356c4909ed89fb 172.24.29.164:10002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: d027252ac021b215b7ef61eb3d309ff64ceda08d 172.24.29.164:10006
   slots: (0 slots) slave
   replicates bda0d2ab4927f5376cd240b233356c4909ed89fb
S: 844a8cb32cd330891651f23c5876a6615f3127ad 172.24.29.164:10005
   slots: (0 slots) slave
   replicates d5db6441763d6888551b8038393c33977a15f509
S: 163489c40568827cf8954682b6d9166aa223ce25 172.24.29.164:10004
   slots: (0 slots) slave
   replicates 68fa0c4db651b2c7fefec0e0d676360e6258cb38
M: 8eb574c8f01e3d36b969f04aad1afb8179e1f805 172.24.29.164:10007
   slots:[0-2],[5],[7] (5 slots) master
   1 additional replica(s)
M: 68fa0c4db651b2c7fefec0e0d676360e6258cb38 172.24.29.164:10003
   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.
>>> Send CLUSTER MEET to node 172.24.29.164:10009 to make it join the cluster.
[OK] New node added correctly.

3.通过redis-cli把10010设为10009的从节点

redis-cli -h 172.24.29.164 -p 10010 cluster replicate fc7a8ebf805a821a17da7cbc1ad3a9197879e0df
OK

4.迁移槽位,reshard

 

redis-cli --cluster reshard 172.24.29.164:10001 
>>> Performing Cluster Check (using node 172.24.29.164:10001)
......
[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)? 1000 //此处输入需要迁移的槽位数量
What is the receiving node ID? fc7a8ebf805a821a17da7cbc1ad3a9197879e0df  //此处输入要迁入的node-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则从其他所有节点输入

会车之后会显示迁移计划

Ready to move 1000 slots.
  Source nodes:
    M: d5db6441763d6888551b8038393c33977a15f509 172.24.29.164:10001
       slots:[3-4],[6],[8-5460] (5456 slots) master
       1 additional replica(s)
    M: bda0d2ab4927f5376cd240b233356c4909ed89fb 172.24.29.164:10002
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    M: 8eb574c8f01e3d36b969f04aad1afb8179e1f805 172.24.29.164:10007
       slots:[0-2],[5],[7] (5 slots) master
       1 additional replica(s)
    M: 68fa0c4db651b2c7fefec0e0d676360e6258cb38 172.24.29.164:10003
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
  Destination node:
    M: fc7a8ebf805a821a17da7cbc1ad3a9197879e0df 172.24.29.164:10009
       slots: (0 slots) master
       1 additional replica(s)
Resharding plan:
    Moving slot 5461 from bda0d2ab4927f5376cd240b233356c4909ed89fb
    Moving slot 5462 from bda0d2ab4927f5376cd240b233356c4909ed89fb
......
Do you want to proceed with the proposed reshard plan (yes/no)? yes //输入yes回车开始执行迁移
Moving slot 5461 from 172.24.29.164:10002 to 172.24.29.164:10009: ....
Moving slot 5462 from 172.24.29.164:10002 to 172.24.29.164:10009: .
Moving slot 5463 from 172.24.29.164:10002 to 172.24.29.164:10009: ...
.......
一直等待迁移完成

 

你可能感兴趣的:(redis)