Redis集群扩展

本次是在之前redis集群的基础上进行操作的。

1、当前redis集群的状态

./install/bin/redis-cli -h 10.10.36.126 -p 7111
10.10.36.126:7111> cluster nodes
ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116 slave 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 0 1483937332566 6 connected
7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111 myself,master - 0 0 1 connected 0-5460
1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113 master - 0 1483937336604 3 connected 10923-16383
17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112 master - 0 1483937333574 2 connected 5461-10922
fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114 slave 7c18d707e804ef4ad492efeb34712c2b94c37913 0 1483937334583 4 connected
4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115 slave 17b0d77e68f8abbac920ccb198493351159a9ce5 0 1483937335595 5 connected

以上集群状态输出信息解析:
(1)节点 ID
(2)IP:PORT
(3)节点状态标识: master、slave、myself、fail?、fail
(4)如果是从节点,表示主节点的 ID;如果是主节点,则为 '-'
(5)集群最近一次向各个节点发送 PING 命令后,过去多长时间还没有接到回复
(6)节点最近一次返回 PONG 的时间戳
(7)节点的配置纪元
(8)本节点的网络连接情况: connected、disconnected
(9)如果是主节点,表示节点包含的槽

2、安装两个新的redis节点

server:10.10.36.126
初始Master:7117
初始Slave:7118
将7117,7118, 17117,17118端口在防火墙中打开
安装步骤见之前的博客,此处不再赘述。
安装完成后,启动这两个redis实例。

3、添加新的Mater节点

(1)add-node 将一个节点添加到集群里面,第一个是新节点 ip:port,第二个是任意一个已存在节点 ip:port

./install/bin/redis-trib add-node 10.10.36.126:7117 10.10.36.126:7111

输出:

>>> Performing Cluster Check (using node 10.10.36.126:7111)
M: 7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116
   slots: (0 slots) slave
   replicates 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb
M: 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114
   slots: (0 slots) slave
   replicates 7c18d707e804ef4ad492efeb34712c2b94c37913
S: 4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115
   slots: (0 slots) slave
   replicates 17b0d77e68f8abbac920ccb198493351159a9ce5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 10.10.36.126:7117 to make it join the cluster.
[OK] New node added correctly.

此时集群状态:

10.10.36.126:7111> cluster nodes
ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116 slave 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 0 1483937680097 6 connected
7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111 myself,master - 0 0 1 connected 0-5460
4e661de53cee487619b63e524dbc0be95199a5ce 10.10.36.126:7117 master - 0 1483937677936 0 connected
1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113 master - 0 1483937676919 3 connected 10923-16383
17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112 master - 0 1483937681126 2 connected 5461-10922
fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114 slave 7c18d707e804ef4ad492efeb34712c2b94c37913 0 1483937679379 4 connected
4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115 slave 17b0d77e68f8abbac920ccb198493351159a9ce5 0 1483937679046 5 connected

节点添加成功,新增的节点不包含任何数据, 因为它没有分配任何 slot。

(2)为新节点分配哈希槽(slot)

只需要指定集群中其中一个节点的地址,redis-trib 就会自动找到集群中的其他节点。

./install/bin/redis-trib reshard 10.10.36.126:7111

输出:

>>> Performing Cluster Check (using node 10.10.36.126:7111)
M: 7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116
   slots: (0 slots) slave
   replicates 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb
M: 4e661de53cee487619b63e524dbc0be95199a5ce 10.10.36.126:7117
   slots: (0 slots) master
   0 additional replica(s)
M: 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112
   slots:5461-10922 (5462 slots) maste
r
   1 additional replica(s)
S: fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114
   slots: (0 slots) slave
   replicates 7c18d707e804ef4ad492efeb34712c2b94c37913
S: 4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115
   slots: (0 slots) slave
   replicates 17b0d77e68f8abbac920ccb198493351159a9ce5
[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)? 

确认打算移动的哈希槽 slots 的数量(这里槽数量设置为 1000),输入1000,回车。

What is the receiving node ID?

指定目标需要使用节点的 ID , 而不是 IP 地址和端口。7117的ID为4e661de53cee487619b63e524dbc0be95199a5ce,输入该数值,回车。

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:

询问重新分片的源节点(source node), 也就是要从哪个节点中取出 1000 个哈希槽,并将这些槽移动到目标节点7117上面。如果我们不打算从特定的节点上取出指定数量的哈希槽, 那么可以向 redis-trib 输入 all , 这样的话, 集群中的所有主节点都会成为源节点, redis-trib 将从各个源节点中各取出一部分哈希槽, 凑够 1000 个, 然后移动到目标节点7117上面.输入all,回车。

