Redis 3.2.4集群實戰


一、Redis Cluster集群設計
Redis集群搭建的方式有多種,例如使用zookeeper等,但從redis3.0之后版本支持Redis-Cluster集群,Redis-Cluster采用無中心結構,每個節點保存數據和整個集群狀態,每個節點都和其他所有節點連接。其redis-cluster架構圖如下:

結構特點:
1、所有的redis節點彼此互聯(PING-PONG機制),內部使用二進制協議優化傳輸速度和帶寬。
2、節點的fail是通過集群中超過半數的節點檢測失效時才生效。
3、客戶端與redis節點直連,不需要中間proxy層。客戶端不需要連接集群所有節點,連接集群中任何一個可用節點即可。
4、redis-cluster把所有的物理節點映射到[0-16383]slot上(不一定是平均分配),cluster負責維護node<->slot<->value。
5、Redis集群預分好16384個桶,當需要在 Redis 集群中放置一個 key-value 時,根據 CRC16(key) mod 16384的值,決定將一個key放到哪個桶中。

1、redis cluster節點分配
假如現在有三個主節點分別是:A, B, C三個節點,它們可以是一台機器上的三個端口,也可以是三台不同的服務器。那么,采用哈希槽 (hash slot)的方式來分配16384個slot的話,它們三個節點分別承擔的slot區間是:
節點A覆蓋0-5460;
節點B覆蓋5461-10922;
節點C覆蓋10923-16383.

獲取數據:
如果存入一個值,按照redis cluster哈希槽的算法: CRC16('key')%16384 = 6782。 那么就會把這個 key 的存儲分配到節點B上了。同樣,當我連接(A,B,C)任何一個節點想獲取'key'這個key時,也會這樣的算法,然后內部跳轉到B節點上獲取數據。

新增一個主節點:
新增一個節點D,redis cluster的這種做法是從各個節點的前面各拿取一部分slot到D上,我會在接下來的實踐中實驗。大致就會變成這樣:
節點A覆蓋1365-5460
節點B覆蓋6827-10922
節點C覆蓋12288-16383
節點D覆蓋0-1364,5461-6826,10923-12287

同樣刪除一個節點也是類似,移動完成后就可以刪除這個節點了。

2、Redis Cluster主從模式
Redis Cluster為了保證數據的高可用性,加入了主從模式,一個主節點對應一個或多個從節點,主節點提供數據存取,從節點則是從主節點拉取數據備份,當這個主節點掛掉后,就會從從節點選取一個來充當主節點,從而保證集群不會掛掉。

上面那個例子里, 集群有ABC三個主節點, 如果這3個節點都沒有加入從節點,如果B掛掉了,我們就無法訪問整個集群了。A和C的slot也無法訪問。

所以我們在集群建立的時候,一定要為每個主節點都添加從節點, 比如像這樣, 集群包含主節點A、B、C, 以及從節點A1、B1、C1, 那么即使B掛掉系統也可以繼續正確工作。

B1節點替代了B節點,所以Redis集群將會選擇B1節點作為新的主節點,集群將會繼續正確地提供服務。 當B重新開啟后,它就會變成B1的從節點。

不過需要注意,如果節點B和B1同時掛了,Redis集群就無法繼續正確地提供服務了。

二、Redis集群搭建

三、redis集群測試

1、查看集群節點信息

[root@localhost src]# ./redis-cli -c -h 192.168.1.105 -p 7000 
192.168.1.105:7000> cluster nodes
4a5dd44e1b57c99c573d03e556f1ca446a1cd907 192.168.1.105:7002 slave afe7e0ca4c438a3f68636e6fc2622d3f910c8c7c 0 1504151069867 4 connected
d33b8453a4f6fdecf597e6ef2f3908b065ce1d4c 192.168.1.160:7005 slave c50954482dada38aca9e9e72cb93f90df52e9ab2 0 1504151070368 6 connected
afe7e0ca4c438a3f68636e6fc2622d3f910c8c7c 192.168.1.160:7003 master - 0 1504151068364 4 connected 5461-10922
e611fea46dc1c7c9a1be2287cf1164a7e9422718 192.168.1.160:7004 slave 2a780de364a6b69e9cb5cb007929f19acf575c93 0 1504151068864 5 connected
2a780de364a6b69e9cb5cb007929f19acf575c93 192.168.1.105:7000 myself,master - 0 0 1 connected 0-5460
c50954482dada38aca9e9e72cb93f90df52e9ab2 192.168.1.105:7001 master - 0 1504151069867 2 connected 10923-16383

