前面在Redis集群架构搭建详解博文中搭建了一个redis集群,这篇博文就是在那个redis集群基础上进行操作的。
一、添加新节点
- 在10.220.5.172上在新配置两个实例
①新增配置
# cd /redis/
# mkdir 6379 6380
# cp 26379/redis-26379.conf 6379/redis-6379.conf && sed -i "s/26379/6379/g" 6379/redis-6379.conf
# cp 26380/redis-26380.conf 6380/redis-6380.conf && sed -i "s/26380/6380/g" 6380/redis-6380.conf
②启动
# /root/redis-4.0.11/src/redis-server /redis/6380/redis-6380.conf
# /root/redis-4.0.11/src/redis-server /redis/6379/redis-6379.conf
③查看是否启动成功
# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 10.220.5.172:16380 *:*
LISTEN 0 128 10.220.5.172:36380 *:*
LISTEN 0 128 10.220.5.172:6379 *:*
LISTEN 0 128 10.220.5.172:26379 *:*
LISTEN 0 128 10.220.5.172:6380 *:*
LISTEN 0 128 10.220.5.172:26380 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 10.220.5.172:16379 *:*
LISTEN 0 128 10.220.5.172:36379 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
- 添加主节点
① 在10.220.5.173节点执行如下命令
# /root/redis-4.0.11/src/redis-trib.rb add-node 10.220.5.172:6379 10.220.5.171:16379
说明:10.220.5.172:6379是新增节点,后面的可以是集群中任意一个旧节点
>>> Adding node 10.220.5.172:6379 to cluster 10.220.5.171:16379
>>> Performing Cluster Check (using node 10.220.5.171:16379)
M: 56d2348f3eb66f669405b8064fb8c068cca94105 10.220.5.171:16379
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 93a37f72f33752779183d70835a101c086bbad01 10.220.5.172:26379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 2c978644384445bf1bdfd3ed2b585f949f574f63 10.220.5.172:26380
slots: (0 slots) slave
replicates 56d2348f3eb66f669405b8064fb8c068cca94105
S: 8540a124e3a876ad2357d6bd10e09091612b3291 10.220.5.173:36380
slots: (0 slots) slave
replicates 93a37f72f33752779183d70835a101c086bbad01
S: 35c6cc6fb84b3c1a76e4f225bb8477f519abb559 10.220.5.171:16380
slots: (0 slots) slave
replicates 66d902b3ad7b4342720f5aeb98dfffa2b9c013fc
M: 66d902b3ad7b4342720f5aeb98dfffa2b9c013fc 10.220.5.173:36379
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 10.220.5.172:6379 to make it join the cluster.
[OK] New node added correctly.
②检查一下是否加入成功
# redis-4.0.11/src/redis-trib.rb check 10.220.5.172:26379
>>> Performing Cluster Check (using node 10.220.5.172:26379)
M: 93a37f72f33752779183d70835a101c086bbad01 10.220.5.172:26379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 8540a124e3a876ad2357d6bd10e09091612b3291 10.220.5.173:36380
slots: (0 slots) slave
replicates 93a37f72f33752779183d70835a101c086bbad01
S: 2c978644384445bf1bdfd3ed2b585f949f574f63 10.220.5.172:26380
slots: (0 slots) slave
replicates 56d2348f3eb66f669405b8064fb8c068cca94105
M: f523db5333150607a347fa9e55a282bcbe67b5aa 10.220.5.172:6379
slots: (0 slots) master
0 additional replica(s)
M: 66d902b3ad7b4342720f5aeb98dfffa2b9c013fc 10.220.5.173:36379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 56d2348f3eb66f669405b8064fb8c068cca94105 10.220.5.171:16379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 35c6cc6fb84b3c1a76e4f225bb8477f519abb559 10.220.5.171:16380
slots: (0 slots) slave
replicates 66d902b3ad7b4342720f5aeb98dfffa2b9c013fc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
可以看到10.220.5.172:6379已经加入到了集群中并且还是主节点其id为:f523db5333150607a347fa9e55a282bcbe67b5aa
- 添加从节点
①在10.220.5.173节点执行如下命令
# /root/redis-4.0.11/src/redis-trib.rb add-node --slave --master-id f523db5333150607a347fa9e55a282bcbe67b5aa 10.220.5.172:6380 10.220.5.171:16380
说明:--slave,表示添加的是从节点
--master-id,后面跟的是主节点的node-d这里是签名上一步添加的那个6379的node id
10.220.5.172:6380,是即将添加入集群的节点,最后面的是原集群的任意一个节点即可
>>> Adding node 10.220.5.172:6380 to cluster 10.220.5.171:16380
>>> Performing Cluster Check (using node 10.220.5.171:16380)
S: 35c6cc6fb84b3c1a76e4f225bb8477f519abb559 10.220.5.171:16380
slots: (0 slots) slave
replicates 66d902b3ad7b4342720f5aeb98dfffa2b9c013fc
M: 93a37f72f33752779183d70835a101c086bbad01 10.220.5.172:26379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 8540a124e3a876ad2357d6bd10e09091612b3291 10.220.5.173:36380
slots: (0 slots) slave
replicates 93a37f72f33752779183d70835a101c086bbad01
S: 2c978644384445bf1bdfd3ed2b585f949f574f63 10.220.5.172:26380
slots: (0 slots) slave
replicates 56d2348f3eb66f669405b8064fb8c068cca94105
M: 56d2348f3eb66f669405b8064fb8c068cca94105 10.220.5.171:16379
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 66d902b3ad7b4342720f5aeb98dfffa2b9c013fc 10.220.5.173:36379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: f523db5333150607a347fa9e55a282bcbe67b5aa 10.220.5.172:6379
slots: (0 slots) master
0 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 10.220.5.172:6380 to make it join the cluster.
Waiting for the cluster to join.......
>>> Configure node as replica of 10.220.5.172:6379.
[OK] New node added correctly.
- 重新分配slot
新增加的节点是没有slots的M: f523db5333150607a347fa9e55a282bcbe67b5aa 10.220.5.172:6379 slots: (0 slots) master
主节点没有slots存取数据的时候就不会被选中,为其分配slots的方式有两种:
- 所有节点的slots收回重新分配
- 从某个节点抽取部分slots分配给新节点
①在10.220.5.173节点执行下面命令
# /root/redis-4.0.11/src/redis-trib.rb reshard 10.220.5.172:6379
How many slots do you want to move (from 1 to 16384)? 1000 >>>设置slot个数
What is the receiving node ID? f523db5333150607a347fa9e55a282bcbe67b5aa >>>新节点的node id 这里就是那个新添加的10.220.5.172:6379的node id
Source node #1:all >>>表示全部节点重新分配槽,此处输入某节点的node id的话表示从某个节点抽取slots分配给新节点
Do you want to proceed with the proposed reshard plan (yes/no)? yes >>>确认重新分配
- 查看一下集群的状态
redis-4.0.11/src/redis-trib.rb check 10.220.5.172:26379
>>> Performing Cluster Check (using node 10.220.5.172:26379)
M: 93a37f72f33752779183d70835a101c086bbad01 10.220.5.172:26379
slots:6129-10922 (4794 slots) master
1 additional replica(s)
S: 76b5218ca0729fbe305c65a8a4aefae4462a58fc 10.220.5.172:6380
slots: (0 slots) slave
replicates f523db5333150607a347fa9e55a282bcbe67b5aa
S: 8540a124e3a876ad2357d6bd10e09091612b3291 10.220.5.173:36380
slots: (0 slots) slave
replicates 93a37f72f33752779183d70835a101c086bbad01
S: 2c978644384445bf1bdfd3ed2b585f949f574f63 10.220.5.172:26380
slots: (0 slots) slave
replicates 56d2348f3eb66f669405b8064fb8c068cca94105
M: f523db5333150607a347fa9e55a282bcbe67b5aa 10.220.5.172:6379
slots:0-665,5461-6128,10923-11588 (2000 slots) master
1 additional replica(s)
可以看到我们新加入的节点已经被分配了slots,我这里为了演示第四步做了两次所以我这里是有2000个slots
M: 66d902b3ad7b4342720f5aeb98dfffa2b9c013fc 10.220.5.173:36379
slots:11589-16383 (4795 slots) master
1 additional replica(s)
M: 56d2348f3eb66f669405b8064fb8c068cca94105 10.220.5.171:16379
slots:666-5460 (4795 slots) master
1 additional replica(s)
S: 35c6cc6fb84b3c1a76e4f225bb8477f519abb559 10.220.5.171:16380
slots: (0 slots) slave
replicates 66d902b3ad7b4342720f5aeb98dfffa2b9c013fc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
二、删除节点
删除从节点,10.220.5.172:6380
# /root/redis-4.0.11/src/redis-trib.rb del-node 10.220.5.171:16379 76b5218ca0729fbe305c65a8a4aefae4462a58fc
说明:
10.220.5.171:16379,集群入口
76b5218ca0729fbe305c65a8a4aefae4462a58fc,要删除的10.220.5.176:6380节点的node id
>>> Removing node 76b5218ca0729fbe305c65a8a4aefae4462a58fc from cluster 10.220.5.172:6380
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
删除主节点,10.220.5.172:6379
如果主节点有从节点,将从节点转移到其他主节点,如果主节点有slot,去掉分配的slot然后再删除主节点
# /root/redis-4.0.11/src/redis-trib.rb reshard 10.220.5.172:6379
How many slots do you want to move (from 1 to 16384)? 1000 >>>这里写被删除的master的全部slot数量
What is the receiving node ID? 56d2348f3eb66f669405b8064fb8c068cca94105 >>>接收被删除节点的slot的master的node id
Source node #1:f523db5333150607a347fa9e55a282bcbe67b5aa >>>被删除的master的node-id
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes >>>是否希望取消slot以后,reshard
#删除节点
# /root/redis-4.0.11/src/redis-trib.rb del-node 10.220.5.171:16379 f523db5333150607a347fa9e55a282bcbe67b5aa
>>> Removing node f523db5333150607a347fa9e55a282bcbe67b5aa from cluster 10.220.5.171:16379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
做到这里开篇新添加的一个master和一个slave节点都被删除了,也就是回到这篇博文开始没有添加节点的状态了。