1. 程式人生 > >Redis叢集新增和刪除節點(主和從)

Redis叢集新增和刪除節點(主和從)

一、原始叢集(6個節點,3主3從):

(1)啟動叢集:因為叢集建立一次就行,所以直接啟動所有節點服務即可

[[email protected] ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7001/redis.conf

[[email protected] ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7002/redis.conf

[[email protected] ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7003/redis.conf

[
[email protected]
~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7004/redis.conf [[email protected] ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7005/redis.conf [[email protected] ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7006/redis.conf

(2)檢視伺服器的執行狀態

[[email protected]
~]# ps -ef | grep redis root 2071 1 0 18:19 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.1.111:7001 [cluster] root 2075 1 0 18:19 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.1.111:7002 [cluster] root 2079 1 0 18:19 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.1.111:7003 [cluster] root 2083 1 0 18:19 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.1.111:7004 [cluster] root 2089 1 0 18:19 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.1.111:7005 [cluster] root 2095 1 0 18:19 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.1.111:7006 [cluster] root 2101 1974 0 18:19 pts/0 00:00:00 grep redis

(3)登入其中一個節點,檢視叢集狀態

[[email protected] src]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.111 -p 7001

192.168.1.111:7001> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:6

cluster_my_epoch:1

cluster_stats_messages_sent:1366

cluster_stats_messages_received:866

二、新建叢集節點操作

(4)我們將新建兩個服務,在之前搭建的叢集中新增兩個節點(一主一從)

Master:7007 Slave:7008

步驟一:建立7007、7008資料夾,拷貝redis.conf檔案到兩個資料夾下,然後進行配置檔案的修改

[[email protected] redis-cluster]# mkdir 7007

[[email protected] redis-cluster]# mkdir 7008

[[email protected] redis-cluster]# cd 7001

[[email protected] 7001]# cp redis.conf /usr/local/redis-cluster/7007/

[[email protected] 7001]# cp redis.conf /usr/local/redis-cluster/7008/

[[email protected] 7001]# vim /usr/local/redis-cluster/7007/redis.conf

修改內容如下:

          port:7007

      dir /usr/local/redis-cluster/7007/

      cluster-config-file nodes7007.conf

[[email protected] 7001]# vim /usr/local/redis-cluster/7008/redis.conf

修改內容如下:

          port:7008

      dir /usr/local/redis-cluster/7008/

      cluster-config-file nodes7008.conf

步驟二:啟動7007和7008倆個服務並檢視服務狀態。

[[email protected] 7001]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7007/redis.conf

[[email protected] 7001]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7008/redis.conf

[[email protected] 7001]# ps -el | grep redis

(5)先稍微看一下redis-trib指令碼能怎麼使用:

[[email protected] src]# ./redis-trib.rb

Usage: redis-trib <command> <options> <arguments ...>



create host1:port1 ... hostN:portN

--replicas <arg>

check host:port

info host:port

fix host:port

--timeout <arg>

reshard host:port

--from <arg>

--to <arg>

--slots <arg>

--yes

--timeout <arg>

--pipeline <arg>

rebalance host:port

--weight <arg>

--auto-weights

--use-empty-masters

--timeout <arg>

--simulate

--pipeline <arg>

--threshold <arg>

add-node new_host:new_port existing_host:existing_port

--slave

--master-id <arg>

del-node host:port node_id

set-timeout host:port milliseconds

call host:port command arg arg .. arg

import host:port

--from <arg>

--copy

--replace

help (show this help)



For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

1 create:建立一個叢集環境host1:port1 ... hostN:portN(叢集中的主從節點比例)

2 call:可以執行redis命令

3 add-node:將一個節點新增到叢集裡,第一個引數為新節點的ip:port,第二個引數為叢集中任意一個已經存在的節點的ip:port

4 del-node:移除一個節點

5 reshard:重新分片

6 check:檢查叢集狀態

(6)新增一個主節點7007(Master)

步驟一:使用add-node命令,第一個為新增節點,第二個為叢集中已知存在節點(任意一個)

[[email protected] src]# ./redis-trib.rb add-node 192.168.1.111:7007 192.168.1.111:7001

>>> Adding node 192.168.1.111:7007 to cluster 192.168.1.111:7001

/usr/local/rvm/gems/ruby-2.4.4/gems/redis-3.2.1/lib/redis/client.rb:443: warning: constant ::Fixnum is deprecated

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

M: 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 192.168.1.111:7001

slots:0-5460 (5461 slots) master

1 additional replica(s)

S: 56fb12f39b02c38f2c43cfc19f7b9faafdd900a0 192.168.1.111:7006

slots: (0 slots) slave

replicates fbd2b0bf4ec21c23af7d15a20964688fb23f214c

M: b9e97e221fb3ae239cc0e2831779381ace7c6b9b 192.168.1.111:7002

slots:5461-10922 (5462 slots) master

1 additional replica(s)

S: 53e5b386290843bb57af3c9cea8af7ccc38314f9 192.168.1.111:7005

slots: (0 slots) slave

replicates b9e97e221fb3ae239cc0e2831779381ace7c6b9b

M: fbd2b0bf4ec21c23af7d15a20964688fb23f214c 192.168.1.111:7003

slots:10923-16383 (5461 slots) master

1 additional replica(s)

S: 146f1006dc5df60c3d4ac2867a21004deb8be6eb 192.168.1.111:7004

slots: (0 slots) slave

replicates 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

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

[OK] New node added correctly.

步驟二:檢視叢集節點狀態:

[[email protected] src]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.111 -p 7001

192.168.1.111:7001> cluster nodes

3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 192.168.1.111:7001 myself,master - 0 0 1 connected 0-5460

56fb12f39b02c38f2c43cfc19f7b9faafdd900a0 192.168.1.111:7006 slave fbd2b0bf4ec21c23af7d15a20964688fb23f214c 0 1534848168901 6 connected

b9e97e221fb3ae239cc0e2831779381ace7c6b9b 192.168.1.111:7002 master - 0 1534848166887 2 connected 5461-10922

53e5b386290843bb57af3c9cea8af7ccc38314f9 192.168.1.111:7005 slave b9e97e221fb3ae239cc0e2831779381ace7c6b9b 0 1534848167894 5 connected

fbd2b0bf4ec21c23af7d15a20964688fb23f214c 192.168.1.111:7003 master - 0 1534848169908 3 connected 10923-16383

bdb0ce9cc5cca545b9182a09346246827af40490 192.168.1.111:7007 master - 0 1534848170916 0 connected

146f1006dc5df60c3d4ac2867a21004deb8be6eb 192.168.1.111:7004 slave 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 0 1534848171419 4 connected

192.168.1.111:7001>

注意:當新增節點以後,新增的節點不會有任何資料,因為它沒分配任何的slot(hash槽),這是拿來存放資料的,Master必須得有才能存放資料。那麼我們需要手動為新節點分配slot。

(7)為新節點7007分配slot槽

步驟一:使用redis-trib命令的reshard,找到叢集中的任意一個主節點,對其進行重新分片工作。

[[email protected] src]# ./redis-trib.rb reshard 192.168.1.111:7001

/usr/local/rvm/gems/ruby-2.4.4/gems/redis-3.2.1/lib/redis/client.rb:443: warning: constant ::Fixnum is deprecated

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

M: 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 192.168.1.111:7001

slots:0-5460 (5461 slots) master

1 additional replica(s)

S: 56fb12f39b02c38f2c43cfc19f7b9faafdd900a0 192.168.1.111:7006

slots: (0 slots) slave

replicates fbd2b0bf4ec21c23af7d15a20964688fb23f214c

M: b9e97e221fb3ae239cc0e2831779381ace7c6b9b 192.168.1.111:7002

slots:5461-10922 (5462 slots) master

1 additional replica(s)

S: 53e5b386290843bb57af3c9cea8af7ccc38314f9 192.168.1.111:7005

slots: (0 slots) slave

replicates b9e97e221fb3ae239cc0e2831779381ace7c6b9b

M: fbd2b0bf4ec21c23af7d15a20964688fb23f214c 192.168.1.111:7003

slots:10923-16383 (5461 slots) master

1 additional replica(s)

M: bdb0ce9cc5cca545b9182a09346246827af40490 192.168.1.111:7007

slots: (0 slots) master

0 additional replica(s)

S: 146f1006dc5df60c3d4ac2867a21004deb8be6eb 192.168.1.111:7004

slots: (0 slots) slave

replicates 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

(提示一)

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

(提示二)

What is the receiving node ID? bdb0ce9cc5cca545b9182a09346246827af40490

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: 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 192.168.1.111:7001

slots:0-5460 (5461 slots) master

1 additional replica(s)

M: b9e97e221fb3ae239cc0e2831779381ace7c6b9b 192.168.1.111:7002

slots:5461-10922 (5462 slots) master

1 additional replica(s)

M: fbd2b0bf4ec21c23af7d15a20964688fb23f214c 192.168.1.111:7003

slots:10923-16383 (5461 slots) master

1 additional replica(s)

Destination node:

M: bdb0ce9cc5cca545b9182a09346246827af40490 192.168.1.111:7007

slots: (0 slots) master

0 additional replica(s)

Resharding plan:(執行分片計劃日誌)

Moving slot 5461 from b9e97e221fb3ae239cc0e2831779381ace7c6b9b

Moving slot 5462 from b9e97e221fb3ae239cc0e2831779381ace7c6b9b

......

Moving slot 0 from 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac

Moving slot 2 from 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac

.....

Moving slot 10923 from fbd2b0bf4ec21c23af7d15a20964688fb23f214c

Moving slot 10924 from fbd2b0bf4ec21c23af7d15a20964688fb23f214c

Moving slot 10925 from fbd2b0bf4ec21c23af7d15a20964688fb23f214c

....

Moving slot 10986 from fbd2b0bf4ec21c23af7d15a20964688fb23f214c

(提示三)

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

Moving slot 5461 from 192.168.1.111:7002 to 192.168.1.111:7007:

......

Moving slot 0 from 192.168.1.111:7001 to 192.168.1.111:7007:

Moving slot 1 from 192.168.1.111:7001 to 192.168.1.111:7007:

......

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

.....

提示一:你希望將多少個槽移動到新的節點上,可以自己設定,比如200個槽

提示二:接收這些槽的節點的id。下個提示是從哪些主節點抽取槽到新節點中:all為所有主節點,done:你可以指定節點:)

提示三:輸入yes確認開始執行分年任務。

(8)在最後,我們檢視一下叢集狀態:

[[email protected] src]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.111 -p 7001

192.168.1.111:7001> cluster nodes

3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 192.168.1.111:7001 myself,master - 0 0 1 connected 66-5460

56fb12f39b02c38f2c43cfc19f7b9faafdd900a0 192.168.1.111:7006 slave fbd2b0bf4ec21c23af7d15a20964688fb23f214c 0 1534850426284 6 connected

b9e97e221fb3ae239cc0e2831779381ace7c6b9b 192.168.1.111:7002 master - 0 1534850428299 2 connected 5528-10922

53e5b386290843bb57af3c9cea8af7ccc38314f9 192.168.1.111:7005 slave b9e97e221fb3ae239cc0e2831779381ace7c6b9b 0 1534850423259 5 connected

fbd2b0bf4ec21c23af7d15a20964688fb23f214c 192.168.1.111:7003 master - 0 1534850427294 3 connected 10989-16383

bdb0ce9cc5cca545b9182a09346246827af40490 192.168.1.111:7007 master - 0 1534850424268 7 connected 0-65 5461-5527 10923-10988

146f1006dc5df60c3d4ac2867a21004deb8be6eb 192.168.1.111:7004 slave 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 0 1534850429305 4 connected

如上所示,現在新增的主節點7007已經有自己的slot槽了,也就是說可以在7007上面進行讀寫資料咯~到現在為止,叢集中已經有7個節點了,而且是4主3從。

那我們再給7007弄個Slave咯,不能太偏心。

(9)新增從節點(7008)到叢集中

步驟一:使用add-node新增新節點

[[email protected] src]# ./redis-trib.rb add-node 192.168.1.111:7008 192.168.1.111:7001

步驟二:提示成功後再檢視一下叢集的節點資訊:

[roo[email protected] src]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.111 -p 7001

192.168.1.111:7001> cluster nodes

6101db56f13af2252e356eb08977b6bd2bb3e2f9 192.168.1.111:7008 master - 0 1534850805149 0 connected

3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 192.168.1.111:7001 myself,master - 0 0 1 connected 66-5460

56fb12f39b02c38f2c43cfc19f7b9faafdd900a0 192.168.1.111:7006 slave fbd2b0bf4ec21c23af7d15a20964688fb23f214c 0 1534850806155 6 connected

b9e97e221fb3ae239cc0e2831779381ace7c6b9b 192.168.1.111:7002 master - 0 1534850804647 2 connected 5528-10922

53e5b386290843bb57af3c9cea8af7ccc38314f9 192.168.1.111:7005 slave b9e97e221fb3ae239cc0e2831779381ace7c6b9b 0 1534850801627 5 connected

fbd2b0bf4ec21c23af7d15a20964688fb23f214c 192.168.1.111:7003 master - 0 1534850805653 3 connected 10989-16383

bdb0ce9cc5cca545b9182a09346246827af40490 192.168.1.111:7007 master - 0 1534850800622 7 connected 0-65 5461-5527 10923-10988

146f1006dc5df60c3d4ac2867a21004deb8be6eb 192.168.1.111:7004 slave 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 0 1534850803640 4 connected

如上面所示,但是我們可以看到,7008還是一個Master節點,而且沒有擁有自己的slot槽。那麼我們接下來要讓它變成從節點。

步驟二:使用cluster replicate命名來給當前節點(從節點7008)指定主節點的id。

首先我們當然需要登入到7008節點的客戶端,然後使用上面的命令來進行操作,現在我們讓7007成為7008的主節點。

[[email protected] src]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.111 -p 7008

192.168.1.111:7008> cluster replicate bdb0ce9cc5cca545b9182a09346246827af40490

OK (提示OK則表示成功!)

步驟三:我們繼續看一下叢集的節點資訊

192.168.1.111:7008> cluster nodes

b9e97e221fb3ae239cc0e2831779381ace7c6b9b 192.168.1.111:7002 master - 0 1534851019889 2 connected 5528-10922

53e5b386290843bb57af3c9cea8af7ccc38314f9 192.168.1.111:7005 slave b9e97e221fb3ae239cc0e2831779381ace7c6b9b 0 1534851021902 2 connected

bdb0ce9cc5cca545b9182a09346246827af40490 192.168.1.111:7007 master - 0 1534851016866 7 connected 0-65 5461-5527 10923-10988

fbd2b0bf4ec21c23af7d15a20964688fb23f214c 192.168.1.111:7003 master - 0 1534851022907 3 connected 10989-16383

3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 192.168.1.111:7001 master - 0 1534851018879 1 connected 66-5460

6101db56f13af2252e356eb08977b6bd2bb3e2f9 192.168.1.111:7008 myself,slave bdb0ce9cc5cca545b9182a09346246827af40490 0 0 0 connected

146f1006dc5df60c3d4ac2867a21004deb8be6eb 192.168.1.111:7004 slave 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 0 1534851020896 1 connected

56fb12f39b02c38f2c43cfc19f7b9faafdd900a0 192.168.1.111:7006 slave fbd2b0bf4ec21c23af7d15a20964688fb23f214c 0 1534851017874 3 connected

(10)我們可以對叢集進行操作了,來驗證是否可以進行讀寫。下面就不貼例子了。

(11)有添加當然就有刪除節點的操作了。刪除從節點非常簡單,刪除的時候指定ip+埠以及節點id即可。但是刪除主節點可不能直接刪除,一定要先將slot槽重新移動到其他主節點那裡再進行刪除操作,不然存放的資料就丟失了。我們現在先嚐試刪除一個從節點(7008)

步驟一:刪除子節點用del-node命令。此命令需要制定刪除節點的ip和埠,以及節點的id。

[[email protected] src]# ./redis-trib.rb del-node 192.168.1.111:7008 6101db56f13af2252e356eb08977b6bd2bb3e2f9
輸出如下:

>>> Removing node 6101db56f13af2252e356eb08977b6bd2bb3e2f9 from cluster 192.168.1.111:7008

/usr/local/rvm/gems/ruby-2.4.4/gems/redis-3.2.1/lib/redis/client.rb:443: warning: constant ::Fixnum is deprecated

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

>>> SHUTDOWN the node.

步驟二:刪除後我們再次檢視叢集的節點資訊,如下所示,7008從節點已經被移除掉。

[[email protected] src]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.111 -p 7001

192.168.1.111:7001> cluster nodes

3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 192.168.1.111:7001 myself,master - 0 0 1 connected 66-5460

56fb12f39b02c38f2c43cfc19f7b9faafdd900a0 192.168.1.111:7006 slave fbd2b0bf4ec21c23af7d15a20964688fb23f214c 0 1534861607148 6 connected

b9e97e221fb3ae239cc0e2831779381ace7c6b9b 192.168.1.111:7002 master - 0 1534861607651 2 connected 5528-10922

53e5b386290843bb57af3c9cea8af7ccc38314f9 192.168.1.111:7005 slave b9e97e221fb3ae239cc0e2831779381ace7c6b9b 0 1534861606140 5 connected

fbd2b0bf4ec21c23af7d15a20964688fb23f214c 192.168.1.111:7003 master - 0 1534861605134 3 connected 10989-16383

bdb0ce9cc5cca545b9182a09346246827af40490 192.168.1.111:7007 master - 0 1534861608155 7 connected 0-65 5461-5527 10923-10988

146f1006dc5df60c3d4ac2867a21004deb8be6eb 192.168.1.111:7004 slave 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 0 1534861609160 4 connected

步驟三:另外,我們可以發現,7008的redis服務程序也被kill掉了。

[[email protected] src]# ps -ef | grep 7008

root 4079 1974 0 22:27 pts/0 00:00:00 grep 7008

(12)刪除從節點就是這麼簡單,那麼我們現在開始嘗試刪除之前新增的主節點7007,當然了,我們必須要記得先將7007的slot槽重新放回其他可用的主節點中,然後再進行刪除節點操作。

步驟一:刪除7007(master)節點之前,我們需要先把其全部的資料(slot槽)移動到其他節點上去(目前只能把master的資料遷移到一個節點上,暫時做不了平均分配功能)。其實就像是我們給7007分配slot槽的操作,只是雙方角色互換了一下,用到的當然也是reshard命令了。

[[email protected] src]# ./redis-trib.rb reshard 192.168.1.111:7007

輸出如下:

/usr/local/rvm/gems/ruby-2.4.4/gems/redis-3.2.1/lib/redis/client.rb:443: warning: constant ::Fixnum is deprecated

>>> Performing Cluster Check (using node 192.168.1.111:7007)

M: bdb0ce9cc5cca545b9182a09346246827af40490 192.168.1.111:7007

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

0 additional replica(s)

S: 146f1006dc5df60c3d4ac2867a21004deb8be6eb 192.168.1.111:7004

slots: (0 slots) slave

replicates 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac

S: 56fb12f39b02c38f2c43cfc19f7b9faafdd900a0 192.168.1.111:7006

slots: (0 slots) slave

replicates fbd2b0bf4ec21c23af7d15a20964688fb23f214c

M: b9e97e221fb3ae239cc0e2831779381ace7c6b9b 192.168.1.111:7002

slots:5528-10922 (5395 slots) master

M: 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 192.168.1.111:7001

slots:66-5460 (5395 slots) master

1 additional replica(s)

M: fbd2b0bf4ec21c23af7d15a20964688fb23f214c 192.168.1.111:7003

slots:10989-16383 (5395 slots) master

1 additional replica(s)

S: 53e5b386290843bb57af3c9cea8af7ccc38314f9 192.168.1.111:7005

slots: (0 slots) slave

replicates b9e97e221fb3ae239cc0e2831779381ace7c6b9b

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

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

(ps:這裡填的就是7007擁有的slot槽數,上面有打印出來的)

What is the receiving node ID? 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac

(ps:這裡是接收slot槽的主節點id,我填的是7001的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:bdb0ce9cc5cca545b9182a09346246827af40490

(ps:這裡是資料來源節點,當然填的是7007的Id了)

Source node #2:done

(ps:輸入done表示可以開始生成遷移計劃)

Ready to move 199 slots.

Source nodes:

M: bdb0ce9cc5cca545b9182a09346246827af40490 192.168.1.111:7007

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

0 additional replica(s)

Destination node:

M: 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 192.168.1.111:7001

slots:66-5460 (5395 slots) master

1 additional replica(s)

Resharding plan:

Moving slot 0 from bdb0ce9cc5cca545b9182a09346246827af40490

.......

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

(ps:輸入yes表示可以開始執行遷移計劃)

Moving slot 0 from 192.168.1.111:7007 to 192.168.1.111:7001:

.........

步驟二:到此為止,我們應該是將7007主節點的slot槽全部遷移到7001上面去了,我們可以看一下現在的叢集節點資訊即可,可以看到,7007後面已經沒有slot槽的資訊了,而7001也多出了slot槽。

[[email protected] src]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.111 -p 7001

192.168.1.111:7001> cluster nodes

3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 192.168.1.111:7001 myself,master - 0 0 8 connected 0-5527 10923-10988

56fb12f39b02c38f2c43cfc19f7b9faafdd900a0 192.168.1.111:7006 slave fbd2b0bf4ec21c23af7d15a20964688fb23f214c 0 1534862478538 6 connected

b9e97e221fb3ae239cc0e2831779381ace7c6b9b 192.168.1.111:7002 master - 0 1534862483568 2 connected 5528-10922

53e5b386290843bb57af3c9cea8af7ccc38314f9 192.168.1.111:7005 slave b9e97e221fb3ae239cc0e2831779381ace7c6b9b 0 1534862479544 5 connected

fbd2b0bf4ec21c23af7d15a20964688fb23f214c 192.168.1.111:7003 master - 0 1534862482563 3 connected 10989-16383

bdb0ce9cc5cca545b9182a09346246827af40490 192.168.1.111:7007 master - 0 1534862480550 7 connected

146f1006dc5df60c3d4ac2867a21004deb8be6eb 192.168.1.111:7004 slave 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 0 1534862476524 8 connected

步驟三:最後我們可以直接使用del-node命令來刪除7007主節點了。

[[email protected] src]# ./redis-trib.rb del-node 192.168.1.111:7007 bdb0ce9cc5cca545b9182a09346246827af40490

>>> Removing node bdb0ce9cc5cca545b9182a09346246827af40490 from cluster 192.168.1.111:7007

/usr/local/rvm/gems/ruby-2.4.4/gems/redis-3.2.1/lib/redis/client.rb:443: warning: constant ::Fixnum is deprecated

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

>>> SHUTDOWN the node.

最後:我們檢視叢集的節點資訊,一切將還原到最初狀態~

[[email protected] src]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.111 -p 7001

192.168.1.111:7001> cluster nodes

3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 192.168.1.111:7001 myself,master - 0 0 8 connected 0-5527 10923-10988

56fb12f39b02c38f2c43cfc19f7b9faafdd900a0 192.168.1.111:7006 slave fbd2b0bf4ec21c23af7d15a20964688fb23f214c 0 1534863137104 6 connected

b9e97e221fb3ae239cc0e2831779381ace7c6b9b 192.168.1.111:7002 master - 0 1534863140120 2 connected 5528-10922

53e5b386290843bb57af3c9cea8af7ccc38314f9 192.168.1.111:7005 slave b9e97e221fb3ae239cc0e2831779381ace7c6b9b 0 1534863141124 5 connected

fbd2b0bf4ec21c23af7d15a20964688fb23f214c 192.168.1.111:7003 master - 0 1534863143138 3 connected 10989-16383

146f1006dc5df60c3d4ac2867a21004deb8be6eb 192.168.1.111:7004 slave 3a9f07e1ed2f9a8c5e42d51f96bce53dc6a567ac 0 1534863142132 8 connected

192.168.1.111:7001>