主節點:7000、7001、7003 ,從節點:7002、7004、7005;7000為7004的主節點,7001為7005的主節點,7003為7002的主節點。

節點7000覆蓋0-5460
節點7001覆蓋10923-16383
節點7003覆蓋5461-10922

2、測試存取值
redis-cli客戶端連接集群需要帶上-c參數,redis-cli -c -p 端口號 -h ip地址 -a redis密碼

[root@localhost src]# ./redis-cli -h 192.168.1.160 -p 7005 -c
192.168.1.160:7005> set aa 123
-> Redirected to slot [1180] located at 192.168.1.105:7000
OK
192.168.1.105:7000> get aa
"123"

根據redis-cluster的key值分配原理,aa應該分配到節點7000[0-5460]上,上面顯示redis cluster自動從7005跳轉到7000節點。

我們可以測試一下7002從節點獲取aa值

[root@localhost src]# ./redis-cli -h 192.168.1.105 -p 7002 -c
192.168.1.105:7002> get aa
-> Redirected to slot [1180] located at 192.168.1.105:7000
"123"

從上面也是自動跳轉至7000獲取值,這也是redis cluster的特點,它是去中心化,每個節點都是對等的,連接哪個節點都可以獲取和設置數據。

四、集群節點選舉
現在模擬將7000主節點掛掉,按照redis-cluster選舉原理會將7000的從節點7004選舉為主節點。

[root@localhost shell]# ps -ef |grep redis
root 19793 1 0 08:12 ? 00:00:14 ./redis-server 192.168.1.105:7001 [cluster]
root 19794 1 0 08:12 ? 00:00:14 ./redis-server 192.168.1.105:7002 [cluster]
root 19795 1 0 08:12 ? 00:00:14 ./redis-server 192.168.1.105:7000 [cluster]
root 22434 16193 0 11:33 pts/1 00:00:00 ./redis-cli -h 192.168.1.105 -p 7002 -c
root 22645 15815 0 11:56 pts/0 00:00:00 grep --color=auto redis
[root@localhost shell]# kill -9 19795

查看集群中的7000節點

[root@localhost src]# ./redis-trib.rb check 192.168.1.105:7000
[ERR] Sorry, can't connect to node 192.168.1.105:7000 

無法check 7000節點

[root@localhost src]# ./redis-trib.rb check 192.168.1.105:7001
>>> Performing Cluster Check (using node 192.168.1.105:7001)
M: 6befec567ca7090eb3731e48fd5275a9853fb394 192.168.1.105:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 910e14d9655a1e6e7fc007e799006d3f0d1cebe5 192.168.1.105:7002
slots: (0 slots) slave
replicates 94f51658302cb5f1d178f14caaa79f27a9ac3703
S: 54ca4fbc71257fd1be5b58d0f545b95d65f8f6b8 192.168.1.160:7005
slots: (0 slots) slave
replicates 6befec567ca7090eb3731e48fd5275a9853fb394
M: cf02aa3d58d48215d9d61121eedd194dc5c50eeb 192.168.1.160:7004
slots:0-5460 (5461 slots) master
0 additional replica(s)
M: 94f51658302cb5f1d178f14caaa79f27a9ac3703 192.168.1.160:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

可以看到集群連接不了7000節點,而7004由原來的S節點轉換為M節點,代替了原來的7000主節點。

