redis集群操作:增加和减少节点

一  增加节点:

STEP1:  我们新建俩个服务,按照之前搭建的集群方式新增俩个节点:(一主一从master、slave)

  Master:7007   Slave: 7008

  (1)创建7007/7008文件夹。拷贝redis.conf文件到对于的7007,7008目录下要进行修改配置文件。

<root@localhost redis-cluster># mkdir 7007 7008

<root@localhost redis-cluster># cp 7001/redis.conf 7007

<root@localhost redis-cluster># cp 7001/redis.conf 7008

<root@localhost redis-cluster># vim 7007/redis.conf

使用 :%s/7001/700*/g来全局替换7007/redis.conf和7008/redis.conf

  其中主要修改内容如下:

    1> port: 7008(7007)

    2> dir /usr/local/redis-cluster/7008(7007)

    3> cluster-config-file nodes/7008(7007).conf

  (2) 启动7007和7008两个服务并查看状态

[root@localhost 7001]# /usr/local/redis/bin/redis-serve /usr/local/redis-cluster/7007/redis.conf

[root@localhost 7001]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7008/redis.conf

[root@localhost 7001]# ps-el | grep redis

 

STEP2.  学习redis-trib命令使用 :

[root@localhost local]# cd /usr/local/redis3.O/src

[root@localhost src]# redis-trib.rb

  (1) create:创建一个集群环境host1:port1 .. hostN:portN (集群中的主从节点比例)

  (2) call:可以执行redis命令

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

  (4)  del-node:移除一个节点

  (5)  reshard:重新分片

  (6)  check:检查集群状态

 

STEP3.  新增一个主节点7007 ( master)

  (1) 使用add-node命令:绿色为新增节点,红色为己知存在节点

[root@localhost 7001]#/usr/local/redis3.0/src/redistrib.rb add-node 192.168.1.39:7007 192.168.1.39:7001

输出如下:

>>> Adding node 192.168.1.39:7008 to cluster 192.168.1.39:7001

Connecting to node 192.168.1.39:7001: OK

Connecting to node 192.168.1.39:7005: OK

Connecting to node 192.168.1.39:7003: OK

Connecting to node 192.168.1.39:7004: OK

Connecting to node 192.168.1.39:7002: OK

Connecting to node 192.168.1.39:7006: OK

>>> Performing Cluster Check (using node 192.168.1.39:7001)

M: 31914885c401993b9287ed272e01e138238045ae 192.168.1.39:7001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: cd03e0d7e796c6611bc61ff6e6def0488651413d 192.168.1.39:7005

   slots: (0 slots) slave

   replicates 3937188975e89fa6e5a637e8c876504604fa7c71

M: 5dea4b4c6462d58ac58576b442fa1ad2941f81f1 192.168.1.39:7003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 15a026c7600b9f3f21d90314bc9c18fda2455dac 192.168.1.39:7004

   slots: (0 slots) slave

   replicates 31914885c401993b9287ed272e01e138238045ae

M: 3937188975e89fa6e5a637e8c876504604fa7c71 192.168.1.39:7002

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

S: c235a093d5ec86085b2628f7b648a6e489c98d7e 192.168.1.39:7006

   slots: (0 slots) slave

   replicates 5dea4b4c6462d58ac58576b442fa1ad2941f81f1

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

Connecting to node 192.168.1.39:7007: OK

>>> Send CLUSTER MEET to node 192.168.1.39:7007 to make it join the cluster.

  (2) 查看集群状态:

 [root@ localhost src]# /usr/local/redis/bin/redis-cli -c-h 192.168.1.39 -p 7001

 192.168.1.39:7001> cluster  nodes

cd03e0d7e796c6611bc61ff6e6def0488651413d 192.168.1.39:7005 slave 3937188975e89fa6e5a637e8c876504604fa7c71 0 1550495698988 5 connected

31914885c401993b9287ed272e01e138238045ae 192.168.1.39:7001 myself,master - 0 0 1 connected 0-5460

5dea4b4c6462d58ac58576b442fa1ad2941f81f1 192.168.1.39:7003 master - 0 1550495698988 3 connected 10923-16383

15a026c7600b9f3f21d90314bc9c18fda2455dac 192.168.1.39:7004 slave 31914885c401993b9287ed272e01e138238045ae 0 1550495697983 4 connected

3937188975e89fa6e5a637e8c876504604fa7c71 192.168.1.39:7002 master - 0 1550495697983 2 connected 5461-10922

63f5caa9e048b791e8c16cb1d694ccbcf5b906c2 192.168.1.39:7008 master - 0 1550495697480 0 connected

c235a093d5ec86085b2628f7b648a6e489c98d7e 192.168.1.39:7007 slave 5dea4b4c6462d58ac58576b442fa1ad2941f81f1 0 1550495698988 6 connected

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

STEP4.  为7007分配slot槽。

  (1) 使用redis-trib 命令,找到集群中的任意一个主节点(红色位置表现集群中的任意一个主节点),对其进行重新分片工作。

[root@localhost 7001]# /usr/local/redis3.0/src/redis-trib.rb reshard 192.168.1.39:7001

输出如下:

>>> Performing Cluster Check (using node 192.168.1.39:7001)

M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.39:7001

sInte0-5460 15461 elntel master

>>> Check slots coverage...

[OK] All 16384 slots covered.(R- )

(提示一:移动多少个槽)

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

(提示二:选择哪一个节点)

What is the receiving node ID? 382634a4025778c040b7213453fd42a709f79e28

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 200 slots.

Source nodes:

  M: 614d0def75663f26 20b6402a017014b57c912dad 192.168.1.39:7001slots:0-5460 (5461 slots) master1 additional replica(s)

  M: 8aa c82b63d42a 1989528cd3906579863a5774e77 192.168.1.39:7002slots:5461-10922 (5462 slots) master

