上一篇說明了基於Redis Cluster搭建Redis叢集的過程,下面繼續說明下Redis叢集中node增減過程。 叢集中增加節點
我們再來測試一下,新加入一個節點,分2種情況,1是作為主節點,2是作為一個節點的從節點。我們分別來試一下: 建立一個 7006 節點,讓其作為一個新的主節點加入:
建立7006目錄,拷貝設定檔,修改連接埠,啟動7006連接埠redis;
[root@spg 7006]# ps -ef | grep redisroot 3063 2974 0 20:04 pts/0 00:00:12 redis-server *:7001 [cluster]root 3081 2974 0 20:05 pts/0 00:00:11 redis-server *:7002 [cluster]root 3093 2974 0 20:05 pts/0 00:00:11 redis-server *:7003 [cluster]root 3109 2974 0 20:06 pts/0 00:00:11 redis-server *:7004 [cluster]root 3123 2974 0 20:06 pts/0 00:00:11 redis-server *:7005 [cluster]root 3487 2974 0 20:30 pts/0 00:00:06 redis-server *:7000 [cluster]root 3981 2974 0 21:09 pts/0 00:00:00 redis-server *:7006 [cluster]root 3993 2974 0 21:09 pts/0 00:00:00 grep --color=auto redis
將7006節點加入叢集中:
redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000
add-node是加入指令,127.0.0.1:7006 表示新加入的節點,127.0.0.1:7000 表示加入的叢集的一個節點,用來辨識是哪個叢集,理論上哪個都可以。
[root@spg 7006]# redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000>>> Adding node 127.0.0.1:7006 to cluster 127.0.0.1:7000Connecting to node 127.0.0.1:7000: OKConnecting to node 127.0.0.1:7003: OKConnecting to node 127.0.0.1:7002: OKConnecting to node 127.0.0.1:7004: OKConnecting to node 127.0.0.1:7001: OKConnecting to node 127.0.0.1:7005: OK>>> Performing Cluster Check (using node 127.0.0.1:7000)S: be26c521481afcd6e739e2bfef69e9dcfb63d0a6 127.0.0.1:7000 slots: (0 slots) slave replicates 1da8a7f4c3cd5d7537e90e0ca5f4fb416f41a40cM: 1da8a7f4c3cd5d7537e90e0ca5f4fb416f41a40c 127.0.0.1:7003 slots:0-5460 (5461 slots) master 1 additional replica(s)M: 947cc4a9e890672cfad4806a5921e9f8bdf05c05 127.0.0.1:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s)S: 05ac96f9cdee679f98e8f7ce8e97cf1cbea608ca 127.0.0.1:7004 slots: (0 slots) slave replicates ce06b13387702c3ee63e0118dd10c5f81a1285b5M: ce06b13387702c3ee63e0118dd10c5f81a1285b5 127.0.0.1:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s)S: b65f33d97416795226964aa22f3b4a8ac7366a99 127.0.0.1:7005 slots: (0 slots) slave replicates 947cc4a9e890672cfad4806a5921e9f8bdf05c05[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.Connecting to node 127.0.0.1:7006: OK>>> Send CLUSTER MEET to node 127.0.0.1:7006 to make it join the cluster.[OK] New node added correctly.
從上面可以看出節點7006已經成功加入到叢集中,此時可以查看叢集節點狀態
[root@spg 7006]# redis-trib.rb check 127.0.0.1:7000Connecting to node 127.0.0.1:7000: OKConnecting to node 127.0.0.1:7006: OKConnecting to node 127.0.0.1:7003: OKConnecting to node 127.0.0.1:7002: OKConnecting to node 127.0.0.1:7004: OKConnecting to node 127.0.0.1:7001: OKConnecting to node 127.0.0.1:7005: OK>>> Performing Cluster Check (using node 127.0.0.1:7000)S: be26c521481afcd6e739e2bfef69e9dcfb63d0a6 127.0.0.1:7000 slots: (0 slots) slave replicates 1da8a7f4c3cd5d7537e90e0ca5f4fb416f41a40cM: fe595e7a38c659a6eb6949bb31fd7474881d6422 127.0.0.1:7006 slots: (0 slots) master 0 additional replica(s)M: 1da8a7f4c3cd5d7537e90e0ca5f4fb416f41a40c 127.0.0.1:7003 slots:0-5460 (5461 slots) master 1 additional replica(s)M: 947cc4a9e890672cfad4806a5921e9f8bdf05c05 127.0.0.1:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s)S: 05ac96f9cdee679f98e8f7ce8e97cf1cbea608ca 127.0.0.1:7004 slots: (0 slots) slave replicates ce06b13387702c3ee63e0118dd10c5f81a1285b5M: ce06b13387702c3ee63e0118dd10c5f81a1285b5 127.0.0.1:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s)S: b65f33d97416795226964aa22f3b4a8ac7366a99 127.0.0.1:7005 slots: (0 slots) slave replicates 947cc4a9e890672cfad4806a5921e9f8bdf05c05[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
可以很清楚的看到節點7006已經成功作為主節點加入到叢集中了。
PS:也可以串連到用戶端查看叢集中節點狀態,如下所示:
[root@spg 7006]# redis-cli -c -p 7006127.0.0.1:7006> cluster nodesfe595e7a38c659a6eb6949bb31fd7474881d6422 127.0.0.1:7006 myself,master - 0 0 0 connectedbe26c521481afcd6e739e2bfef69e9dcfb63d0a6 127.0.0.1:7000 slave 1da8a7f4c3cd5d7537e90e0ca5f4fb416f41a40c 0 1456667011409 7 connected947cc4a9e890672cfad4806a5921e9f8bdf05c05 127.0.0.1:7002 master - 0 1456667008376 3 connected 10923-16383ce06b13387702c3ee63e0118dd10c5f81a1285b5 127.0.0.1:7001 master - 0 1456667007371 2 connected 5461-10922b65f33d97416795226964aa22f3b4a8ac7366a99 127.0.0.1:7005 slave 947cc4a9e890672cfad4806a5921e9f8bdf05c05 0 1456667013968 3 connected05ac96f9cdee679f98e8f7ce8e97cf1cbea608ca 127.0.0.1:7004 slave ce06b13387702c3ee63e0118dd10c5f81a1285b5 0 1456667013457 2 connected1da8a7f4c3cd5d7537e90e0ca5f4fb416f41a40c 127.0.0.1:7003 master - 0 1456667012429 7 connected 0-5460
但是,通過上面發現:
M: fe595e7a38c659a6eb6949bb31fd7474881d6422 127.0.0.1:7006 slots: (0 slots) master 0 additional replica(s)
0 slots。也就是說,雖然它現在是主節點,但是,卻沒有分配任何節點給它,也就是它現在還不負責資料存取。
看來,redis cluster 不是在新加節點的時候幫我們做好了遷移工作,需要我們手動對叢集進行重新分區遷移,也是這個命令:
redis-trib.rb reshard 127.0.0.1:7000
這個命令是用來遷移slot節點的,後面的127.0.0.1:7000是表示是哪個叢集,連接埠填[7000-7006]都可以,我們結果運行如下:
[root@spg 7006]# redis-trib.rb reshard 127.0.0.1:7000Connecting to node 127.0.0.1:7000: OKConnecting to node 127.0.0.1:7006: OKConnecting to node 127.0.0.1:7003: OKConnecting to node 127.0.0.1:7002: OKConnecting to node 127.0.0.1:7004: OKConnecting to node 127.0.0.1:7001: OKConnecting to node 127.0.0.1:7005: OK>>> Performing Cluster Check (using node 127.0.0.1:7000)S: be26c521481afcd6e739e2bfef69e9dcfb63d0a6 127.0.0.1:7000 slots: (0 slots) slave replicates 1da8a7f4c3cd5d7537e90e0ca5f4fb416f41a40cM: fe595e7a38c659a6eb6949bb31fd7474881d6422 127.0.0.1:7006 slots: (0 slots) master 0 additional replica(s)M: 1da8a7f4c3cd5d7537e90e0ca5f4fb416f41a40c 127.0.0.1:7003 slots:0-5460 (5461 slots) master 1 additional replica(s)M: 947cc4a9e890672cfad4806a5921e9f8bdf05c05 127.0.0.1:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s)S: 05ac96f9cdee679f98e8f7ce8e97cf1cbea608ca 127.0.0.1:7004 slots: (0 slots) slave replicates ce06b13387702c3ee63e0118dd10c5f81a1285b5M: ce06b13387702c3ee63e0118dd10c5f81a1285b5 127.0.0.1:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s)S: b65f33d97416795226964aa22f3b4a8ac7366a99 127.0.0.1:7005 slots: (0 slots) slave replicates 947cc4a9e890672cfad4806a5921e9f8bdf05c05[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)?
它提示我們需要遷移多少slot到7006上,我們可以算一下:16384/4 = 4096,也就是說,為了平衡分配起見,我們需要移動4096個槽點到7006上。
好,那輸入4096:
How many slots do you want to move (from 1 to 16384)? 4096What is the receiving node ID?
此時又提示我們,接受的node ID是多少,7006的id 我們通過上面就可以看到是fe595e7a38c659a6eb6949bb31fd7474881d6422
What is the receiving node ID? fe595e7a38c659a6eb6949bb31fd7474881d6422Please 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:
接著, redis-trib 會向你詢問重新分區的源節點(source node), 也即是, 要從哪個節點中取出 4096 個雜湊槽, 並將這些槽移動到7006節點上面。