Redis series (three): horizontal scaling and stretching Redis cluster

First, the horizontal expansion Redis cluster

  After Redis3.0 version, with the function of the cluster, providing higher than the previous versions of Sentinel model performance and availability, but the horizontal expansion of the cluster was too much trouble, then introduce Redis How do high-availability cluster level extension, in the original 2 new node 6 based on the cluster nodes, from three main 3 from the main 4 into the 4, 3 from the previous 3 main deployment see Redis series (B): Redis high availability cluster , following FIG. :

 Second, the specific operation of the horizontal extension

   The redis-5.0.2 folder to the new host 192.168.160.154 up, (1) scp -r /usr/local/redis-5.0.2 [email protected] : / usr / local / go to 192.168. host 160.154 (2) cd / usr / local

    new redis start two instances, and then check whether the successful launch (1) /usr/local/redis-5.0.2/bin/redis-server /usr/local/redis-5.0.2/redis-cluster/700*/ redis.conf (2) ps -ef | grep redis see whether a successful start

   ③  View redis cluster command help (1) cd /usr/local/redis-5.0.2 (2) bin / redis-cli --cluster help

create: Create a cluster host1: port1 ... hostN: portN
call: redis command can be executed
the Add - the Node: add a node to the cluster, the first argument for the new node ip: port, ip second parameter is a cluster of any existing node: port
del-node:移除一个节点
reshard:重新分片
check:检查集群状态

  ④ 使用add-node命令新增一个主节点192.168.160.154:7001(master),前面的ip:port为新增节点,后面的ip:port为集群中已存在节点(1)/usr/local/redis-5.0.2/bin/redis-cli --cluster add-node 192.168.160.154:7001 192.168.160.146:7001

   (1)连接任意一个客户端即可:./redis-cli -c -h -p (-a访问服务端密码,-c表示集群模式,指定ip地址和端口号)如:/usr/local/redis-5.0.2/bin/redis-cli -c -h 192.168.160.146 -p 700* (2)进行验证: cluster info(查看集群信息)、cluster nodes(查看节点列表)

    使用redis-cli命令为192.168.160.154:7001分配slots槽位,找到集群中的任意一个主节点,对其进行重新分片工作。(1)/usr/local/redis-5.0.2/bin/redis-cli --cluster reshard 192.168.160.146:7001

How many slots do you want to move (from 1 to 16384)? 4000
(ps:需要多少个槽移动到新的节点上,自己设置,比如4000个hash槽)
What is the receiving node ID? 44b0bd6cf056af7dbbfa0dd9497def1cfc21eb6d
(ps:把这4000个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为从所有主节点中分别抽取相应的槽数指定到新节点中,抽取的总槽数为4000个;或者输入原节点ID然后输入done,意思将输入的节点ID,抽取的总槽数为4000个)
Do you want to proceed with the proposed reshard plan (yes/no)? yes
(ps:输入yes确认开始执行分片任务)

  ⑦ 查看下最新的集群状态

 

    添加从节点192.168.160.154:7002到集群中去并查看集群状态

  (1)/usr/local/redis-5.0.2/bin/redis-cli --cluster add-node 192.168.160.154:7002 192.168.160.146:7001

  (2)cluster nodes

  (3)如上图所示,还是一个master节点,没有被分配任何的hash槽。我们需要执行replicate命令来指定当前节点(从节点)的主节点id为哪个,首先需要连接新加的192.168.160.154:7002节点的客户端,然后使用集群命令进行操作,把当前的192.168.160.154:7002(slave)节点指定到一个主节点下(这里使用之前创建的192.168.160.154:7001主节点)

  (3-1)/usr/local/redis-5.0.2/bin/redis-cli -c -h 192.168.160.154 -p 7002

  (3-2)cluster replicate 44b0bd6cf056af7dbbfa0dd9497def1cfc21eb6d #后面这串id为192.168.160.154:7001的节点id

   (3-2)cluster nodes

 三、水平伸缩具体操作

  目的还原成原始集群,如下图:

    删除192.168.160.154:7002从节点,用del-node删除从节点192.168.160.154:7002,指定删除节点ip和端口,以及节点id(192.168.160.154:7002节点id) 

  (1)/usr/local/redis-5.0.2/bin/redis-cli --cluster del-node 192.168.160.154:7002 564963541c243365cbb20aed69e98048d21d68fd

   (2)cluster nodes

    删除192.168.160.154:7001主节点,这个步骤相对麻烦一些,因为主节点的里面是有分配了slots槽位,所以必须先把192.168.160.154:7001里的slots槽位放入到其他的可用主节点中去,然后再进行移除节点操作,不然会出现数据丢失问题(目前只能把master的数据迁移到一个节点上,暂时做不了平均分配功能)

  (1)/usr/local/redis-5.0.2/bin/redis-cli --cluster reshard 192.168.160.154:7001

How many slots do you want to move (from 1 to 16384)? 4000
(ps:需要多少个槽移动到新的节点上,自己设置,比如4000个hash槽)
What is the receiving node ID? e7f80ba80749904838b6d779a0646e7f22313624
(ps:把这4000个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:44b0bd6cf056af7dbbfa0dd9497def1cfc21eb6d
Source node 1:done
(ps:输入all为从所有主节点中分别抽取相应的槽数指定到新节点中,抽取的总槽数为4000个;或者输入原节点ID然后输入done,意思将输入的节点ID,抽取的总槽数为4000个)
Do you want to proceed with the proposed reshard plan (yes/no)? yes
(ps:输入yes确认开始执行分片任务)

  (2)cluster nodes 已经成功的把192.168.160.154:7001主节点的数据迁移到192.168.160.146:7001上去了

    (3)最后我们直接使用del-node命令删除192.168.160.154:7001主节点即可

 

   (4)最后执行cluster nodes

Guess you like

Origin www.cnblogs.com/toby-xu/p/11964409.html