[root@localhost src]# ./redis-cli -p 7005 -c -h 192.168.1.160
192.168.1.160:7005> cluster nodes
28a51a8e34920e2d48fc1650a9c9753ff73dad5d 192.168.1.105:7000 master,fail - 1504065401123 1504065399320 1 disconnected
54ca4fbc71257fd1be5b58d0f545b95d65f8f6b8 192.168.1.160:7005 myself,slave 6befec567ca7090eb3731e48fd5275a9853fb394 0 0 6 connected
cf02aa3d58d48215d9d61121eedd194dc5c50eeb 192.168.1.160:7004 master - 0 1504065527701 8 connected 0-5460
94f51658302cb5f1d178f14caaa79f27a9ac3703 192.168.1.160:7003 master - 0 1504065528203 4 connected 5461-10922
6befec567ca7090eb3731e48fd5275a9853fb394 192.168.1.105:7001 master - 0 1504065527702 2 connected 10923-16383
910e14d9655a1e6e7fc007e799006d3f0d1cebe5 192.168.1.105:7002 slave 94f51658302cb5f1d178f14caaa79f27a9ac3703 0 1504065528706 4 connected

7004由原來的S節點轉換為M節點,代替了原來的7000節點,7000節點提示master,fail。

現在我們將7000節點恢復,看是否會自動加入集群中以及充當的M節點還是S節點。

[root@localhost src]# ./redis-server ../../redis_cluster/7000/redis.conf &

 

在check一下7000節點

[root@localhost src]# ./redis-trib.rb check 192.168.1.105:7000
>>> Performing Cluster Check (using node 192.168.1.105:7000)
S: 28a51a8e34920e2d48fc1650a9c9753ff73dad5d 192.168.1.105:7000
slots: (0 slots) slave
replicates cf02aa3d58d48215d9d61121eedd194dc5c50eeb
S: 910e14d9655a1e6e7fc007e799006d3f0d1cebe5 192.168.1.105:7002
slots: (0 slots) slave
replicates 94f51658302cb5f1d178f14caaa79f27a9ac3703
M: 94f51658302cb5f1d178f14caaa79f27a9ac3703 192.168.1.160:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 54ca4fbc71257fd1be5b58d0f545b95d65f8f6b8 192.168.1.160:7005
slots: (0 slots) slave
replicates 6befec567ca7090eb3731e48fd5275a9853fb394
M: cf02aa3d58d48215d9d61121eedd194dc5c50eeb 192.168.1.160:7004
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 6befec567ca7090eb3731e48fd5275a9853fb394 192.168.1.105:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

可以看到7000節點變成了cf02aa3d58d48215d9d61121eedd194dc5c50eeb 7004的從節點。

五、集群節點添加
新增節點包括新增主節點、從節點兩種情況。以下分別做一下測試:

a、新增主節點
新增一個節點7006作為主節點修改配置文件。

啟動7006 redis服務

[root@localhost src]# ./redis-server ../../redis_cluster/7006/redis.conf &
[1] 23839
[root@localhost src]# ps -ef|grep redis
root 18678 1 0 08:12 ? 00:00:37 ./redis-server 192.168.1.160:7004 [cluster]
root 18679 1 0 08:12 ? 00:00:37 ./redis-server 192.168.1.160:7005 [cluster]
root 18680 1 0 08:12 ? 00:00:38 ./redis-server 192.168.1.160:7003 [cluster]
root 22185 15199 0 11:57 pts/1 00:00:00 ./redis-cli -p 7005 -c -h 192.168.1.160
root 23840 1 0 14:09 ? 00:00:00 ./redis-server 192.168.1.160:7006 [cluster]
root 23844 14745 0 14:09 pts/0 00:00:00 grep --color=auto redis
[1]+ 完成 ./redis-server ../../redis_cluster/7006/redis.conf

上面可以看到,7006已經啟動,現在加入集群中。添加使用redis-trib.rb的add-node命令:

./redis-trib.rb add-node 192.168.1.160:7006 192.168.1.160:7003

add-node是加入集群節點,192.168.1.160:7006為要加入的節點,192.168.1.160:7003表示加入的集群的一個節點,用來辨識是哪個集群,理論上哪個集群的節點都可以。

