欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

redis高可用集群介紹

這篇文章為大家?guī)碛嘘P(guān)redis高可用集群的詳細(xì)介紹。大部分知識(shí)點(diǎn)都是大家經(jīng)常用到的,為此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。

為樂清等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及樂清網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站建設(shè)、樂清網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

1,前言

Redis是我們目前最大規(guī)模使用的緩存中間件,由于它強(qiáng)大高效而又便捷的功能,得到廣泛的使用。
Redis在2015年發(fā)布了3.0.0,官方支持redis cluster。至此結(jié)束了redis沒有集群的時(shí)代,之前我們用的redis cluster多的是twitter發(fā)布的Twemproxy還有就是豌豆莢開發(fā)的codis。本文將進(jìn)行理解和實(shí)踐redis cluster。下面,我盡量用通熟易懂的方式來闡述。

2,redis Cluster設(shè)計(jì)要點(diǎn)

redis cluster在設(shè)計(jì)的時(shí)候,就考慮到了去中心化,去中間件,也就是說,在集群中的每個(gè)節(jié)點(diǎn)都是平等的關(guān)系,都是對(duì)等的,每個(gè)節(jié)點(diǎn)都保存各自的數(shù)據(jù)和整個(gè)集群的狀態(tài)。每個(gè)節(jié)點(diǎn)都和其他所有節(jié)點(diǎn)連接,而且這些連接保持活躍,這樣就保證了我們只需要連接集群中的任意一個(gè)節(jié)點(diǎn),就可以獲取到其他節(jié)點(diǎn)的數(shù)據(jù)。

3,redis集群原理

我們知道集群中的每個(gè)節(jié)點(diǎn)都是對(duì)等的關(guān)系并且都保存各自的數(shù)據(jù),那么redis是如何合理分配這些節(jié)點(diǎn)和數(shù)據(jù)的呢?
Redis Cluster沒有使用傳統(tǒng)的一致性哈希來分配數(shù)據(jù),而是采用另外一種哈希槽(hash slot)的方式來分配的。
redis cluster默認(rèn)分配了16384個(gè)slot,當(dāng)我們set一個(gè)key時(shí),會(huì)采用CRC16算法來取模得到所屬的slot,然后將這個(gè)key分到哈希槽區(qū)間的節(jié)點(diǎn)上,具體算法就是:CRC16(key) % 16384。
需要注意的是:Redis集群至少需要3個(gè)節(jié)點(diǎn),因?yàn)橥镀比蒎e(cuò)機(jī)制要求超過半數(shù)節(jié)點(diǎn)認(rèn)為某個(gè)節(jié)點(diǎn)掛了該節(jié)點(diǎn)才是掛了,所以2個(gè)節(jié)點(diǎn)無法構(gòu)成集群。

所以,我們假設(shè)現(xiàn)在有3個(gè)節(jié)點(diǎn)已經(jīng)部署成redis cluster,分別是: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-5461; 節(jié)點(diǎn)B覆蓋 5462-10922; 節(jié)點(diǎn)C覆蓋 10923-16383;
那么,現(xiàn)在我想設(shè)置一個(gè)key,比如叫my_name:set my_name linux
按照redis cluster的哈希槽算法:CRC16('my_name')%16384 = 2412。 那么就會(huì)把這個(gè)key的存儲(chǔ)分配到A上了。
同樣,當(dāng)我連接(A,B,C)任何一個(gè)節(jié)點(diǎn)想獲取my_name這個(gè)key時(shí),也會(huì)這樣的算法,然后內(nèi)部跳轉(zhuǎn)到A節(jié)點(diǎn)上獲取數(shù)據(jù)。

這種哈希槽分配方式的好處就是很清晰,比如我想新增一個(gè)節(jié)點(diǎn)ID,redis cluster的這種做法是從各個(gè)節(jié)點(diǎn)的前面各拿取一部分slot到D上。大致就會(huì)變成這樣:(我會(huì)在接下來的實(shí)踐中實(shí)驗(yàn))
節(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)了。
所以redis cluster 就是這樣一個(gè)形狀:
redis高可用集群介紹

4,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ù)正確工作。因?yàn)锽1節(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),而不會(huì)重新變成主節(jié)點(diǎn)。
如果節(jié)點(diǎn)B和B1同時(shí)掛了,Redis集群就無法繼續(xù)正常地提供服務(wù)了,一般情況下,是不會(huì)也不允許兩節(jié)點(diǎn)同時(shí)掛掉。
流程下圖所示:
redis高可用集群介紹

搭建redis高可用集群

依據(jù)redis cluster內(nèi)部故障轉(zhuǎn)移實(shí)現(xiàn)原理,reids集群至少需要3個(gè)節(jié)點(diǎn),并且要保證集群的高可用,需要每個(gè)節(jié)點(diǎn)都有從節(jié)點(diǎn),因此搭建redis集群至少需要6臺(tái)服務(wù)器,三主三從。
條件有限,并且是測(cè)試環(huán)境,所以我們?cè)趦膳_(tái)機(jī)器上創(chuàng)建一個(gè)偽集群,通過不同的TCP端口啟動(dòng)多個(gè)redis實(shí)例,組成集群,當(dāng)然實(shí)際生產(chǎn)環(huán)境的Redis集群搭建和這里是一樣的。

目前redis cluster 的搭建有兩種方式:

1,手動(dòng)方式搭建,即手動(dòng)執(zhí)行cluster命令,一步步完成搭建流程。
2,自動(dòng)方式搭建,即使用官方提供的集群管理工具快速搭建。

兩種方式原理一樣,自動(dòng)搭建方式只是將手動(dòng)搭建方式中需要執(zhí)行的redis命令封裝到了可執(zhí)行程序。生產(chǎn)環(huán)境中推薦使用自動(dòng)方式搭建,簡(jiǎn)單快捷,不易出錯(cuò);本文實(shí)戰(zhàn)演示兩種方式都會(huì)提及。
環(huán)境描述:

主機(jī)A:172.16.1.100(CentOS 7.3),啟動(dòng)三個(gè)實(shí)例7000,7001,7002;全部為主
主機(jī)B:172.16.1.110(CentOS 7.3),啟動(dòng)三個(gè)實(shí)例8000,8001,8002;全部為從

一,手動(dòng)方式搭建

1,安裝redis

A主機(jī):
[root@redis01-server ~]# tar zxf redis-4.0.14.tar.gz 
[root@redis01-server ~]# mv redis-4.0.14 /usr/local/redis
[root@redis01-server ~]# cd /usr/local/redis/
[root@redis01-server redis]# make && make install
#安裝完成,修改配置文件:
[root@redis01-server ~]# vim /usr/local/redis/redis.conf 
  69 bind 172.16.1.100  #設(shè)置為當(dāng)前redis主機(jī)的ip地址
  92 port 7000  #設(shè)置redis的監(jiān)聽端口
 136 daemonize yes   #以守護(hù)進(jìn)程運(yùn)行redis實(shí)例
 814 cluster-enabled yes   #啟動(dòng)集群模式
 822 cluster-config-file nodes-7000.conf   #設(shè)置當(dāng)前節(jié)點(diǎn)集群配置文件路徑
 828 cluster-node-timeout 5000  #設(shè)置當(dāng)前連接超時(shí)秒數(shù)
 672 appendonly yes   #開啟AOF持久化模式
 676 appendfilename "appendonly-7000.aof"  #保存數(shù)據(jù)的AOF文件名稱
