Redis集群——Redis4.0.1高可用集群水平扩展

根据Redis4.0.1版本高可用集群模式搭建(3.9.2版本适用)这篇文章已经可以成功搭建一个由三个小集群构成的redis高可用集群,现在来对这个集群进行水平扩展,再次添加一个主节点和一个从节点。

PS:由于本人的电脑配置有限,就不再搞一个虚拟机了,仍然在192.168.1.12机器上添加这两个节点。

扩展节点

现在192.168.1.12机器上有7003和7006两个节点,添加7007和7008节点

第一步,创建文件夹

mkdir -p /opt/redis-cluster/{7007,7008}

第二步,复制配置文件到7007和7008文件夹下

cd /opt/redis-cluster
cp 7003/redis-7003.conf 7007/redis-7007.conf
cp 7003/redis-7003.conf 7008/redis-7008.conf

第三步,修改redis-7007.conf和redis-7008的conf,把端口修改为对应的7007和7008,可以使用批量修改。

第四步,启动这两个节点

redis-server /opt/redis-cluster/7007/redis-7007.conf 
redis-server /opt/redis-cluster/7008/redis-7008.conf 

第五步,开放端口

firewall-cmd --zone=public --add-port=7007/tcp --permanent
firewall-cmd --zone=public --add-port=17007/tcp --permanent
firewall-cmd --zone=public --add-port=7008/tcp --permanent
firewall-cmd --zone=public --add-port=17008/tcp --permanent
firewall-cmd --reload

现在这两个节点还没有加入到集群中,先把主节点7007加入到集群中。仍然需要使用redis-trib.rb命令来操作,所以我们回到192.168.1.10服务器上操作。

添加主节点

通过add-node命令添加节点,192.168.1.12:7007是要添加的节点,192.168.1.10:7001是集群已经存在的任意一个节点

[root@localhost src]# ./redis-trib.rb add-node 192.168.1.12:7007 192.168.1.10:7001

当看到[OK] New node added correctly.就是添加成功了。现在查看一下集群的节点信息

集群中已经有7007节点了并且是主节点,现在就算是直接使用集群也不会向7007节点里写入数据,因为7007节点还没有分配任何的slot(hash槽),现在为7007分配slot。

root@localhost src]# ./redis-trib.rb reshard 192.168.1.11:7002

reshard表示重新分配slot,后面跟的是集群中任意一个主节点,回车命令

打印很多信息

How many slots do you want to move (from 1 to 16384)? 

这个是问你要分配多少slot到新的节点上,从1到16384选一个数字,(16384是可以配置的),这里输入600

What is the receiving node ID? 89b421f99a3c7f2d7afe09ceefb2f9449abfc209

要分配到哪个id的节点,这里输入7007节点的id

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选择在当前所有主节点中取出slot分配给7007节点

输入主节点的id则是选择指定的主节点取出slot分配给7007节点

输入all,回车

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

是否开始分配,输入yes,然后打印在哪个节点取了哪个slot分配到7007节点

到现在就分配slot结束,再次查看集群节点信息。

发现7007节点已经有slot了,可以写入数据了。到此为止,7007已经加入到集群中,并且是主节点。

添加从节点

现在开始添加从节点,和添加7007节点一样使用add-node命令,出现[OK] New node added correctly.就是添加成功了

[root@localhost src]# ./redis-trib.rb add-node 192.168.1.12:7008 192.168.1.10:7001

查看节点信息

发现7008节点已经添加到集群了,但是是一个主节点,现在把7008节点改成7007的从节点

通过redis-cli -c -h 192.168.1.12 -p 7008进入到7008节点

redis-cli -c -h 192.168.1.12 -p 7008

通过cluster replicate 89b421f99a3c7f2d7afe09ceefb2f9449abfc209命令将7008节点改为7007的从节点

89b421f99a3c7f2d7afe09ceefb2f9449abfc209是7007节点的id

再次查看节点信息

7008节点已经改成7007的从节点了。

到此为止已经完成了水平扩展,添加了一个主节点和一个从节点。

移除节点

移除从节点(7008节点)

通过./redis-trib.rb的del-node移除,192.168.1.12:7008是移除的节点ip和端口,5a1c5ec76126a1d0ce95b1f7ab70112777088c34

是移除的节点的id

./redis-trib.rb del-node 192.168.1.12:7008 5a1c5ec76126a1d0ce95b1f7ab70112777088c34

打印

>>> Removing node 5a1c5ec76126a1d0ce95b1f7ab70112777088c34 from cluster 192.168.1.12:7008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
而且去12服务器查看7008的服务也关掉了

[root@localhost redis-4.0.1]# ps -ef | grep redis
root       1227      1  0 15:14 ?        00:00:10 redis-server 192.168.1.12:7003 [cluster]
root       1232      1  0 15:14 ?        00:00:09 redis-server 192.168.1.12:7006 [cluster]
root       1283      1  0 16:37 ?        00:00:02 redis-server 192.168.1.12:7007 [cluster]
root       1375   1204  0 17:20 pts/0    00:00:00 grep --color=auto redis

查看节点信息,已经没有7008节点了


移除主节点(7007节点)

在移除主节点前必须先移除它的slot,保证数据不被丢失还是通过reshard命令

[root@localhost src]# ./redis-trib.rb reshard 192.168.1.12:7007

How many slots do you want to move (from 1 to 16384)? 

这里输入599,是不能把600个都移走的

What is the receiving node ID? 927d10c90f1db9654a21ed6b9998e43fda64f6e4

要分配到哪个id的节点,这里输入7002节点的id,即移动7007的slot到7002节点(必须选主节点)

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:
89b421f99a3c7f2d7afe09ceefb2f9449abfc209
输入7007节点的id,即把7007节点作为数据源把slot移动到7002节点

Source node #2:done

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

开始打印移动slot的信息

再次查看节点信息,7007节点没有slot了。

删除7007节点

./redis-trib.rb del-node 192.168.1.12:7007 89b421f99a3c7f2d7afe09ceefb2f9449abfc209

[root@localhost src]# ./redis-trib.rb del-node 192.168.1.12:7007 89b421f99a3c7f2d7afe09ceefb2f9449abfc209
>>> Removing node 89b421f99a3c7f2d7afe09ceefb2f9449abfc209 from cluster 192.168.1.12:7007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

再次查看节点信息,发现7007节点已经移除,并且把slot移动到了7002节点

到此为止就成功的移除了一个主节点一个从节点,

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值