執行以下add-node

[root@localhost src]# ./redis-trib.rb add-node 192.168.1.160:7006 192.168.1.160:7003 
>>> Adding node 192.168.1.160:7006 to cluster 192.168.1.160:7003
>>> Performing Cluster Check (using node 192.168.1.160:7003)
M: 94f51658302cb5f1d178f14caaa79f27a9ac3703 192.168.1.160:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 910e14d9655a1e6e7fc007e799006d3f0d1cebe5 192.168.1.105:7002
slots: (0 slots) slave
replicates 94f51658302cb5f1d178f14caaa79f27a9ac3703
M: 6befec567ca7090eb3731e48fd5275a9853fb394 192.168.1.105:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 28a51a8e34920e2d48fc1650a9c9753ff73dad5d 192.168.1.105:7000
slots: (0 slots) slave
replicates cf02aa3d58d48215d9d61121eedd194dc5c50eeb
S: 54ca4fbc71257fd1be5b58d0f545b95d65f8f6b8 192.168.1.160:7005
slots: (0 slots) slave
replicates 6befec567ca7090eb3731e48fd5275a9853fb394
M: cf02aa3d58d48215d9d61121eedd194dc5c50eeb 192.168.1.160:7004
slots:0-5460 (5461 slots) master
1 additional replica(s)
[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.160:7006 to make it join the cluster.
[OK] New node added correctly.
[1]+ 完成 ./redis-server ../../redis_cluster/7006/redis.conf

可以看到7006加入這個Cluster,並成為一個新的節點。

check下7006節點狀態

[root@localhost src]# ./redis-trib.rb check 192.168.1.160:7006
>>> Performing Cluster Check (using node 192.168.1.160:7006)
M: 2335477842e1b02e143fabaf0d77d12e1e8e6f56 192.168.1.160:7006
slots: (0 slots) master
0 additional replica(s)
M: 6befec567ca7090eb3731e48fd5275a9853fb394 192.168.1.105:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 28a51a8e34920e2d48fc1650a9c9753ff73dad5d 192.168.1.105:7000
slots: (0 slots) slave
replicates cf02aa3d58d48215d9d61121eedd194dc5c50eeb
M: 94f51658302cb5f1d178f14caaa79f27a9ac3703 192.168.1.160:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 54ca4fbc71257fd1be5b58d0f545b95d65f8f6b8 192.168.1.160:7005
slots: (0 slots) slave
replicates 6befec567ca7090eb3731e48fd5275a9853fb394
S: 910e14d9655a1e6e7fc007e799006d3f0d1cebe5 192.168.1.105:7002
slots: (0 slots) slave
replicates 94f51658302cb5f1d178f14caaa79f27a9ac3703
M: cf02aa3d58d48215d9d61121eedd194dc5c50eeb 192.168.1.160:7004
slots:0-5460 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

M: 2335477842e1b02e143fabaf0d77d12e1e8e6f56 192.168.1.160:7006
slots: (0 slots) master
0 additional replica(s)
上面信息可以看到有4個M節點,3個S節點,7006成為了M主節點,它沒有附屬的從節點,而且Cluster並未給7006分配哈希卡槽(0 slots)。

redis-cluster在新增節點時並未分配卡槽,需要我們手動對集群進行重新分片遷移數據,需要重新分片命令reshard。

./redis-trib.rb reshard 192.168.1.160:7005

這個命令是用來遷移slot節點的,后面的192.168.1.160:7005是表示是哪個集群,端口填[7000-7007]都可以,執行結果如下:

[root@localhost redis-cluster]# ./redis-trib.rb reshard 192.168.1.160:7005 
>>> Performing Cluster Check (using node 192.168.1.160:7005) 
M: a5db243087d8bd423b9285fa8513eddee9bb59a6 192.168.1.160:7005 
slots:5461-10922 (5462 slots) master 
1 additional replica(s) 
S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 192.168.1.160:7004 
slots: (0 slots) slave 
replicates dd19221c404fb2fc4da37229de56bab755c76f2b 
M: f9886c71e98a53270f7fda961e1c5f730382d48f 192.168.1.160:7003 
slots:10923-16383 (5461 slots) master 
1 additional replica(s) 
S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 192.168.1.160:7002 
slots: (0 slots) slave 
replicates a5db243087d8bd423b9285fa8513eddee9bb59a6 
M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 192.168.1.160:7007 
slots: (0 slots) master 
0 additional replica(s) 
M: dd19221c404fb2fc4da37229de56bab755c76f2b 192.168.1.160:7001 
slots:0-5460 (5461 slots) master 
1 additional replica(s) 
S: 8bb3ede48319b46d0015440a91ab277da9353c8b 192.168.1.160:7006 
slots: (0 slots) slave 
replicates f9886c71e98a53270f7fda961e1c5f730382d48f 
[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個節點:16384/4 = 4096,我們需要移動4096個槽點到7006上。

[OK] All 16384 slots covered. 
How many slots do you want to move (from 1 to 16384)? 4096 
What is the receiving node ID?

需要輸入7006的節點id,2335477842e1b02e143fabaf0d77d12e1e8e6f56

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:

redis-trib 會向你詢問重新分片的源節點(source node),即要從特定的哪個節點中取出 4096 個哈希槽,還是從全部節點提取4096個哈希槽, 並將這些槽移動到7006節點上面。

如果我們不打算從特定的節點上取出指定數量的哈希槽,那么可以向redis-trib輸入 all,這樣的話, 集群中的所有主節點都會成為源節點,redis-trib從各個源節點中各取出一部分哈希槽,湊夠4096個,然后移動到7006節點上:

Source node #1:all

 

然后開始從別的主節點遷移哈希槽,並且確認。
Moving slot 1343 from dd19221c404fb2fc4da37229de56bab755c76f2b
Moving slot 1344 from dd19221c404fb2fc4da37229de56bab755c76f2b
Moving slot 1345 from dd19221c404fb2fc4da37229de56bab755c76f2b
Moving slot 1346 from dd19221c404fb2fc4da37229de56bab755c76f2b
Moving slot 1347 from dd19221c404fb2fc4da37229de56bab755c76f2b
Moving slot 1348 from dd19221c404fb2fc4da37229de56bab755c76f2b
Moving slot 1349 from dd19221c404fb2fc4da37229de56bab755c76f2b
Moving slot 1350 from dd19221c404fb2fc4da37229de56bab755c76f2b
Moving slot 1351 from dd19221c404fb2fc4da37229de

確認之后,redis-trib就開始執行分片操作,將哈希槽一個一個從源主節點移動到7006目標主節點。
重新分片結束后我們可以check以下節點的分配情況。

[root@localhost src]# ./redis-trib.rb check 192.168.1.160:7006
>>> Performing Cluster Check (using node 192.168.1.160:7006)
M: 2335477842e1b02e143fabaf0d77d12e1e8e6f56 192.168.1.160:7006
slots:0-1179,5461-6826 (2546 slots) master
0 additional replica(s)
M: 6befec567ca7090eb3731e48fd5275a9853fb394 192.168.1.105:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 28a51a8e34920e2d48fc1650a9c9753ff73dad5d 192.168.1.105:7000
slots: (0 slots) slave
replicates cf02aa3d58d48215d9d61121eedd194dc5c50eeb
M: 94f51658302cb5f1d178f14caaa79f27a9ac3703 192.168.1.160:7003
slots:6827-10922 (4096 slots) master
1 additional replica(s)
S: 54ca4fbc71257fd1be5b58d0f545b95d65f8f6b8 192.168.1.160:7005
slots: (0 slots) slave
replicates 6befec567ca7090eb3731e48fd5275a9853fb394
S: 910e14d9655a1e6e7fc007e799006d3f0d1cebe5 192.168.1.105:7002
slots: (0 slots) slave
replicates 94f51658302cb5f1d178f14caaa79f27a9ac3703
M: cf02aa3d58d48215d9d61121eedd194dc5c50eeb 192.168.1.160:7004
slots:1180-5460 (4281 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.

slots:0-1179,5461-6826 (2546 slots) master

可以看到7007節點分片的哈希槽片不是連續的,間隔的移動。

[root@localhost src]# ./redis-cli -p 7006 -c -h 192.168.1.160
192.168.1.160:7006> get aa
-> Redirected to slot [1180] located at 192.168.1.160:7004
"aa"
192.168.1.160:7004> 

可以看到將7006的aa[1180]移動到7004節點上,表明主節點7006添加成功。

2、新增從節點
新增一個節點7007節點,使用add-node --slave命令。
redis-trib增加從節點的命令為:

./redis-trib.rb add-node --slave --master-id $[nodeid] 192.168.1.160:7007 192.168.1.160:7005

nodeid為要加到master主節點的node id,192.168.1.160:7007為新增的從節點,192.168.1.160:7005為集群的一個節點(集群的任意節點都行),用來辨識是哪個集群;如果沒有給定那個主節點--master-id的話,redis-trib將會新增的從節點隨機到從節點較少的主節點上。

現在指定主節點添加從節點,給7006增加7007從節點。。。

./redis-trib.rb add-node --slave --master-id 2335477842e1b02e143fabaf0d77d12e1e8e6f56 192.168.1.160:7007 192.168.1.160:7005

 

[root@localhost src]# ./redis-trib.rb add-node --slave --master-id 2335477842e1b02e143fabaf0d77d12e1e8e6f56 192.168.1.160:7007 192.168.1.160:7005 
>>> Adding node 192.168.1.160:7007 to cluster 192.168.1.160:7005
>>> Performing Cluster Check (using node 192.168.1.160:7005)
S: 54ca4fbc71257fd1be5b58d0f545b95d65f8f6b8 192.168.1.160:7005
slots: (0 slots) slave
replicates 6befec567ca7090eb3731e48fd5275a9853fb394
S: 28a51a8e34920e2d48fc1650a9c9753ff73dad5d 192.168.1.105:7000
slots: (0 slots) slave
replicates cf02aa3d58d48215d9d61121eedd194dc5c50eeb
M: 2335477842e1b02e143fabaf0d77d12e1e8e6f56 192.168.1.160:7006
slots:0-1179,5461-6826 (2546 slots) master
0 additional replica(s)
M: cf02aa3d58d48215d9d61121eedd194dc5c50eeb 192.168.1.160:7004
slots:1180-5460 (4281 slots) master
1 additional replica(s)
M: 94f51658302cb5f1d178f14caaa79f27a9ac3703 192.168.1.160:7003
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: 6befec567ca7090eb3731e48fd5275a9853fb394 192.168.1.105:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 910e14d9655a1e6e7fc007e799006d3f0d1cebe5 192.168.1.105:7002
slots: (0 slots) slave
replicates 94f51658302cb5f1d178f14caaa79f27a9ac3703
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
[WARNING] Node 192.168.1.160:7006 has slots in importing state (1180).
[WARNING] Node 192.168.1.160:7004 has slots in migrating state (1180).
[WARNING] The following slots are open: 1180
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.1.160:7007 to make it join the cluster.
Waiting for the cluster to join...
>>> Configure node as replica of 192.168.1.160:7006.
[OK] New node added correctly.

自此從節點添加成功。

六、節點的移除
和添加節點一樣,移除節點也有移除主節點,從節點。

1、移除主節點
移除節點使用redis-trib的del-node命令,

./redis-trib.rb del-node 192.168.1.160:7005 ${node-id}

192.168.1.160:7005為集群節點,node-id為要刪除的主節點。和添加節點不同,移除節點node-id是必需的,測試刪除7006主節點:

./redis-trib.rb del-node 192.168.1.160:7006 2335477842e1b02e143fabaf0d77d12e1e8e6f56

[root@localhost src]# ./redis-trib.rb del-node 192.168.1.160:7005 2335477842e1b02e143fabaf0d77d12e1e8e6f56
>>> Removing node 2335477842e1b02e143fabaf0d77d12e1e8e6f56 from cluster 192.168.1.160:7005
[ERR] Node 192.168.1.160:7006 is not empty! Reshard data away and try again.

redis cluster提示7006已經有數據了,不能夠被刪除,需要將他的數據轉移出去,也就是和新增主節點一樣需重新分片。

[root@localhost redis-cluster]# ./redis-trib.rb reshard 192.168.1.160:7006 

執行以后會提示我們移除的大小,因為7006占用了2546個槽點

>>> Check for open slots... 
>>> Check slots coverage... 
[OK] All 16384 slots covered. 
How many slots do you want to move (from 1 to 16384)? 2546


提示移動的node id,填寫7004的node id,用來接收7006的solt。

How many slots do you want to move (from 1 to 16384)? 4096 
What is the receiving node ID? 2335477842e1b02e143fabaf0d77d12e1e8e6f56 #接收的node 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: c1fafa48cb761eeddcc56e8c821eae5fef6a28bc #刪除的node id
Source node #2: done
Do you want to proceed with the proposed reshard plan (yes/no)? yes

 

確認之后7006的卡槽是否移到到7000上:

[root@localhost src]# ./redis-trib.rb check 192.168.1.160:7007
>>> Performing Cluster Check (using node 192.168.1.160:7007)
M: c1fafa48cb761eeddcc56e8c821eae5fef6a28bc 192.168.1.160:7007
slots: (0 slots) master
0 additional replica(s)
S: 94f51658302cb5f1d178f14caaa79f27a9ac3703 192.168.1.160:7003
slots: (0 slots) slave
replicates 910e14d9655a1e6e7fc007e799006d3f0d1cebe5
S: 54ca4fbc71257fd1be5b58d0f545b95d65f8f6b8 192.168.1.160:7005
slots: (0 slots) slave
replicates 6befec567ca7090eb3731e48fd5275a9853fb394
M: 28a51a8e34920e2d48fc1650a9c9753ff73dad5d 192.168.1.105:7000
slots:0-8619,10923-11927 (9625 slots) master
1 additional replica(s)
S: cf02aa3d58d48215d9d61121eedd194dc5c50eeb 192.168.1.160:7004
slots: (0 slots) slave
replicates 28a51a8e34920e2d48fc1650a9c9753ff73dad5d
M: 910e14d9655a1e6e7fc007e799006d3f0d1cebe5 192.168.1.105:7002
slots:8620-10922 (2303 slots) master
1 additional replica(s)
M: 6befec567ca7090eb3731e48fd5275a9853fb394 192.168.1.105:7001
slots:11928-16383 (4456 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

可以看到7000有9625個卡槽,而7007有0個卡槽。

./redis-trib.rb del-node 192.168.1.160:7006 2335477842e1b02e143fabaf0d77d12e1e8e6f56

 

2、移除從節點
比如刪除7007節點:

[root@localhost src]# ./redis-trib.rb del-node 192.168.1.160:7005 2335477842e1b02e143fabaf0d77d12e1e8e6f56 
>>> Removing node 2335477842e1b02e143fabaf0d77d12e1e8e6f56 from cluster 192.168.1.160:7006
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

ok,測試到這兒吧。

 

Node has slots in importing state異常解決:
[WARNING] Node 192.168.1.160:7006 has slots in importing state (1180).
[WARNING] Node 192.168.1.160:7004 has slots in migrating state (1180).
[WARNING] The following slots are open: 1180
解決方案:redis-cli客戶端登錄7004、7006端口,分別執行cluster setslot 1180 stable命令,如下所示:

[root@localhost src]# ./redis-cli -c -h 192.168.1.160 -p 7004 
192.168.1.160:7004> cluster setslot 1180 stable
OK

[root@localhost src]# ./redis-cli -c -h 192.168.1.160 -p 7006 
192.168.1.160:7006> cluster setslot 1180 stable
OK

 

Redis 3.2.1集群搭建

Redis Cluster集群的搭建與實踐

全面剖析Redis Cluster原理和應用


免責聲明!

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



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