Redis Cluster集群的搭建与实践[转]
Redis Cluster集群的搭建與實(shí)踐
Redis Cluster集群
一、redis-cluster設(shè)計(jì)
Redis集群搭建的方式有多種,例如使用zookeeper等,但從redis 3.0之后版本支持redis-cluster集群,Redis-Cluster采用無中心結(jié)構(gòu),每個(gè)節(jié)點(diǎn)保存數(shù)據(jù)和整個(gè)集群狀態(tài),每個(gè)節(jié)點(diǎn)都和其他所有節(jié)點(diǎn)連接。其redis-cluster架構(gòu)圖如下:
其結(jié)構(gòu)特點(diǎn):
1、所有的redis節(jié)點(diǎn)彼此互聯(lián)(PING-PONG機(jī)制),內(nèi)部使用二進(jìn)制協(xié)議優(yōu)化傳輸速度和帶寬。2、節(jié)點(diǎn)的fail是通過集群中超過半數(shù)的節(jié)點(diǎn)檢測(cè)失效時(shí)才生效。3、客戶端與redis節(jié)點(diǎn)直連,不需要中間proxy層.客戶端不需要連接集群所有節(jié)點(diǎn),連接集群中任何一個(gè)可用節(jié)點(diǎn)即可。4、redis-cluster把所有的物理節(jié)點(diǎn)映射到[0-16383]slot上(不一定是平均分配),cluster 負(fù)責(zé)維護(hù)node<->slot<->value。5、Redis集群預(yù)分好16384個(gè)桶,當(dāng)需要在 Redis 集群中放置一個(gè) key-value 時(shí),根據(jù) CRC16(key) mod 16384的值,決定將一個(gè)key放到哪個(gè)桶中。1、redis cluster節(jié)點(diǎn)分配
現(xiàn)在我們是三個(gè)主節(jié)點(diǎn)分別是:A, B, C 三個(gè)節(jié)點(diǎn),它們可以是一臺(tái)機(jī)器上的三個(gè)端口,也可以是三臺(tái)不同的服務(wù)器。那么,采用哈希槽 (hash slot)的方式來分配16384個(gè)slot 的話,它們?nèi)齻€(gè)節(jié)點(diǎn)分別承擔(dān)的slot 區(qū)間是:
節(jié)點(diǎn)A覆蓋0-5460;節(jié)點(diǎn)B覆蓋5461-10922;節(jié)點(diǎn)C覆蓋10923-16383.獲取數(shù)據(jù):如果存入一個(gè)值,按照redis cluster哈希槽的算法: CRC16('key')%16384 = 6782。 那么就會(huì)把這個(gè)key 的存儲(chǔ)分配到 B 上了。同樣,當(dāng)我連接(A,B,C)任何一個(gè)節(jié)點(diǎn)想獲取'key'這個(gè)key時(shí),也會(huì)這樣的算法,然后內(nèi)部跳轉(zhuǎn)到B節(jié)點(diǎn)上獲取數(shù)據(jù) 新增一個(gè)主節(jié)點(diǎn):新增一個(gè)節(jié)點(diǎn)D,redis cluster的這種做法是從各個(gè)節(jié)點(diǎn)的前面各拿取一部分slot到D上,我會(huì)在接下來的實(shí)踐中實(shí)驗(yàn)。大致就會(huì)變成這樣:節(jié)點(diǎn)A覆蓋1365-5460 節(jié)點(diǎn)B覆蓋6827-10922 節(jié)點(diǎn)C覆蓋12288-16383 節(jié)點(diǎn)D覆蓋0-1364,5461-6826,10923-12287同樣刪除一個(gè)節(jié)點(diǎn)也是類似,移動(dòng)完成后就可以刪除這個(gè)節(jié)點(diǎn)了。2、Redis Cluster主從模式redis cluster 為了保證數(shù)據(jù)的高可用性,加入了主從模式,一個(gè)主節(jié)點(diǎn)對(duì)應(yīng)一個(gè)或多個(gè)從節(jié)點(diǎn),主節(jié)點(diǎn)提供數(shù)據(jù)存取,從節(jié)點(diǎn)則是從主節(jié)點(diǎn)拉取數(shù)據(jù)備份,當(dāng)這個(gè)主節(jié)點(diǎn)掛掉后,就會(huì)有這個(gè)從節(jié)點(diǎn)選取一個(gè)來充當(dāng)主節(jié)點(diǎn),從而保證集群不會(huì)掛掉。上面那個(gè)例子里, 集群有ABC三個(gè)主節(jié)點(diǎn), 如果這3個(gè)節(jié)點(diǎn)都沒有加入從節(jié)點(diǎn),如果B掛掉了,我們就無法訪問整個(gè)集群了。A和C的slot也無法訪問。所以我們?cè)诩航⒌臅r(shí)候,一定要為每個(gè)主節(jié)點(diǎn)都添加了從節(jié)點(diǎn), 比如像這樣, 集群包含主節(jié)點(diǎn)A、B、C, 以及從節(jié)點(diǎn)A1、B1、C1, 那么即使B掛掉系統(tǒng)也可以繼續(xù)正確工作。B1節(jié)點(diǎn)替代了B節(jié)點(diǎn),所以Redis集群將會(huì)選擇B1節(jié)點(diǎn)作為新的主節(jié)點(diǎn),集群將會(huì)繼續(xù)正確地提供服務(wù)。 當(dāng)B重新開啟后,它就會(huì)變成B1的從節(jié)點(diǎn)。不過需要注意,如果節(jié)點(diǎn)B和B1同時(shí)掛了,Redis集群就無法繼續(xù)正確地提供服務(wù)了。二、redis集群的搭建
集群中至少應(yīng)該有奇數(shù)個(gè)節(jié)點(diǎn),所以至少有三個(gè)節(jié)點(diǎn),每個(gè)節(jié)點(diǎn)至少有一個(gè)備份節(jié)點(diǎn),所以下面使用6節(jié)點(diǎn)(主節(jié)點(diǎn)、備份節(jié)點(diǎn)由redis-cluster集群確定)。下面使用redis-3.2.0安裝,下載地址 1、安裝redis節(jié)點(diǎn)指定端口解壓redis壓縮包,編譯安裝[plain] view plain copy
[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,復(fù)制redis.conf配置文件配置redis的配置文件redis.confdaemonize yes #后臺(tái)啟動(dòng)port 7001 #修改端口號(hào),從7001到7006cluster-enabled yes #開啟cluster,去掉注釋cluster-config-file nodes.confcluster-node-timeout 15000appendonly yes復(fù)制六份,修改對(duì)應(yīng)的端口號(hào)2、安裝redis-trib所需的 ruby腳本復(fù)制redis解壓文件src下的redis-trib.rb文件到redis-cluster目錄[plain] view plain copy
[root@localhost redis-cluster]# cp /usr/andy/redis/redis-3.2.0/src/redis-trib.rb ./ 安裝ruby環(huán)境:[plain] view plain copy
[root@localhost redis-cluster]# yum install ruby [root@localhost redis-cluster]# yum install rubygems 安裝redis-trib.rb運(yùn)行依賴的ruby的包redis-3.2.2.gem,下載[plain] view plain copy
[root@localhost redis-cluster]# gem install redis-3.2.2.gem 3、啟動(dòng)所有的redis節(jié)點(diǎn)可以寫一個(gè)命令腳本start-all.sh[plain] view plain copy
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 .. 設(shè)置權(quán)限啟動(dòng)[plain] view plain copy
[root@localhost redis-cluster]# chmod 777 start-all.sh [root@localhost redis-cluster]# ./start-all.sh 查看redis進(jìn)程啟動(dòng)狀態(tài)[plain] view plain copy
[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個(gè)節(jié)點(diǎn)已經(jīng)啟動(dòng)成功
殺死全部的幾點(diǎn):[plain] view plain copy
[root@localhost redis-cluster]# pkill -9 redis 4、使用redis-trib.rb創(chuàng)建集群[plain] view plain copy
./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 參數(shù)表示為每個(gè)主節(jié)點(diǎn)創(chuàng)建一個(gè)從節(jié)點(diǎn),其他參數(shù)是實(shí)例的地址集合。[plain] view plain copy
[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. 上面顯示創(chuàng)建成功,有3個(gè)主節(jié)點(diǎn),3個(gè)從節(jié)點(diǎn),每個(gè)節(jié)點(diǎn)都是成功連接狀態(tài)。3個(gè)主節(jié)點(diǎn)[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
[plain] view plain copy
[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做測(cè)試的時(shí)間(都在一臺(tái)服務(wù)器時(shí)),ip應(yīng)該使用127.0.0.1,如果使用局域網(wǎng)ip,也會(huì)報(bào)節(jié)點(diǎn)創(chuàng)建失敗。三、redis集群的測(cè)試
1、測(cè)試存取值
[plain] view plain copy
[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> 根據(jù)redis-cluster的key值分配,name應(yīng)該分配到節(jié)點(diǎn)7002[5461-10922]上,上面顯示redis cluster自動(dòng)從7001跳轉(zhuǎn)到了7002節(jié)點(diǎn)。我們可以測(cè)試一下7006從節(jié)點(diǎn)獲取name值[plain] view plain copy
[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的從節(jié)點(diǎn),從上面也是自動(dòng)跳轉(zhuǎn)至7002獲取值,這也是redis cluster的特點(diǎn),它是去中心化,每個(gè)節(jié)點(diǎn)都是對(duì)等的,連接哪個(gè)節(jié)點(diǎn)都可以獲取和設(shè)置數(shù)據(jù)。四、集群節(jié)點(diǎn)選舉
現(xiàn)在模擬將7002節(jié)點(diǎn)掛掉,按照redis-cluster原理會(huì)選舉會(huì)將 7002的從節(jié)點(diǎn)7005選舉為主節(jié)點(diǎn)。[plain] view plain copy
[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節(jié)點(diǎn)[plain] view plain copy
[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節(jié)點(diǎn),而7005有原來的S轉(zhuǎn)換為M節(jié)點(diǎn),代替了原來的7002節(jié)點(diǎn)。我們可以獲取name值:[plain] view plain copy
[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節(jié)點(diǎn)連入,自動(dòng)跳轉(zhuǎn)到7005節(jié)點(diǎn),并且獲取name值。
現(xiàn)在我們將7002節(jié)點(diǎn)恢復(fù),看是否會(huì)自動(dòng)加入集群中以及充當(dāng)?shù)腗還是S節(jié)點(diǎn)。[plain] view plain copy
[root@localhost redis-cluster]# cd redis02 [root@localhost redis02]# ./redis-server redis.conf [root@localhost redis02]# 在check一下7002節(jié)點(diǎn)[plain] view plain copy
[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節(jié)點(diǎn)變成了a5db243087d8bd423b9285fa8513eddee9bb59a6 7005的從節(jié)點(diǎn)。五、集群節(jié)點(diǎn)添加
節(jié)點(diǎn)新增包括新增主節(jié)點(diǎn)、從節(jié)點(diǎn)兩種情況。以下分別做一下測(cè)試:
1、新增主節(jié)點(diǎn)
[plain] view plain copy
[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 啟動(dòng)7007redis服務(wù)[plain] view plain copy
[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已經(jīng)啟動(dòng),現(xiàn)在加入集群中。添加使用redis-trib.rb的add-node命令
[plain] view plain copy
./redis-trib.rb add-node 127.0.0.1:7007 127.0.0.1:7002 add-node是加入集群節(jié)點(diǎn),127.0.0.1:7007為要加入的節(jié)點(diǎn),127.0.0.1:7002 表示加入的集群的一個(gè)節(jié)點(diǎn),用來辨識(shí)是哪個(gè)集群,理論上那個(gè)集群的節(jié)點(diǎn)都可以。執(zhí)行以下add-node[plain] view plain copy
[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加入這個(gè)Cluster,并成為一個(gè)新的節(jié)點(diǎn)。可以check以下7007節(jié)點(diǎn)狀態(tài)[plain] view plain copy
[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個(gè)M節(jié)點(diǎn),3個(gè)S節(jié)點(diǎn),7007成為了M主節(jié)點(diǎn),它沒有附屬的從節(jié)點(diǎn),而且Cluster并未給7007分配哈希卡槽(0 slots)。
可以從客戶端連接集群查看一下,集群節(jié)點(diǎn)的連接情況
[plain] view plain copy
[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在新增節(jié)點(diǎn)時(shí)并未分配卡槽,需要我們手動(dòng)對(duì)集群進(jìn)行重新分片遷移數(shù)據(jù),需要重新分片命令 reshardredis-trib.rb reshard 127.0.0.1:7005這個(gè)命令是用來遷移slot節(jié)點(diǎn)的,后面的127.0.0.1:7005是表示是哪個(gè)集群,端口填[7000-7007]都可以,執(zhí)行結(jié)果如下:[plain] view plain copy
[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個(gè)哈希槽給4個(gè)節(jié)點(diǎn):16384/4 = 4096,我們需要移動(dòng)4096個(gè)槽點(diǎn)到7007上。[plain] view plain copy
[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的節(jié)點(diǎn)id,ee3efb90e5ac0725f15238a64fc60a18a71205d7[plain] view plain copy
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 會(huì)向你詢問重新分片的源節(jié)點(diǎn)(source node),即,要從特點(diǎn)的哪個(gè)節(jié)點(diǎn)中取出 4096 個(gè)哈希槽,還是從全部節(jié)點(diǎn)提取4096個(gè)哈希槽, 并將這些槽移動(dòng)到7007節(jié)點(diǎn)上面。如果我們不打算從特定的節(jié)點(diǎn)上取出指定數(shù)量的哈希槽,那么可以向redis-trib輸入 all,這樣的話, 集群中的所有主節(jié)點(diǎn)都會(huì)成為源節(jié)點(diǎn),redis-trib從各個(gè)源節(jié)點(diǎn)中各取出一部分哈希槽,湊夠4096個(gè),然后移動(dòng)到7007節(jié)點(diǎn)上:[plain] view plain copy
Source node #1:all 然后開始從別的主節(jié)點(diǎn)遷移哈希槽,并且確認(rèn)。[plain] view plain copy
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 確認(rèn)之后,redis-trib就開始執(zhí)行分片操作,將哈希槽一個(gè)一個(gè)從源主節(jié)點(diǎn)移動(dòng)到7007目標(biāo)主節(jié)點(diǎn)。重新分片結(jié)束后我們可以check以下節(jié)點(diǎn)的分配情況。[plain] view plain copy
[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節(jié)點(diǎn)分片的哈希槽片不是連續(xù)的,間隔的移動(dòng)。
[plain] view plain copy
[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]移動(dòng)到7007節(jié)點(diǎn)上,主節(jié)點(diǎn)7007添加成功。2、新增從節(jié)點(diǎn)新增一個(gè)節(jié)點(diǎn)7008節(jié)點(diǎn),使用add-node --slave命令。[plain] view plain copy
[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.confredis-trib增加從節(jié)點(diǎn)的命令為:
[plain] view plain copy
./redis-trib.rb add-node --slave --master-id $[nodeid] 127.0.0.1:7008 127.0.0.1:7000 nodeid為要加到master主節(jié)點(diǎn)的node id,127.0.0.1:7008為新增的從節(jié)點(diǎn),127.0.0.1:7000為集群的一個(gè)節(jié)點(diǎn)(集群的任意節(jié)點(diǎn)都行),用來辨識(shí)是哪個(gè)集群;如果沒有給定那個(gè)主節(jié)點(diǎn)--master-id的話,redis-trib將會(huì)將新增的從節(jié)點(diǎn)隨機(jī)到從節(jié)點(diǎn)較少的主節(jié)點(diǎn)上。現(xiàn)在我們添加一下7008,看是否會(huì)自動(dòng)加到?jīng)]有從節(jié)點(diǎn)的7007主節(jié)點(diǎn)上。[plain] view plain copy
[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]# 可以看到自動(dòng)選擇了127.0.0.1:7007為master主節(jié)點(diǎn),并且添加成功。可以check一下7008:[plain] view plain copy
[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的從節(jié)點(diǎn)。再測(cè)試一下指定主節(jié)點(diǎn)添加從節(jié)點(diǎn),給7007增加7009從節(jié)點(diǎn)。[plain] view plain copy
[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主節(jié)點(diǎn)上[plain] view plain copy
[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]# 顯示從節(jié)點(diǎn)7009節(jié)點(diǎn)添加到7007主節(jié)點(diǎn),可以看一下7007的從節(jié)點(diǎn),如下:[plain] view plain copy
[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個(gè)slave 7008,7009。我們測(cè)試一下7007節(jié)點(diǎn)掛掉,看7008和7009那個(gè)成為主節(jié)點(diǎn)。[plain] view plain copy
[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成了主節(jié)點(diǎn)。重啟7007之后,會(huì)自動(dòng)變成7009的從節(jié)點(diǎn)。[plain] view plain copy
[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]# 驗(yàn)證了之前的測(cè)試。六、節(jié)點(diǎn)的移除
和節(jié)點(diǎn)添加一樣,移除節(jié)點(diǎn)也有移除主節(jié)點(diǎn),從節(jié)點(diǎn)。
1、移除主節(jié)點(diǎn)
[plain] view plain copy
redis-trib del-node 127.0.0.1:7002 ${node-id} 127.0.0.1:7002位集群節(jié)點(diǎn),node-id為要?jiǎng)h除的主節(jié)點(diǎn)。 和添加節(jié)點(diǎn)不同,移除節(jié)點(diǎn)node-id是必需的,測(cè)試刪除7001主節(jié)點(diǎn):[plain] view plain copy
[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已經(jīng)有數(shù)據(jù)了,不能夠被刪除,需要將他的數(shù)據(jù)轉(zhuǎn)移出去,也就是和新增主節(jié)點(diǎn)一樣需重新分片。
[plain] view plain copy
[root@localhost redis-cluster]# ./redis-trib.rb reshard 127.0.0.1:7002執(zhí)行以后會(huì)提示我們移除的大小,因?yàn)?001占用了4096個(gè)槽點(diǎn)
[plain] view plain copy
>>> 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
提示移動(dòng)的node id,填寫7009的node id。
[plain] view plain copy
How many slots do you want to move (from 1 to 16384)? 4096 What is the receiving node ID?需要移動(dòng)到全部主節(jié)點(diǎn)上還是單個(gè)主節(jié)點(diǎn)
[plain] view plain copy
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個(gè)槽點(diǎn)移動(dòng)到7009上,填寫7001的node id :dd19221c404fb2fc4da37229de56bab755c76f2b
[plain] view plain copy
Source node #1:dd19221c404fb2fc4da37229de56bab755c76f2b Source node #2:done Do you want to proceed with the proposed reshard plan (yes/no)? yes確認(rèn)之后會(huì)一個(gè)一個(gè)將7001的卡槽移到到7009上。
[plain] view plain copy
[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個(gè)卡槽,而7009有8192個(gè)卡槽。在執(zhí)行移除操作[plain] view plain copy
[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]# 已經(jīng)刪除了7001節(jié)點(diǎn)。[plain] view plain copy
[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已經(jīng)連接不了;而7001的從節(jié)點(diǎn)7004自動(dòng)分配到了7009主節(jié)點(diǎn)中,7009現(xiàn)在3個(gè)從節(jié)點(diǎn)。
2、移除從節(jié)點(diǎn)
比如刪除7009的7008節(jié)點(diǎn):[plain] view plain copy
[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]# 刪除從節(jié)點(diǎn)比較方便,現(xiàn)在redis-cluster中有3個(gè)主節(jié)點(diǎn),4個(gè)從節(jié)點(diǎn),如下:[plain] view plain copy
[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,測(cè)試到這兒吧。
轉(zhuǎn)載于:https://blog.51cto.com/9280078/2129919
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的Redis Cluster集群的搭建与实践[转]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Github、Jekyll 搭建及优化静
- 下一篇: 刚刚,Redis公布了5.0版本12项新