B主機(jī):
[root@redis02-server ~]# tar zxf redis-4.0.14.tar.gz 
[root@redis02-server ~]# mv redis-4.0.14 /usr/local/redis
[root@redis02-server ~]# cd /usr/local/redis
[root@redis02-server redis]# make && make install
[root@redis02-server ~]# vim /usr/local/redis/redis.conf
bind 172.16.1.110
port 8000   
daemonize yes
cluster-enabled yes
cluster-config-file nodes-8000.conf
cluster-node-timeout 5000
appendonly yes
appendfilename "appendonly-8000.aof"

2,根據(jù)上述的規(guī)劃,創(chuàng)建各個(gè)節(jié)點(diǎn)啟動(dòng)配置文件的存放目錄

A主機(jī):
[root@redis01-server ~]# mkdir /usr/local/redis-cluster
[root@redis01-server ~]# cd /usr/local/redis-cluster/
[root@redis01-server redis-cluster]# mkdir {7000,7001,7002}
B主機(jī):
[root@redis02-server ~]# mkdir /usr/local/redis-cluster
[root@redis02-server ~]# cd /usr/local/redis-cluster/
[root@redis02-server redis-cluster]# mkdir  {8000,8001,8002}

3,將主配文件分別拷貝到相對(duì)應(yīng)的目錄下

A主機(jī):
[root@redis01-server ~]# cp /usr/local/redis/redis.conf  /usr/local/redis-cluster/7000/
[root@redis01-server ~]# cp /usr/local/redis/redis.conf  /usr/local/redis-cluster/7001/
[root@redis01-server ~]# cp /usr/local/redis/redis.conf  /usr/local/redis-cluster/7002/
B主機(jī):
[root@redis02-server ~]# cp /usr/local/redis/redis.conf  /usr/local/redis-cluster/8000/
[root@redis02-server ~]# cp /usr/local/redis/redis.conf  /usr/local/redis-cluster/8001/
[root@redis02-server ~]# cp /usr/local/redis/redis.conf  /usr/local/redis-cluster/8002/
#修改各自的監(jiān)聽TCP端口號(hào):
A主機(jī):
[root@redis01-server ~]# sed -i "s/7000/7001/g" /usr/local/redis-cluster/7001/redis.conf 
[root@redis01-server ~]# sed -i "s/7000/7002/g" /usr/local/redis-cluster/7002/redis.conf 
B主機(jī):
[root@redis02-server ~]# sed -i "s/8000/8001/g" /usr/local/redis-cluster/8001/redis.conf 
[root@redis02-server ~]# sed -i "s/8000/8002/g" /usr/local/redis-cluster/8002/redis.conf 

4,啟動(dòng)reids服務(wù)

A主機(jī):
[root@redis01-server ~]# ln -s /usr/local/bin/redis-server /usr/local/sbin/
[root@redis01-server ~]# redis-server  /usr/local/redis-cluster/7000/redis.conf 
[root@redis01-server ~]# redis-server  /usr/local/redis-cluster/7001/redis.conf 
[root@redis01-server ~]# redis-server  /usr/local/redis-cluster/7002/redis.conf 
#確保各節(jié)點(diǎn)服務(wù)正常運(yùn)行:
[root@redis01-server ~]# ps -ef | grep redis
root      19595      1  0 04:55 ?        00:00:00 redis-server 172.16.1.100:7000 [cluster]
root      19602      1  0 04:56 ?        00:00:00 redis-server 172.16.1.100:7001 [cluster]
root      19607      1  0 04:56 ?        00:00:00 redis-server 172.16.1.100:7002 [cluster]
root      19612   2420  0 04:58 pts/0    00:00:00 grep --color=auto redis
B主機(jī):
[root@redis02-server ~]# ln -s /usr/local/bin/redis-server  /usr/local/sbin/
[root@redis02-server ~]# redis-server /usr/local/redis-cluster/8000/redis.conf 
[root@redis02-server ~]# redis-server /usr/local/redis-cluster/8001/redis.conf 
[root@redis02-server ~]# redis-server /usr/local/redis-cluster/8002/redis.conf
[root@redis02-server ~]# ps -ef | grep redis
root      18485      1  0 00:17 ?        00:00:00 redis-server 172.16.1.110:8000 [cluster]
root      18490      1  0 00:17 ?        00:00:00 redis-server 172.16.1.110:8001 [cluster]
root      18495      1  0 00:17 ?        00:00:00 redis-server 172.16.1.110:8002 [cluster]
root      18501   1421  0 00:19 pts/0    00:00:00 grep --color=auto redis

5,節(jié)點(diǎn)握手
雖然上面6各節(jié)點(diǎn)都啟用集群支持,但默認(rèn)情況下它們是不相互信任或者說沒有聯(lián)系的。節(jié)點(diǎn)握手就是在各個(gè)節(jié)點(diǎn)之間創(chuàng)建鏈接(每個(gè)節(jié)點(diǎn)與其他節(jié)點(diǎn)相連),形成一個(gè)完整的網(wǎng)絡(luò),即集群。
節(jié)點(diǎn)握手的命令如下:

cluster meet ip port

我們創(chuàng)建的6個(gè)節(jié)點(diǎn)可以通過redis-cli 連接到A節(jié)點(diǎn)執(zhí)行如下五組命令完成握手:
注意:需要關(guān)閉集群中各臺(tái)主機(jī)的防火墻:systemctl stop firewalld ,否則無法完成握手

[root@redis01-server ~]# redis-cli  -h 172.16.1.100 -p 7000
172.16.1.100:7000> cluster meet 172.16.1.100 7001
OK
172.16.1.100:7000> cluster meet 172.16.1.100 7002
OK
172.16.1.100:7000> cluster meet 172.16.1.110 8000
OK
172.16.1.100:7000> cluster meet 172.16.1.110 8001
OK
172.16.1.100:7000> cluster meet 172.16.1.110 8002
OK
#查看握手是否正常:
172.16.1.100:7000> cluster nodes
060a11f6985df66e4b9cf596355bbe334f843587 172.16.1.100:7001@17001 master - 0 1584155029000 1 connected
2fb26d79f703f9fbd8841e4ee93ea88f7df5dad9 172.16.1.110:8002@18002 master - 0 1584155029000 5 connected
6d3ac8cf0dc3c8400d2df8d0559fbe8bdce0c34d 172.16.1.110:8000@18000 master - 0 1584155029243 3 connected
cc3b16e067bf1ce9978c13870f0e1d538102a733 172.16.1.110:8001@18001 master - 0 1584155030000 0 connected
0f74b9e2d07e159fdc0fc1edffd3d0b305adc2fd 172.16.1.100:7000@17000 myself,master - 0 1584155028000 2 connected
c0fefd1442b3fa4e41eb6fba5073dcc1427ca812 172.16.1.100:7002@17002 master - 0 1584155030249 4 connected

