版本说明:
系统:centos 7
docker:18
redis:6.0.3
一、水平扩容
参考上面博客Docker初识:Redis服务的搭建(集群),创建三主三从的redis集群,现在新增一主一从两个节点,在原集群的6个节点的基础上新增2个节点,由原来的3主3从变成4主4从。
其服务器如下:
IP | PORT | 主从说明 |
172.17.0.6 | 7001 | master |
172.17.0.6 | 7002 | master |
172.17.0.6 | 7003 | master |
172.17.0.6 | 7004 | slave |
172.17.0.6 | 7005 | salve |
172.17.0.6 | 7006 | slave |
扩容IP | 扩容Port | |
172.17.0.6 | 7007 | master |
172.17.0.6 | 7008 | slave |
其部署结构如下:
1、编辑配置文件并启动7007、7008两个节点。
首先构建相关目录以及配置文件。
redis-cluster.tmpl
# redis端口
port ${PORT}
# 关闭保护模式
protected-mode no
#监听
bind 0.0.0.0
# 开启集群
cluster-enabled yes
# 集群节点配置
cluster-config-file nodes.conf
# 超时
cluster-node-timeout 5000
# 集群节点IP host模式为宿主机IP
cluster-announce-ip 172.17.0.6
# 集群节点端口 7001 - 7006
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
# RDB
# #表示900 秒内如果至少有 1 个 key 的值变化,则保存
save 900 1
# #表示300 秒内如果至少有 10 个 key 的值变化,则保存
save 300 10
# #表示60 秒内如果至少有 10000 个 key 的值变化,则保存
save 60 10000
#AOF
# 开启 appendonly 备份模式
appendonly yes
# 每秒钟备份
appendfsync everysec
# 对aof文件进行压缩时,是否执行同步操作
no-appendfsync-on-rewrite no
# 当目前aof文件大小超过上一次重写时的aof文件大小的100%时会再次进行重写
auto-aof-rewrite-percentage 100
# 重写前AOF文件的大小最小值 默认 64mb
auto-aof-rewrite-min-size 64mb
# 此处绑定ip 可以是阿里内网ip 和 本地ip 也可以直接注释掉该项
#bind 172.17.0.6
#设置redis密码 各个节点请保持密码一致
requirepass 123456
masterauth 123456
执行文件:build.sh
for port in `seq 7007 7008`; do \
mkdir -p ./redis-cluster/${port}/conf \
&& PORT=${port} envsubst < ./redis-cluster.tmpl > ./redis-cluster/${port}/conf/redis.conf \
&& mkdir -p ./redis-cluster/${port}/data; \
done
docker-compose-redis-cluster-dilatancy.yml
version: '3.3'
services:
redis7001:
image: 'redis'
container_name: redis7007-dilatancy
command:
["redis-server", "/usr/local/etc/redis/redis.conf"]
volumes:
- ./redis-cluster/7007/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-cluster/7007/data:/data
ports:
- "7007:7007"
- "17007:17007"
environment:
- TZ=Asia/Shanghai
redis7002:
image: 'redis'
container_name: redis7008-dilatancy
command:
["redis-server", "/usr/local/etc/redis/redis.conf"]
volumes:
- ./redis-cluster/7008/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-cluster/7008/data:/data
ports:
- "7008:7008"
- "17008:17008"
environment:
- TZ=Asia/Shanghai
up.sh
docker-compose -f docker-compose-redis-cluster-dilatancy.yml up -d
down.sh
docker-compose -f docker-compose-redis-cluster-dilatancy.yml down
生成如下结构目录:
[root@VM_0_6_centos cluster3]# tree
.
|-- build.sh
|-- docker-compose-redis-cluster-dilatancy.yml
|-- down.sh
|-- redis-cluster
| |-- 7007
| | |-- conf
| | | `-- redis.conf
| | `-- data
| `-- 7008
| |-- conf
| | `-- redis.conf
| `-- data
|-- redis-cluster.tmpl
`-- up.sh
7 directories, 14 files
启动7007与7008。
[root@VM_0_6_centos cluster3]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4dea99dd3397 redis "docker-entrypoint.s…" 20 hours ago Up 20 hours 0.0.0.0:7007->7007/tcp, 6379/tcp, 0.0.0.0:17007->17007/tcp redis7007-dilatancy
6cd3a0244d70 redis "docker-entrypoint.s…" 20 hours ago Up 20 hours 0.0.0.0:7008->7008/tcp, 6379/tcp, 0.0.0.0:17008->17008/tcp redis7008-dilatancy
57be8ef8c5a9 redis "docker-entrypoint.s…" 22 hours ago Up 22 hours 0.0.0.0:7002->7002/tcp, 6379/tcp, 0.0.0.0:17002->17002/tcp redis7002
c6e09abc1399 redis "docker-entrypoint.s…" 22 hours ago Up 22 hours 0.0.0.0:7006->7006/tcp, 6379/tcp, 0.0.0.0:17006->17006/tcp redis7006
bbfa9dae9a36 redis "docker-entrypoint.s…" 22 hours ago Up 22 hours 0.0.0.0:7004->7004/tcp, 6379/tcp, 0.0.0.0:17004->17004/tcp redis7004
2cb4107f3849 redis "docker-entrypoint.s…" 22 hours ago Up 22 hours 0.0.0.0:7005->7005/tcp, 6379/tcp, 0.0.0.0:17005->17005/tcp redis7005
35b71161ad74 redis "docker-entrypoint.s…" 22 hours ago Up 22 hours 0.0.0.0:7001->7001/tcp, 6379/tcp, 0.0.0.0:17001->17001/tcp redis7001
a2152178bb37 redis "docker-entrypoint.s…" 22 hours ago Up 22 hours 0.0.0.0:7003->7003/tcp, 6379/tcp, 0.0.0.0:17003->17003/tcp redis7003
进入7001查看集群信息,可以发现7007与7008还没有加入集群。
127.0.0.1:7002> cluster nodes
2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006@17006 master - 0 1623911251795 7 connected 0-5460
bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003@17003 master - 0 1623911250000 3 connected 10923-16383
bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002@17002 myself,master - 0 1623911248000 2 connected 5461-10922
2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001@17001 slave 2db63b6ea82c538005d5cdb7e77cc13c15418066 0 1623911250792 7 connected
d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005@17005 slave bbef93cba563619e2939d51cf7efe47f582eb103 0 1623911250593 5 connected
02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004@17004 slave bbb8871b895d55d9db7d4b7960abce8f3710d307 0 1623911250000 4 connected
127.0.0.1:7002>
注意:以上集群因进行过容错测试,所以导致主从节点信息与上述描述略微差异,因重新选举导致,不影响水平扩容结果。
2、新增master节点7007
进入7002,使用 add-node 命令新增一个主节点7007(master),看到日志最后有"[OK] New node added correctly"提示代表新节点加入成功。
redis-cli --cluster add-node 172.17.0.6:7007 172.17.0.6:7002 -a 123456
第一个IP端口表示新的master节点,第二个ip端口代表原先集群master节点。
[root@VM_0_6_centos cluster3]# docker exec --it redis7002 /bin/bash
unknown flag: --it
See 'docker exec --help'.
[root@VM_0_6_centos cluster3]# docker exec -it redis7002 /bin/bash
root@57be8ef8c5a9:/data# redis-cli --cluster add-node 172.17.0.6:7007 172.17.0.6:7002 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.17.0.6:7007 to cluster 172.17.0.6:7002
>>> Performing Cluster Check (using node 172.17.0.6:7002)
M: bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001
slots: (0 slots) slave
replicates 2db63b6ea82c538005d5cdb7e77cc13c15418066
S: d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005
slots: (0 slots) slave
replicates bbef93cba563619e2939d51cf7efe47f582eb103
S: 02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004
slots: (0 slots) slave
replicates bbb8871b895d55d9db7d4b7960abce8f3710d307
[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.17.0.6:7007 to make it join the cluster.
[OK] New node added correctly.
添加成功后,再去查看集群状态
127.0.0.1:7002> cluster nodes
2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006@17006 master - 0 1623912971000 7 connected 0-5460
bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003@17003 master - 0 1623912971586 3 connected 10923-16383
bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002@17002 myself,master - 0 1623912969000 2 connected 5461-10922
2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001@17001 slave 2db63b6ea82c538005d5cdb7e77cc13c15418066 0 1623912971687 7 connected
d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005@17005 slave bbef93cba563619e2939d51cf7efe47f582eb103 0 1623912971000 5 connected
02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004@17004 slave bbb8871b895d55d9db7d4b7960abce8f3710d307 0 1623912970000 4 connected
64172884ed36bc655accacd0ca94439db09efc92 172.17.0.6:7007@17007 master - 0 1623912971586 0 connected
节点7007新增成功,但是新增的节点不会有任何数据,因为它还没有分配任何的slot(hash槽),我们需要为新节点手工分配hash槽。
使用redis-cli命令为9007分配hash槽,找到集群中的任意一个主节点,对其进行重新分片工作。
执行reshard命令对集群进行重新分片,其中172.17.0.6:7002 为任意一个主节点的IP。
redis-cli -a 123456 --cluster reshard 172.17.0.6:7002
root@57be8ef8c5a9:/data# redis-cli --cluster add-node 172.17.0.6:7007 172.17.0.6:7002 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.17.0.6:7007 to cluster 172.17.0.6:7002
>>> Performing Cluster Check (using node 172.17.0.6:7002)
M: bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001
slots: (0 slots) slave
replicates 2db63b6ea82c538005d5cdb7e77cc13c15418066
S: d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005
slots: (0 slots) slave
replicates bbef93cba563619e2939d51cf7efe47f582eb103
S: 02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004
slots: (0 slots) slave
replicates bbb8871b895d55d9db7d4b7960abce8f3710d307
[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.17.0.6:7007 to make it join the cluster.
[OK] New node added correctly.
root@57be8ef8c5a9:/data# redis-cli -a 123456 --cluster reshard 172.17.0.6:7002
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.17.0.6:7002)
M: bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001
slots: (0 slots) slave
replicates 2db63b6ea82c538005d5cdb7e77cc13c15418066
S: d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005
slots: (0 slots) slave
replicates bbef93cba563619e2939d51cf7efe47f582eb103
S: 02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004
slots: (0 slots) slave
replicates bbb8871b895d55d9db7d4b7960abce8f3710d307
M: 64172884ed36bc655accacd0ca94439db09efc92 172.17.0.6:7007
slots: (0 slots) master
[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)? 4096
What is the receiving node ID? 64172884ed36bc655accacd0ca94439db09efc92
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
Ready to move 4096 slots.
Source nodes:
M: bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
Destination node:
M: 64172884ed36bc655accacd0ca94439db09efc92 172.17.0.6:7007
slots: (0 slots) master
Resharding plan:
Moving slot 5461 from bbb8871b895d55d9db7d4b7960abce8f3710d307
Moving slot 5462 from bbb8871b895d55d9db7d4b7960abce8f3710d307
Moving slot 5463 from bbb8871b895d55d9db7d4b7960abce8f3710d307
Moving slot 5464 from bbb8871b895d55d9db7d4b7960abce8f3710d307
Moving slot 5465 from bbb8871b895d55d9db7d4b7960abce8f3710d307
......
下面是迁移和分片过程
在执行过程中可以发现,需要手动输入分配节点地址,分配多少hash槽等。其中部分需要输入参数:
#需要多少个槽移动到新的节点上,自定义,比如4069个hash槽
How many slots do you want to move (from 1 to 16384)? 4096
#把这4096个hash槽移动到哪个节点上去,需要指定节点id,这里为7007节点的id,ID可以从增加节点输出日志中查看)
What is the receiving node ID? 64172884ed36bc655accacd0ca94439db09efc92
#输入all为从所有主节点中分别抽取相应的槽数指定到新节点中,抽取的总槽数为4096个
#输入done,意思将输入的节点ID,抽取的总槽数为4096个
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
... ...
#输入yes确认开始执行分片任务
Do you want to proceed with the proposed reshard plan (yes/no)?yes
执行完成后,可以查看集群状态。
127.0.0.1:7002> cluster nodes
2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006@17006 master - 0 1623921699448 7 connected 1365-5460
bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003@17003 master - 0 1623921698547 3 connected 12288-16383
bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002@17002 myself,master - 0 1623921698000 2 connected 6827-10922
2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001@17001 slave 2db63b6ea82c538005d5cdb7e77cc13c15418066 0 1623921698446 7 connected
d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005@17005 slave bbef93cba563619e2939d51cf7efe47f582eb103 0 1623921699549 5 connected
02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004@17004 slave bbb8871b895d55d9db7d4b7960abce8f3710d307 0 1623921699549 4 connected
64172884ed36bc655accacd0ca94439db09efc92 172.17.0.6:7007@17007 master - 0 1623921699000 8 connected 0-1364 5461-6826 10923-12287
如上所示,可以发现7007已经分配hash槽成功,说明7007已经成功加入集群,并且是master。
2、为7007添加从节点7008。
首先把7008节点加入集群,并查看集群状态。
root@57be8ef8c5a9:/data# redis-cli --cluster add-node 172.17.0.6:7008 172.17.0.6:7002 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.17.0.6:7008 to cluster 172.17.0.6:7002
>>> Performing Cluster Check (using node 172.17.0.6:7002)
M: bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
M: 2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001
slots: (0 slots) slave
replicates 2db63b6ea82c538005d5cdb7e77cc13c15418066
S: d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005
slots: (0 slots) slave
replicates bbef93cba563619e2939d51cf7efe47f582eb103
S: 02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004
slots: (0 slots) slave
replicates bbb8871b895d55d9db7d4b7960abce8f3710d307
M: 64172884ed36bc655accacd0ca94439db09efc92 172.17.0.6:7007
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
[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.17.0.6:7008 to make it join the cluster.
[OK] New node added correctly.
root@57be8ef8c5a9:/data#
127.0.0.1:7002> cluster nodes
0c3f76bba92c7121b95ba35dab5cf4c383443096 172.17.0.6:7008@17008 master - 0 1623922864552 0 connected
2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006@17006 master - 0 1623922864000 7 connected 1365-5460
bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003@17003 master - 0 1623922863000 3 connected 12288-16383
bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002@17002 myself,master - 0 1623922864000 2 connected 6827-10922
2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001@17001 slave 2db63b6ea82c538005d5cdb7e77cc13c15418066 0 1623922863000 7 connected
d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005@17005 slave bbef93cba563619e2939d51cf7efe47f582eb103 0 1623922864552 5 connected
02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004@17004 slave bbb8871b895d55d9db7d4b7960abce8f3710d307 0 1623922864952 4 connected
64172884ed36bc655accacd0ca94439db09efc92 172.17.0.6:7007@17007 master - 0 1623922864552 8 connected 0-1364 5461-6826 10923-12287
其次,进入新节点7008中,配置7008为7007从节点。
cluster replicate 主节点ID
[root@VM_0_6_centos ~]# docker exec -it redis7008-dilatancy /bin/bash
root@6cd3a0244d70:/data# redis-cli -p 7008 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7008> cluster replicate 64172884ed36bc655accacd0ca94439db09efc92
OK
127.0.0.1:7008>
查看集群状态。
127.0.0.1:7002> cluster nodes
0c3f76bba92c7121b95ba35dab5cf4c383443096 172.17.0.6:7008@17008 slave 64172884ed36bc655accacd0ca94439db09efc92 0 1623923282512 8 connected
2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006@17006 master - 0 1623923282512 7 connected 1365-5460
bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003@17003 master - 0 1623923282913 3 connected 12288-16383
bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002@17002 myself,master - 0 1623923281000 2 connected 6827-10922
2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001@17001 slave 2db63b6ea82c538005d5cdb7e77cc13c15418066 0 1623923281000 7 connected
d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005@17005 slave bbef93cba563619e2939d51cf7efe47f582eb103 0 1623923282000 5 connected
02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004@17004 slave bbb8871b895d55d9db7d4b7960abce8f3710d307 0 1623923282512 4 connected
64172884ed36bc655accacd0ca94439db09efc92 172.17.0.6:7007@17007 master - 0 1623923282512 8 connected 0-1364 5461-6826 10923-12287
原先的7008已经变成7007的从节点。
最后,检查,各节点槽的均衡性。
root@6cd3a0244d70:/data# redis-cli -a 123456 --cluster rebalance 172.17.0.6:7002
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.17.0.6:7002)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 2.00% threshold.
结论:No rebalancing needed! All nodes are within the 2.00% threshold.
二、集群的缩容
将新增的主从节点7007与7008从现有的集群中卸载掉。
在尝试删除之前加入的主节点7007之前,先删除7007的从节点7008,之后,再移除7007这个主节点。这个步骤相对比较麻烦一些,因为主节点的里面是有分配了hash槽的,所以我们这里必须先把7007里的hash槽放入到其他的可用主节点中去,然后再进行移除节点操作,不然会出现数据丢失问题(目前只能把master的数据迁移到一个节点上,暂时做不了平均分配功能)。
首先,进入从节点7008,执行del-node删除从节点。
[root@VM_0_6_centos ~]# docker exec -it redis7008-dilatancy /bin/bash
root@6cd3a0244d70:/data#
root@6cd3a0244d70:/data# redis-cli --cluster del-node 172.17.0.6:7008 0c3f76bba92c7121b95ba35dab5cf4c383443096 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 0c3f76bba92c7121b95ba35dab5cf4c383443096 from cluster 172.17.0.6:7008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
root@6cd3a0244d70:/data#
删除成功,查看集群状态信息。
127.0.0.1:7002> cluster nodes
2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006@17006 master - 0 1623995452000 7 connected 1365-5460
bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003@17003 master - 0 1623995453580 3 connected 12288-16383
bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002@17002 myself,master - 0 1623995452000 2 connected 6827-10922
2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001@17001 slave 2db63b6ea82c538005d5cdb7e77cc13c15418066 0 1623995453080 7 connected
d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005@17005 slave bbef93cba563619e2939d51cf7efe47f582eb103 0 1623995453000 5 connected
02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004@17004 slave bbb8871b895d55d9db7d4b7960abce8f3710d307 0 1623995452579 4 connected
64172884ed36bc655accacd0ca94439db09efc92 172.17.0.6:7007@17007 master - 0 1623995452076 8 connected 0-1364 5461-6826 10923-12287
127.0.0.1:7002>
迁移待删除节点7007的hash槽,操作和扩容抽取hash槽操作一直,只不过Source node 2:done。
进入7007节点,执行reshard。
[root@VM_0_6_centos ~]# docker exec -it redis7007-dilatancy /bin/bash
root@4dea99dd3397:/data# redis-cli -a 123456 --cluster reshard 172.17.0.6:7007
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.17.0.6:7007)
M: 64172884ed36bc655accacd0ca94439db09efc92 172.17.0.6:7007
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005
slots: (0 slots) slave
replicates bbef93cba563619e2939d51cf7efe47f582eb103
M: bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004
slots: (0 slots) slave
replicates bbb8871b895d55d9db7d4b7960abce8f3710d307
M: 2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
S: 2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001
slots: (0 slots) slave
replicates 2db63b6ea82c538005d5cdb7e77cc13c15418066
M: bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002
slots:[6827-10922] (4096 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.
#这里是需要把数据移动到哪?迁移到172.17.0.6:7002
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? bbb8871b895d55d9db7d4b7960abce8f3710d307
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.
#这里是需要数据源,也就是待迁移的节点:172.17.0.6:7007)
Source node #1: 64172884ed36bc655accacd0ca94439db09efc92
#这里直接输入done 开始生成迁移计划
Source node #2: done
... ... 开始迁移
至此,我们已经成功的把7007主节点的数据迁移到7002上去了,我们可以看一下现在的集群状态如下图,你会发现7007下面已经没有任何hash槽了,证明迁移成功!
127.0.0.1:7002> cluster nodes
2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006@17006 master - 0 1623996216089 7 connected 1365-5460
bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003@17003 master - 0 1623996217000 3 connected 12288-16383
bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002@17002 myself,master - 0 1623996216000 9 connected 0-1364 5461-12287
2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001@17001 slave 2db63b6ea82c538005d5cdb7e77cc13c15418066 0 1623996217000 7 connected
d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005@17005 slave bbef93cba563619e2939d51cf7efe47f582eb103 0 1623996217590 5 connected
02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004@17004 slave bbb8871b895d55d9db7d4b7960abce8f3710d307 0 1623996217389 9 connected
64172884ed36bc655accacd0ca94439db09efc92 172.17.0.6:7007@17007 master - 0 1623996216389 8 connected
进入7007删除7007节点。
[root@VM_0_6_centos ~]# docker exec -it redis7007-dilatancy /bin/bash
root@4dea99dd3397:/data# redis-cli --cluster del-node 172.17.0.6:7007 64172884ed36bc655accacd0ca94439db09efc92 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 64172884ed36bc655accacd0ca94439db09efc92 from cluster 172.17.0.6:7007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
查看集群信息
127.0.0.1:7002> cluster nodes
2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006@17006 master - 0 1623996409820 7 connected 1365-5460
bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003@17003 master - 0 1623996408016 3 connected 12288-16383
bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002@17002 myself,master - 0 1623996409000 9 connected 0-1364 5461-12287
2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001@17001 slave 2db63b6ea82c538005d5cdb7e77cc13c15418066 0 1623996408000 7 connected
d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005@17005 slave bbef93cba563619e2939d51cf7efe47f582eb103 0 1623996408516 5 connected
02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004@17004 slave bbb8871b895d55d9db7d4b7960abce8f3710d307 0 1623996408817 9 connected
7007与7008节点删除成功。但是,可以发现7001~7006的hash槽分配额不均匀,可以在任意主节点上,执行rebalance操作进行槽位的负载均衡!
[root@VM_0_6_centos ~]# docker exec -it redis7002 /bin/bash
root@57be8ef8c5a9:/data# redis-cli --cluster rebalance 172.17.0.6:7002
[ERR] Node 172.17.0.6:7002 NOAUTH Authentication required.
root@57be8ef8c5a9:/data# redis-cli -a 123456 --cluster rebalance 172.17.0.6:7002
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.17.0.6:7002)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 3 nodes. Total weight = 3.00
Moving 1366 slots from 172.17.0.6:7002 to 172.17.0.6:7006
######################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
Moving 1365 slots from 172.17.0.6:7002 to 172.17.0.6:7003
#####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
root@57be8ef8c5a9:/data#
查看集群状态信息。
127.0.0.1:7002> cluster nodes
2db63b6ea82c538005d5cdb7e77cc13c15418066 172.17.0.6:7006@17006 master - 0 1623996635348 10 connected 0-5461
bbef93cba563619e2939d51cf7efe47f582eb103 172.17.0.6:7003@17003 master - 0 1623996636551 11 connected 5462-6826 12288-16383
bbb8871b895d55d9db7d4b7960abce8f3710d307 172.17.0.6:7002@17002 myself,master - 0 1623996634000 9 connected 6827-12287
2a57d563dfb9d1f3091fb09f1d3393f706ec7f81 172.17.0.6:7001@17001 slave 2db63b6ea82c538005d5cdb7e77cc13c15418066 0 1623996636350 10 connected
d9724cee129f31240cdd1c1e1f9bbd4c6314d441 172.17.0.6:7005@17005 slave bbef93cba563619e2939d51cf7efe47f582eb103 0 1623996635046 11 connected
02ded0630fe3d3b365e36b4b4cf14e1fa75611e8 172.17.0.6:7004@17004 slave bbb8871b895d55d9db7d4b7960abce8f3710d307 0 1623996636000 9 connected
至此,完成。