Redis学习总结第四章--Redis集群水平扩展

Redis学习总结第三章--Redis集群水平扩展

 

在这篇博客里《Redis学习总结第二章--Redis 搭建高可用集群》我们搭建的集群由6个节点组成,6个节点分布在三台机器上,采用三主三从的模式

 

 

 

1、启动集群

# 启动整个集群

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8001/redis-8001.conf

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8004/redis-8004.conf

 

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8002/redis-8002.conf

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8005/redis-8005.conf

 

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8003/redis-8003.conf

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8006/redis-8006.conf

# 客户端连接8001端口的redis实例

/usr/local/lanbing/redis-5.0.4/src/redis-cli -a lanbing -c -h 192.168.0.1 -p 8001

# 查看集群状态

192.168.01:8001> cluster  nodes

 

 

 

2、集群操作

我们在原始集群基础上再增加一主(8007)一从(8008),增加节点后的集群参见下图,新增节点用虚线框表示

克隆虚拟机

此操作可以看《Redis学习总结第二章--Redis 搭建高可用集群》这篇文章。

 

增加redis实例

# 在/usr/local/lanbing/redis-cluster下改8001和8004文件夹名为8007和8008文件夹,并修改redis-8001.conf中8001--》8007;redis-8004.conf中8004--》8008;

 

# 修改8007文件夹下的redis-8001.conf配置文件

vim /usr/local/redis-cluster/8007/redis.conf

 

# 修改如下内容:

port:8007

dir /usr/local/lanbing/redis-cluster/8007/

cluster-config-file nodes-8007.conf

 

# 修改8008文件夹下的redis-8004.conf配置文件

vim /usr/local/lanbing/redis-cluster/8008/redis-8004.conf

修改内容如下:

port:8008

dir /usr/local/lanbing/redis-cluster/8008/

cluster-config-file nodes8008.conf

 

启动redis:

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8007/redis-8007.conf

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8008/redis-8008.conf

 

查看redis集群的命令帮助

cd /usr/local/lanbing/redis-5.0.4

src/redis-cli --cluster help

1.create:创建一个集群环境host1:port1 ... hostN:portN

2.call:可以执行redis命令

3.add-node:将一个节点添加到集群里,第一个参数为新节点的ip:port,第二个参数为集群中任意一个已经存在的节点的ip:port

4.del-node:移除一个节点

5.reshard:重新分片

6.check:检查集群状态

 

增加8007主节点:

/usr/local/lanbing/redis-5.0.4/src/redis-cli -a lanbing --cluster add-node 节点Ip:8007  集群中任意masterIp:8002

注意:当添加节点成功以后,新增的节点不会有任何数据,因为它还没有分配任何的slot(hash槽),我们需要为新节点手工分配hash槽

 

# 使用redis-cli命令为8007分配hash槽,找到集群中的任意一个主节点(红色位置表示集群中的任意一个主节点),对其进行重新分片工作。

/usr/local/lanbing/redis-5.0.4/src/redis-cli -a lanbing --cluster reshard 192.168.0.61:8001

 

输出如下:

... ...

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

(ps:需要多少个槽移动到新的节点上,自己设置,比如600个hash槽)

What is the receiving node ID? eb57a5700ee6f9ff099b3ce0d03b1a50ff247c3c

(ps:把这600个hash槽移动到哪个节点上去,需要指定节点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

(ps:输入all为从所有主节点(8001,8002,8003)中分别抽取相应的槽数指定到新节点中,抽取的总槽数为600个)

 ... ...

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

(ps:输入yes确认开始执行分片任务)

... ...

 

# 查看下最新的集群状态

 

 

配置8008为8007的从节点

# 添加从节点8008到集群中去并查看集群状态

/usr/local/lanbing/redis-5.0.4/src/redis-cli -a lanbing --cluster add-node 节点Ip:8008 集群中任意masterIp:8003

如图所示,还是一个master节点,没有被分配任何的hash槽。

 

# 我们需要执行replicate命令来指定当前节点(从节点)的主节点id为哪个,首先需要连接新加的8008节点的客户端,然后使用集群命令进行操作,把当前的8008(slave)节点指定到一个主节点下(这里使用之前创建的8007主节点,红色表示节点id)

/usr/local/redis-5.0.2/src/redis-cli -c -h 192.168.0.64 -p 8008

192.168.0.61:8008> cluster replicate eb57a5700ee6f9ff099b3ce0d03b1a50ff247c3c

# 查看集群状态,8008节点已成功添加为8007节点的从节点

 

 

 

删除8008从节点

# 用del-node删除从节点8008,指定删除节点ip和端口,以及节点id(红色为8008节点id)

/usr/local/redis-5.0.2/src/redis-cli --cluster del-node 192.168.0.64:8008 1805b6339d91b0e051f46845eebacb9bc43baefe

# 再次查看集群状态,如下图所示,8008这个slave节点已经移除,并且该节点的redis服务也已被停止

 

 

删除8007主节点

最后,我们尝试删除之前加入的主节点8007,这个步骤相对比较麻烦一些,因为主节点的里面是有分配了hash槽的,所以我们这里必须先把8007里的hash槽放入到其他的可用主节点中去,然后再进行移除节点操作,不然会出现数据丢失问题(目前只能把master的数据迁移到一个节点上,暂时做不了平均分配功能),执行命令如下:

 

/usr/local/lanbing/redis-5.0.4/src/redis-cli -a lanbing --cluster reshard 节点Ip:8007输出如下:

 ... ...

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

What is the receiving node ID? deedad3c34e8437baa6ff013fd3d1461a0c2e761

(ps:这里是需要把数据移动到哪?8001的主节点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:eb57a5700ee6f9ff099b3ce0d03b1a50ff247c3c

(ps:这里是需要数据源,也就是我们的8007节点id)

Source node 2:done

(ps:这里直接输入done 开始生成迁移计划)

 ... ...

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

(ps:这里输入yes开始迁移)

 

 

 

# 最后我们直接使用del-node命令删除8007主节点即可(红色表示8007的节点id)。

/usr/local/redis-5.0.2/src/redis-cli --cluster del-node 192.168.0.64:8007    eb57a5700ee6f9ff099b3ce0d03b1a50ff247c3c

# 查看集群状态,一切还原为最初始状态啦!大功告成!

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章