可以看到,集群中所有節(jié)點(diǎn)都已經(jīng)建立鏈接,自此,節(jié)點(diǎn)握手完成。
#雖然節(jié)點(diǎn)已經(jīng)建立鏈接,但此時(shí)redis集群還并沒有處于上線狀態(tài),執(zhí)行cluster info命令來查看目前集群的運(yùn)行狀態(tài):

172.16.1.100:7000> cluster info
cluster_state:fail    #表示當(dāng)前集群處于下線狀態(tài)
cluster_slots_assigned:0     #為0 表示目前所有槽沒有被分配到節(jié)點(diǎn)上
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:0
cluster_current_epoch:5
cluster_my_epoch:2
cluster_stats_messages_ping_sent:612
cluster_stats_messages_pong_sent:627
cluster_stats_messages_meet_sent:10
cluster_stats_messages_sent:1249

6,分配槽位
只有給集群中所有主節(jié)點(diǎn)分配好槽位(slot),集群才能正常上線,分配槽位的命令如下:
cluster addslots slot [slot ...]
#根據(jù)預(yù)先規(guī)劃,需要使用cluster addslots 命令手動(dòng)將16384個(gè)哈希槽大致均等分配給主節(jié)點(diǎn)A,B,C。

[root@redis01-server ~]# redis-cli -h 172.16.1.100 -p 7000 cluster addslots {0..5461}
OK
[root@redis01-server ~]# redis-cli -h 172.16.1.100 -p 7001 cluster addslots {5462..10922}
OK
[root@redis01-server ~]# redis-cli -h 172.16.1.100 -p 7002 cluster addslots {10923..16383}
OK

槽位分配完成后,可以再次查看目前集群的狀態(tài):

[root@redis01-server ~]# redis-cli -h 172.16.1.100 -p 7000 cluster info
cluster_state:ok  
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:5
cluster_my_epoch:2
cluster_stats_messages_ping_sent:2015
cluster_stats_messages_pong_sent:2308
cluster_stats_messages_meet_sent:10
cluster_stats_messages_sent:4333
cluster_stats_messages_ping_received:2308
cluster_stats_messages_pong_received:2007
cluster_stats_messages_received:4315

cluster_state:ok 證明redis集群成功上線。

#如需刪除分配槽,可以執(zhí)行cluster delslots 命令,例如:
[root@redis01-server ~]# redis-cli -h 172.16.1.100 -p 7000 cluster delslots {10923..16383}

#查看槽位分配情況:
[root@redis01-server ~]# redis-cli -h 172.16.1.100 -p 7000 cluster nodes
060a11f6985df66e4b9cf596355bbe334f843587 172.16.1.100:7001@17001 master - 0 1584156388000 1 connected 5462-10922
2fb26d79f703f9fbd8841e4ee93ea88f7df5dad9 172.16.1.110:8002@18002 master - 0 1584156387060 5 connected
6d3ac8cf0dc3c8400d2df8d0559fbe8bdce0c34d 172.16.1.110:8000@18000 master - 0 1584156386056 3 connected
cc3b16e067bf1ce9978c13870f0e1d538102a733 172.16.1.110:8001@18001 master - 0 1584156387000 0 connected
0f74b9e2d07e159fdc0fc1edffd3d0b305adc2fd 172.16.1.100:7000@17000 myself,master - 0 1584156387000 2 connected 0-5461
c0fefd1442b3fa4e41eb6fba5073dcc1427ca812 172.16.1.100:7002@17002 master - 0 1584156386000 4 connected 10923-16383

可以看到三個(gè)主節(jié)點(diǎn)槽位已完成分配,但是還有三個(gè)從節(jié)點(diǎn)沒有使用,如果此時(shí)有一個(gè)主節(jié)點(diǎn)故障,那么整個(gè)集群也就掛了,所以我們需要為從節(jié)點(diǎn)配置主節(jié)點(diǎn),實(shí)現(xiàn)高可用。

7,主從復(fù)制

集群復(fù)制命令如下:
cluster replicate node-id

1)連接集群中任意一個(gè)節(jié)點(diǎn),獲得所有主節(jié)點(diǎn)的node-id

[root@redis01-server ~]# redis-cli -h 172.16.1.110 -p  8000 cluster nodes
2fb26d79f703f9fbd8841e4ee93ea88f7df5dad9 172.16.1.110:8002@18002 master - 0 1584157179543 5 connected
c0fefd1442b3fa4e41eb6fba5073dcc1427ca812 172.16.1.100:7002@17002 master - 0 1584157179946 4 connected 10923-16383
6d3ac8cf0dc3c8400d2df8d0559fbe8bdce0c34d 172.16.1.110:8000@18000 myself,master - 0 1584157179000 3 connected
060a11f6985df66e4b9cf596355bbe334f843587 172.16.1.100:7001@17001 master - 0 1584157180952 1 connected 5462-10922
cc3b16e067bf1ce9978c13870f0e1d538102a733 172.16.1.110:8001@18001 master - 0 1584157180549 0 connected
0f74b9e2d07e159fdc0fc1edffd3d0b305adc2fd 172.16.1.100:7000@17000 master - 0 1584157179544 2 connected 0-5461

2)執(zhí)行如下三組命令分別為從節(jié)點(diǎn)指定其主節(jié)點(diǎn),使集群可以自動(dòng)完成主從復(fù)制

[root@redis02-server ~]# redis-cli -h 172.16.1.110 -p 8000 cluster replicate 0f74b9e2d07e159fdc0fc1edffd3d0b305adc2fd
OK
[root@redis02-server ~]# redis-cli -h 172.16.1.110 -p 8001 cluster replicate 060a11f6985df66e4b9cf596355bbe334f843587
OK
[root@redis02-server ~]# redis-cli -h 172.16.1.110 -p 8002 cluster replicate c0fefd1442b3fa4e41eb6fba5073dcc1427ca812
OK

3)查看集群中各個(gè)節(jié)點(diǎn)的復(fù)制狀態(tài)信息:

[root@redis02-server ~]# redis-cli -h 172.16.1.110 -p 8000 cluster nodes
2fb26d79f703f9fbd8841e4ee93ea88f7df5dad9 172.16.1.110:8002@18002 slave c0fefd1442b3fa4e41eb6fba5073dcc1427ca812 0 1584157699631 5 connected
c0fefd1442b3fa4e41eb6fba5073dcc1427ca812 172.16.1.100:7002@17002 master - 0 1584157700437 4 connected 10923-16383
6d3ac8cf0dc3c8400d2df8d0559fbe8bdce0c34d 172.16.1.110:8000@18000 myself,slave 0f74b9e2d07e159fdc0fc1edffd3d0b305adc2fd 0 1584157699000 3 connected
060a11f6985df66e4b9cf596355bbe334f843587 172.16.1.100:7001@17001 master - 0 1584157701442 1 connected 5462-10922
cc3b16e067bf1ce9978c13870f0e1d538102a733 172.16.1.110:8001@18001 slave 060a11f6985df66e4b9cf596355bbe334f843587 0 1584157699932 1 connected
0f74b9e2d07e159fdc0fc1edffd3d0b305adc2fd 172.16.1.100:7000@17000 master - 0 1584157700000 2 connected 0-5461

