真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

redis高可用集群介紹

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

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

1,前言

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

2,redis Cluster設(shè)計要點

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

3,redis集群原理

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

所以,我們假設(shè)現(xiàn)在有3個節(jié)點已經(jīng)部署成redis cluster,分別是:A,B,C 三個節(jié)點,他們可以是一臺機器上的三個端口,也可以是三臺不同的服務(wù)器;那么采用哈希槽(hash slot)的方式來分配16384個slot,它們?nèi)齻€節(jié)點分別承擔的slot區(qū)間是:
節(jié)點A覆蓋0-5461; 節(jié)點B覆蓋 5462-10922; 節(jié)點C覆蓋 10923-16383;
那么,現(xiàn)在我想設(shè)置一個key,比如叫my_name:set my_name linux
按照redis cluster的哈希槽算法:CRC16('my_name')%16384 = 2412。 那么就會把這個key的存儲分配到A上了。
同樣,當我連接(A,B,C)任何一個節(jié)點想獲取my_name這個key時,也會這樣的算法,然后內(nèi)部跳轉(zhuǎn)到A節(jié)點上獲取數(shù)據(jù)。

這種哈希槽分配方式的好處就是很清晰,比如我想新增一個節(jié)點ID,redis cluster的這種做法是從各個節(jié)點的前面各拿取一部分slot到D上。大致就會變成這樣:(我會在接下來的實踐中實驗)
節(jié)點A覆蓋 1365-5460;節(jié)點B覆蓋 6827-10922;節(jié)點C覆蓋 12288-16383; 節(jié)點D覆蓋0-1364,5461-6826,10923-12287;
同樣,刪除一個節(jié)點也是類似,移動完成后就可以刪除這個節(jié)點了。
所以redis cluster 就是這樣一個形狀:
redis高可用集群介紹

4,redis cluster主從模式

redis cluster為了保證數(shù)據(jù)的高可用性,加入了主從模式,一個主節(jié)點對應一個或多個從節(jié)點,主節(jié)點提供數(shù)據(jù)存取,從節(jié)點則是從主節(jié)點拉取數(shù)據(jù)備份,當這個主節(jié)點掛掉后,就會有這個從節(jié)點存取一個來充當主節(jié)點,從而保證集群不會掛掉。
上邊那個例子里,集群有ABC三個主節(jié)點,如果這3個節(jié)點都沒有加入從節(jié)點,如果B掛掉了,我們就無法訪問整個集群了。A和C的slot也無法訪問。所以我們在集群建立的時候,一定要為每個主節(jié)點都添加從節(jié)點,比如像這樣,集群包含主節(jié)點A,B,C,以及從節(jié)點A1,B1,C1,那么即使B掛掉,系統(tǒng)也可以繼續(xù)正確工作。因為B1節(jié)點提替代了B節(jié)點,所以redis集群將會選擇B1節(jié)點作為新的主節(jié)點,集群將會繼續(xù)正確地提供服務(wù)。需要注意的是,當B重新開啟后,它就會變成B1的從節(jié)點,而不會重新變成主節(jié)點。
如果節(jié)點B和B1同時掛了,Redis集群就無法繼續(xù)正常地提供服務(wù)了,一般情況下,是不會也不允許兩節(jié)點同時掛掉。
流程下圖所示:
redis高可用集群介紹

搭建redis高可用集群

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

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

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

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

主機A:172.16.1.100(CentOS 7.3),啟動三個實例7000,7001,7002;全部為主
主機B:172.16.1.110(CentOS 7.3),啟動三個實例8000,8001,8002;全部為從

一,手動方式搭建

1,安裝redis

A主機:
[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è)置為當前redis主機的ip地址
  92 port 7000  #設(shè)置redis的監(jiān)聽端口
 136 daemonize yes   #以守護進程運行redis實例
 814 cluster-enabled yes   #啟動集群模式
 822 cluster-config-file nodes-7000.conf   #設(shè)置當前節(jié)點集群配置文件路徑
 828 cluster-node-timeout 5000  #設(shè)置當前連接超時秒數(shù)
 672 appendonly yes   #開啟AOF持久化模式
 676 appendfilename "appendonly-7000.aof"  #保存數(shù)據(jù)的AOF文件名稱
B主機:
[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)建各個節(jié)點啟動配置文件的存放目錄

