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結束!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM