Redis Cluster集羣
1、redis-cluster設計
Redis集羣搭建的方式有多種,例如使用zookeeper等,但從redis 3.0以後版本支持redis-cluster集羣,Redis-Cluster採用無中心結構,每一個節點保存數據和整個集羣狀態,每一個節點都和其餘全部 節點鏈接。其redis-cluster架構圖以下:node
其結構特色:redis
一、全部的redis節點彼此互聯(PING-PONG機制),內部使用二進制協議優化傳輸速度和帶寬。
二、節點的fail是經過集羣中超過半數的節點檢測失效時才生效。
三、客戶端與redis節點直連,不須要中間proxy層.客戶端不須要鏈接集羣全部節點,鏈接集羣中任何一個可用節點便可。
四、redis-cluster把全部的物理節點映射到[0-16383]slot上(不必定是平均分配),cluster 負責維護node<->slot<->value。
算法
五、Redis集羣預分好16384個桶,當須要在 Redis 集羣中放置一個 key-value 時,根據 CRC16(key) mod 16384的值,決定將一個key放到哪一個桶中。
ruby
一、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節點上獲取數據 app
新增一個主節點:tcp
新增一個節點D,redis cluster的這種作法是從各個節點的前面各拿取一部分slot到D上,我會在接下來的實踐中實驗。大體就會變成這樣:
svg
節點A覆蓋1365-5460
節點B覆蓋6827-10922
節點C覆蓋12288-16383
節點D覆蓋0-1364,5461-6826,10923-12287
一樣刪除一個節點也是相似,移動完成後就能夠刪除這個節點了。測試
二、Redis Cluster主從模式
redis cluster 爲了保證數據的高可用性,加入了主從模式,一個主節點對應一個或多個從節點,主節點提供數據存取,從節點則是從主節點拉取數據備份,當這個主節點掛掉後,就會有這個從節點選取一個來充當主節點,從而保證集羣不會掛掉。
上面那個例子裏, 集羣有ABC三個主節點, 若是這3個節點都沒有加入從節點,若是B掛掉了,咱們就沒法訪問整個集羣了。A和C的slot也沒法訪問。
因此咱們在集羣創建的時候,必定要爲每一個主節點都添加了從節點, 好比像這樣, 集羣包含主節點A、B、C, 以及從節點A一、B一、C1, 那麼即便B掛掉系統也能夠繼續正確工做。
B1節點替代了B節點,因此Redis集羣將會選擇B1節點做爲新的主節點,集羣將會繼續正確地提供服務。 當B從新開啓後,它就會變成B1的從節點。
不過須要注意,若是節點B和B1同時掛了,Redis集羣就沒法繼續正確地提供服務了。
2、redis集羣的搭建
集羣中至少應該有奇數個節點,因此至少有三個節點,每一個節點至少有一個備份節點,因此下面使用6節點(主節點、備份節點由redis-cluster集羣肯定)。
下面使用redis-3.2.0安裝,下載地址
一、安裝redis節點指定端口
解壓redis壓縮包,編譯安裝
- [root@localhost redis-3.2.0]# tar xzf redis-3.2.0.tar.gz
- [root@localhost redis-3.2.0]# cd redis-3.2.0
- [root@localhost redis-3.2.0]# make
- [root@localhost redis01]# make install PREFIX=/usr/andy/redis-cluster
在redis-cluster下 修改bin文件夾爲redis01,複製redis.conf配置文件
配置redis的配置文件redis.conf
daemonize yes #後臺啓動
port 7001 #修改端口號,從7001到7006
cluster-enabled yes #開啓cluster,去掉註釋
cluster-config-file nodes.conf
cluster-node-timeout 15000
appendonly yes
複製六份,修改對應的端口號
二、安裝redis-trib所需的 ruby腳本
複製redis解壓文件src下的redis-trib.rb文件到redis-cluster目錄
- [root@localhost redis-cluster]# cp /usr/andy/redis/redis-3.2.0/src/redis-trib.rb ./
安裝ruby環境:
- [root@localhost redis-cluster]# yum install ruby
- [root@localhost redis-cluster]# yum install rubygems
安裝redis-trib.rb運行依賴的ruby的包redis-3.2.2.gem,下載
- [root@localhost redis-cluster]# gem install redis-3.2.2.gem
三、啓動全部的redis節點
能夠寫一個命令腳本start-all.sh
- cd redis01
- ./redis-server redis.conf
- cd ..
- cd redis02
- ./redis-server redis.conf
- cd ..
- cd redis03
- ./redis-server redis.conf
- cd ..
- cd redis04
- ./redis-server redis.conf
- cd ..
- cd redis05
- ./redis-server redis.conf
- cd ..
- cd redis06
- ./redis-server redis.conf
- cd ..
設置權限啓動
- [root@localhost redis-cluster]# chmod 777 start-all.sh
- [root@localhost redis-cluster]# ./start-all.sh
查看redis進程啓動狀態
- [root@localhost redis-cluster]# ps -ef | grep redis
-
- root 4547 1 0 23:12 ? 00:00:00 ./redis-server 127.0.0.1:7001 [cluster]
- root 4551 1 0 23:12 ? 00:00:00 ./redis-server 127.0.0.1:7002 [cluster]
- root 4555 1 0 23:12 ? 00:00:00 ./redis-server 127.0.0.1:7003 [cluster]
- root 4559 1 0 23:12 ? 00:00:00 ./redis-server 127.0.0.1:7004 [cluster]
- root 4563 1 0 23:12 ? 00:00:00 ./redis-server 127.0.0.1:7005 [cluster]
- root 4567 1 0 23:12 ? 00:00:00 ./redis-server 127.0.0.1:7006 [cluster]
- root 4840 4421 0 23:26 pts/1 00:00:00 grep --color=auto redis
能夠看到redis的6個節點已經啓動成功
殺死所有的幾點:
- [root@localhost redis-cluster]# pkill -9 redis
四、使用redis-trib.rb建立集羣
- ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
使用create命令 --replicas 1 參數表示爲每一個主節點建立一個從節點,其餘參數是實例的地址集合。
- [root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
- >>> Creating cluster
- >>> Performing hash slots allocation on 6 nodes...
- Using 3 masters:
- 127.0.0.1:7001
- 127.0.0.1:7002
- 127.0.0.1:7003
- Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
- Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
- Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
- M: dfd510594da614469a93a0a70767ec9145aefb1a 127.0.0.1:7001
- slots:0-5460 (5461 slots) master
- M: e02eac35110bbf44c61ff90175e04d55cca097ff 127.0.0.1:7002
- slots:5461-10922 (5462 slots) master
- M: 4385809e6f4952ecb122dbfedbee29109d6bb234 127.0.0.1:7003
- slots:10923-16383 (5461 slots) master
- S: ec02c9ef3acee069e8849f143a492db18d4bb06c 127.0.0.1:7004
- replicates dfd510594da614469a93a0a70767ec9145aefb1a
- S: 83e5a8bb94fb5aaa892cd2f6216604e03e4a6c75 127.0.0.1:7005
- replicates e02eac35110bbf44c61ff90175e04d55cca097ff
- S: 10c097c429ca24f8720986c6b66f0688bfb901ee 127.0.0.1:7006
- replicates 4385809e6f4952ecb122dbfedbee29109d6bb234
- Can I set the above configuration? (type 'yes' to accept): yes
- >>> Nodes configuration updated
- >>> Assign a different config epoch to each node
- >>> Sending CLUSTER MEET messages to join the cluster
- Waiting for the cluster to join......
- >>> Performing Cluster Check (using node 127.0.0.1:7001)
- M: dfd510594da614469a93a0a70767ec9145aefb1a 127.0.0.1:7001
- slots:0-5460 (5461 slots) master
- M: e02eac35110bbf44c61ff90175e04d55cca097ff 127.0.0.1:7002
- slots:5461-10922 (5462 slots) master
- M: 4385809e6f4952ecb122dbfedbee29109d6bb234 127.0.0.1:7003
- slots:10923-16383 (5461 slots) master
- M: ec02c9ef3acee069e8849f143a492db18d4bb06c 127.0.0.1:7004
- slots: (0 slots) master
- replicates dfd510594da614469a93a0a70767ec9145aefb1a
- M: 83e5a8bb94fb5aaa892cd2f6216604e03e4a6c75 127.0.0.1:7005
- slots: (0 slots) master
- replicates e02eac35110bbf44c61ff90175e04d55cca097ff
- M: 10c097c429ca24f8720986c6b66f0688bfb901ee 127.0.0.1:7006
- slots: (0 slots) master
- replicates 4385809e6f4952ecb122dbfedbee29109d6bb234
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
上面顯示建立成功,有3個主節點,3個從節點,每一個節點都是成功鏈接狀態。
3個主節點[M]以及分配的哈希卡槽以下:
M: dfd510594da614469a93a0a70767ec9145aefb1a 127.0.0.1:7001
slots:0-5460 (5461 slots) master
M: e02eac35110bbf44c61ff90175e04d55cca097ff 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
M: 4385809e6f4952ecb122dbfedbee29109d6bb234 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
3個從節點[S]以及附屬的主節點以下:
S: ec02c9ef3acee069e8849f143a492db18d4bb06c 127.0.0.1:7004
replicates dfd510594da614469a93a0a70767ec9145aefb1a
S: 83e5a8bb94fb5aaa892cd2f6216604e03e4a6c75 127.0.0.1:7005
replicates e02eac35110bbf44c61ff90175e04d55cca097ff
S: 10c097c429ca24f8720986c6b66f0688bfb901ee 127.0.0.1:7006
replicates 4385809e6f4952ecb122dbfedbee29109d6bb234
以上集羣安裝成功了,若是安裝未成功報以下錯誤
>>> Creating cluster
[ERR] Sorry, can't connect to node ....
須要安裝最新的ruby源碼,下載
- [root@localhost redis-cluster]# tar -zxvf ruby-2.3.1.tar.gz
- [root@localhost redis-cluster]# cd
- [root@localhost redis-cluster]# ./configure --prefix=/usr/local/ruby-2.3.1
- [root@localhost redis-cluster]# make && make install
- [root@localhost redis-cluster]#gem install redis
還有一種狀況是,在VMware作測試的時間(都在一臺服務器時),ip應該使用127.0.0.1,若是使用局域網ip,也會報節點建立失敗。
3、redis集羣的測試
一、測試存取值
客戶端鏈接集羣redis-cli須要帶上 -c ,redis-cli -c -p 端口號
- [root@localhost redis01]# ./redis-cli -c -p 7001
- 127.0.0.1:7001> set name andy
- -> Redirected to slot [5798] located at 127.0.0.1:7002
- OK
- 127.0.0.1:7002> get name
- "andy"
- 127.0.0.1:7002>
根據redis-cluster的key值分配,name應該分配到節點7002[5461-10922]上,上面顯示redis cluster自動從7001跳轉到了7002節點。
咱們能夠測試一下7006從節點獲取name值
- [root@localhost redis06]# ./redis-cli -c -p 7006
- 127.0.0.1:7006> get name
- -> Redirected to slot [5798] located at 127.0.0.1:7002
- "andy"
- 127.0.0.1:7002>
7006位7003的從節點,從上面也是自動跳轉至7002獲取值,這也是redis cluster的特色,它是去中心化,每一個節點都是對等的,鏈接哪一個節點均可以獲取和設置數據。
4、集羣節點選舉
如今模擬將7002節點掛掉,按照redis-cluster原理會選舉會將 7002的從節點7005選舉爲主節點。
- [root@localhost redis-cluster]# ps -ef | grep redis
- root 7950 1 0 12:50 ? 00:00:28 ./redis-server 127.0.0.1:7001 [cluster]
- root 7952 1 0 12:50 ? 00:00:29 ./redis-server 127.0.0.1:7002 [cluster]
- root 7956 1 0 12:50 ? 00:00:29 ./redis-server 127.0.0.1:7003 [cluster]
- root 7960 1 0 12:50 ? 00:00:29 ./redis-server 127.0.0.1:7004 [cluster]
- root 7964 1 0 12:50 ? 00:00:29 ./redis-server 127.0.0.1:7005 [cluster]
- root 7966 1 0 12:50 ? 00:00:29 ./redis-server 127.0.0.1:7006 [cluster]
- root 11346 10581 0 14:57 pts/2 00:00:00 grep --color=auto redis
- [root@localhost redis-cluster]# kill 7952
在查看集羣中的7002節點
- [root@localhost redis-cluster]#
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7002
- [ERR] Sorry, can't connect to node 127.0.0.1:7002
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7005
- >>> Performing Cluster Check (using node 127.0.0.1:7005)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:5461-10922 (5462 slots) master
- 0 additional replica(s)
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:10923-16383 (5461 slots) master
- 1 additional replica(s)
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:0-5460 (5461 slots) master
- 1 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1: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.
- [root@localhost redis-cluster]#
能夠看到集羣鏈接不了7002節點,而7005有原來的S轉換爲M節點,代替了原來的7002節點。咱們能夠獲取name值:
- [root@localhost redis01]# ./redis-cli -c -p 7001
- 127.0.0.1:7001> get name
- -> Redirected to slot [5798] located at 127.0.0.1:7005
- "andy"
- 127.0.0.1:7005>
- 127.0.0.1:7005>
從7001節點連入,自動跳轉到7005節點,而且獲取name值。
如今咱們將7002節點恢復,看是否會自動加入集羣中以及充當的M仍是S節點。
- [root@localhost redis-cluster]# cd redis02
- [root@localhost redis02]# ./redis-server redis.conf
- [root@localhost redis02]#
在check一下7002節點
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7002
- >>> Performing Cluster Check (using node 127.0.0.1:7002)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:10923-16383 (5461 slots) master
- 1 additional replica(s)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:5461-10922 (5462 slots) master
- 1 additional replica(s)
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- 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.
- [root@localhost redis-cluster]#
能夠看到7002節點變成了a5db243087d8bd423b9285fa8513eddee9bb59a6 7005的從節點。
5、集羣節點添加
節點新增包括新增主節點、從節點兩種狀況。如下分別作一下測試:
一、新增主節點
新增一個節點7007做爲主節點修改配置文件
- [root@localhost redis-cluster]# cp -r redis01 redis07
- [root@localhost redis-cluster]# cd redis07/
- [root@localhost redis07]# sed -i "s/7001/7007/g" ./redis.conf
啓動7007redis服務
- [root@localhost redis07]# ./redis-server redis.conf
- [root@localhost redis07]# netstat -anp | grep 7007
- tcp 0 0 127.0.0.1:17007 0.0.0.0:* LISTEN 13441/./redis-serve
- tcp 0 0 127.0.0.1:7007 0.0.0.0:* LISTEN 13441/./redis-serve
- [root@localhost redis07]#
上面能夠看到,7007已經啓動,如今加入集羣中。添加使用redis-trib.rb的add-node命令
- ./redis-trib.rb add-node 127.0.0.1:7007 127.0.0.1:7002
add-node是加入集羣節點,127.0.0.1:7007爲要加入的節點,127.0.0.1:7002 表示加入的集羣的一個節點,用來辨識是哪一個集羣,理論上那個集羣的節點均可以。
執行如下add-node
- [root@localhost redis-cluster]# ./redis-trib.rb add-node 127.0.0.1:7007 127.0.0.1:7002
- >>> Adding node 127.0.0.1:7007 to cluster 127.0.0.1:7002
- >>> Performing Cluster Check (using node 127.0.0.1:7002)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:10923-16383 (5461 slots) master
- 1 additional replica(s)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:5461-10922 (5462 slots) master
- 1 additional replica(s)
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- 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 127.0.0.1:7007 to make it join the cluster.
- [OK] New node added correctly.
- [root@localhost redis-cluster]#
能夠看到7007加入這個Cluster,併成爲一個新的節點。
能夠check如下7007節點狀態
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7007
- >>> Performing Cluster Check (using node 127.0.0.1:7007)
- M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots: (0 slots) master
- 0 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:0-5460 (5461 slots) master
- 1 additional replica(s)
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:10923-16383 (5461 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:5461-10922 (5462 slots) master
- 1 additional replica(s)
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
- [root@localhost redis-cluster]#
M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
slots: (0 slots) master
0 additional replica(s)
上面信息能夠看到有4個M節點,3個S節點,7007成爲了M主節點,它沒有附屬的從節點,並且Cluster並未給7007分配哈希卡槽(0 slots)。
能夠從客戶端鏈接集羣查看一下,集羣節點的鏈接狀況
- [root@localhost redis-cluster]# cd redis07/
- [root@localhost redis07]# ./redis-cli -c -p 7007
- 127.0.0.1:7007> cluster nodes
- 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006 slave f9886c71e98a53270f7fda961e1c5f730382d48f 0 1462955393326 3 connected
- dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001 master - 0 1462955388247 1 connected 0-5460
- ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007 myself,master - 0 0 0 connected
- f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003 master - 0 1462955390270 3 connected 10923-16383
- 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002 slave a5db243087d8bd423b9285fa8513eddee9bb59a6 0 1462955394334 7 connected
- a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005 master - 0 1462955392309 7 connected 5461-10922
- 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004 slave dd19221c404fb2fc4da37229de56bab755c76f2b 0 1462955389663 1 connected
- 127.0.0.1:7007>
redis-cluster在新增節點時並未分配卡槽,須要咱們手動對集羣進行從新分片遷移數據,須要從新分片命令 reshard
redis-trib.rb reshard 127.0.0.1:7005
這個命令是用來遷移slot節點的,後面的127.0.0.1:7005是表示是哪一個集羣,端口填[7000-7007]均可以,執行結果以下:
- [root@localhost redis-cluster]# ./redis-trib.rb reshard 127.0.0.1:7005
- >>> Performing Cluster Check (using node 127.0.0.1:7005)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:5461-10922 (5462 slots) master
- 1 additional replica(s)
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:10923-16383 (5461 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots: (0 slots) master
- 0 additional replica(s)
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:0-5460 (5461 slots) master
- 1 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1: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到7007上,咱們平分16384個哈希槽給4個節點:16384/4 = 4096,咱們須要移動4096個槽點到7007上。
- [OK] All 16384 slots covered.
- How many slots do you want to move (from 1 to 16384)? 4096
- What is the receiving node ID?
須要輸入7007的節點id,ee3efb90e5ac0725f15238a64fc60a18a71205d7
- 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個哈希槽, 並將這些槽移動到7007節點上面。
若是咱們不打算從特定的節點上取出指定數量的哈希槽,那麼能夠向redis-trib輸入 all,這樣的話, 集羣中的全部主節點都會成爲源節點,redis-trib從各個源節點中各取出一部分哈希槽,湊夠4096個,而後移動到7007節點上:
而後開始從別的主節點遷移哈希槽,而且確認。
- 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 dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1352 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1353 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1354 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1355 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1356 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1357 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1358 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1359 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1360 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1361 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1362 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1363 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Moving slot 1364 from dd19221c404fb2fc4da37229de56bab755c76f2b
- Do you want to proceed with the proposed reshard plan (yes/no)? yes
確認以後,redis-trib就開始執行分片操做,將哈希槽一個一個從源主節點移動到7007目標主節點。
從新分片結束後咱們能夠check如下節點的分配狀況。
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7001
- >>> Performing Cluster Check (using node 127.0.0.1:7001)
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:1365-5460 (4096 slots) master
- 1 additional replica(s)
- M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots:0-1364,5461-6826,10923-12287 (4096 slots) master
- 0 additional replica(s)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:6827-10922 (4096 slots) master
- 1 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
- [root@localhost redis-cluster]#
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
能夠看到7007節點分片的哈希槽片不是連續的,間隔的移動。
- [root@localhost redis-cluster]# cd redis07/
- [root@localhost redis07]# ./redis-cli -c 7007
- Could not connect to Redis at 127.0.0.1:6379: Connection refused
- [root@localhost redis07]# ./redis-cli -c -p 7007
- 127.0.0.1:7007> keys *
- 1) "name"
- 2) "age"
- 127.0.0.1:7007>
- 127.0.0.1:7007>
能夠看到將7001的age[741]和name[5798]移動到7007節點上,
主節點7007添加成功。
二、新增從節點
新增一個節點7008節點,使用add-node --slave命令。
- [root@localhost redis-cluster]# cp -r redis01/ redis08
- [root@localhost redis-cluster]# cd redis08/
- [root@localhost redis08]# sed -i "s/7001/7008/g" ./redis.conf
- [root@localhost redis08]# ./redis-server redis.conf
redis-trib增長從節點的命令爲:
- ./redis-trib.rb add-node --slave --master-id $[nodeid] 127.0.0.1:7008 127.0.0.1:7000
nodeid爲要加到master主節點的node id,127.0.0.1:7008爲新增的從節點,127.0.0.1:7000爲集羣的一個節點(集羣的任意節點都行),用來辨識是哪一個集羣;若是沒有給定那個主節點--master-id的話,redis-trib將會將新增的從節點隨機到從節點較少的主節點上。
如今咱們添加一下7008,看是否會自動加到沒有從節點的7007主節點上。
- [root@localhost redis-cluster]# ./redis-trib.rb add-node --slave 127.0.0.1:7008 127.0.0.1:7001>>> Adding node 127.0.0.1:7008 to cluster 127.0.0.1:7001
- >>> Performing Cluster Check (using node 127.0.0.1:7001)
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:1365-5460 (4096 slots) master
- 1 additional replica(s)
- M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots:0-1364,5461-6826,10923-12287 (4096 slots) master
- 0 additional replica(s)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:6827-10922 (4096 slots) master
- 1 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
- Automatically selected master 127.0.0.1:7007
- >>> Send CLUSTER MEET to node 127.0.0.1:7008 to make it join the cluster.
- Waiting for the cluster to join.
- >>> Configure node as replica of 127.0.0.1:7007.
- [OK] New node added correctly.
- [root@localhost redis-cluster]#
能夠看到自動選擇了127.0.0.1:7007爲master主節點,而且添加成功。
能夠check一下7008:
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7008
- >>> Performing Cluster Check (using node 127.0.0.1:7008)
- S: 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008
- slots: (0 slots) slave
- replicates ee3efb90e5ac0725f15238a64fc60a18a71205d7
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:6827-10922 (4096 slots) master
- 1 additional replica(s)
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:1365-5460 (4096 slots) master
- 1 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots:0-1364,5461-6826,10923-12287 (4096 slots) master
- 1 additional replica(s)
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
- [root@localhost redis-cluster]#
能夠看到7008做爲了7007的從節點。
再測試一下指定主節點添加從節點,給7007增長7009從節點。
- [root@localhost redis-cluster]# cp -r redis01/ redis09
- [root@localhost redis-cluster]# cd redis09
- [root@localhost redis09]# sed -i "s/7001/7009/g" ./redis.conf
- [root@localhost redis09]# ./redis-server redis.conf
添加7007主節點上
- [root@localhost redis-cluster]# ./redis-trib.rb add-node --slave --master-id ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7009 127.0.0.1:7001
- >>> Adding node 127.0.0.1:7009 to cluster 127.0.0.1:7001
- >>> Performing Cluster Check (using node 127.0.0.1:7001)
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:1365-5460 (4096 slots) master
- 1 additional replica(s)
- S: 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008
- slots: (0 slots) slave
- replicates ee3efb90e5ac0725f15238a64fc60a18a71205d7
- M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots:0-1364,5461-6826,10923-12287 (4096 slots) master
- 1 additional replica(s)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:6827-10922 (4096 slots) master
- 1 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
- >>> Send CLUSTER MEET to node 127.0.0.1:7009 to make it join the cluster.
- Waiting for the cluster to join.
- >>> Configure node as replica of 127.0.0.1:7007.
- [OK] New node added correctly.
- [root@localhost redis-cluster]#
顯示從節點7009節點添加到7007主節點,能夠看一下7007的從節點,以下:
- [root@localhost redis-cluster]# cd ./redis07
- [root@localhost redis07]# ./redis-cli -c -p 7007 cluster nodes | grep ee3efb90e5ac0725f15238a64fc60a18a71205d7
- 1f51443ede952b98724fea2a12f61fe710ab6cb1 127.0.0.1:7009 slave ee3efb90e5ac0725f15238a64fc60a18a71205d7 0 1462962710266 8 connected
- ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007 myself,master - 0 0 8 connected 0-1364 5461-6826 10923-12287
- 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008 slave ee3efb90e5ac0725f15238a64fc60a18a71205d7 0 1462962711607 8 connected
- [root@localhost redis07]#
maser 7007有2個slave 7008,7009。
咱們測試一下7007節點掛掉,看7008和7009那個成爲主節點。
- [root@localhost redis-cluster]# ps -ef | grep redis
- root 7950 1 0 12:50 ? 00:02:05 ./redis-server 127.0.0.1:7001 [cluster]
- root 7956 1 0 12:50 ? 00:02:11 ./redis-server 127.0.0.1:7003 [cluster]
- root 7960 1 0 12:50 ? 00:01:47 ./redis-server 127.0.0.1:7004 [cluster]
- root 7964 1 0 12:50 ? 00:02:07 ./redis-server 127.0.0.1:7005 [cluster]
- root 7966 1 0 12:50 ? 00:01:46 ./redis-server 127.0.0.1:7006 [cluster]
- root 12070 1 0 15:14 ? 00:01:08 ./redis-server 127.0.0.1:7002 [cluster]
- root 13441 1 0 16:09 ? 00:01:25 ./redis-server 127.0.0.1:7007 [cluster]
- root 15939 1 0 17:41 ? 00:00:20 ./redis-server 127.0.0.1:7008 [cluster]
- root 16623 1 0 18:07 ? 00:00:10 ./redis-server 127.0.0.1:7009 [cluster]
- root 17295 10581 0 18:37 pts/2 00:00:00 grep --color=auto redis
- [root@localhost redis-cluster]# kill -9 13441
- [root@localhost redis-cluster]# cd ./redis08
- [root@localhost redis08]# ./redis-cli -c -p 7008
- 127.0.0.1:7008> get name
- -> Redirected to slot [5798] located at 127.0.0.1:7009
- "andy"
- 127.0.0.1:7009> cluster nodes
- ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007 master,fail - 1462963082317 1462963080194 8 disconnected
- 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004 slave dd19221c404fb2fc4da37229de56bab755c76f2b 0 1462963170968 1 connected
- f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003 master - 0 1462963168525 3 connected 12288-16383
- dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001 master - 0 1462963164466 1 connected 1365-5460
- 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008 slave 1f51443ede952b98724fea2a12f61fe710ab6cb1 0 1462963167508 9 connected
- 1f51443ede952b98724fea2a12f61fe710ab6cb1 127.0.0.1:7009 myself,master - 0 0 9 connected 0-1364 5461-6826 10923-12287
- 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002 slave a5db243087d8bd423b9285fa8513eddee9bb59a6 0 1462963170564 7 connected
- 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006 slave f9886c71e98a53270f7fda961e1c5f730382d48f 0 1462963167915 3 connected
- a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005 master - 0 1462963169538 7 connected 6827-10922
- 127.0.0.1:7009>
能夠看到7009代替7007成了主節點。
重啓7007以後,會自動變成7009的從節點。
- [root@localhost redis-cluster]# cd redis07
- [root@localhost redis07]# ./redis-server redis.conf
- [root@localhost redis07]# cd ../
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7007
- >>> Performing Cluster Check (using node 127.0.0.1:7007)
- S: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- M: 1f51443ede952b98724fea2a12f61fe710ab6cb1 127.0.0.1:7009
- slots:0-1364,5461-6826,10923-12287 (4096 slots) master
- 2 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:1365-5460 (4096 slots) master
- 1 additional replica(s)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:6827-10922 (4096 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
- [root@localhost redis-cluster]#
驗證了以前的測試。
6、節點的移除
和節點添加同樣,移除節點也有移除主節點,從節點。
一、移除主節點
移除節點使用redis-trib的del-node命令,
- redis-trib del-node 127.0.0.1:7002 ${node-id}
127.0.0.1:7002位集羣節點,node-id爲要刪除的主節點。 和添加節點不一樣,移除節點node-id是必需的,測試刪除7001主節點:
- [root@localhost redis-cluster]# ./redis-trib.rb del-node 127.0.0.1:7001 <span style="font-size: 14px;">dd19221c404fb2fc4da37229de56bab755c76f2b</span>
- >>> Removing node <span style="font-size: 14px;">dd19221c404fb2fc4da37229de56bab755c76f2b</span> from cluster 127.0.0.1:7002
- [ERR] Node 127.0.0.1:7001 is not empty! Reshard data away and try again.
- [root@localhost redis-cluster]#
redis cluster提示7001已經有數據了,不可以被刪除,須要將他的數據轉移出去,也就是和新增主節點同樣需從新分片。
- [root@localhost redis-cluster]# ./redis-trib.rb reshard 127.0.0.1:7002
執行之後會提示咱們移除的大小,由於7001佔用了4096個槽點
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
- How many slots do you want to move (from 1 to 16384)?
輸入4096
提示移動的node id,填寫7009的node id。
- How many slots do you want to move (from 1 to 16384)? 4096
- What is the receiving 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:
將4096個槽點移動到7009上,填寫7001的node id :dd19221c404fb2fc4da37229de56bab755c76f2b
- Source node #1:dd19221c404fb2fc4da37229de56bab755c76f2b
- Source node #2:done
- Do you want to proceed with the proposed reshard plan (yes/no)? yes
確認以後會一個一個將7001的卡槽移到到7009上。
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7009
- >>> Performing Cluster Check (using node 127.0.0.1:7009)
- M: 1f51443ede952b98724fea2a12f61fe710ab6cb1 127.0.0.1:7009
- slots:0-6826,10923-12287 (8192 slots) master
- 3 additional replica(s)
- S: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots: (0 slots) master
- 0 additional replica(s)
- S: 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:6827-10922 (4096 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.
- [root@localhost redis-cluster]#
能夠看到7001有0個卡槽,而7009有8192個卡槽。
在執行移除操做
- [root@localhost redis-cluster]# ./redis-trib.rb del-node 127.0.0.1:7002 dd19221c404fb2fc4da37229de56bab755c76f2b
- >>> Removing node dd19221c404fb2fc4da37229de56bab755c76f2b from cluster 127.0.0.1:7002
- >>> Sending CLUSTER FORGET messages to the cluster...
- >>> SHUTDOWN the node.
- [root@localhost redis-cluster]#
已經刪除了7001節點。
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7001
- [ERR] Sorry, can't connect to node 127.0.0.1:7001
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7009
- >>> Performing Cluster Check (using node 127.0.0.1:7009)
- M: 1f51443ede952b98724fea2a12f61fe710ab6cb1 127.0.0.1:7009
- slots:0-6826,10923-12287 (8192 slots) master
- 3 additional replica(s)
- S: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:6827-10922 (4096 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.
- [root@localhost redis-cluster]#
能夠看到7001已經鏈接不了;而7001的從節點7004自動分配到了7009主節點中,7009如今3個從節點。
二、移除從節點
好比刪除7009的7008節點:
- [root@localhost redis-cluster]# ./redis-trib.rb del-node 127.0.0.1:7009 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5
- >>> Removing node 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 from cluster 127.0.0.1:7009
- >>> Sending CLUSTER FORGET messages to the cluster...
- >>> SHUTDOWN the node.
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7008
- [ERR] Sorry, can't connect to node 127.0.0.1:7008
- [root@localhost redis-cluster]#
刪除從節點比較方便,如今redis-cluster中有3個主節點,4個從節點,以下:
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7009
- >>> Performing Cluster Check (using node 127.0.0.1:7009)
- M: 1f51443ede952b98724fea2a12f61fe710ab6cb1 127.0.0.1:7009
- slots:0-6826,10923-12287 (8192 slots) master
- 2 additional replica(s)
- S: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:6827-10922 (4096 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.
- [root@localhost redis-cluster]#
ok,測試到這兒吧。