A主機:
[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主機:
[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,將主配文件分別拷貝到相對應的目錄下

A主機:
[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主機:
[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端口號:
A主機:
[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主機:
[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,啟動reids服務(wù)

A主機:
[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é)點服務(wù)正常運行:
[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主機:
[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é)點握手
雖然上面6各節(jié)點都啟用集群支持,但默認情況下它們是不相互信任或者說沒有聯(lián)系的。節(jié)點握手就是在各個節(jié)點之間創(chuàng)建鏈接(每個節(jié)點與其他節(jié)點相連),形成一個完整的網(wǎng)絡(luò),即集群。
節(jié)點握手的命令如下:

cluster meet ip port

我們創(chuàng)建的6個節(jié)點可以通過redis-cli 連接到A節(jié)點執(zhí)行如下五組命令完成握手:
注意:需要關(guān)閉集群中各臺主機的防火墻: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é)點都已經(jīng)建立鏈接,自此,節(jié)點握手完成。
#雖然節(jié)點已經(jīng)建立鏈接,但此時redis集群還并沒有處于上線狀態(tài),執(zhí)行cluster info命令來查看目前集群的運行狀態(tài):

172.16.1.100:7000> cluster info
cluster_state:fail    #表示當前集群處于下線狀態(tài)
cluster_slots_assigned:0     #為0 表示目前所有槽沒有被分配到節(jié)點上
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é)點分配好槽位(slot),集群才能正常上線,分配槽位的命令如下:
cluster addslots slot [slot ...]
#根據(jù)預先規(guī)劃,需要使用cluster addslots 命令手動將16384個哈希槽大致均等分配給主節(jié)點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

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

7,主從復制

集群復制命令如下:
cluster replicate node-id

1)連接集群中任意一個節(jié)點,獲得所有主節(jié)點的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é)點指定其主節(jié)點,使集群可以自動完成主從復制

[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)查看集群中各個節(jié)點的復制狀態(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é)點都作為對應主節(jié)點的備份節(jié)點,至此,已經(jīng)成功以手動方式搭建一個redis集群。
總結(jié)手動搭建redis集群的關(guān)鍵步驟:

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

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

二,自動方式搭建

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

主機A:172.16.1.100(CentOS 7.3),啟動三個實例7000,7001,7002;
主機B:172.16.1.110(CentOS 7.3),啟動三個實例8000,8001,8002;

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

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

1)安裝Ruby環(huán)境以及其他依賴項(兩臺主機)
[root@redis-01 ~]# yum -y install ruby ruby-devel rubygems rpm-build openssl openssl-devel
#確認安裝版本:
[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ù)表示為每個主節(jié)點創(chuàng)建一個從節(jié)點(隨機分配),其他參數(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 `
'

上面報錯是需要redis的gem包來安裝ruby和redis的接口,安裝gem包,網(wǎng)址: https://rubygems.org/gems/redis/  選擇對應的版本下載,這里選擇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會提示你做了什么配置,輸入yes接受,集群就被配置和加入了,意思是實例會經(jīng)過互相交流后啟動。
至此,集群可以說是搭建完成了,一條命令解決,可以說是非常方便。

3,測試集群

1)測試集群的狀態(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個主節(jié)點(M),分別是7000,8000,7001,3個從節(jié)點(S),分別是7002,8001,8002。每個節(jié)點都是成功的連接狀態(tài)。

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

[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è)置一個值
-> Redirected to slot [12803] located at 172.16.1.100:7001 
//前面有說到,分配key的時候,它會使用CRC16('my_name')%16384算法來計算將這個key放到哪個節(jié)點,這里分配到了12803 ,所以slot就分配到了 7001(范圍:10923-16383)這個節(jié)點上
OK
172.16.1.100:7001> get my_name    //獲取該數(shù)據(jù)值
"linux"

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

[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)測試集群的高可用
當前我的redis集群有3個主節(jié)點(7000,8000,7001)提供數(shù)據(jù)存儲和讀取,3個從節(jié)點(7002,8001,8002)負責把主節(jié)點的數(shù)據(jù)同步到自己的節(jié)點上來,所以我們來看一下從節(jié)點的appendonly.aof的內(nèi)容(因為剛才創(chuàng)建的值分配給了7001節(jié)點,而7001主節(jié)點對應的從節(jié)點是8001,所以我們查看8001的aof文件)

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

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

#下面,我們模擬其中一臺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  

#測試集群的狀態(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é)果可以看到,當7000主節(jié)點掛掉了,那么這個時候,7000的從節(jié)點只有8002一個,所以8002就會被選舉成master節(jié)點了。并且原來7000節(jié)點上的數(shù)據(jù)不會丟失,而是會轉(zhuǎn)移到了8002節(jié)點上,當用戶再次獲取數(shù)據(jù)時,則是從8002上面獲取了。

既然7000節(jié)點服務(wù)器因為某些原因宕機了,但是當我們把故障解決后,重新將7000節(jié)點加入集群中,那么7000節(jié)點會在集群中充當什么角色呢?

[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é)點已經(jīng)成功加入集群,但它卻作為了8002的從節(jié)點。

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

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

[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 
#啟動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é)點加入集群

[root@redis-01 ~]# redis-trib.rb add-node 172.16.1.100:7003 172.16.1.100:7000   //add-node是加入指令,前面表示新加入的節(jié)點,后邊表示加入的集群的一個節(jié)點,用來辨識是哪個集群,理論上哪個都可以
>>> 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é)點連接成功,而且也已經(jīng)加入到集群了,我們再來檢查一下:

[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個節(jié)點,7003也作為了master節(jié)點,但是有注意到7003節(jié)點的slots是0;也就是說,雖然它現(xiàn)在是主節(jié)點,但是并沒有分配任何slot給它,所以它現(xiàn)在還不負責數(shù)據(jù)的存取。所以需要我們手動對集群進行重新分片遷移;

3)遷移slot節(jié)點

#這個命令是用來遷移slot節(jié)點的,后邊的172.16.1.100:7000 表示是哪個集群,端口隨便哪個節(jié)點都是可以的:
[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,也就是說,為了負載均衡,我們需要移動4096個槽點到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 會向你詢問重新分片的源節(jié)點(source node),也就是要從哪個節(jié)點中取出 4096 個哈希槽, 并將這些槽移動到7003節(jié)點上面。如果我們不打算從特定的節(jié)點上取出指定數(shù)量的哈希槽, 那么可以向 redis-trib 輸入 all ,這樣的話, 集群中的所有主節(jié)點都會成為源節(jié)點, redis-trib 將從各個源節(jié)點中各取出一部分哈希槽, 湊夠 4096 個, 然后移動到7003節(jié)點上,所以我們輸入all:

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

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

[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é)點上的slot遷移到了7003上。原來,它只是間隔的移動,并不是銜接的整體移動,我們來驗證7003節(jié)點上邊是否有數(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é)點已經(jīng)正常工作了。

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

/使用add-node -slave --master-id命令,master-id指向的是你需要選擇哪個節(jié)點作為新加入從節(jié)點的主節(jié)點,172.16.1.110:8003表示你需要新加入的從節(jié)點,最后則是選擇當前集群中的任意一個節(jié)點即可
[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é)點,并且成功了。我們來檢查一下集群各個節(jié)點的狀態(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
#驗證該從節(jié)點是否能夠在集群中通信:
[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é)點加入成功,并且正常工作

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

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

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

1,移除主節(jié)點

//和新加節(jié)點不同的是,移除需要節(jié)點的node-id。那么我們嘗試將8000這個主節(jié)點移除:
[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.

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

[root@redis-01 ~]# redis-trib.rb reshard 172.16.1.100:7000
#提示,我們要分多少個槽點,由于8000上有4096個槽點,所以這里填寫4096
How many slots do you want to move (from 1 to 16384)? 4096
#提示我們,需要移動到哪個id上,那就選擇移動到8002主節(jié)點上
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)鍵了,他要我們從哪個節(jié)點去轉(zhuǎn)移數(shù)據(jù)到8002,因為我們是要移除8000的,所以,我們就得填寫8000節(jié)點的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é)點中原有的數(shù)據(jù)就遷移成功了。
我們檢查一下節(jié)點的狀態(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é)點上的slots已經(jīng)為0,而它上邊的slots已經(jīng)遷移到了8002節(jié)點上了。

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

[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é)點移除成功。

2,移除從節(jié)點
移除一個從節(jié)點就簡單多了,因為不需要考慮數(shù)據(jù)的遷移,我們將7002這個從節(jié)點給移除:

[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é)點已經(jīng)成功移除。
[root@redis-02 ~]# redis-trib.rb check 172.16.1.100:7000   //檢查當前集群的狀態(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高可用集群的詳細內(nèi)容了,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊!


新聞標題:redis高可用集群介紹
本文路徑:http://weahome.cn/article/pchgie.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部