可以看到所有從節(jié)點(diǎn)都作為對(duì)應(yīng)主節(jié)點(diǎn)的備份節(jié)點(diǎn),至此,已經(jīng)成功以手動(dòng)方式搭建一個(gè)redis集群。
總結(jié)手動(dòng)搭建redis集群的關(guān)鍵步驟:

1,在各節(jié)點(diǎn)安裝redis
2,修改配置文件,開啟集群模式
3,啟動(dòng)各節(jié)點(diǎn)redis服務(wù)
4,節(jié)點(diǎn)握手
5,為主節(jié)點(diǎn)分配槽位
6,主從節(jié)點(diǎn)建立復(fù)制關(guān)系

推薦博文:
redis的 rdb 和 aof 持久化的區(qū)別:https://www.cnblogs.com/shizhengwen/p/9283973.html

二,自動(dòng)方式搭建

Redis 3.0版本之后官方發(fā)布了一個(gè)集群管理工具 redis-trib.rb,集成在Redis源碼包的src目錄下。其封裝了redis提供的集群命令,使用簡(jiǎn)單,便捷。
環(huán)境描述:

主機(jī)A:172.16.1.100(CentOS 7.3),啟動(dòng)三個(gè)實(shí)例7000,7001,7002;
主機(jī)B:172.16.1.110(CentOS 7.3),啟動(dòng)三個(gè)實(shí)例8000,8001,8002;

1,執(zhí)行上邊”手動(dòng)搭建redis“的步驟1-4(到啟動(dòng)redis即可(確保redis服務(wù)正常運(yùn)行),關(guān)閉防火墻。

2,集群管理工具搭建
redis-trib.rb 是 Redis 作者使用 Ruby 語言開發(fā)的,故使用該工具之前還需要先在機(jī)器上安裝 Ruby 環(huán)境。
注意的是:從Redis 5.0 版本開始便把這個(gè)工具集成到 redis-cli 中,以--cluster參數(shù)提供使用,其中create命令可以用來創(chuàng)建集群。

1)安裝Ruby環(huán)境以及其他依賴項(xiàng)(兩臺(tái)主機(jī))
[root@redis-01 ~]# yum -y install ruby ruby-devel rubygems rpm-build openssl openssl-devel
#確認(rèn)安裝版本:
[root@redis-01 ~]# ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]

2)使用redis-trib.rb 腳本搭建集群

