我们在上一章讲了如何创建集群,今天我们来实现下集群的伸缩。
添加节点
操作流程
1.启动节点
2.将节点加入到集群中
3.将数据槽从原来的节点迁移部分到新节点上
实践
1)准备两个新节点并启动
[root@VM_0_15_centos config]# sed 's/7000/7006/g' redis-7000.conf > redis-7006.conf
[root@VM_0_15_centos config]# sed 's/7000/7007/g' redis-7000.conf > redis-7007.conf
[root@VM_0_15_centos config]# cd ..
[root@VM_0_15_centos redis4]# ./redis-server config/redis-7006.conf
[root@VM_0_15_centos redis4]# ./redis-server config/redis-7007.conf
[root@VM_0_15_centos redis4]# ps -ef | grep redis
root 10788 1 0 14:20 ? 00:00:00 ./redis-server *:7006 [cluster]
root 10793 1 0 14:20 ? 00:00:00 ./redis-server *:7007 [cluster]
root 10817 1362 0 14:21 pts/0 00:00:00 grep --color=auto redis
root 29570 1 0 10:15 ? 00:00:11 ./redis-server *:7000 [cluster]
root 29575 1 0 10:15 ? 00:00:10 ./redis-server *:7001 [cluster]
root 29581 1 0 10:15 ? 00:00:12 ./redis-server *:7002 [cluster]
root 29587 1 0 10:15 ? 00:00:12 ./redis-server *:7003 [cluster]
root 29593 1 0 10:15 ? 00:00:11 ./redis-server *:7004 [cluster]
root 29599 1 0 10:15 ? 00:00:11 ./redis-server *:7005 [cluster]
2) 将节点加入集群
有两种方式可以添加:
- 原生命令
- redis-trib 工具
原生命令
# 进入任一集群节点
[root@VM_0_15_centos redis4]# ./redis-cli -p 7000
# 将新增加的节点加入到集群中来
127.0.0.1:7000> cluster meet 127.0.0.1 7006
OK
127.0.0.1:7000> cluster meet 127.0.0.1 7007
OK
redis-trib 工具
redis-trib.rb add-node 127.0.0.1:6387 127.0.0.1:7006
redis-trib.rb add-node 127.0.0.1:6388 127.0.0.1:7007
新节点刚开始都是主节点状态,但是由于没有负责的槽,所以不能接受任何读写操作。对于新节点的后续操作一般有两种选择:
◆ 为它迁移槽和数据实现扩容
◆ 作为其他主节点的从节点负责故障转移
3) 迁移槽和数据,将 7000 端口中的部分槽迁移到 7006 端口中
# redis-trib.rb 执行槽重分片命令
[root@VM_0_15_centos redis4]# ./redis-trib.rb reshard 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 5de648cc0cc7c503169638bbe3a56fc3f4c3aa72 127.0.0.1:7000
slots:666-5460 (4795 slots) master
0 additional replica(s)
M: 90ab3e9992a4b0f6abddc75d03fce63099b4fb38 127.0.0.1:7003
slots:6128-10922 (4795 slots) master
1 additional replica(s)
M: 65cc70270ae44c1c8ca29bb09c609b5b5c878eef 127.0.0.1:7007
slots: (0 slots) master
0 additional replica(s)
S: 892668ae60f407be5094a667f3532e760b867e64 127.0.0.1:7005
slots: (0 slots) slave
replicates 90ab3e9992a4b0f6abddc75d03fce63099b4fb38
M: 1e3ec7626adcb2662700aa0ca9933d60232847a6 127.0.0.1:7002
slots:11589-16383 (4795 slots) master
1 additional replica(s)
M: 03274e13cb092f92a51a05a5eaca3a7a5618c356 127.0.0.1:7006
slots:0-665,5461-6127,10923-11588 (1999 slots) master
0 additional replica(s)
S: 5a2bbf95047bdf9719ca60152cf91e48e8a956bd 127.0.0.1:7004
slots: (0 slots) slave
replicates 1e3ec7626adcb2662700aa0ca9933d60232847a6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 需要迁移的槽数量,这里选择2000
How many slots do you want to move (from 1 to 16384)? 2000
# 选择要迁移到的目标节点,这里选择 7006 端口对应的节点
What is the receiving node ID? 03274e13cb092f92a51a05a5eaca3a7a5618c356
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,这里选择全部 all
Source node #1: all
# 数据迁移之前会打印出所有的槽从源节点到目标节点的计划,确认计划无误后输入 yes 执行迁移工作,计划执行完成之后reshard命令自动退出。
Do you want to proceed with the proposed reshard plan (yes/no)? yes
4) 将 70007 端口设为 7006 端口的从服务器
[root@VM_0_15_centos redis4]# ./redis-cli -h 127.0.0.1 -p 7000 cluster nodes
90ab3e9992a4b0f6abddc75d03fce63099b4fb38 127.0.0.1:7003@17003 master - 0 1542955379299 2 connected 6794-10922
65cc70270ae44c1c8ca29bb09c609b5b5c878eef 127.0.0.1:7007@17007 master - 0 1542955376000 0 connected
892668ae60f407be5094a667f3532e760b867e64 127.0.0.1:7005@17005 slave 90ab3e9992a4b0f6abddc75d03fce63099b4fb38 0 1542955376000 6 connected
1e3ec7626adcb2662700aa0ca9933d60232847a6 127.0.0.1:7002@17002 master - 0 1542955376294 3 connected 12255-16383
03274e13cb092f92a51a05a5eaca3a7a5618c356 127.0.0.1:7006@17006 master - 0 1542955378298 7 connected 0-1332 5461-6793 10923-12254
5a2bbf95047bdf9719ca60152cf91e48e8a956bd 127.0.0.1:7004@17004 slave 1e3ec7626adcb2662700aa0ca9933d60232847a6 0 1542955377296 4 connected
5de648cc0cc7c503169638bbe3a56fc3f4c3aa72 127.0.0.1:7000@17000 myself,master - 0 1542955377000 1 connected 1333-5460
[root@VM_0_15_centos redis4]# ./redis-cli -h 127.0.0.1 -p 7007 cluster replicate 03274e13cb092f92a51a05a5eaca3a7a5618c356
OK
删除节点
操作流程
删除节点跟增加节点的流程刚好相反:
1.将数据槽从当前节点迁移到其他节点上
2.让其他节点忘记当前节点
3.将节点服务停止
实践
1)迁移槽
查看节点信息
[root@VM_0_15_centos redis4]# ./redis-cli -h 127.0.0.1 -p 7000 cluster nodes
90ab3e9992a4b0f6abddc75d03fce63099b4fb38 127.0.0.1:7003@17003 master - 0 1542955959000 2 connected 6794-10922
65cc70270ae44c1c8ca29bb09c609b5b5c878eef 127.0.0.1:7007@17007 slave 03274e13cb092f92a51a05a5eaca3a7a5618c356 0 1542955961405 7 connected
892668ae60f407be5094a667f3532e760b867e64 127.0.0.1:7005@17005 slave 90ab3e9992a4b0f6abddc75d03fce63099b4fb38 0 1542955960404 6 connected
1e3ec7626adcb2662700aa0ca9933d60232847a6 127.0.0.1:7002@17002 master - 0 1542955959402 3 connected 12255-16383
03274e13cb092f92a51a05a5eaca3a7a5618c356 127.0.0.1:7006@17006 master - 0 1542955958400 7 connected 0-1332 5461-6793 10923-12254
5a2bbf95047bdf9719ca60152cf91e48e8a956bd 127.0.0.1:7004@17004 slave 1e3ec7626adcb2662700aa0ca9933d60232847a6 0 1542955958000 4 connected
5de648cc0cc7c503169638bbe3a56fc3f4c3aa72 127.0.0.1:7000@17000 myself,master - 0 1542955959000 1 connected 1333-5460
可以看到 7006 节点上有三个范围的槽 0-1332
5461-6793
10923-12254
# 从 7006 节点迁移 1333 槽到 7000 节点
[root@VM_0_15_centos redis4]# ./redis-trib.rb reshard --from 03274e13cb092f92a51a05a5eaca3a7a5618c356 --to 5de648cc0cc7c503169638bbe3a56fc3f4c3aa72 --slots 1333 127.0.0.1:7006
# 确认迁移
Do you want to proceed with the proposed reshard plan (yes/no)? yes
在查看下节点信息
[root@VM_0_15_centos redis4]# ./redis-cli -h 127.0.0.1 -p 7000 cluster nodes
03274e13cb092f92a51a05a5eaca3a7a5618c356 127.0.0.1:7006@17006 master - 0 1542956707000 7 connected 5461-6793 10923-12254
可以看到 7006 节点 只剩下 5461-6793
和 10923-12254
,依次迁移
[root@VM_0_15_centos redis4]# ./redis-trib.rb reshard --from 03274e13cb092f92a51a05a5eaca3a7a5618c356 --to 5de648cc0cc7c503169638bbe3a56fc3f4c3aa72 --slots 1333 127.0.0.1:7006
[root@VM_0_15_centos redis4]# ./redis-trib.rb reshard --from 03274e13cb092f92a51a05a5eaca3a7a5618c356 --to 5de648cc0cc7c503169638bbe3a56fc3f4c3aa72 --slots 1 127.0.0.1:7006
查看节点信息:
[root@VM_0_15_centos redis4]# ./redis-cli -h 127.0.0.1 -p 7000 cluster nodes
03274e13cb092f92a51a05a5eaca3a7a5618c356 127.0.0.1:7006@17006 master - 0 1542956939273 7 connected
已经没有槽了。
2)忘记节点
# 先下从节点 7007 端口
[root@VM_0_15_centos redis4]# ./redis-trib.rb del-node 127.0.0.1:7000 65cc70270ae44c1c8ca29bb09c609b5b5c878eef
>>> Removing node 65cc70270ae44c1c8ca29bb09c609b5b5c878eef from cluster 127.0.0.1:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
# 再下主节点 7006 端口
[root@VM_0_15_centos redis4]# ./redis-trib.rb del-node 127.0.0.1:7000 03274e13cb092f92a51a05a5eaca3a7a5618c356
>>> Removing node 03274e13cb092f92a51a05a5eaca3a7a5618c356 from cluster 127.0.0.1:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
# 查看节点情况
[root@VM_0_15_centos redis4]# ./redis-cli -h 127.0.0.1 -p 7000 cluster nodes
90ab3e9992a4b0f6abddc75d03fce63099b4fb38 127.0.0.1:7003@17003 master - 0 1542957326994 2 connected 6794-10922
892668ae60f407be5094a667f3532e760b867e64 127.0.0.1:7005@17005 slave 90ab3e9992a4b0f6abddc75d03fce63099b4fb38 0 1542957325992 6 connected
1e3ec7626adcb2662700aa0ca9933d60232847a6 127.0.0.1:7002@17002 master - 0 1542957327995 3 connected 12255-16383
5a2bbf95047bdf9719ca60152cf91e48e8a956bd 127.0.0.1:7004@17004 slave 1e3ec7626adcb2662700aa0ca9933d60232847a6 0 1542957325000 4 connected
5de648cc0cc7c503169638bbe3a56fc3f4c3aa72 127.0.0.1:7000@17000 myself,master - 0 1542957326000 8 connected 0-6793 10923-12254
查看 Redis 进程
[root@VM_0_15_centos redis4]# ps -ef | grep redis
root 9326 1362 0 15:17 pts/0 00:00:00 grep --color=auto redis
root 29570 1 0 10:15 ? 00:00:17 ./redis-server *:7000 [cluster]
root 29575 1 0 10:15 ? 00:00:12 ./redis-server *:7001 [cluster]
root 29581 1 0 10:15 ? 00:00:17 ./redis-server *:7002 [cluster]
root 29587 1 0 10:15 ? 00:00:17 ./redis-server *:7003 [cluster]
root 29593 1 0 10:15 ? 00:00:14 ./redis-server *:7004 [cluster]
root 29599 1 0 10:15 ? 00:00:14 ./redis-server *:7005 [cluster]
可以看到 7006 和 7007 端口的服务已经停止了