一、前言
系统环境:Mac OS
Redis版本:redis-5.0.3
之前用redis-6.2.5 测试了主从配置和哨兵模式,但是使用该版本搭建cluster 集群时,报以下错误:
AntonyZhang:8001 zwq$ redis-cli --cluster create --cluster-replicas 1 192.168.1.101:8001 192.168.1.101:8002 192.168.1.101:8003 192.168.1.101:8004 192.168.1.101:8005 192.168.1.101:8006
[ERR] Node 192.168.1.101:8001 NOAUTH Authentication required.
在网上没有找到可用的解决方案,之后使用redis-5.0.3,就没有出现该问题。
二、集群搭建
2.1 集群节点信息配置
redis集群需要至少三个master节点,本次搭建三个master节点,并且给每个master再搭建一个slave节点,总共6个redis节点。这里用一台机器部署6个redis实例,3组一主一从。
搭建集群的步骤如下:
1、在的redis安装目录下/usr/local/redis-5.0.3
创建文件夹cluster
,然后在其下面创建6个文件夾:
AntonyZhang:cluster zwq$ mkdir 8001 8002 8003 8004 8005 8006
2、把/usr/local/redis-5.0.3/
下的redis.conf配置文件copy到8001下,修改如下内容:
(1)daemonize yes
(2)port 8001 #(分别对每个redis实例的端口号进行设置)
(3)pidfile /var/run/redis_8001.pid # 把pid进程号写入pidfile配置的文件
(4)dir /usr/local/redis-5.0.3/cluster/8001/ #(指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据)
(5)cluster-enabled yes #(启动集群模式)
(6)cluster-config-file nodes-8001.conf #(集群节点信息文件,这里800x最好和port对应上)
(7)cluster-node-timeout 10000 # 防止因网络波动,就可能发生选主事件(其实集群实例时正常的),从而引发脑裂问题
(8)# bind 127.0.0.1(bind绑定的是自己机器网卡的ip,如果有多块网卡可以配多个ip,代表允许客户端通过机器的哪些网卡ip去访问,内网一般可以不配置bind,注释掉即可)
(9)protected-mode no #(关闭保护模式)
(10)appendonly yes
# 如果要设置密码需要增加如下配置:
(11)requirepass 111111 #(设置redis访问密码)
(12)masterauth 111111 #(设置集群节点间访问密码,跟上面一致)
3、把修改后的配置文件,copy到其它5个文件夹下,修改第2、3、4、6项里的端口号,可以用批量替换::%s/源字符串/目的字符串/g
4、分别启动6个redis实例
AntonyZhang:cluster zwq$ pwd
/usr/local/redis-5.0.3/cluster
AntonyZhang:cluster zwq$ redis-server ./8001/redis.conf
AntonyZhang:cluster zwq$ redis-server ./8002/redis.conf
AntonyZhang:cluster zwq$ redis-server ./8003/redis.conf
AntonyZhang:cluster zwq$ redis-server ./8004/redis.conf
AntonyZhang:cluster zwq$ redis-server ./8005/redis.conf
AntonyZhang:cluster zwq$ redis-server ./8006/redis.conf
5、查看是否启动成功
AntonyZhang:cluster zwq$ ls
8001 8002 8003 8004 8005 8006
AntonyZhang:cluster zwq$ ps -ef|grep redis
501 75832 1 0 7:57下午 ?? 0:01.20 redis-server *:8001 [cluster]
501 75836 1 0 7:57下午 ?? 0:01.17 redis-server *:8002 [cluster]
501 75838 1 0 7:57下午 ?? 0:01.16 redis-server *:8003 [cluster]
501 75840 1 0 7:58下午 ?? 0:01.16 redis-server *:8004 [cluster]
501 75844 1 0 7:58下午 ?? 0:01.13 redis-server *:8006 [cluster]
501 75894 1 0 8:02下午 ?? 0:00.38 redis-server *:8005 [cluster]
501 75918 64451 0 8:03下午 ttys000 0:00.00 grep redis
2.2 创建集群
用redis-cli创建整个redis集群(redis5以前的版本集群是依靠ruby脚本redis-trib.rb实现)
2.2.1 注意事项📢
如果以上redis实例分布部署在不同机器,为了保证redis实例要能相互访问,可以先简单把所有机器防火墙关掉,如果不关闭防火墙则需要打开redis服务端口和集群节点gossip通信端口16379(默认是在redis端口号上加1W)。
关闭防火墙:
systemctl stop firewalld # 临时关闭防火墙
systemctl disable firewalld # 禁止开机启动
2.2.2 创建步骤
1. 用redis-cli创建整个redis集群
AntonyZhang:cluster zwq$ redis-cli -a 111111 --cluster create --cluster-replicas 1 192.168.1.100:8001 192.168.1.100:8002 192.168.1.100:8003 192.168.1.100:8004 192.168.1.100:8005 192.168.1.100:8006
命令格式:redis-cli -a password --cluster create --cluster-replicas 1 ip:port
命令中的“1”
代表为每个创建的主服务器节点创建一个从服务器节点
2. 验证集群
以集群节点方式 登录客户端,查看集群信息:
-c
: 集群模式
-a
: 服务端密码
cluster nodes
:查看节点列表
cluster info
:查看集群信息
AntonyZhang:8005 zwq$ redis-cli -a 111111 -c -h 192.168.1.100 -p 8001
192.168.1.100:8001> cluster nodes
fade8fc7aa5ebfcf84562d915092e87218ed9d5b 192.168.1.100:8006@18006 slave 85f4fe250d89d842c6b642b4daf1abe97b599541 0 1628165064766 6 connected
611f422d6f7121dd8f10c1e0f617feebacddb673 192.168.1.100:8001@18001 myself,master - 0 1628165067000 1 connected 0-5460
3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 192.168.1.100:8005@18005 slave 611f422d6f7121dd8f10c1e0f617feebacddb673 0 1628165069820 5 connected
70414914178a2f1bbbb8c4608d09c5b1f6aa3df3 192.168.1.100:8004@18004 slave 86f3e32a70827a05586690b99074f3a10b0b4555 0 1628165069000 4 connected
86f3e32a70827a05586690b99074f3a10b0b4555 192.168.1.100:8003@18003 master - 0 1628165068000 3 connected 10923-16383
85f4fe250d89d842c6b642b4daf1abe97b599541 192.168.1.100:8002@18002 master - 0 1628165068811 2 connected 5461-10922
根据节点列表,可以得到集群的节点信息如下:
3. 数据操作验证
192.168.1.100:8001> set tonight 666
OK
192.168.1.100:8001> quit
AntonyZhang:8005 zwq$ redis-cli -a 111111 -c -h 192.168.1.100 -p 8005
192.168.1.100:8005> get tonight
-> Redirected to slot [262] located at 192.168.1.100:8001
"666"
192.168.1.100:8001> quit
AntonyZhang:8005 zwq$ redis-cli -a 111111 -c -h 192.168.1.100 -p 8003
192.168.1.100:8003> get tonight
-> Redirected to slot [262] located at 192.168.1.100:8001
"666"
4. Jedis客户端访问
1、引入jedis坐标依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2、测试代码
public class JedisClusterTest {
public static void main(String[] args) throws IOException {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(10);
config.setMinIdle(5);
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("192.168.1.100", 8001));
jedisClusterNode.add(new HostAndPort("192.168.1.100", 8002));
jedisClusterNode.add(new HostAndPort("192.168.1.100", 8003));
jedisClusterNode.add(new HostAndPort("192.168.1.100", 8004));
jedisClusterNode.add(new HostAndPort("192.168.1.100", 8005));
jedisClusterNode.add(new HostAndPort("192.168.1.100", 8006));
JedisCluster jedisCluster = null;
try {
//connectionTimeout:指的是连接一个url的连接等待时间
//soTimeout:指的是连接上一个url,获取response的返回等待时间
jedisCluster = new JedisCluster(jedisClusterNode, 6000, 5000, 10, "111111", config);
System.out.println(jedisCluster.set("cluster", "myCluster233"));
System.out.println(jedisCluster.get("cluster"));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedisCluster != null)
jedisCluster.close();
}
}
}
运行结果如下:
OK
myCluster233
2.3 集群脑裂数据丢失问题
概念:redis集群没有过半机制会有脑裂问题,网络分区导致脑裂后多个主节点对外提供写服务,一旦网络分区恢复,会将其中一个主节点变为从节点,这时会有大量数据丢失。
规避方法:可以在redis配置里加上参数min-replicas-to-write
(这种方法不可能百分百避免数据丢失,参考集群leader选举机制):
min-replicas-to-write 1 //写数据成功最少同步的slave数量,这个数量可以模仿大于半数机制配置,比如集群总共三个节点可以配置1,加上leader就是2,超过了半数
-
配置该值时:当客户端写入数据到服务端时,主节点写入成功后,不会立即通知客户端写入数据成功,而是将数据同步至1个slave节点成功后,才通知客户端写入成功,因为此时写入成功的节点为2个(master+slave1)> 主从节点总数2个/2=1
-
不配置该值时:只要master节点写入成功,就会通知客户端操作成功
1. 不配置min-replicas-to-write
1、关闭slave节点,当在master启动之后,客户端写入数据成功!8005 是 8001 的slave节点。
AntonyZhang:cluster zwq$ ps -ef|grep redis
501 75832 1 0 7:57下午 ?? 0:01.20 redis-server *:8001 [cluster]
501 75836 1 0 7:57下午 ?? 0:01.17 redis-server *:8002 [cluster]
501 75838 1 0 7:57下午 ?? 0:01.16 redis-server *:8003 [cluster]
501 75840 1 0 7:58下午 ?? 0:01.16 redis-server *:8004 [cluster]
501 75844 1 0 7:58下午 ?? 0:01.13 redis-server *:8006 [cluster]
501 75894 1 0 8:02下午 ?? 0:00.38 redis-server *:8005 [cluster]
501 75918 64451 0 8:03下午 ttys000 0:00.00 grep redis
AntonyZhang:cluster zwq$ kill 75894
AntonyZhang:8005 zwq$ redis-cli -a 111111 -c -h 192.168.1.100 -p 8001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.100:8001> set today 666
OK
2. 配置min-replicas-to-write
1、关闭master
AntonyZhang:cluster zwq$ ps -ef|grep redis
501 75832 1 0 7:57下午 ?? 0:06.86 redis-server *:8001 [cluster]
501 75836 1 0 7:57下午 ?? 0:06.81 redis-server *:8002 [cluster]
501 75838 1 0 7:57下午 ?? 0:06.86 redis-server *:8003 [cluster]
501 75840 1 0 7:58下午 ?? 0:06.82 redis-server *:8004 [cluster]
501 75844 1 0 7:58下午 ?? 0:06.76 redis-server *:8006 [cluster]
501 76256 64451 0 8:32下午 ttys000 0:00.00 grep redis
501 75956 72280 0 8:07下午 ttys001 0:00.01 redis-cli -a 111111 -c -h 192.168.1.100 -p 8003
AntonyZhang:cluster zwq$ kill 75832
AntonyZhang:cluster zwq$ ps -ef|grep redis
501 75836 1 0 7:57下午 ?? 0:07.61 redis-server *:8002 [cluster]
501 75838 1 0 7:57下午 ?? 0:07.64 redis-server *:8003 [cluster]
501 75840 1 0 7:58下午 ?? 0:07.60 redis-server *:8004 [cluster]
501 75844 1 0 7:58下午 ?? 0:07.56 redis-server *:8006 [cluster]
501 76282 64451 0 8:34下午 ttys000 0:00.00 grep redis
2、在master的conf文件配置该值后,重启master实例
AntonyZhang:cluster zwq$ vim ./8001/redis.conf
8001 - redis.conf设置如下:
min-replicas-to-write 1
重新启动8001的实例:
AntonyZhang:cluster zwq$ redis-server ./8001/redis.conf
3、在客户端写入数据,会失败!因为此时salve是关闭的, 数据只在一个节点写入成功,违
背了超过半数写入成功才算成功的原则
AntonyZhang:8005 zwq$ redis-cli -a 111111 -c -h 192.168.1.100 -p 8001
192.168.1.100:8001> set today1 666
(error) NOREPLICAS Not enough good replicas to write.
4、此时,启动slave后,再次写入数据,便会写入成功
AntonyZhang:cluster zwq$ redis-server ./8005/redis.conf
AntonyZhang:8005 zwq$ redis-cli -a 111111 -c -h 192.168.1.100 -p 8001
192.168.1.100:8001> set today1 666
OK
2.4 主从切换验证
1、kill 掉master 8001 的实例
AntonyZhang:cluster zwq$ ps -ef|grep redis
501 75836 1 0 7:57下午 ?? 0:10.74 redis-server *:8002 [cluster]
501 75838 1 0 7:57下午 ?? 0:10.77 redis-server *:8003 [cluster]
501 75840 1 0 7:58下午 ?? 0:10.71 redis-server *:8004 [cluster]
501 75844 1 0 7:58下午 ?? 0:10.67 redis-server *:8006 [cluster]
501 76300 1 0 8:36下午 ?? 0:02.58 redis-server *:8001 [cluster]
501 76328 1 0 8:39下午 ?? 0:01.64 redis-server *:8005 [cluster]
501 76404 64451 0 8:47下午 ttys000 0:00.00 grep redis
AntonyZhang:cluster zwq$ kill 76300
2、登录 8003的集群节点客户端,可以看到8005由salve 晋升到了master节点
AntonyZhang:8005 zwq$ redis-cli -a 111111 -c -h 192.168.1.100 -p 8003
192.168.1.100:8003> cluster nodes
3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 192.168.1.100:8005@18005 master - 0 1628167702278 7 connected 0-5460
fade8fc7aa5ebfcf84562d915092e87218ed9d5b 192.168.1.100:8006@18006 slave 85f4fe250d89d842c6b642b4daf1abe97b599541 0 1628167703293 6 connected
70414914178a2f1bbbb8c4608d09c5b1f6aa3df3 192.168.1.100:8004@18004 slave 86f3e32a70827a05586690b99074f3a10b0b4555 0 1628167703000 4 connected
85f4fe250d89d842c6b642b4daf1abe97b599541 192.168.1.100:8002@18002 master - 0 1628167704304 2 connected 5461-10922
86f3e32a70827a05586690b99074f3a10b0b4555 192.168.1.100:8003@18003 myself,master - 0 1628167702000 3 connected 10923-16383
611f422d6f7121dd8f10c1e0f617feebacddb673 192.168.1.100:8001@18001 master,fail - 1628167669733 1628167668821 1 disconnected
3、重新启动8001 实例后,再次查看集群各节点信息。可以看到,此时8001已经是8005的slave节点了
AntonyZhang:8005 zwq$ redis-cli -a 111111 -c -h 192.168.1.100 -p 8003
192.168.1.100:8003> cluster nodes
3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 192.168.1.100:8005@18005 master - 0 1628167998000 7 connected 0-5460
fade8fc7aa5ebfcf84562d915092e87218ed9d5b 192.168.1.100:8006@18006 slave 85f4fe250d89d842c6b642b4daf1abe97b599541 0 1628167999000 6 connected
70414914178a2f1bbbb8c4608d09c5b1f6aa3df3 192.168.1.100:8004@18004 slave 86f3e32a70827a05586690b99074f3a10b0b4555 0 1628167999003 4 connected
85f4fe250d89d842c6b642b4daf1abe97b599541 192.168.1.100:8002@18002 master - 0 1628168001024 2 connected 5461-10922
86f3e32a70827a05586690b99074f3a10b0b4555 192.168.1.100:8003@18003 myself,master - 0 1628167998000 3 connected 10923-16383
611f422d6f7121dd8f10c1e0f617feebacddb673 192.168.1.100:8001@18001 slave 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 0 1628168000014 7 connected
三、集群水平扩展
3.1 新增两个redis实例
1、创建 8007 、8008两个文件夹,复制8001的redis.conf 到这两个目录下,分别批量修改文件中 “8001” 为8007 / 8008
2、启动新加的两个实例
AntonyZhang:cluster zwq$ redis-server 8007/redis.conf
AntonyZhang:cluster zwq$ redis-server 8008/redis.conf
3、查看所有实例
AntonyZhang:cluster zwq$ ps -ef|grep redis
501 75836 1 0 7:57下午 ?? 0:18.82 redis-server *:8002 [cluster]
501 75838 1 0 7:57下午 ?? 0:18.88 redis-server *:8003 [cluster]
501 75840 1 0 7:58下午 ?? 0:18.81 redis-server *:8004 [cluster]
501 75844 1 0 7:58下午 ?? 0:18.74 redis-server *:8006 [cluster]
501 76328 1 0 8:39下午 ?? 0:09.74 redis-server *:8005 [cluster]
501 76454 1 0 8:52下午 ?? 0:06.39 redis-server *:8001 [cluster]
501 76701 1 0 9:30下午 ?? 0:01.26 redis-server *:8007 [cluster]
501 76772 1 0 9:36下午 ?? 0:00.02 redis-server *:8008 [cluster]
3.2 将8007实例加入集群
1. 使用客户端查看集群帮助手册
AntonyZhang:8005 zwq$ redis-cli --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN
--cluster-replicas <arg>
check host:port
--cluster-search-multiple-owners
info host:port
fix host:port
--cluster-search-multiple-owners
reshard host:port
--cluster-from <arg>
--cluster-to <arg>
--cluster-slots <arg>
--cluster-yes
--cluster-timeout <arg>
--cluster-pipeline <arg>
--cluster-replace
rebalance host:port
--cluster-weight <node1=w1...nodeN=wN>
--cluster-use-empty-masters
--cluster-timeout <arg>
--cluster-simulate
--cluster-pipeline <arg>
--cluster-threshold <arg>
--cluster-replace
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id <arg>
del-node host:port node_id
call host:port command arg arg .. arg
set-timeout host:port milliseconds
import host:port
--cluster-from <arg>
--cluster-copy
--cluster-replace
2. 使用add-node
命令为集群添加新节点
add-node
new_host:new_port existing_host:existing_port
- new_host:new_port:新节点IP:新节点端口
- existing_host:existing_port:表示集群中已经存在的任意一个节点,新节点会以master节点方式加入集群
将8007节点加入集群:
AntonyZhang:cluster zwq$ redis-cli -a 111111 --cluster add-node 192.168.1.100:8007 192.168.1.100:8001
>>> Adding node 192.168.1.100:8007 to cluster 192.168.1.100:8001
>>> Performing Cluster Check (using node 192.168.1.100:8001)
S: 611f422d6f7121dd8f10c1e0f617feebacddb673 192.168.1.100:8001
slots: (0 slots) slave
replicates 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3
S: 70414914178a2f1bbbb8c4608d09c5b1f6aa3df3 192.168.1.100:8004
slots: (0 slots) slave
replicates 86f3e32a70827a05586690b99074f3a10b0b4555
S: fade8fc7aa5ebfcf84562d915092e87218ed9d5b 192.168.1.100:8006
slots: (0 slots) slave
replicates 85f4fe250d89d842c6b642b4daf1abe97b599541
M: 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 192.168.1.100:8005
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 85f4fe250d89d842c6b642b4daf1abe97b599541 192.168.1.100:8002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 86f3e32a70827a05586690b99074f3a10b0b4555 192.168.1.100:8003
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 192.168.1.100:8007 to make it join the cluster.
[OK] New node added correctly.
3. 查看集群最新节点列表
以集群方式,登录8003查看集群节点信息;可以看到,新加入集群的master节点8007没有分配slot槽位,因此无法设置数据
4. 为新节点8007分配槽位
因为Redis Cluster将所有数据划分为 16384 个slots(槽位),每个节点负责其中一部分槽位。槽位的信息存储于每个节点中,所以8007节点的槽位来自于其它节点,需要将其它节点的部分槽位段移动至8007节点
使用客户端的reshard
命令完成槽位迁移:
AntonyZhang:cluster zwq$ redis-cli -a 111111 --cluster reshard 192.168.1.100:8001
>>> Performing Cluster Check (using node 192.168.1.100:8001)
S: 611f422d6f7121dd8f10c1e0f617feebacddb673 192.168.1.100:8001
slots: (0 slots) slave
replicates 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3
M: d4d7760c2fdacc4e295458191287d1e78cc74c77 192.168.1.100:8007
slots: (0 slots) master
S: 70414914178a2f1bbbb8c4608d09c5b1f6aa3df3 192.168.1.100:8004
slots: (0 slots) slave
replicates 86f3e32a70827a05586690b99074f3a10b0b4555
S: fade8fc7aa5ebfcf84562d915092e87218ed9d5b 192.168.1.100:8006
slots: (0 slots) slave
replicates 85f4fe250d89d842c6b642b4daf1abe97b599541
M: 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 192.168.1.100:8005
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 85f4fe250d89d842c6b642b4daf1abe97b599541 192.168.1.100:8002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 86f3e32a70827a05586690b99074f3a10b0b4555 192.168.1.100:8003
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.
How many slots do you want to move (from 1 to 16384)? 500
What is the receiving node ID? d4d7760c2fdacc4e295458191287d1e78cc74c77
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 500 slots.
Source nodes:
M: 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 192.168.1.100:8005
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 85f4fe250d89d842c6b642b4daf1abe97b599541 192.168.1.100:8002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 86f3e32a70827a05586690b99074f3a10b0b4555 192.168.1.100:8003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
Destination node:
M: d4d7760c2fdacc4e295458191287d1e78cc74c77 192.168.1.100:8007
slots: (0 slots) master
Resharding plan:
Moving slot 5461 from 85f4fe250d89d842c6b642b4daf1abe97b599541
Moving...
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 5461 from 192.168.1.100:8002 to 192.168.1.100:8007:
...
Moving slot 65 from 192.168.1.100:8005 to 192.168.1.100:8007:
...
Moving slot 11070 from 192.168.1.100:8003 to 192.168.1.100:8007:
...
其中,需要与命令行手输交互
的信息有如下几个值:
1)How many slots do you want to move (from 1 to 16384)? 500
(想要移动到新节点的槽位数量)
2)What is the receiving node ID? d4d7760c2fdacc4e295458191287d1e78cc74c77
(将槽位数量移动到集群的哪个节点id上,此处为8007的集群节点ID)
3)Source node #1: all
(移动的槽位从所有master节点抽取)
4)Do you want to proceed with the proposed reshard plan (yes/no)? yes
(确认移动槽位)
5. 查看最新的集群节点信息
移动完成后,可以看到,8007节点已经有了槽位(移动槽位的同时,槽位中的数据也会被同步移动)
3.3 将8008实例加入集群
1. 使用add-node
命令为集群添加新节点
AntonyZhang:cluster zwq$ redis-cli -a 111111 --cluster add-node 192.168.1.100:8008 192.168.1.100:8001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.1.100:8008 to cluster 192.168.1.100:8001
>>> Performing Cluster Check (using node 192.168.1.100:8001)
S: 611f422d6f7121dd8f10c1e0f617feebacddb673 192.168.1.100:8001
slots: (0 slots) slave
replicates 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3
M: d4d7760c2fdacc4e295458191287d1e78cc74c77 192.168.1.100:8007
slots:[0-165],[5461-5627],[10923-11088] (499 slots) master
S: 70414914178a2f1bbbb8c4608d09c5b1f6aa3df3 192.168.1.100:8004
slots: (0 slots) slave
replicates 86f3e32a70827a05586690b99074f3a10b0b4555
S: fade8fc7aa5ebfcf84562d915092e87218ed9d5b 192.168.1.100:8006
slots: (0 slots) slave
replicates 85f4fe250d89d842c6b642b4daf1abe97b599541
M: 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 192.168.1.100:8005
slots:[166-5460] (5295 slots) master
1 additional replica(s)
M: 85f4fe250d89d842c6b642b4daf1abe97b599541 192.168.1.100:8002
slots:[5628-10922] (5295 slots) master
1 additional replica(s)
M: 86f3e32a70827a05586690b99074f3a10b0b4555 192.168.1.100:8003
slots:[11089-16383] (5295 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 192.168.1.100:8008 to make it join the cluster.
[OK] New node added correctly.
2. 查看集群最新节点列表
可以看到,8008在加入集群后,默认是master节点
3. 设置8008节点为8007的salve节点
1.登录8008集群节点客户端
2.执行命令 cluster replicate d4d7760c2fdacc4e295458191287d1e78cc74c77
其中,d4d7760c2fdacc4e295458191287d1e78cc74c77
为8007集群节点ID
AntonyZhang:cluster zwq$ redis-cli -a 111111 -c -h 192.168.1.100 -p 8008
192.168.1.100:8008> cluster replicate d4d7760c2fdacc4e295458191287d1e78cc74c77
OK
4. 查看最新的集群节点信息
可以看到8008已经是8007的slave节点了
192.168.1.100:8008> cluster nodes
70414914178a2f1bbbb8c4608d09c5b1f6aa3df3 192.168.1.100:8004@18004 slave 86f3e32a70827a05586690b99074f3a10b0b4555 0 1628172186000 3 connected
611f422d6f7121dd8f10c1e0f617feebacddb673 192.168.1.100:8001@18001 slave 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 0 1628172188000 7 connected
85f4fe250d89d842c6b642b4daf1abe97b599541 192.168.1.100:8002@18002 master - 0 1628172186000 2 connected 5628-10922
d4d7760c2fdacc4e295458191287d1e78cc74c77 192.168.1.100:8007@18007 master - 0 1628172188950 8 connected 0-165 5461-5627 10923-11088
6fa5379d8c4a86c847cc214995962f1df7c0b4d3 192.168.1.100:8008@18008 myself,slave d4d7760c2fdacc4e295458191287d1e78cc74c77 0 1628172187000 0 connected
86f3e32a70827a05586690b99074f3a10b0b4555 192.168.1.100:8003@18003 master - 0 1628172187000 3 connected 11089-16383
3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 192.168.1.100:8005@18005 master - 0 1628172189958 7 connected 166-5460
fade8fc7aa5ebfcf84562d915092e87218ed9d5b 192.168.1.100:8006@18006 slave 85f4fe250d89d842c6b642b4daf1abe97b599541 0 1628172187938 2 connected
3.4 删除8008 salve节点
1. 使用del-node
删除节点
6fa5379d8c4a86c847cc214995962f1df7c0b4d3
为8008节点在集群中的ID
删除后,节点实例也会shutdown
AntonyZhang:cluster zwq$ redis-cli -a 111111 --cluster del-node 192.168.1.100:8008 6fa5379d8c4a86c847cc214995962f1df7c0b4d3
>>> Removing node 6fa5379d8c4a86c847cc214995962f1df7c0b4d3 from cluster 192.168.1.100:8008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
2. 查看集群节点列表
可以看到,8008节点已经不存在了
AntonyZhang:cluster zwq$ redis-cli -a 111111 -c -h 192.168.1.100 -p 8007
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.100:8007> cluster nodes
70414914178a2f1bbbb8c4608d09c5b1f6aa3df3 192.168.1.100:8004@18004 slave 86f3e32a70827a05586690b99074f3a10b0b4555 0 1628172806155 3 connected
3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 192.168.1.100:8005@18005 master - 0 1628172805145 7 connected 166-5460
d4d7760c2fdacc4e295458191287d1e78cc74c77 192.168.1.100:8007@18007 myself,master - 0 1628172803000 8 connected 0-165 5461-5627 10923-11088
fade8fc7aa5ebfcf84562d915092e87218ed9d5b 192.168.1.100:8006@18006 slave 85f4fe250d89d842c6b642b4daf1abe97b599541 0 1628172805000 2 connected
611f422d6f7121dd8f10c1e0f617feebacddb673 192.168.1.100:8001@18001 slave 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 0 1628172804000 7 connected
85f4fe250d89d842c6b642b4daf1abe97b599541 192.168.1.100:8002@18002 master - 0 1628172804133 2 connected 5628-10922
86f3e32a70827a05586690b99074f3a10b0b4555 192.168.1.100:8003@18003 master - 0 1628172807168 3 connected 11089-16383
8008实例也已经关闭了:
AntonyZhang:cluster zwq$ ps -ef|grep redis
501 75836 1 0 7:57下午 ?? 0:26.92 redis-server *:8002 [cluster]
501 75838 1 0 7:57下午 ?? 0:27.02 redis-server *:8003 [cluster]
501 75840 1 0 7:58下午 ?? 0:26.65 redis-server *:8004 [cluster]
501 75844 1 0 7:58下午 ?? 0:26.56 redis-server *:8006 [cluster]
501 76328 1 0 8:39下午 ?? 0:17.84 redis-server *:8005 [cluster]
501 76454 1 0 8:52下午 ?? 0:14.25 redis-server *:8001 [cluster]
501 76701 1 0 9:30下午 ?? 0:09.33 redis-server *:8007 [cluster]
501 77121 64451 0 10:14下午 ttys000 0:00.00 grep redis
501 77111 72280 0 10:13下午 ttys001 0:00.01 redis-cli -a 111111 -c -h 192.168.1.100 -p 8007
3.5 删除8007 master节点
1. 移动主节点槽位到其它master节点
因为8007是主节点,拥有槽位;所以删除之前,必须把槽位移动到其它节点中去,否则槽位中的数据会丢失
AntonyZhang:cluster zwq$ redis-cli -a 111111 --cluster reshard 192.168.1.100:8007
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.1.100:8007)
M: d4d7760c2fdacc4e295458191287d1e78cc74c77 192.168.1.100:8007
slots:[0-165],[5461-5627],[10923-11088] (499 slots) master
S: 70414914178a2f1bbbb8c4608d09c5b1f6aa3df3 192.168.1.100:8004
slots: (0 slots) slave
replicates 86f3e32a70827a05586690b99074f3a10b0b4555
M: 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 192.168.1.100:8005
slots:[166-5460] (5295 slots) master
1 additional replica(s)
S: fade8fc7aa5ebfcf84562d915092e87218ed9d5b 192.168.1.100:8006
slots: (0 slots) slave
replicates 85f4fe250d89d842c6b642b4daf1abe97b599541
S: 611f422d6f7121dd8f10c1e0f617feebacddb673 192.168.1.100:8001
slots: (0 slots) slave
replicates 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3
M: 85f4fe250d89d842c6b642b4daf1abe97b599541 192.168.1.100:8002
slots:[5628-10922] (5295 slots) master
1 additional replica(s)
M: 86f3e32a70827a05586690b99074f3a10b0b4555 192.168.1.100:8003
slots:[11089-16383] (5295 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.
How many slots do you want to move (from 1 to 16384)? 500
What is the receiving node ID? 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3
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: d4d7760c2fdacc4e295458191287d1e78cc74c77
Source node #2: done
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 0 from 192.168.1.100:8007 to 192.168.1.100:8005:
...
在命令行手动交互
输入的信息如下:
1)How many slots do you want to move (from 1 to 16384)? 500
(移动的槽位数量)
2)What is the receiving node ID? 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3
(目标集群master节点ID,此处为接收8007槽位的节点ID,选择了8005)
3)Source node #1: d4d7760c2fdacc4e295458191287d1e78cc74c77
(移出槽位的集群节点ID,此处为8007)
4)Source node #2: done
(只移动一个节点,所以第二个节点输入 done,表示结束)
5)Do you want to proceed with the proposed reshard plan (yes/no)? yes
(确认移动)
2. 查看集群节点信息
可以看到,8007节点已经没有了槽位,它的槽位已经被移动到了节点8005中
192.168.1.100:8007> cluster nodes
70414914178a2f1bbbb8c4608d09c5b1f6aa3df3 192.168.1.100:8004@18004 slave 86f3e32a70827a05586690b99074f3a10b0b4555 0 1628173512415 3 connected
3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 192.168.1.100:8005@18005 master - 0 1628173511000 9 connected 0-5627 10923-11088
d4d7760c2fdacc4e295458191287d1e78cc74c77 192.168.1.100:8007@18007 myself,master - 0 1628173512000 8 connected
fade8fc7aa5ebfcf84562d915092e87218ed9d5b 192.168.1.100:8006@18006 slave 85f4fe250d89d842c6b642b4daf1abe97b599541 0 1628173510000 2 connected
611f422d6f7121dd8f10c1e0f617feebacddb673 192.168.1.100:8001@18001 slave 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 0 1628173511404 9 connected
85f4fe250d89d842c6b642b4daf1abe97b599541 192.168.1.100:8002@18002 master - 0 1628173509378 2 connected 5628-10922
86f3e32a70827a05586690b99074f3a10b0b4555 192.168.1.100:8003@18003 master - 0 1628173508000 3 connected 11089-16383
3. 删除节点
AntonyZhang:cluster zwq$ redis-cli -a 111111 --cluster del-node 192.168.1.100:8007 d4d7760c2fdacc4e295458191287d1e78cc74c77
>>> Removing node d4d7760c2fdacc4e295458191287d1e78cc74c77 from cluster 192.168.1.100:8007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
4. 查看集群节点列表:
可以看到,集群又恢复到了6个实例时的节点状态
192.168.1.100:8004> cluster nodes
3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 192.168.1.100:8005@18005 master - 0 1628173694000 9 connected 0-5627 10923-11088
fade8fc7aa5ebfcf84562d915092e87218ed9d5b 192.168.1.100:8006@18006 slave 85f4fe250d89d842c6b642b4daf1abe97b599541 0 1628173696000 6 connected
85f4fe250d89d842c6b642b4daf1abe97b599541 192.168.1.100:8002@18002 master - 0 1628173698025 2 connected 5628-10922
70414914178a2f1bbbb8c4608d09c5b1f6aa3df3 192.168.1.100:8004@18004 myself,slave 86f3e32a70827a05586690b99074f3a10b0b4555 0 1628173697000 4 connected
611f422d6f7121dd8f10c1e0f617feebacddb673 192.168.1.100:8001@18001 slave 3d8c69d44c982f1ff0cb77e464eb7b2c1d89e1d3 0 1628173695998 9 connected
86f3e32a70827a05586690b99074f3a10b0b4555 192.168.1.100:8003@18003 master - 0 1628173697011 3 connected 11089-16383