[root@redis-01 ~]# ln  -s /usr/local/redis/src/redis-trib.rb /usr/local/sbin/
[root@redis-01 ~]# redis-trib.rb create --replicas 1 172.16.1.100:7000 172.16.1.100:7001 172.16.1.100:7002 172.16.1.110:8000 172.16.1.110:8001 172.16.1.110:8002
#這里使用create命令,--replicas 1 參數(shù)表示為每個(gè)主節(jié)點(diǎn)創(chuàng)建一個(gè)從節(jié)點(diǎn)(隨機(jī)分配),其他參數(shù)是實(shí)例的地址集合
/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)
    from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/local/sbin/redis-trib.rb:25:in `<main>'

上面報(bào)錯(cuò)是需要redis的gem包來安裝ruby和redis的接口,安裝gem包,網(wǎng)址: https://rubygems.org/gems/redis/  選擇對(duì)應(yīng)的版本下載,這里選擇3.3.0版本:

[root@redis-01 ~]# gem install -l redis-3.3.0.gem 
Successfully installed redis-3.3.0
Parsing documentation for redis-3.3.0
Installing ri documentation for redis-3.3.0
1 gem installed

#重新創(chuàng)建集群

[root@redis-01 ~]# redis-trib.rb create --replicas 1 172.16.1.100:7000 172.16.1.100:7001 172.16.1.100:7002 172.16.1.110:8000 172.16.1.110:8001 172.16.1.110:8002
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.16.1.100:7000
172.16.1.110:8000
172.16.1.100:7001
Adding replica 172.16.1.110:8002 to 172.16.1.100:7000
Adding replica 172.16.1.100:7002 to 172.16.1.110:8000
Adding replica 172.16.1.110:8001 to 172.16.1.100:7001
M: 8613a457f8009aaf784df0ac6d7039034b16b6a6 172.16.1.100:7000
   slots:0-5460 (5461 slots) master
M: 5721b77733b1809449c6fc5806f38f1cacb1de8c 172.16.1.100:7001
   slots:10923-16383 (5461 slots) master
S: d161ab43746405c2b517e3ffc98321956431191c 172.16.1.100:7002
   replicates 4debd0b5743826d203d1af777824eb1b83105d21
M: 4debd0b5743826d203d1af777824eb1b83105d21 172.16.1.110:8000
   slots:5461-10922 (5462 slots) master
S: 948421116dd1859002c78a2df0b9845bdc7db631 172.16.1.110:8001
   replicates 5721b77733b1809449c6fc5806f38f1cacb1de8c
S: 3cc268dfbb918a99159900643b318ec87ba03ad9 172.16.1.110:8002
   replicates 8613a457f8009aaf784df0ac6d7039034b16b6a6
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 172.16.1.100:7000)
M: 8613a457f8009aaf784df0ac6d7039034b16b6a6 172.16.1.100:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 3cc268dfbb918a99159900643b318ec87ba03ad9 172.16.1.110:8002
   slots: (0 slots) slave
   replicates 8613a457f8009aaf784df0ac6d7039034b16b6a6
S: 948421116dd1859002c78a2df0b9845bdc7db631 172.16.1.110:8001
   slots: (0 slots) slave
   replicates 5721b77733b1809449c6fc5806f38f1cacb1de8c
M: 4debd0b5743826d203d1af777824eb1b83105d21 172.16.1.110:8000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: d161ab43746405c2b517e3ffc98321956431191c 172.16.1.100:7002
   slots: (0 slots) slave
   replicates 4debd0b5743826d203d1af777824eb1b83105d21
M: 5721b77733b1809449c6fc5806f38f1cacb1de8c 172.16.1.100: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.

redis-trib會(huì)提示你做了什么配置,輸入yes接受,集群就被配置和加入了,意思是實(shí)例會(huì)經(jīng)過互相交流后啟動(dòng)。
至此,集群可以說是搭建完成了,一條命令解決,可以說是非常方便。

3,測(cè)試集群

1)測(cè)試集群的狀態(tài):

[root@redis-01 ~]# redis-trib.rb check 172.16.1.100:7000 
>>> Performing Cluster Check (using node 172.16.1.100:7000)
M: 8613a457f8009aaf784df0ac6d7039034b16b6a6 172.16.1.100:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 3cc268dfbb918a99159900643b318ec87ba03ad9 172.16.1.110:8002
   slots: (0 slots) slave
   replicates 8613a457f8009aaf784df0ac6d7039034b16b6a6
S: 948421116dd1859002c78a2df0b9845bdc7db631 172.16.1.110:8001
   slots: (0 slots) slave
   replicates 5721b77733b1809449c6fc5806f38f1cacb1de8c
M: 4debd0b5743826d203d1af777824eb1b83105d21 172.16.1.110:8000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: d161ab43746405c2b517e3ffc98321956431191c 172.16.1.100:7002
   slots: (0 slots) slave
   replicates 4debd0b5743826d203d1af777824eb1b83105d21
M: 5721b77733b1809449c6fc5806f38f1cacb1de8c 172.16.1.100:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)

可以看到有3個(gè)主節(jié)點(diǎn)(M),分別是7000,8000,7001,3個(gè)從節(jié)點(diǎn)(S),分別是7002,8001,8002。每個(gè)節(jié)點(diǎn)都是成功的連接狀態(tài)。

2)測(cè)試連接集群
集群搭建成功了;按照redis cluster的特點(diǎn),它是去中心化,每個(gè)節(jié)點(diǎn)都是對(duì)等的,所以,你連接哪個(gè)節(jié)點(diǎn)都可以獲取和設(shè)置數(shù)據(jù),接下來進(jìn)行測(cè)試:

[root@redis-01 ~]# redis-cli -h 172.16.1.100 -p 7000 -c  //集群模式需要加上-c參數(shù)
172.16.1.100:7000> set my_name linux  //設(shè)置一個(gè)值
-> Redirected to slot [12803] located at 172.16.1.100:7001 
//前面有說到,分配key的時(shí)候,它會(huì)使用CRC16('my_name')%16384算法來計(jì)算將這個(gè)key放到哪個(gè)節(jié)點(diǎn),這里分配到了12803 ,所以slot就分配到了 7001(范圍:10923-16383)這個(gè)節(jié)點(diǎn)上
OK
172.16.1.100:7001> get my_name    //獲取該數(shù)據(jù)值
"linux"

redis cluster 采用的方式很直接,創(chuàng)建完key后,它直接跳轉(zhuǎn)到7001節(jié)點(diǎn)了,而不是還在自身的7000節(jié)點(diǎn),現(xiàn)在我們連接8002這個(gè)從節(jié)點(diǎn):

[root@redis-01 ~]# redis-cli -h 172.16.1.110 -p 8002 -c 
172.16.1.110:8002> get my_name
-> Redirected to slot [12803] located at 172.16.1.100:7001
"linux"
//同樣是獲取 key(my_name)的值,它同樣也是跳轉(zhuǎn)到了7001上,并返回該數(shù)據(jù)值

3)測(cè)試集群的高可用
當(dāng)前我的redis集群有3個(gè)主節(jié)點(diǎn)(7000,8000,7001)提供數(shù)據(jù)存儲(chǔ)和讀取,3個(gè)從節(jié)點(diǎn)(7002,8001,8002)負(fù)責(zé)把主節(jié)點(diǎn)的數(shù)據(jù)同步到自己的節(jié)點(diǎn)上來,所以我們來看一下從節(jié)點(diǎn)的appendonly.aof的內(nèi)容(因?yàn)閯偛艅?chuàng)建的值分配給了7001節(jié)點(diǎn),而7001主節(jié)點(diǎn)對(duì)應(yīng)的從節(jié)點(diǎn)是8001,所以我們查看8001的aof文件)

[root@redis-02 ~]# cat appendonly-8001.aof 
*2
$6
SELECT
$1
0
*3
$3
set
$7
my_name
$5
linux

可以看到的確是從主節(jié)點(diǎn)同步過來的數(shù)據(jù)。
注意:你的redis在哪個(gè)路徑下啟動(dòng),dump.rdb 文件或者appendonly.aof文件就會(huì)產(chǎn)生在啟動(dòng)所在的目錄,如果想要自定義路徑可以修改配置文件:
263 dir ./      #把相對(duì)路徑修改為絕對(duì)路徑

#下面,我們模擬其中一臺(tái)master主服務(wù)器掛掉:

[root@redis-01 ~]# ps -ef | grep redis
root       5598      1  0 01:02 ?        00:00:06 redis-server 172.16.1.100:7000 [cluster]
root       5603      1  0 01:02 ?        00:00:06 redis-server 172.16.1.100:7001 [cluster]
root       5608      1  0 01:02 ?        00:00:06 redis-server 172.16.1.100:7002 [cluster]
root      19735   2242  0 03:32 pts/0    00:00:00 grep --color=auto redis
[root@redis-01 ~]# kill 5598  

#測(cè)試集群的狀態(tài):

[root@redis-01 ~]# redis-trib.rb check 172.16.1.100:7000
[ERR] Sorry, can't connect to node 172.16.1.100:7000
[root@redis-01 ~]# redis-trib.rb check 172.16.1.100:7001
>>> Performing Cluster Check (using node 172.16.1.100:7001)
M: 5721b77733b1809449c6fc5806f38f1cacb1de8c 172.16.1.100:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: d161ab43746405c2b517e3ffc98321956431191c 172.16.1.100:7002
   slots: (0 slots) slave
   replicates 4debd0b5743826d203d1af777824eb1b83105d21
S: 948421116dd1859002c78a2df0b9845bdc7db631 172.16.1.110:8001
   slots: (0 slots) slave
   replicates 5721b77733b1809449c6fc5806f38f1cacb1de8c
M: 4debd0b5743826d203d1af777824eb1b83105d21 172.16.1.110:8000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 3cc268dfbb918a99159900643b318ec87ba03ad9 172.16.1.110:8002
   slots:0-5460 (5461 slots) master
   0 additional replica(s)

從上面的結(jié)果可以看到,當(dāng)7000主節(jié)點(diǎn)掛掉了,那么這個(gè)時(shí)候,7000的從節(jié)點(diǎn)只有8002一個(gè),所以8002就會(huì)被選舉成master節(jié)點(diǎn)了。并且原來7000節(jié)點(diǎn)上的數(shù)據(jù)不會(huì)丟失,而是會(huì)轉(zhuǎn)移到了8002節(jié)點(diǎn)上,當(dāng)用戶再次獲取數(shù)據(jù)時(shí),則是從8002上面獲取了。

既然7000節(jié)點(diǎn)服務(wù)器因?yàn)槟承┰蝈礄C(jī)了,但是當(dāng)我們把故障解決后,重新將7000節(jié)點(diǎn)加入集群中,那么7000節(jié)點(diǎn)會(huì)在集群中充當(dāng)什么角色呢?

[root@redis-01 ~]# redis-server  /usr/local/redis-cluster/7000/redis.conf 
[root@redis-01 ~]# ps -ef | grep redis
root       5603      1  0 01:02 ?        00:00:08 redis-server 172.16.1.100:7001 [cluster]
root       5608      1  0 01:02 ?        00:00:08 redis-server 172.16.1.100:7002 [cluster]
root      19771      1  0 03:50 ?        00:00:00 redis-server 172.16.1.100:7000 [cluster]
root      19789   2242  0 03:51 pts/0    00:00:00 grep --color=auto redis
#查看集群的狀態(tài):
[root@redis-01 ~]# redis-trib.rb  check 172.16.1.100:7001
>>> Performing Cluster Check (using node 172.16.1.100:7001)
M: 5721b77733b1809449c6fc5806f38f1cacb1de8c 172.16.1.100:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: d161ab43746405c2b517e3ffc98321956431191c 172.16.1.100:7002
   slots: (0 slots) slave
   replicates 4debd0b5743826d203d1af777824eb1b83105d21
S: 948421116dd1859002c78a2df0b9845bdc7db631 172.16.1.110:8001
   slots: (0 slots) slave
   replicates 5721b77733b1809449c6fc5806f38f1cacb1de8c
M: 4debd0b5743826d203d1af777824eb1b83105d21 172.16.1.110:8000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 3cc268dfbb918a99159900643b318ec87ba03ad9 172.16.1.110:8002
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 8613a457f8009aaf784df0ac6d7039034b16b6a6 172.16.1.100:7000
   slots: (0 slots) slave
   replicates 3cc268dfbb918a99159900643b318ec87ba03ad9

可以看到7000節(jié)點(diǎn)已經(jīng)成功加入集群,但它卻作為了8002的從節(jié)點(diǎn)。

4,集群中加入新節(jié)點(diǎn)

新加入一個(gè)節(jié)點(diǎn),分兩種情況:1是作為主節(jié)點(diǎn),2是作為一個(gè)主節(jié)點(diǎn)的從節(jié)點(diǎn),我們分別來實(shí)踐一下。
1,作為主節(jié)點(diǎn)加入
1)新建一個(gè)7003節(jié)點(diǎn),作為一個(gè)新的master節(jié)點(diǎn)加入:

[root@redis-01 ~]# mkdir /usr/local/redis-cluster/7003
[root@redis-01 ~]# cd /usr/local/redis-cluster/
[root@redis-01 redis-cluster]# cp 7000/redis.conf 7003/
[root@redis-01 redis-cluster]# sed -i "s/7000/7003/g" 7003/redis.conf 
#啟動(dòng)7003 redis服務(wù):
[root@redis-01 ~]# redis-server /usr/local/redis-cluster/7003/redis.conf 
[root@redis-01 ~]# ps -ef | grep redis
root       5603      1  0 01:02 ?        00:00:09 redis-server 172.16.1.100:7001 [cluster]
root       5608      1  0 01:02 ?        00:00:09 redis-server 172.16.1.100:7002 [cluster]
root      19771      1  0 03:50 ?        00:00:00 redis-server 172.16.1.100:7000 [cluster]
root      19842      1  0 04:06 ?        00:00:00 redis-server 172.16.1.100:7003 [cluster]
root      19847   2242  0 04:06 pts/0    00:00:00 grep --color=auto redis

2)將7003節(jié)點(diǎn)加入集群

[root@redis-01 ~]# redis-trib.rb add-node 172.16.1.100:7003 172.16.1.100:7000   //add-node是加入指令,前面表示新加入的節(jié)點(diǎn),后邊表示加入的集群的一個(gè)節(jié)點(diǎn),用來辨識(shí)是哪個(gè)集群,理論上哪個(gè)都可以
>>> Adding node 172.16.1.100:7003 to cluster 172.16.1.100:7000
>>> Performing Cluster Check (using node 172.16.1.100:7000)
S: 8613a457f8009aaf784df0ac6d7039034b16b6a6 172.16.1.100:7000
   slots: (0 slots) slave
   replicates 3cc268dfbb918a99159900643b318ec87ba03ad9
S: 948421116dd1859002c78a2df0b9845bdc7db631 172.16.1.110:8001
   slots: (0 slots) slave
   replicates 5721b77733b1809449c6fc5806f38f1cacb1de8c
M: 3cc268dfbb918a99159900643b318ec87ba03ad9 172.16.1.110:8002
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 5721b77733b1809449c6fc5806f38f1cacb1de8c 172.16.1.100:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: d161ab43746405c2b517e3ffc98321956431191c 172.16.1.100:7002
   slots: (0 slots) slave
   replicates 4debd0b5743826d203d1af777824eb1b83105d21
M: 4debd0b5743826d203d1af777824eb1b83105d21 172.16.1.110:8000
   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.
>>> Send CLUSTER MEET to node 172.16.1.100:7003 to make it join the cluster.
[OK] New node added correctly.

表示新的節(jié)點(diǎn)連接成功,而且也已經(jīng)加入到集群了,我們?cè)賮頇z查一下:

[root@redis-01 ~]# redis-trib.rb check 172.16.1.100:7003  
>>> Performing Cluster Check (using node 172.16.1.100:7003)
M: edd51c8389ba069d49fe54c24c535716ce06e62b 172.16.1.100:7003
   slots: (0 slots) master
   0 additional replica(s)
M: 3cc268dfbb918a99159900643b318ec87ba03ad9 172.16.1.110:8002
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 5721b77733b1809449c6fc5806f38f1cacb1de8c 172.16.1.100:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: d161ab43746405c2b517e3ffc98321956431191c 172.16.1.100:7002
   slots: (0 slots) slave
   replicates 4debd0b5743826d203d1af777824eb1b83105d21
S: 948421116dd1859002c78a2df0b9845bdc7db631 172.16.1.110:8001
   slots: (0 slots) slave
   replicates 5721b77733b1809449c6fc5806f38f1cacb1de8c
S: 8613a457f8009aaf784df0ac6d7039034b16b6a6 172.16.1.100:7000
   slots: (0 slots) slave
   replicates 3cc268dfbb918a99159900643b318ec87ba03ad9
M: 4debd0b5743826d203d1af777824eb1b83105d21 172.16.1.110:8000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)

可以看到集群中有7個(gè)節(jié)點(diǎn),7003也作為了master節(jié)點(diǎn),但是有注意到7003節(jié)點(diǎn)的slots是0;也就是說,雖然它現(xiàn)在是主節(jié)點(diǎn),但是并沒有分配任何slot給它,所以它現(xiàn)在還不負(fù)責(zé)數(shù)據(jù)的存取。所以需要我們手動(dòng)對(duì)集群進(jìn)行重新分片遷移;

3)遷移slot節(jié)點(diǎn)

#這個(gè)命令是用來遷移slot節(jié)點(diǎn)的,后邊的172.16.1.100:7000 表示是哪個(gè)集群,端口隨便哪個(gè)節(jié)點(diǎn)都是可以的:
[root@redis-01 ~]# redis-trib.rb  reshard 172.16.1.100:7000 
How many slots do you want to move (from 1 to 16384)?   
#回車后,它提示我們需要遷移多少slot到7003上,我們可以算一下:16384/4 = 4096,也就是說,為了負(fù)載均衡,我們需要移動(dòng)4096個(gè)槽點(diǎn)到7003上
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? 
#它又提示我們,接受的node ID是多少,7003的id我們通過上面的信息就可以獲得
What is the receiving node ID? edd51c8389ba069d49fe54c24c535716ce06e62b
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),也就是要從哪個(gè)節(jié)點(diǎn)中取出 4096 個(gè)哈希槽, 并將這些槽移動(dòng)到7003節(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)到7003節(jié)點(diǎn)上,所以我們輸入all:

Source node #1:all 
#接下來就開始遷移了,并且會(huì)詢問你是否確認(rèn):
Do you want to proceed with the proposed reshard plan (yes/no)? yes

輸入yes回車后,redis-trib就會(huì)正式開始執(zhí)行重新分片操作,將指定的哈希槽從源節(jié)點(diǎn)一個(gè)個(gè)地移動(dòng)到7003節(jié)點(diǎn)上面。遷移完畢之后,我們來檢查一下:

[root@redis-01 ~]# redis-trib.rb check 172.16.1.100:7000
>>> Performing Cluster Check (using node 172.16.1.100:7000)
S: 8613a457f8009aaf784df0ac6d7039034b16b6a6 172.16.1.100:7000
   slots: (0 slots) slave
   replicates 3cc268dfbb918a99159900643b318ec87ba03ad9
S: 948421116dd1859002c78a2df0b9845bdc7db631 172.16.1.110:8001
   slots: (0 slots) slave
   replicates 5721b77733b1809449c6fc5806f38f1cacb1de8c
M: edd51c8389ba069d49fe54c24c535716ce06e62b 172.16.1.100:7003
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
M: 3cc268dfbb918a99159900643b318ec87ba03ad9 172.16.1.110:8002
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
M: 5721b77733b1809449c6fc5806f38f1cacb1de8c 172.16.1.100:7001
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: d161ab43746405c2b517e3ffc98321956431191c 172.16.1.100:7002
   slots: (0 slots) slave
   replicates 4debd0b5743826d203d1af777824eb1b83105d21
M: 4debd0b5743826d203d1af777824eb1b83105d21 172.16.1.110:8000
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)

我們著重看7003:”0-1364,5461-6826,10923-12287 (4096 slots) “
這些原來在其他節(jié)點(diǎn)上的slot遷移到了7003上。原來,它只是間隔的移動(dòng),并不是銜接的整體移動(dòng),我們來驗(yàn)證7003節(jié)點(diǎn)上邊是否有數(shù)據(jù):

[root@redis-01 ~]# redis-cli -h 172.16.1.100 -p 7003 -c
172.16.1.100:7003> get my_name
-> Redirected to slot [12803] located at 172.16.1.100:7001
"linux"
//證明7003主節(jié)點(diǎn)已經(jīng)正常工作了。

2,作為從節(jié)點(diǎn)加入
1)新建一個(gè)8003節(jié)點(diǎn),作為7003的從節(jié)點(diǎn),步驟類似,這里就省略了,啟動(dòng)8003的redis服務(wù)后,我們把它加入到集群中的從節(jié)點(diǎn)中:

/使用add-node -slave --master-id命令,master-id指向的是你需要選擇哪個(gè)節(jié)點(diǎn)作為新加入從節(jié)點(diǎn)的主節(jié)點(diǎn),172.16.1.110:8003表示你需要新加入的從節(jié)點(diǎn),最后則是選擇當(dāng)前集群中的任意一個(gè)節(jié)點(diǎn)即可
[root@redis-02 ~]# redis-trib.rb add-node --slave --master-id edd51c8389ba069d49fe54c24c535716ce06e62b 172.16.1.110:8003 172.16.1.110:8000
>>> Adding node 172.16.1.110:8003 to cluster 172.16.1.110:8000
>>> Performing Cluster Check (using node 172.16.1.110:8000)
M: 4debd0b5743826d203d1af777824eb1b83105d21 172.16.1.110:8000
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: 3cc268dfbb918a99159900643b318ec87ba03ad9 172.16.1.110:8002
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
M: edd51c8389ba069d49fe54c24c535716ce06e62b 172.16.1.100:7003
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
S: d161ab43746405c2b517e3ffc98321956431191c 172.16.1.100:7002
   slots: (0 slots) slave
   replicates 4debd0b5743826d203d1af777824eb1b83105d21
M: 5721b77733b1809449c6fc5806f38f1cacb1de8c 172.16.1.100:7001
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: 948421116dd1859002c78a2df0b9845bdc7db631 172.16.1.110:8001
   slots: (0 slots) slave
   replicates 5721b77733b1809449c6fc5806f38f1cacb1de8c
S: 8613a457f8009aaf784df0ac6d7039034b16b6a6 172.16.1.100:7000
   slots: (0 slots) slave
   replicates 3cc268dfbb918a99159900643b318ec87ba03ad9
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.16.1.110:8003 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 172.16.1.100:7003.
[OK] New node added correctly.

上邊提示說,已經(jīng)選擇了7003作為master節(jié)點(diǎn),并且成功了。我們來檢查一下集群各個(gè)節(jié)點(diǎn)的狀態(tài):

[root@redis-02 ~]# redis-trib.rb check 172.16.1.110:8003
>>> Performing Cluster Check (using node 172.16.1.110:8003)
S: 4ffabff843d32364fc506f3445980f5a04aa4292 172.16.1.110:8003
   slots: (0 slots) slave
   replicates edd51c8389ba069d49fe54c24c535716ce06e62b
M: 4debd0b5743826d203d1af777824eb1b83105d21 172.16.1.110:8000
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
S: 8613a457f8009aaf784df0ac6d7039034b16b6a6 172.16.1.100:7000
   slots: (0 slots) slave
   replicates 3cc268dfbb918a99159900643b318ec87ba03ad9
M: 3cc268dfbb918a99159900643b318ec87ba03ad9 172.16.1.110:8002
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
M: 5721b77733b1809449c6fc5806f38f1cacb1de8c 172.16.1.100:7001
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
M: edd51c8389ba069d49fe54c24c535716ce06e62b 172.16.1.100:7003
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   1 additional replica(s)
S: d161ab43746405c2b517e3ffc98321956431191c 172.16.1.100:7002
   slots: (0 slots) slave
   replicates 4debd0b5743826d203d1af777824eb1b83105d21
S: 948421116dd1859002c78a2df0b9845bdc7db631 172.16.1.110:8001
   slots: (0 slots) slave
   replicates 5721b77733b1809449c6fc5806f38f1cacb1de8c
#驗(yàn)證該從節(jié)點(diǎn)是否能夠在集群中通信:
[root@redis-02 ~]# redis-cli -h 172.16.1.110 -p 8003 -c
172.16.1.110:8003> get my_name
-> Redirected to slot [12803] located at 172.16.1.100:7001
"linux"
//證明從節(jié)點(diǎn)加入成功,并且正常工作

5,集群中移除節(jié)點(diǎn)

redis集群中有添加節(jié)點(diǎn),那肯定就會(huì)有移除節(jié)點(diǎn)的需求,redis cluster 同樣支持移除節(jié)點(diǎn)的功能,同樣也是redis-trib.rb的用法。

語法格式:
redis-trib del-node ip:端口  `<node-id>`

1,移除主節(jié)點(diǎn)

//和新加節(jié)點(diǎn)不同的是,移除需要節(jié)點(diǎn)的node-id。那么我們嘗試將8000這個(gè)主節(jié)點(diǎn)移除:
[root@redis-02 ~]# redis-trib.rb del-node 172.16.1.110:8000 4debd0b5743826d203d1af777824eb1b83105d21
>>> Removing node 4debd0b5743826d203d1af777824eb1b83105d21 from cluster 172.16.1.110:8000
[ERR] Node 172.16.1.110:8000 is not empty! Reshard data away and try again.

報(bào)錯(cuò)了,它提示我們說,由于8000節(jié)點(diǎn)里面已經(jīng)有數(shù)據(jù)了,不能被移除,要先將它的數(shù)據(jù)轉(zhuǎn)移出去,也就是說得重新分片,所以用上面增加新節(jié)點(diǎn)后的分片方式一樣,再重新分片一次:

[root@redis-01 ~]# redis-trib.rb reshard 172.16.1.100:7000
#提示,我們要分多少個(gè)槽點(diǎn),由于8000上有4096個(gè)槽點(diǎn),所以這里填寫4096
How many slots do you want to move (from 1 to 16384)? 4096
#提示我們,需要移動(dòng)到哪個(gè)id上,那就選擇移動(dòng)到8002主節(jié)點(diǎn)上
What is the receiving node ID? 3cc268dfbb918a99159900643b318ec87ba03ad9
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:
#這里就是關(guān)鍵了,他要我們從哪個(gè)節(jié)點(diǎn)去轉(zhuǎn)移數(shù)據(jù)到8002,因?yàn)槲覀兪且瞥?000的,所以,我們就得填寫8000節(jié)點(diǎn)的id了:
Source node #1:4debd0b5743826d203d1af777824eb1b83105d21
Source node #2:done  //輸入done命令,表示結(jié)束
Do you want to proceed with the proposed reshard plan (yes/no)? yes   //輸入yes

ok,這樣8000主節(jié)點(diǎn)中原有的數(shù)據(jù)就遷移成功了。
我們檢查一下節(jié)點(diǎn)的狀態(tài):

[root@redis-01 ~]# redis-trib.rb check 172.16.1.100:7000
>>> Performing Cluster Check (using node 172.16.1.100:7000)
S: 8613a457f8009aaf784df0ac6d7039034b16b6a6 172.16.1.100:7000
   slots: (0 slots) slave
   replicates 3cc268dfbb918a99159900643b318ec87ba03ad9
S: 4ffabff843d32364fc506f3445980f5a04aa4292 172.16.1.110:8003
   slots: (0 slots) slave
   replicates edd51c8389ba069d49fe54c24c535716ce06e62b
S: 948421116dd1859002c78a2df0b9845bdc7db631 172.16.1.110:8001
   slots: (0 slots) slave
   replicates 5721b77733b1809449c6fc5806f38f1cacb1de8c
M: edd51c8389ba069d49fe54c24c535716ce06e62b 172.16.1.100:7003
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   1 additional replica(s)
M: 3cc268dfbb918a99159900643b318ec87ba03ad9 172.16.1.110:8002
   slots:1365-5460,6827-10922 (8192 slots) master
   2 additional replica(s)
M: 5721b77733b1809449c6fc5806f38f1cacb1de8c 172.16.1.100:7001
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: d161ab43746405c2b517e3ffc98321956431191c 172.16.1.100:7002
   slots: (0 slots) slave
   replicates 3cc268dfbb918a99159900643b318ec87ba03ad9
M: 4debd0b5743826d203d1af777824eb1b83105d21 172.16.1.110:8000
   slots: (0 slots) master
   0 additional replica(s)
//可以看到8000節(jié)點(diǎn)上的slots已經(jīng)為0,而它上邊的slots已經(jīng)遷移到了8002節(jié)點(diǎn)上了。

#現(xiàn)在重新對(duì)8000主節(jié)點(diǎn)進(jìn)行移除操作:

[root@redis-02 ~]# redis-trib.rb del-node 172.16.1.110:8000 4debd0b5743826d203d1af777824eb1b83105d21
>>> Removing node 4debd0b5743826d203d1af777824eb1b83105d21 from cluster 172.16.1.110:8000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[root@redis-02 ~]# redis-trib.rb check 172.16.1.110:8000
[ERR] Sorry, can't connect to node 172.16.1.110:8000

ok,主節(jié)點(diǎn)移除成功。

2,移除從節(jié)點(diǎn)
移除一個(gè)從節(jié)點(diǎn)就簡(jiǎn)單多了,因?yàn)椴恍枰紤]數(shù)據(jù)的遷移,我們將7002這個(gè)從節(jié)點(diǎn)給移除:

[root@redis-02 ~]# redis-trib.rb del-node 172.16.1.100:7002  d161ab43746405c2b517e3ffc98321956431191c
>>> Removing node d161ab43746405c2b517e3ffc98321956431191c from cluster 172.16.1.100:7002
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
//提示7002從節(jié)點(diǎn)已經(jīng)成功移除。
[root@redis-02 ~]# redis-trib.rb check 172.16.1.100:7000   //檢查當(dāng)前集群的狀態(tài)信息
>>> Performing Cluster Check (using node 172.16.1.100:7000)
S: 8613a457f8009aaf784df0ac6d7039034b16b6a6 172.16.1.100:7000
   slots: (0 slots) slave
   replicates 3cc268dfbb918a99159900643b318ec87ba03ad9
S: 4ffabff843d32364fc506f3445980f5a04aa4292 172.16.1.110:8003
   slots: (0 slots) slave
   replicates edd51c8389ba069d49fe54c24c535716ce06e62b
S: 948421116dd1859002c78a2df0b9845bdc7db631 172.16.1.110:8001
   slots: (0 slots) slave
   replicates 5721b77733b1809449c6fc5806f38f1cacb1de8c
M: edd51c8389ba069d49fe54c24c535716ce06e62b 172.16.1.100:7003
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   1 additional replica(s)
M: 3cc268dfbb918a99159900643b318ec87ba03ad9 172.16.1.110:8002
   slots:1365-5460,6827-10922 (8192 slots) master
   1 additional replica(s)
M: 5721b77733b1809449c6fc5806f38f1cacb1de8c 172.16.1.100:7001
   slots:12288-16383 (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.

以上就是redis高可用集群的詳細(xì)內(nèi)容了,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊!

文章標(biāo)題:redis高可用集群介紹
分享地址:http://aaarwkj.com/article4/pchgie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、關(guān)鍵詞優(yōu)化、移動(dòng)網(wǎng)站建設(shè)網(wǎng)站導(dǎo)航、品牌網(wǎng)站設(shè)計(jì)、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名
成人国产av一区二区三区| 亚洲三区四区视频在线观看| 亚洲一区二区天堂av| 天堂av一区二区三区| 亚洲国产成人欧美日韩另类| 中文字幕人妻少妇美臀| 国产真人免费作爱视频网站| 国产成人综合亚洲不卡| 久久日韩制服丝袜人妻| 婷婷精品国产欧美精品亚洲| 亚洲天堂av在线观看| 无码精品人妻一区二区三区中| 一区二区三区不卡中文字幕| 深夜成人免费观看视频| 精品国产成人一区二区| 午夜午色夜之日本福利片| 色哟哟视频免费在线观看| 精品人妻av中文字幕乱| 亚洲男人av天堂午夜在| 日韩成人高清免费在线| 黑丝美女国产精品久久久| 中文字幕中文字幕乱码| 亚洲成人有码在线观看| 好吊妞视频这里只有精| 精品蜜桃臀91人少妇| 久久久国产精品免费看| 一区二区蜜桃在线观看| 亚洲激情在线观看一区| 欧美日韩中文字幕精品| 亚洲国产黄片在线播放| 亚洲激情av在线观看| 亚洲av丰满熟妇在线观看| 最美是你免费视频观看| 九九九视频在线观看免费| 日韩大片一区二区三区在线观看 | 亚洲人妻av一区二区三区| 精品黄色大片不卡国产| 国产欧美日韩另类视频| 国产三级传媒在线观看| 人人妻人人澡人人揉| av在线播放网址网站|