(提示三:是否执行)

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

Moving slot 65 from 192.168.1.39:7001 to 192.168.1.39:7007:

Moving slot 10923 from 192.168.1.39:7003 to 192.168.1.39:7007:

Moving slot 5527 from 192.168.1.39:7002 to 192.168.1.39:7007:


 

1提示一:是希望你需要多少个槽移动到新的节点上,可以自己设置,比如200个槽。

2提示二:是你需要把这200个slot槽移动到哪个节点上去(需要指定节点id), 并且下个提示是输入all,为从所有主节点(7001、7002、7003)中分别抽取响应的槽数(一共为200个槽到指定的新节点中!,并且会打印执行分片的计划。)

3提示三:是否执行

 

STEP5.  添加从节点(7008) 到集群中去。

  (1) 还是需要执行add-node命令:

[root@localhost 7001]#/usr/local/redis3.0/sc/redis trib.rb add-node 192.168.1.39:7008 192.168.1.39:7001

   提示添加成功后我们继续看一下集群的状态:

[root@ localhost src]# /usr/local/redis/bin/redis-cli -c-h 192.168.1.39 -p 7001

192.168.1.39:7001> cluster  nodes

(我们可以在控制台看到集群信息)

从第一行我们可以看出还是一个master节点,没有被分配任何slot槽。

  (2) 我们需要执行replicate命令来指定当前节点(从节点)的主节点id为哪个。

  首先需要登录新加的7008节点的客户端,然后使用集群命令进行操作,把当前的7008(slave )节点指定到一个主节点下(这里使用之前创建的7007主节点,红色表示节点id)

[root@localhost ~]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.39 -p 7008

192.168.1.39:7008> cluster replicate 382634a4025778c040b7213453fd42a 709f79e28

192.168.1.39:7008> OK (提示OK则操作成功)

我们继续看一下集群的状态:

[root@ localhost src]# /usr/local/redis/bin/redis-cli -c-h 192.168.1.39 -p 7001

192.168.1.39:7001> cluster  nodes

    (我们可以在控制台看到集群信息)

可以看出我们已经成功的把7008放到7007这个主节点下面了,到此为止我们已经成功的添加完一个从节点了。

STEP6.  我们可以对集群进行操作,来验证下是否可以进行读写(当然可以)。

二  删除一个节点

STEP1. 删除从节点7008,输入del-node命令,指定刪除节点ip和端口,以及节点id (红色为7008节点id)

[root@localhost 7001]# /usr/local/redis3.0/src/redis-trib.rb del-node 192.168.1.39:7008 97b0e0115326833724eb0ffe1d0574ee34618e9f

 输出如下:

>>>  Removing  node  97b0e0115326833724eboffe 1d0574ee34618e9f  from  cluster192.168.1.39:7008

Connecting to node 192.168.1.39:7008: OK

Connecting to node 192.168.1.39:7003: OK

Connecting to node 192.168.1.39:7006: OK

Connecting to node 192.168.1.39:7002: OK

Connecting to node 192.168.1.39:7005: OK

Connecting to node 192.168.1.39:7001: OK

Connecting to node 192.168.1.39:7004: OK

Connecting to node 192.168.1.39:7007: OK

>>> Sending CLUSTER FORGET messages to the cluster...

STEP2.  最后,我们尝试删除之前加入的主节点7007,这个步骤会相对比较麻烦一些,因为主节点的里面是有分配了slot 槽的,所以我们这里必须先把7007里的slot 槽放入到其他的可用主节点中去,然后再进行移除节点操作才行,不然会出现数据丢失问题。

  (1) 删除7007 (master)节点之前,我们需要先把其全部的数据(slot槽)移动到其他节点上去(目前只能把master的数据迁移到一个节点上,暂时做不了平均分配功能)。

[root@localhost 7001]# /usr/local/redis3.0/src/redis-trib.rb reshard 192.168.1.39:7007

输出如下:

>>> Check slots coverage..

[OK] All 16384 slots covered.

(这里不会正好是200个槽)
How many slots do you want to move (from 1 to 16384)? 199

(这里是需要把数据移动到哪?7001的主节点ID)

What is the receiving node ID? 614d0def75663f2620b6402a017014b57c912dad

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,

(这里需要数据源,也就是我们的7007节点id)
Source node #1:382634a4025778c040b7213453fd42a709f79e28

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

Source node #2:done

Ready to move 199 slots.
Source nodes:

  M: 382634a4025778c040b7213453fd42a709f79e28 192.168.1.39:7007

  slots:0-65,5461-5527,1092310988 (199 slots) master

  O additional replica(s)

  (2)最后我直接使用del-node命令刪除7007主节点即可(紅色表示7007的节点id)。

[root@localhost 7001]# /usr/local/redis3.0/src/redis-trib.rb del-node 192.168.1.39:7007 382634a4025778c040b7213453fd42a709f79e28

输出如下:

>>>Removing  node  382634a4025778c040b7213453fd42a 709f79e28  from

cluster192.168.1.39:7007

Connecting to node 192.168.1.39:7007: OK

Connecting to node 192.168.1.39:7006: OK

Connecting to node 192.168.1.39:7004: OK

Connecting to node 192.168.1.397001: OK

Connecting to node 192.168.1.39:7002: OK

Connecting to node 192.168.1.39:7005: OK

Connecting to node 192.168.1.39:7003: OK

>>> Sending CLUSTER FORGET messages to the cluster..

>>> SHUTDOWN the node. 

最后:我们査看集群状志,一切还原为最初始状态啦! OK结束!

 

猜你喜欢

转载自www.cnblogs.com/programmlover/p/10398571.html