redis clusert分布式集群
一,Redis Cluster 分布式集概述
0)Redis Cluster 是社区版推出的 Redis 分布式集群解决方案,主要解决 Redis 分布式方面的需求,比如,当遇到单机内存,并发和流量等瓶颈的时候,Redis Cluster 能起到很好的负载均衡的目的。
1)Redis集群是一个可以在多个Redis节点之间进行数据共享的设施(installation)。
2)Redis集群不支持那些需要同时处理多个键的Redis命令,因为执行这些命令需要在多个Redis节点之间移动数据,并且在高负载的情况下,这些命令将降低Redis集群的性能,并导致不可预测的行为。
3)Redis集群通过分区(partition)来提供一定程度的可用性(availability):即使集群中有一部分节点失效或者无法进行通讯,集群也可以继续处理命令请求。
4)Redis集群有将数据自动切分(split)到多个节点的能力。
1.1 高性能
在搭建集群时,会为每一个分片的主节点,对应一个从节点,实现slaveof功能,同时当主节点down,实现类似于sentinel的自动failover的功能。
1.2Redis Cluster客户端连接任意节点
如图所示,当我们用客户端连接A分片时,如果按照数据的取模,我们想要访问的数据,不在A分片中,那么集群会自动将请求进行转发
1.3 redis集群数据共享
Redis 集群使用数据分片(sharding)而非一致性哈希(consistency hashing)来实现: 一个 Redis 集群包含 16384 个哈希槽(hash slot), 数据库中的每个键都属于这 16384 个哈希槽的其中一个, 集群使用公式 CRC16(key) % 16384 来计算键 key 属于哪个槽, 其中 CRC16(key) 语句用于计算键 key 的 CRC16 校验和 。
1.节点 A 负责处理 0 号至 5500 号哈希槽。
2.节点 B 负责处理 5501 号至 11000 号哈希槽。
3.节点 C 负责处理 11001 号至 16384 号哈希槽
1.4 redis clusert运行机制
有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.
节点的fail是通过集群中超过半数的master节点检测失效时才生效.
客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->key
1.5 redis clusert如何做集群复制
为了使得集群在一部分节点下线或者无法与集群的大多数(majority)节点进行通讯的情况下, 仍然可以正常运作, Redis 集群对节点使用了主从复制功能: 集群中的每个节点都有 1 个至 N 个复制品(replica), 其中一个复制品为主节点(master), 而其余的 N-1 个复制品为从节点(slave)。
在之前列举的节点 A 、B 、C 的例子中, 如果节点 B 下线了, 那么集群将无法正常运行, 因为集群找不到节点来处理 5501 号至 11000 号的哈希槽。
假如在创建集群的时候(或者至少在节点 B 下线之前), 我们为主节点 B 添加了从节点 B1 , 那么当主节点 B 下线的时候, 集群就会将 B1 设置为新的主节点, 并让它代替下线的主节点 B , 继续处理 5501 号至 11000 号的哈希槽, 这样集群就不会因为主节点 B 的下线而无法正常运作了。
不过如果节点 B 和 B1 都下线的话, Redis 集群还是会停止运作
集群的复制特性重用了 SLAVEOF 命令的代码,所以集群节点的复制行为和 SLAVEOF 命令的复制行为完全相同。
1.6 redis cluster故障转移
)在集群里面,节点会对其他节点进行下线检测。
2)当一个主节点下线时,集群里面的其他主节点负责对下线主节点进行故障移。
3)换句话说,集群的节点集成了下线检测和故障转移等类似 Sentinel 的功能。
4)因为 Sentinel 是一个独立运行的监控程序,而集群的下线检测和故障转移等功能是集成在节点里面的,它们的运行模式非常地不同,所以尽管这两者的功能很相似,但集群的实现没有重用 Sentinel 的代码。
1.7 Redis Cluster中执行命令的两种情
1)命令发送到了正确的节点:命令要处理的键所在的槽正好是由接收命令的节点负责,那么该节点执行命令,就像单机 Redis 服务器一样。
2)命令发送到了错误的节点:接收到命令的节点并非处理键所在槽的节点,那么节点将向客户端返回一个转向(redirection)错误,告知客户端应该到哪个节点去执行这个命令,客户端会根据错误提示的信息,重新向正确的节点发送命令。
二,集群安装部署
6个redis实例,一般会放到3台硬件服务器
注:在企业规划中,一个分片的两个分到不同的物理机,防止硬件主机宕机造成的整个分片数据丢失。
2.1 环境规划
| 内网ip | 端口 |
| --- | --- | --- |
| 10.0.0.81 | 7000,70001 |
| 10.0.0.82 | 7002,70003 |
| 10.0.0.83 | 7004,70005 |
2.2 配置集群
# 示例一台
[root@redis01 /usr/local/redis/cluster/cluster_7000]# vim cluster_7000.conf
daemonize yes
port 6381
dir /usr/local/redis/cluster/cluster_7000/
cluster-enabled yes
cluster-config-file cluster_7000_auto.conf
cluster-node-timeout 5000
cluster-require-full-coverage no
bind 0.0.0.0
protected-mode no
save ""
appendonly no
requirepass 1
masterauth 1
logfile "/usr/local/redis/cluster/cluster_7000/redis_cluster.log"
pidfile "/usr/local/redis/cluster/cluster_7000/redis_cluster.pid"
# 配置解释
daemonize yes
port 7000
dir /var/lib/redis
## 启动集群模式
cluster-enabled yes
## 设置集群配置文件名(配置文件是由redis自己创建的,不需要手动创建)
cluster-config-file redis-cluster-6381.conf
## 集群内部沟通超时时间
cluster-node-timeout 5000
## 是否集群内所有的节点都能够正常提供服务才算集群正常
cluster-require-full-coverage no
bind 0.0.0.0
protected-mode no
save ""
appendonly no
requirepass 1
masterauth 1
logfile "/var/log/redis/redis-6381.log"
2.3 启动集群
[root@redis01 ~]# /usr/local/redis/bin/redis-server /usr/local/redis/cluster/cluster_7000/cluster_7000.conf
[root@redis01 ~]# for i in `seq 6`;do /usr/local/redis/bin/redis-server /usr/local/redis/cluster/cluster_700${i}/cluster_700${i}.conf;done
2.4 查看启动状态
[root@redis01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:17000 0.0.0.0:* LISTEN 15353/redis-server
tcp 0 0 0.0.0.0:17001 0.0.0.0:* LISTEN 15468/redis-server
tcp 0 0 0.0.0.0:17002 0.0.0.0:* LISTEN 15361/redis-server
tcp 0 0 0.0.0.0:17003 0.0.0.0:* LISTEN 15363/redis-server
tcp 0 0 0.0.0.0:17004 0.0.0.0:* LISTEN 15369/redis-server
tcp 0 0 0.0.0.0:17005 0.0.0.0:* LISTEN 15371/redis-server
tcp 0 0 0.0.0.0:17006 0.0.0.0:* LISTEN 15483/redis-server
tcp 0 0 0.0.0.0:7000 0.0.0.0:* LISTEN 15353/redis-server
tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 15468/redis-server
tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN 15361/redis-server
tcp 0 0 0.0.0.0:7003 0.0.0.0:* LISTEN 15363/redis-server
tcp 0 0 0.0.0.0:7004 0.0.0.0:* LISTEN 15369/redis-server
tcp 0 0 0.0.0.0:7005 0.0.0.0:* LISTEN 15371/redis-server
tcp 0 0 0.0.0.0:7006 0.0.0.0:* LISTEN 15483/redis-server
2.5 集群握手
# 集群握手的目的是为了绑定集群
[root@redis01 ~]# for i in 7001 7002 7003 7004 7005 7006;
> do
> /usr/local/redis/bin/redis-cli -a 1 -p 7000 cluster meet 172.16.1.81 $i
> done
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
2.6 查看握手结果
[root@redis01 ~]# /usr/local/redis/bin/redis-cli -a 1 -p 7000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
48184f81c2c695869a32d8b8dc02382ea52d7420 172.16.1.81:7006@17006 master - 0 1608356375802 6 connected
c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000@17000 myself,master - 0 1608356374000 0 connected
a47861348ce0f0725914b7ecc9e8e62e937ad9c9 172.16.1.81:7003@17003 master - 0 1608356374795 2 connected
b913386a514f464b64d60a4fde7100a1039c4222 172.16.1.81:7002@17002 master - 0 1608356374000 1 connected
b1c0938e1fe8b4fe375cb691f9bb7243901913d9 172.16.1.81:7004@17004 master - 0 1608356375600 3 connected
6aa18115c3f918c9ae40156805b8aa748d39d8a5 172.16.1.81:7005@17005 master - 0 1608356374090 4 connected
1e077002ea5fd001d6e779412f0428dcb11dbeeb 172.16.1.81:7001@17001 master - 0 1608356375600 5 connected
# 集群已经绑定了但是互相认为自己是主
三,为集群节点主库分配数据槽
# 编写脚本分配数据槽
[root@redis01 ~]# vim allot_data.sh
#!/bin/bash
start=$1
end=$2
port=$3
IP="172.16.1.81"
password=$4
for i in `seq ${start} ${end}`
do
/usr/local/redis/bin/redis-cli -h ${IP} -a ${password} -p ${port} cluster addslots $i
done
# 执行脚本分配数据槽
[root@redis01 ~]# ./allot_data.sh 0 5461 7000 1
[root@redis01 ~]# ./allot_data.sh 5462 10922 7001 1
[root@redis01 ~]# ./allot_data.sh 10933 16383 7002 1
# 查看分配卡槽的结果
[root@redis01 ~]# /usr/local/redis/bin/redis-cli -a 1 -p 7000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
48184f81c2c695869a32d8b8dc02382ea52d7420 172.16.1.81:7006@17006 master - 0 1608358303519 6 connected
c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000@17000 myself,master - 0 1608358303000 0 connected 0-5461 # 数据槽区间
a47861348ce0f0725914b7ecc9e8e62e937ad9c9 172.16.1.81:7003@17003 master - 0 1608358303519 2 connected
b913386a514f464b64d60a4fde7100a1039c4222 172.16.1.81:7002@17002 master - 0 1608358303000 1 connected 10923-16383 # 数据槽区间
b1c0938e1fe8b4fe375cb691f9bb7243901913d9 172.16.1.81:7004@17004 master - 0 1608358302514 3 connected
6aa18115c3f918c9ae40156805b8aa748d39d8a5 172.16.1.81:7005@17005 master - 0 1608358302000 4 connected
1e077002ea5fd001d6e779412f0428dcb11dbeeb 172.16.1.81:7001@17001 master - 0 1608358302513 5 connected 5462-10922 # 数据槽区间
3.1 绑定主从关系
[root@redis01 ~]# /usr/local/redis/bin/redis-cli -a 1 -p 7003 cluster replicate 1e077002ea5fd001d6e779412f0428dcb11dbeeb
[root@redis01 ~]# /usr/local/redis/bin/redis-cli -a 1 -p 7004 cluster replicate b913386a514f464b64d60a4fde7100a1039c4222
[root@redis01 ~]# /usr/local/redis/bin/redis-cli -a 1 -p 7005 cluster replicate c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6
[root@redis01 ~]# /usr/local/redis/bin/redis-cli -a 1 -p 7006 cluster replicate c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6
# 查看主从关系
[root@redis01 ~]# /usr/local/redis/bin/redis-cli -p 7000 -a 1 -h 172.16.1.81 cluster nodes
48184f81c2c695869a32d8b8dc02382ea52d7420 172.16.1.81:7006@17006 slave c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 0 1608359218000 0 connected
c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000@17000 myself,master - 0 1608359218000 0 connected 0-5461
a47861348ce0f0725914b7ecc9e8e62e937ad9c9 172.16.1.81:7003@17003 slave 1e077002ea5fd001d6e779412f0428dcb11dbeeb 0 1608359218852 5 connected
b913386a514f464b64d60a4fde7100a1039c4222 172.16.1.81:7002@17002 master - 0 1608359219355 1 connected 10923-16383
b1c0938e1fe8b4fe375cb691f9bb7243901913d9 172.16.1.81:7004@17004 slave b913386a514f464b64d60a4fde7100a1039c4222 0 1608359219556 1 connected
6aa18115c3f918c9ae40156805b8aa748d39d8a5 172.16.1.81:7005@17005 slave c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 0 1608359218549 0 connected
1e077002ea5fd001d6e779412f0428dcb11dbeeb 172.16.1.81:7001@17001 master - 0 1608359219556 5 connected 5462-10922
3.2 集群主从关系
端口 | 角色 | 从库端口 |
---|---|---|
7000 | 主库 | 7006,7005 |
70001 | 主库 | 7003 |
7002 | 主库 | 7004 |
3.3 测试集群
[root@redis01 ~]# /usr/local/redis/bin/redis-cli -p 7000 -c -a 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7000> set a b
-> Redirected to slot [15495] located at 172.16.1.81:7002
OK
四,增加节点做从库
# 启动两个新节点
[root@redis01 ~]# /usr/local/redis/bin/redis-server /usr/local/redis/cluster/cluster_7007/cluster_7007.conf
[root@redis01 ~]# /usr/local/redis/bin/redis-server /usr/local/redis/cluster/cluster_7008/cluster_7008.conf
# 新节点握手集群绑定
[root@redis01 ~]# redis-cli -p 7000 -a 1 cluster meet 172.16.1.81 7007
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
[root@redis01 ~]# redis-cli -p 7008 -a 1 cluster meet 172.16.1.81 7007
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
# 查看新集群状态
[root@redis01 ~]# redis-cli -p 7007 -a 1 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
48184f81c2c695869a32d8b8dc02382ea52d7420 172.16.1.81:7006@17006 slave c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 0 1608363044555 0 connected
c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000@17000 master - 0 1608363044555 0 connected 0-5461
b1c0938e1fe8b4fe375cb691f9bb7243901913d9 172.16.1.81:7004@17004 slave b913386a514f464b64d60a4fde7100a1039c4222 0 1608363045059 1 connected
a47861348ce0f0725914b7ecc9e8e62e937ad9c9 172.16.1.81:7003@17003 slave 1e077002ea5fd001d6e779412f0428dcb11dbeeb 0 1608363044000 5 connected
1e077002ea5fd001d6e779412f0428dcb11dbeeb 172.16.1.81:7001@17001 master - 0 1608363044555 5 connected 5462-10922
aa7fe1c114b81870c0882f62e46984b628e2a73a 172.16.1.81:7007@17007 myself,master - 0 1608363043000 7 connected
6aa18115c3f918c9ae40156805b8aa748d39d8a5 172.16.1.81:7005@17005 slave c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 0 1608363044000 0 connected
56bcb9fd289b378bd514bc479e649eef674d2b9a 172.16.1.81:7008@17008 master - 0 1608363043000 8 connected
b913386a514f464b64d60a4fde7100a1039c4222 172.16.1.81:7002@17002 master - 0 1608363044757 1 connected 10923-16383
4.1 新节点做主从关系
# 集群节点7007 绑定集群节点7001认主库
[root@redis01 ~]# redis-cli -p 7007 -a 1 cluster replicate 1e077002ea5fd001d6e779412f0428dcb11dbeeb
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
# 集群节点7008 绑定集群节点7002认主库
[root@redis01 ~]# redis-cli -p 7008 -a 1 cluster replicate b913386a514f464b64d60a4fde7100a1039c4222
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
4.2 现在集群主从关系表
端口 | 角色 | 从库端口 |
---|---|---|
7000 | 主库 | 7006,7005 |
70001 | 主库 | 7003,7007 |
7002 | 主库 | 7004,7008 |
五,增加新节点做主库从新分配卡槽
5.1 启动新示例
[root@redis01 /usr/local/redis/cluster/cluster_7009]# redis-server cluster_7009.conf
5.2 加入集群握手
[root@redis01 ~]# redis-cli -p 7000 -a 1 cluster meet 172.16.1.81 7009
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
5.3 查看集群状态
[root@redis01 ~]# redis-cli -p 7000 -a 1 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1 172.16.1.81:7009@17009 master - 0 1608366704563 9 connected
48184f81c2c695869a32d8b8dc02382ea52d7420 172.16.1.81:7006@17006 slave c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 0 1608366703558 0 connected
6aa18115c3f918c9ae40156805b8aa748d39d8a5 172.16.1.81:7005@17005 slave c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 0 1608366704765 0 connected
b913386a514f464b64d60a4fde7100a1039c4222 172.16.1.81:7002@17002 master - 0 1608366705000 1 connected 10923-16383
1e077002ea5fd001d6e779412f0428dcb11dbeeb 172.16.1.81:7001@17001 master - 0 1608366705570 5 connected 5462-10922
56bcb9fd289b378bd514bc479e649eef674d2b9a :0@0 slave,fail,noaddr b913386a514f464b64d60a4fde7100a1039c4222 1608365972565 1608365970050 1 disconnected
aa7fe1c114b81870c0882f62e46984b628e2a73a 172.16.1.81:7007@17007 slave 1e077002ea5fd001d6e779412f0428dcb11dbeeb 0 1608366705771 5 connected
c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000@17000 myself,master - 0 1608366704000 0 connected 0-5461
a47861348ce0f0725914b7ecc9e8e62e937ad9c9 172.16.1.81:7003@17003 slave 1e077002ea5fd001d6e779412f0428dcb11dbeeb 0 1608366705000 5 connected
b1c0938e1fe8b4fe375cb691f9bb7243901913d9 172.16.1.81:7004@17004 slave b913386a514f464b64d60a4fde7100a1039c4222 0 1608366704000 1 connected
5.4 重新分配卡槽
# 添加和删除节点的流程
1.新节点添加槽位
2.源节点中的数据进行迁移
3.源节点数据迁移完毕
4.迁移下一个槽位的数据,依次循环
[root@redis01 ~]# redis-cli -p 7009 -a 1 cluster addslots 0 4095
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(error) ERR Slot 0 is already busy
# 提示已经没有多的卡槽了 分配完了
5.5 刷新集群检查
[root@redis01 ~]# redis-cli -a 1 -p 7000 --cluster fix 172.16.1.81:7000
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.1.81:7000 (c94f0e1c...) -> 0 keys | 5462 slots | 2 slaves.
172.16.1.81:7009 (a1aa1c42...) -> 0 keys | 0 slots | 0 slaves.
172.16.1.81:7002 (b913386a...) -> 1 keys | 5461 slots | 1 slaves.
172.16.1.81:7001 (1e077002...) -> 0 keys | 5461 slots | 2 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.16.1.81:7000)
M: c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000
slots:[0-5461] (5462 slots) master
2 additional replica(s)
M: a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1 172.16.1.81:7009
slots: (0 slots) master
S: 48184f81c2c695869a32d8b8dc02382ea52d7420 172.16.1.81:7006
slots: (0 slots) slave
replicates c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6
S: 6aa18115c3f918c9ae40156805b8aa748d39d8a5 172.16.1.81:7005
slots: (0 slots) slave
replicates c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6
M: b913386a514f464b64d60a4fde7100a1039c4222 172.16.1.81:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 1e077002ea5fd001d6e779412f0428dcb11dbeeb 172.16.1.81:7001
slots:[5462-10922] (5461 slots) master
2 additional replica(s)
S: aa7fe1c114b81870c0882f62e46984b628e2a73a 172.16.1.81:7007
slots: (0 slots) slave
replicates 1e077002ea5fd001d6e779412f0428dcb11dbeeb
S: a47861348ce0f0725914b7ecc9e8e62e937ad9c9 172.16.1.81:7003
slots: (0 slots) slave
replicates 1e077002ea5fd001d6e779412f0428dcb11dbeeb
S: b1c0938e1fe8b4fe375cb691f9bb7243901913d9 172.16.1.81:7004
slots: (0 slots) slave
replicates b913386a514f464b64d60a4fde7100a1039c4222
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
5.6 初始化数据槽(重新分配卡槽)
[root@redis01 ~]# redis-cli -a 1 -p 7000 --cluster reshard 172.16.1.81:7000
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.16.1.81:7000)
M: c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000
slots:[0-5461] (5462 slots) master
2 additional replica(s)
M: a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1 172.16.1.81:7009
slots: (0 slots) master
S: 48184f81c2c695869a32d8b8dc02382ea52d7420 172.16.1.81:7006
slots: (0 slots) slave
replicates c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6
S: 6aa18115c3f918c9ae40156805b8aa748d39d8a5 172.16.1.81:7005
slots: (0 slots) slave
replicates c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6
M: b913386a514f464b64d60a4fde7100a1039c4222 172.16.1.81:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 1e077002ea5fd001d6e779412f0428dcb11dbeeb 172.16.1.81:7001
slots:[5462-10922] (5461 slots) master
2 additional replica(s)
S: aa7fe1c114b81870c0882f62e46984b628e2a73a 172.16.1.81:7007
slots: (0 slots) slave
replicates 1e077002ea5fd001d6e779412f0428dcb11dbeeb
S: a47861348ce0f0725914b7ecc9e8e62e937ad9c9 172.16.1.81:7003
slots: (0 slots) slave
replicates 1e077002ea5fd001d6e779412f0428dcb11dbeeb
S: b1c0938e1fe8b4fe375cb691f9bb7243901913d9 172.16.1.81:7004
slots: (0 slots) slave
replicates b913386a514f464b64d60a4fde7100a1039c4222
[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)?4092
# 输入要分配卡槽的主库节点id
What is the receiving node ID? a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1
# 选择是否全部重新分配
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 4092 slots.
Source nodes:
M: c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000
slots:[0-5461] (5462 slots) master
2 additional replica(s)
M: b913386a514f464b64d60a4fde7100a1039c4222 172.16.1.81:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 1e077002ea5fd001d6e779412f0428dcb11dbeeb 172.16.1.81:7001
slots:[5462-10922] (5461 slots) master
2 additional replica(s)
# 目标卡槽所在的节点
Destination node:
M: a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1 172.16.1.81:7009
slots: (0 slots) master
Resharding plan:
Moving slot 0 from c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6
Moving slot 1 from c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6
......
......
# 是否按照计划的方案分配卡槽
Do you want to proceed with the proposed reshard plan (yes/no)?yes
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 0 from 172.16.1.81:7000 to 172.16.1.81:7009:
Moving slot 1 from 172.16.1.81:7000 to 172.16.1.81:7009:
......
.....
5.7 查看重新分配的卡槽
[root@redis01 ~]# redis-cli -a 1 -p 7009 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1e077002ea5fd001d6e779412f0428dcb11dbeeb 172.16.1.81:7001@17001 master - 0 1608368888005 5 connected 6825-10922
c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000@17000 master - 0 1608368889000 0 connected 1365-5461
aa7fe1c114b81870c0882f62e46984b628e2a73a 172.16.1.81:7007@17007 slave 1e077002ea5fd001d6e779412f0428dcb11dbeeb 0 1608368889112 5 connected
a47861348ce0f0725914b7ecc9e8e62e937ad9c9 172.16.1.81:7003@17003 slave 1e077002ea5fd001d6e779412f0428dcb11dbeeb 0 1608368888509 5 connected
a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1 172.16.1.81:7009@17009 myself,master - 0 1608368888000 9 connected 0-1364 5462-6824 10923-12285
6aa18115c3f918c9ae40156805b8aa748d39d8a5 172.16.1.81:7005@17005 slave c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 0 1608368889515 0 connected
b913386a514f464b64d60a4fde7100a1039c4222 172.16.1.81:7002@17002 master - 0 1608368889000 1 connected 12286-16383
b1c0938e1fe8b4fe375cb691f9bb7243901913d9 172.16.1.81:7004@17004 slave b913386a514f464b64d60a4fde7100a1039c4222 0 1608368888609 1 connected
48184f81c2c695869a32d8b8dc02382ea52d7420 172.16.1.81:7006@17006 slave a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1 0 1608368889000 9 connected
5.8 删除节点
# 删除节点端口为7009的主库
[root@redis01 ~]# redis-cli --cluster reshard 172.16.1.81:7009 -a 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.16.1.81:7009)
M: a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1 172.16.1.81:7009
slots:[0-1364],[5462-6824],[10923-12285] (4091 slots) master
1 additional replica(s)
M: 1e077002ea5fd001d6e779412f0428dcb11dbeeb 172.16.1.81:7001
slots:[6825-10922] (4098 slots) master
2 additional replica(s)
M: c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000
slots:[1365-5461] (4097 slots) master
1 additional replica(s)
S: aa7fe1c114b81870c0882f62e46984b628e2a73a 172.16.1.81:7007
slots: (0 slots) slave
replicates 1e077002ea5fd001d6e779412f0428dcb11dbeeb
S: a47861348ce0f0725914b7ecc9e8e62e937ad9c9 172.16.1.81:7003
slots: (0 slots) slave
replicates 1e077002ea5fd001d6e779412f0428dcb11dbeeb
S: 6aa18115c3f918c9ae40156805b8aa748d39d8a5 172.16.1.81:7005
slots: (0 slots) slave
replicates c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6
M: b913386a514f464b64d60a4fde7100a1039c4222 172.16.1.81:7002
slots:[12286-16383] (4098 slots) master
1 additional replica(s)
S: b1c0938e1fe8b4fe375cb691f9bb7243901913d9 172.16.1.81:7004
slots: (0 slots) slave
replicates b913386a514f464b64d60a4fde7100a1039c4222
S: 48184f81c2c695869a32d8b8dc02382ea52d7420 172.16.1.81:7006
slots: (0 slots) slave
replicates a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1
[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)? 4091
# 目标卡槽id
What is the receiving node ID? c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6
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
Source node #1: a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1
Source node #2: done
Ready to move 4091 slots.
Source nodes:
M: a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1 172.16.1.81:7009
slots:[0-1364],[5462-6824],[10923-12285] (4091 slots) master
1 additional replica(s)
Destination node:
M: c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000
slots:[1365-5461] (4097 slots) master
1 additional replica(s)
# 分配的计划
Resharding plan:
Moving slot 0 from a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1
Moving slot 1 from a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1
Moving slot 2 from a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1
......
......
# 是否按照计划分配
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 0 from 172.16.1.81:7009 to 172.16.1.81:7000:
Moving slot 1 from 172.16.1.81:7009 to 172.16.1.81:7000:
.......
5.9 查看重新分配节点数据
# 可以看到节点主库7009的端口已经没有卡槽点了
[root@redis01 ~]# redis-cli -a 1 -p 7000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1 172.16.1.81:7009@17009 master - 0 1608371687873 9 connected
48184f81c2c695869a32d8b8dc02382ea52d7420 172.16.1.81:7006@17006 slave c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 0 1608371686869 10 connected
6aa18115c3f918c9ae40156805b8aa748d39d8a5 172.16.1.81:7005@17005 slave c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 0 1608371686000 10 connected
b913386a514f464b64d60a4fde7100a1039c4222 172.16.1.81:7002@17002 master - 0 1608371686066 1 connected 12286-16383
1e077002ea5fd001d6e779412f0428dcb11dbeeb 172.16.1.81:7001@17001 master - 0 1608371686000 5 connected 6825-10922
56bcb9fd289b378bd514bc479e649eef674d2b9a :0@0 slave,fail,noaddr b913386a514f464b64d60a4fde7100a1039c4222 1608365972565 1608365970050 1 disconnected
aa7fe1c114b81870c0882f62e46984b628e2a73a 172.16.1.81:7007@17007 slave 1e077002ea5fd001d6e779412f0428dcb11dbeeb 0 1608371686568 5 connected
c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000@17000 myself,master - 0 1608371685000 10 connected 0-6824 10923-12285
a47861348ce0f0725914b7ecc9e8e62e937ad9c9 172.16.1.81:7003@17003 slave 1e077002ea5fd001d6e779412f0428dcb11dbeeb 0 1608371687000 5 connected
b1c0938e1fe8b4fe375cb691f9bb7243901913d9 172.16.1.81:7004@17004 slave b913386a514f464b64d60a4fde7100a1039c4222 0 1608371687070 1 connected
[root@redis01 ~]#
7.10 从集群中删除节点7009的主库
[root@redis01 ~]# redis-cli --cluster del-node -a 1 172.16.1.81:7009 a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node a1aa1c42370891f6ca6a6a5fd8c73123c1dba4a1 from cluster 172.16.1.81:7009
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
# 查看集群节点已经没有7009端口的主库了说明已经删除了
[root@redis01 ~]# redis-cli -a 1 -p 7000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
48184f81c2c695869a32d8b8dc02382ea52d7420 172.16.1.81:7006@17006 slave c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 0 1608372004000 10 connected
6aa18115c3f918c9ae40156805b8aa748d39d8a5 172.16.1.81:7005@17005 slave c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 0 1608372005070 10 connected
b913386a514f464b64d60a4fde7100a1039c4222 172.16.1.81:7002@17002 master - 0 1608372005000 1 connected 12286-16383
1e077002ea5fd001d6e779412f0428dcb11dbeeb 172.16.1.81:7001@17001 master - 0 1608372005573 5 connected 6825-10922
56bcb9fd289b378bd514bc479e649eef674d2b9a :0@0 slave,fail,noaddr b913386a514f464b64d60a4fde7100a1039c4222 1608365972565 1608365970050 1 disconnected
aa7fe1c114b81870c0882f62e46984b628e2a73a 172.16.1.81:7007@17007 slave 1e077002ea5fd001d6e779412f0428dcb11dbeeb 0 1608372005070 5 connected
c94f0e1c1547d05d2f50b581f0e197c9f2c00cc6 172.16.1.81:7000@17000 myself,master - 0 1608372003000 10 connected 0-6824 10923-12285
a47861348ce0f0725914b7ecc9e8e62e937ad9c9 172.16.1.81:7003@17003 slave 1e077002ea5fd001d6e779412f0428dcb11dbeeb 0 1608372004000 5 connected
b1c0938e1fe8b4fe375cb691f9bb7243901913d9 172.16.1.81:7004@17004 slave b913386a514f464b64d60a4fde7100a1039c4222 0 1608372005673 1 connected