输出选中的1000个哈希槽,并提示:

Do you want to proceed with the proposed reshard plan (yes/no)?

输入 yes 并再次按回车确认, redis-trib 就会正式开始执行重新分片操作, 将指定的哈希槽从源节点一个个地移动到目标节点7117上面。

此时,集群状态:

10.10.36.126:7111> cluster nodes
ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116 slave 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 0 1483938536376 6 connected
7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111 myself,master - 0 0 1 connected 333-5460
4e661de53cee487619b63e524dbc0be95199a5ce 10.10.36.126:7117 master - 0 1483938534192 7 connected 0-332 5461-5794 10923-11255
1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113 master - 0 1483938533089 3 connected 11256-16383
17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112 master - 0 1483938530973 2 connected 5795-10922
fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114 slave 7c18d707e804ef4ad492efeb34712c2b94c37913 0 1483938534528 4 connected
4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115 slave 17b0d77e68f8abbac920ccb198493351159a9ce5 0 1483938535291 5 connected

可以看到7117上已拥有三段哈希槽。

使用check命令检查集群:

./install/bin/redis-trib check 10.10.36.126:7111

>>> Performing Cluster Check (using node 10.10.36.126:7111)
M: 7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111
   slots:333-5460 (5128 slots) master
   1 additional replica(s)
S: ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116
   slots: (0 slots) slave
   replicates 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb
M: 4e661de53cee487619b63e524dbc0be95199a5ce 10.10.36.126:7117
   slots:0-332,5461-5794,10923-11255 (1000 slots) master
   
0 additional replica(s)
M: 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113
   slots:11256-16383 (5128 slots) master
   1 additional replica(s)
M: 17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112
   slots:5795-10922 (5128 slots) master
   1 additional replica(s)
S: fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114
   slots: (0 slots) slave
   replicates 7c18d707e804ef4ad492efeb34712c2b94c37913
S: 4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115
   slots: (0 slots) slave
   replicates 17b0d77e68f8abbac920ccb198493351159a9ce5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

可以看到集群正常,7117为主节点,没有从节点。

4、添加Slave节点:

(1)再次使用上面的方法将7118节点加入集群,但不分配哈希槽。

10.10.36.126:7111> cluster nodes

fad49e6d14290225a06f072e319bebef8c1307fd 10.10.36.126:7118 master - 0 1483938984865 0 connected
ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116 slave 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 0 1483938985896 6 connected
7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111 myself,master - 0 0 1 connected 333-5460
4e661de53cee487619b63e524dbc0be95199a5ce 10.10.36.126:7117 master - 0 1483938979720 7 connected 0-332 5461-5794 10923-11255
1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113 master - 0 1483938984146 3 connected 11256-16383
17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112 master - 0 1483938981781 2 connected 5795-10922
fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114 slave 7c18d707e804ef4ad492efeb34712c2b94c37913 0 1483938982812 4 connected
4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115 slave 17b0d77e68f8abbac920ccb198493351159a9ce5 0 1483938980750 5 connected

(2)将其加入到Master节点

连接7118节点

./install/bin/redis-cli -h 10.10.36.126 -p 7118

使用cluster replicate 对应 master 的 node-id,将其分配到相应的master上。

10.10.36.126:7118> cluster replicate 4e661de53cee487619b63e524dbc0be95199a5ce

此时的集群状态:

10.10.36.126:7118> cluster nodes
17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112 master - 0 1483939475795 2 connected 5795-10922
fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114 slave 7c18d707e804ef4ad492efeb34712c2b94c37913 0 1483939477136 1 connected
fad49e6d14290225a06f072e319bebef8c1307fd 10.10.36.126:
7118 myself,slave 4e661de53cee487619b63e524dbc0be95199a5ce 0 0 0 connected
4e661de53cee487619b63e524dbc0be95199a5ce 10.10.36.126:7117 master - 0 1483939479916 7 connected 0-332 5461-5794 10923-11255
ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116 slave 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 0 1483939474760 3 connected
4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115 slave 17b0d77e68f8abbac920ccb198493351159a9ce5 0 1483939477862 2 connected
1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113 master - 0 1483939478888 3 connected 11256-16383
7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111 master - 0 1483939480946 1 connected 333-5460

可以看到这时 7118 已变成 7117 的 slave节点。

5、reshard  数据

对于负载/数据不均匀的情况,可以在线 reshard slot 来解决,方法与添加新 master 的 reshard 一样,只是需要 reshard 的 master 节点是老节点。

6、移除slave节点

