Redis集群新旧节点替换

Redis集群是一个由多个主从节点群组成的分布式服务集群,它具有复制、高可用和分片特性。Redis集群不需要sentinel哨兵也能完成节点移除和故障转移的功能。需要将每个节点设置成集群模式,这种集群模式没有中心节点,可水平扩展,据官方文档称可以线性扩展到上万个节点(官方推荐不超过1000个节点)。redis集群的性能和高可用性均优于之前版本的哨兵模式,且集群配置非常简单。redis集群的运用主要是针对海量数据+高并发+高可用的场景。

创建redis集群

1.选择其中任意一台redis进入容器

1
命令:docker exec -it redis-6370 /bin/bash

2.创建集群

命令:redis-cli –cluster create –cluster-replicas 1 -a redis密码 ip1:端口 ip2:端口

1
2
确认配置,输入yes回车
Can I set the above configuration? (type 'yes' to accept): yes

redis新旧节点更换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
这里加了6007 6008 用来替换 6006 6001

### 1.添加节点
进入随便一个节点的redis
redis-cli -p 6001 -c -a redis密码

添加主机到节点
127.0.0.1:6001> cluster meet 192.168.227.103 6001

#查看集群情况
127.0.0.1:6001> cluster nodes

新加入集群的节点都为master

exit #退出终端模式

删除旧的从节点

1
2
互为主从节点的信息可以看从节点后跟的是哪个主节点的ID,主节点可以有多个从节点。 
redis-cli --cluster del-node 192.168.227.103:6001 ce06463feb18192efe50ff488db70158c6848246 -a redis密码

把6003的slots全部转移到新节点6007

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1.查看slost分配情况 redis-cli --cluster info 192.168.227.103:6006 -a 123456
192.168.227.103:6006 (ad7c370f...) -> 5010 keys | 4657 slots | 0 slaves.
192.168.227.103:6008 (98468d12...) -> 0 keys | 0 slots | 0 slaves.
192.168.227.103:6002 (8381b3cb...) -> 0 keys | 5462 slots | 1 slaves.
192.168.227.103:6003 (5fc1f981...) -> 0 keys | 2169 slots | 1 slaves.
192.168.227.103:6007 (d68bf743...) -> 0 keys | 0 slots | 0 slaves.

2.重新分配slots
这里以删除其中一个主节点端口6006为例,他的slost为4657
选择需要移除替换的旧节点重新分配slost到新的resale节点:redis-cli --cluster reshard 192.168.227.103:6006 -a redis密码

How many slots do you want to move (from 1 to 16384)? 4657
(旧节点的slost值)
What is the receiving node ID?
(选择需要带入节点的ID)

填写需要被分配的旧节点ID
Source node #1: ad7c370f4d937bf4192018d24a7778bb6caae090
Source node #2: done

回车

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

3.再次查看slost分配情况,被转移的slots值为0
root@localhost:/data# redis-cli --cluster info 192.168.227.103:6006 -a redis密码
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.227.103:6006 (ad7c370f...) -> 0 keys | 0 slots | 0 slaves.
192.168.227.103:6008 (98468d12...) -> 0 keys | 0 slots | 0 slaves.
192.168.227.103:6002 (8381b3cb...) -> 0 keys | 5462 slots | 1 slaves.
192.168.227.103:6003 (5fc1f981...) -> 0 keys | 2169 slots | 1 slaves.
192.168.227.103:6007 (d68bf743...) -> 5010 keys | 8753 slots | 0 slaves.

删除旧的节点6006

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1.分别进入6006和6008 ,配置为6007的从节点,再删除6006
redis-cli -p 6379 -c -a redis密码

使用cluster replicate + 主节点ID,就能指定当前节点为目标节点的从节点:
127.0.0.1:6006> cluster replicate d68bf7433254a80f40a0602145f232b4f26d75b5
OK

127.0.0.1:6008> cluster replicate d68bf7433254a80f40a0602145f232b4f26d75b5
OK



2.删除旧的主节点(就是已经变成从节点的6006)
redis-cli --cluster del-node 192.168.227.103:6006 ad7c370f4d937bf4192018d24a7778bb6caae090 -a redis密码

>>> Removing node ad7c370f4d937bf4192018d24a7778bb6caae090 from cluster 192.168.227.103:6006
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.


删除主节点前重新分片主节点到其他的主节点上,否者移除不出集群 redis-cli --cluster reshard 192.168.16.102:6303 -a redis密码
删除集群需要确保节点的是主从,如果是双主需要降级一个为从,先删除从,再删主

slots分配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
检查集群态 redis-cli --cluster check 192.168.227.102:6303 -a redis密码 
查看slost分配情况 redis-cli --cluster info 192.168.227.102:6303 -a redis密码

如果slots分配不平均,就把占有最大的分配平均其他,明显6007占用最多
root@localhost:/data# redis-cli --cluster info 192.168.227.103:6007 -a redis密码
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.227.103:6007 (d68bf743...) -> 5010 keys | 8753 slots | 1 slaves.
192.168.227.103:6002 (8381b3cb...) -> 0 keys | 5462 slots | 1 slaves.
192.168.227.103:6003 (5fc1f981...) -> 0 keys | 2169 slots | 1 slaves.


平均slost: redis-cli --cluster rebalance 192.168.227.103:6007 -a redis密码

再查看已经正常
root@localhost:/data# redis-cli --cluster info 192.168.227.103:6007 -a redis密码
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.227.103:6007 (d68bf743...) -> 5010 keys | 5461 slots | 1 slaves.
192.168.227.103:6002 (8381b3cb...) -> 0 keys | 5461 slots | 1 slaves.
192.168.227.103:6003 (5fc1f981...) -> 0 keys | 5462 slots | 1 slaves.

如有报错无法重新分配slots,可以尝试修复redis
修复集群 redis-cli –cluster fix 192.168.227.102:6303 -a redis密码