./install/bin/redis-trib del-node 10.10.36.126:7118 fad49e6d14290225a06f072e319bebef8c1307fd

需要同时提供被移除的ip:port 和ID信息。

集群状态:

10.10.36.126:7111> cluster nodes
ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116 slave 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 0 1483940054832 6 connected
7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111 myself,master - 0 0 1 connected 333-5460
4e661de53cee487619b63e524dbc0be95199a5ce 10.10.36.126:7117 master - 0 1483940055987 7 connected 0-332 5461-5794 10923-11255
1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113 master - 0 1483940051569 3 connected 11256-16383
17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112 master - 0 1483940053720 2 connected 5795-10922
fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114 slave 7c18d707e804ef4ad492efeb34712c2b94c37913 0 1483940050851 4 connected
4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115 slave 17b0d77e68f8abbac920ccb198493351159a9ce5 0 1483940052602 5 connected

可以得知7118节点已被移除成功。

移除一个节点,对应的节点进程也会被关闭。

7、移除master节点

移除master 节点之前首先要使用 reshard 移除该 master 的全部 slot,然后再移除当前节点(目前只能把被移除master 的 slot 迁移到一个节点上),操作和分配 slot 类似,指定具体的 Source node 即可。

./install/bin/redis-trib reshard 10.10.36.126:7111

输出:

>>> Performing Cluster Check (using node 10.10.36.126:7111)
M: 7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111
   slots:333-5460 (5128 slots) master
   1 additional replica(s)
S: ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116
   slots: (0 slots) slave
   replicates 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb
M: 4e661de53cee487619b63e524dbc0be95199a5ce 10.10.36.126:7117
   slots:0-332,5461-5794,10923-11255 (
1000 slots) master
   0 additional replica(s)
M: 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113
   slots:11256-16383 (5128 slots) master
   1 additional replica(s)
M: 17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112
   slots:5795-10922 (5128 slots) master
   1 additional replica(s)
S: fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114
   slots: (0 slots) slave
   replicates 7c18d707e804ef4ad492efeb34712c2b94c37913
S: 4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115
   slots: (0 slots) slave
   replicates 17b0d77e68f8abbac920ccb198493351159a9ce5
[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   #7117总的哈希槽

What is the receiving node ID? 7c18d707e804ef4ad492efeb34712c2b94c37913   #7111的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:
4e661de53cee487619b63e524dbc0be95199a5ce  #7117的ID
Source node #2: done

Do you want to proceed with the proposed reshard plan (yes/no)? yes

此时集群状态:

10.10.36.126:7111> cluster nodes
ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116 slave 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 0 1483940783308 6 connected
7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111 myself,master - 0 0 8 connected 0-5794 10923-11255
4e661de53cee487619b63e524dbc0be95199a5ce 10.10.36.126:7117 master - 0 1483940782284 7 connected
1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113 master - 0 1483940786396 3 connected 11256-16383
17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112 master - 0 1483940787425 2 connected 5795-10922
fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114 slave 7c18d707e804ef4ad492efeb34712c2b94c37913 0 1483940784351 8 connected
4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115 slave 17b0d77e68f8abbac920ccb198493351159a9ce5 0 1483940785372 5 connected

确认已清空该 Master 节点的所有 slot 后就可以删除该节点了(命令与删除 slave 节点一样):

./install/bin/redis-trib del-node 10.10.36.126:7117 4e661de53cee487619b63e524dbc0be95199a5ce

此时的redis实例:

ps -ef | grep redis
wangzi     6189      1  0 12:38 ?        00:00:09 ./install/bin/redis-server 10.10.36.126:7111 [cluster]
wangzi     6193      1  0 12:38 ?        00:00:08 ./install/bin/redis-server 10.10.36.126:7112 [cluster]
wangzi     6197      1  0 12:39 ?        00:00:09 ./install/bin/redis-server 10.10.36.126:7113 [cluster]
wangzi     6201      1  0 12:39 ?        00:00:07 ./install/bin/redis-server 10.10.36.126:7114 [cluster]
wangzi     6207      1  0 12:39 ?        00:00:07 ./install/bin/redis-server 10.10.36.126:7115 [cluster]
wangzi     6213      1  0 12:39 ?        00:00:07 ./install/bin/redis-server 10.10.36.126:7116 [cluster]
wangzi     6420   4060  0 12:48 pts/1    00:00:00 ./install/bin/redis-cli -h 10.10.36.126 -p 7111
wangzi     7536   3988  0 13:49 pts/0    00:00:00 grep --color=auto redis

你可能感兴趣的:(NoSQL)