Redis cluster集群扩容缩容原理
1. Redis Cluster集群擴(kuò)容
1.1 擴(kuò)容原理
- redis cluster可以實(shí)現(xiàn)對(duì)節(jié)點(diǎn)的靈活上下線控制
- 3個(gè)主節(jié)點(diǎn)分別維護(hù)自己負(fù)責(zé)的槽和對(duì)應(yīng)的數(shù)據(jù),如果希望加入一個(gè)節(jié)點(diǎn)實(shí)現(xiàn)擴(kuò)容,就需要把一部分槽和數(shù)據(jù)遷移和新節(jié)點(diǎn)
- 每個(gè)master把一部分槽和數(shù)據(jù)遷移到新的節(jié)點(diǎn)node04
1.2 擴(kuò)容過程
準(zhǔn)備新節(jié)點(diǎn)
-
準(zhǔn)備兩個(gè)配置文件redis_6379.conf和redis_6380.conf
-
daemonize yes
-
port 6379
-
logfile "/var/log/redis/redis_6379.log"
-
pidfile /var/run/redis/redis_6379.pid
-
dir /data/redis/6379
-
bind 10.0.0.103
-
protected-mode no
-
# requirepass 123456
-
appendonly yes
-
cluster-enabled yes
-
cluster-node-timeout 15000
-
cluster-config-file /opt/redis/conf/nodes-6379.conf
-
# 另一份配置文件
-
daemonize yes
-
port 6380
-
logfile "/var/log/redis/redis_6380.log"
-
pidfile /var/run/redis/redis_6380.pid
-
dir /data/redis/6379
-
bind 10.0.0.103
-
protected-mode no
-
# requirepass 123456
-
appendonly yes
-
cluster-enabled yes
-
cluster-node-timeout 15000
-
cluster-config-file /opt/redis/conf/nodes-6380.onf
-
創(chuàng)建目錄
-
mkdir -p /var/log/redis
-
touch /var/log/redis/redis_6379.log
-
touch /var/log/redis/redis_6380.log
-
mkdir -p /var/run/redis
-
mkdir -p /data/redis/6379
-
mkdir -p /data/redis/6380
-
mkdir -p /opt/redis/conf
-
新節(jié)點(diǎn)啟動(dòng)redis服務(wù)
-
[root@node04 redis]# bin/redis-server conf/redis_6379.conf
-
[root@node04 redis]# bin/redis-server conf/redis_6380.conf
-
[root@node04 opt]# ps -ef | grep redis
-
root 1755 1 0 19:06 ? 00:00:00 redis-server 10.0.0.103:6379 [cluster]
-
root 1757 1 0 19:06 ? 00:00:00 redis-server 10.0.0.103:6380 [cluster]
新節(jié)點(diǎn)加入集群
-
在原有集群任意節(jié)點(diǎn)內(nèi)執(zhí)行以下命令
-
root@node01 opt]# redis-cli -c -h 10.0.0.100 -p 6380
-
10.0.0.100:6380> cluster meet 10.0.0.103 6379
-
OK
-
10.0.0.100:6380> cluster meet 10.0.0.103 6380
-
OK
-
集群內(nèi)新舊節(jié)點(diǎn)經(jīng)過一段時(shí)間的通信之后,所有節(jié)點(diǎn)會(huì)更新它們的狀態(tài)并保存到本地
-
10.0.0.100:6380> cluster nodes
-
# 可以看到新加入兩個(gè)服務(wù)(10.0.0.103:6379/10.0.0.103:6380)都是master,它們還沒有管理slot
-
4fb4c538d5f29255f6212f2eae8a761fbe364a89 10.0.0.101:6380@16380 master - 0 1585048391000 7 connected 0-5460
-
690b2e1f604a0227068388d3e5b1f1940524c565 10.0.0.102:6379@16379 master - 0 1585048389000 3 connected 10923-16383
-
1be5d1aaaa9e9542224554f461694da9cba7c2b8 10.0.0.101:6379@16379 master - 0 1585048392055 2 connected 5461-10922
-
724a8a15f4efe5a01454cb971d7471d6e84279f3 10.0.0.103:6379@16379 master - 0 1585048388000 8 connected
-
ed9b72fffd04b8a7e5ad20afdaf1f53e0eb95011 10.0.0.103:6380@16380 master - 0 1585048391046 0 connected
-
89f52bfbb8803db19ab0c5a90adc4099df8287f7 10.0.0.100:6379@16379 slave 4fb4c538d5f29255f6212f2eae8a761fbe364a89 0 1585048388000 7 connected
-
86e1881611440012c87fbf3fa98b7b6d79915e25 10.0.0.102:6380@16380 slave 1be5d1aaaa9e9542224554f461694da9cba7c2b8 0 1585048389033 6 connected
-
8c13a2afa76194ef9582bb06675695bfef76b11d 10.0.0.100:6380@16380 myself,slave 690b2e1f604a0227068388d3e5b1f1940524c565 0 1585048390000 4 connected
-
新節(jié)點(diǎn)剛開始都是master節(jié)點(diǎn),但是由于沒有負(fù)責(zé)的槽,所以不能接收任何讀寫操作,對(duì)新節(jié)點(diǎn)的后續(xù)操作,一般有兩種選擇:
- 從其他的節(jié)點(diǎn)遷移槽和數(shù)據(jù)給新節(jié)點(diǎn)
- 作為其他節(jié)點(diǎn)的slave負(fù)責(zé)故障轉(zhuǎn)移
redis-trib.rb工具也實(shí)現(xiàn)了為現(xiàn)有集群添加新節(jié)點(diǎn)的命令,同時(shí)也實(shí)現(xiàn)了直接添加為slave的支持:
-
# 新節(jié)點(diǎn)加入集群
-
redis-trib.rb add-node new_host:new_port old_host:old_port
-
# 新節(jié)點(diǎn)加入集群并作為指定master的slave
-
redis-trib.rb add-node new_host:new_port old_host:old_port --slave --master-id <master-id>
建議使用redis-trib.rb add-node將新節(jié)點(diǎn)添加到集群中,該命令會(huì)檢查新節(jié)點(diǎn)的狀態(tài),如果新節(jié)點(diǎn)已經(jīng)加入了其他集群或者已經(jīng)包含數(shù)據(jù),則會(huì)報(bào)錯(cuò),而使用cluster meet命令則不會(huì)做這樣的檢查,假如新節(jié)點(diǎn)已經(jīng)存在數(shù)據(jù),則會(huì)合并到集群中,造成數(shù)據(jù)不一致
遷移slot和數(shù)據(jù)
-
slot遷移是集群伸縮的最核心步驟
-
假設(shè)原有3個(gè)master,每個(gè)master負(fù)責(zé)10384 / 3 ≈ 5461個(gè)slot
-
加入一個(gè)新的master之后,每個(gè)master負(fù)責(zé)10384 / 4 = 4096個(gè)slot
-
確定好遷移計(jì)劃之后,例如,每個(gè)master將超過4096個(gè)slot的部分遷移到新的master中,然后開始以slot為單位進(jìn)行遷移
-
每個(gè)slot的遷移過程如下所示:
- 對(duì)目標(biāo)節(jié)點(diǎn)發(fā)送cluster setslot {slot_id} importing {sourceNodeId}命令,目標(biāo)節(jié)點(diǎn)的狀態(tài)被標(biāo)記為"importing",準(zhǔn)備導(dǎo)入這個(gè)slot的數(shù)據(jù)
- 對(duì)源節(jié)點(diǎn)發(fā)送cluster setslot {slot_id} migrating {targetNodeID}命令,源節(jié)點(diǎn)的狀態(tài)被標(biāo)記為"migrating",準(zhǔn)備遷出slot的數(shù)據(jù)
- 源節(jié)點(diǎn)執(zhí)行cluster getkeysinslot {slot_id} {count}命令,獲取這個(gè)slot的所有的key列表(分批獲取,count指定一次獲取的個(gè)數(shù)),然后針對(duì)每個(gè)key進(jìn)行遷移
- 在源節(jié)點(diǎn)執(zhí)行migrate {targetIp} {targetPort} "" 0 {timeout} keys {keys}命令,把一批批key遷移到目標(biāo)節(jié)點(diǎn)(redis-3.0.6之前一次只能遷移一個(gè)key),具體來說,源節(jié)點(diǎn)對(duì)遷移的key執(zhí)行dump指令得到序列化內(nèi)容,然后通過客戶端向目標(biāo)節(jié)點(diǎn)發(fā)送攜帶著序列化內(nèi)容的restore指令,目標(biāo)節(jié)點(diǎn)進(jìn)行反序列化后將接收到的內(nèi)容存入自己的內(nèi)存中,目標(biāo)節(jié)點(diǎn)給客戶端返回"OK",然后源節(jié)點(diǎn)刪除這個(gè)key,這樣,一個(gè)key的遷移過程就結(jié)束了
- 所有的key都遷移完成后,一個(gè)slot的遷移就結(jié)束了
- 遷移所有的slot(應(yīng)該被遷移的那些),所有的slot遷移完成后,新的集群的slot就重新分配完成了,向集群內(nèi)所有master發(fā)送cluster setslot {slot_id} node {targetNodeId}命令,通知他們哪些槽被遷移到了哪些master上,讓它們更新自己的信息
-
slot遷移的其他說明
- 遷移過程是同步的,在目標(biāo)節(jié)點(diǎn)執(zhí)行restore指令到原節(jié)點(diǎn)刪除key之間,原節(jié)點(diǎn)的主線程處于阻塞狀態(tài),直到key被刪除成功
- 如果遷移過程突然出現(xiàn)網(wǎng)路故障,整個(gè)slot遷移只進(jìn)行了一半,這時(shí)兩個(gè)節(jié)點(diǎn)仍然會(huì)被標(biāo)記為中間過濾狀態(tài),即"migrating"和"importing",下次遷移工具連接上之后,會(huì)繼續(xù)進(jìn)行遷移
- 在遷移過程中,如果每個(gè)key的內(nèi)容都很小,那么遷移過程很快,不會(huì)影響到客戶端的正常訪問
- 如果key的內(nèi)容很大,由于遷移一個(gè)key的遷移過程是阻塞的,就會(huì)同時(shí)導(dǎo)致原節(jié)點(diǎn)和目標(biāo)節(jié)點(diǎn)的卡頓,影響集群的穩(wěn)定性,所以,集群環(huán)境下,業(yè)務(wù)邏輯要盡可能的避免大key的產(chǎn)生
-
手動(dòng)完成slot遷移的過程
-
# 目標(biāo)節(jié)點(diǎn)690b2e1f604a0227068388d3e5b1f1940524c565準(zhǔn)備導(dǎo)入4096號(hào)slot
-
# 節(jié)點(diǎn)ID通過cluster nodes命令查看
-
cluster setslot 4096 importing 690b2e1f604a0227068388d3e5b1f1940524c565
-
# 源節(jié)點(diǎn)86e1881611440012c87fbf3fa98b7b6d79915e25準(zhǔn)備導(dǎo)出4096號(hào)slot
-
cluster setslot 4096 migrating 86e1881611440012c87fbf3fa98b7b6d79915e25
-
# 批量獲取4096號(hào)槽的100個(gè)key
-
cluster getkeysinslot 4096 100
-
# 批量遷移這些key
-
migrate 10.0.0.100 6379 "" 0 5000 keys key1 key2 ... key100
-
# 通過所有master,4096號(hào)槽被遷移到目標(biāo)節(jié)點(diǎn)690b2e1f604a0227068388d3e5b1f1940524c565
-
10.0.0.100:6379> cluster setslot 4096 node 690b2e1f604a0227068388d3e5b1f1940524c565
-
10.0.0.101:6379> cluster setslot 4096 node 690b2e1f604a0227068388d3e5b1f1940524c565
-
10.0.0.102:6379> cluster setslot 4096 node 690b2e1f604a0227068388d3e5b1f1940524c565
-
10.0.0.103:6379> cluster setslot 4096 node 690b2e1f604a0227068388d3e5b1f1940524c565
-
使用redis-trib.rb工具完成slot遷移
-
redis-trib.rb reshard host:port --from <arg> --to <arg> --slots <arg> --yes --timeout <arg> --pipeline <arg>
-
host:port:隨便指定一個(gè)集群中的host:port,用以獲取全部集群的信息
-
--from:源節(jié)點(diǎn)的id,提示用戶輸入
-
--to:目標(biāo)節(jié)點(diǎn)的id,提示用戶輸入
-
--slots:需要遷移的slot的總數(shù)量,提示用戶輸入
-
--yes:當(dāng)打印出slot遷移計(jì)劃后是否需要用戶輸入yes確認(rèn)后執(zhí)行
-
--timeout:控制每次migrate操作的超時(shí)時(shí)間,默認(rèn)60000ms
-
--pipeline:控制每次批量遷移的key的數(shù)量,默認(rèn)10
-
[root@node01 redis]# redis-trib.rb reshard 10.0.0.100:6379
-
>>> Performing Cluster Check (using node 10.0.0.100:6379)
-
S: 89f52bfbb8803db19ab0c5a90adc4099df8287f7 10.0.0.100:6379
-
slots: (0 slots) slave
-
replicates 4fb4c538d5f29255f6212f2eae8a761fbe364a89
-
S: 8c13a2afa76194ef9582bb06675695bfef76b11d 10.0.0.100:6380
-
slots: (0 slots) slave
-
replicates 690b2e1f604a0227068388d3e5b1f1940524c565
-
M: 690b2e1f604a0227068388d3e5b1f1940524c565 10.0.0.102:6379
-
slots:10923-16383 (5461 slots) master
-
1 additional replica(s)
-
M: 4fb4c538d5f29255f6212f2eae8a761fbe364a89 10.0.0.101:6380
-
slots:0-5460 (5461 slots) master
-
1 additional replica(s)
-
M: ed9b72fffd04b8a7e5ad20afdaf1f53e0eb95011 10.0.0.103:6380
-
slots: (0 slots) master
-
0 additional replica(s)
-
S: 86e1881611440012c87fbf3fa98b7b6d79915e25 10.0.0.102:6380
-
slots: (0 slots) slave
-
replicates 1be5d1aaaa9e9542224554f461694da9cba7c2b8
-
M: 1be5d1aaaa9e9542224554f461694da9cba7c2b8 10.0.0.101:6379
-
slots:5461-10922 (5462 slots) master
-
1 additional replica(s)
-
M: 724a8a15f4efe5a01454cb971d7471d6e84279f3 10.0.0.103:6379
-
slots: (0 slots) master
-
0 additional replica(s)
-
[OK] All nodes agree about slots configuration.
-
>>> Check for open slots...
-
>>> Check slots coverage...
-
[OK] All 16384 slots covered.
-
# 要遷移多少個(gè)slot?
-
How many slots do you want to move (from 1 to 16384)? 4096
-
# 遷移到那個(gè)master?
-
What is the receiving node ID? 724a8a15f4efe5a01454cb971d7471d6e84279f3
-
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:4fb4c538d5f29255f6212f2eae8a761fbe364a89
-
Source node #2:690b2e1f604a0227068388d3e5b1f1940524c565
-
Source node #3:1be5d1aaaa9e9542224554f461694da9cba7c2b8
-
Source node #4:done
-
Ready to move 4096 slots.
-
Source nodes:
-
M: 4fb4c538d5f29255f6212f2eae8a761fbe364a89 10.0.0.101:6380
-
slots:0-5460 (5461 slots) master
-
1 additional replica(s)
-
M: 690b2e1f604a0227068388d3e5b1f1940524c565 10.0.0.102:6379
-
slots:10923-16383 (5461 slots) master
-
1 additional replica(s)
-
M: 1be5d1aaaa9e9542224554f461694da9cba7c2b8 10.0.0.101:6379
-
slots:5461-10922 (5462 slots) master
-
1 additional replica(s)
-
Destination node:
-
M: 724a8a15f4efe5a01454cb971d7471d6e84279f3 10.0.0.103:6379
-
slots: (0 slots) master
-
0 additional replica(s)
-
Resharding plan:
-
Moving slot 5461 from 1be5d1aaaa9e9542224554f461694da9cba7c2b8
-
Moving slot 5462 from 1be5d1aaaa9e9542224554f461694da9cba7c2b8
-
Moving slot 5463 from 1be5d1aaaa9e9542224554f461694da9cba7c2b8
-
......
-
10.0.0.100:6380> cluster nodes
-
1be5d1aaaa9e9542224554f461694da9cba7c2b8 10.0.0.101:6379@16379 master - 0 1585053959158 2 connected 6827-10922
-
# 可以看到新加入的一個(gè)節(jié)點(diǎn)已經(jīng)分配到了slot
-
724a8a15f4efe5a01454cb971d7471d6e84279f3 10.0.0.103:6379@16379 master - 0 1585053957000 8 connected 0-1364 5461-6826 10923-12287
-
4fb4c538d5f29255f6212f2eae8a761fbe364a89 10.0.0.101:6380@16380 master - 0 1585053960166 7 connected 1365-5460
-
ed9b72fffd04b8a7e5ad20afdaf1f53e0eb95011 10.0.0.103:6380@16380 master - 0 1585053957000 0 connected
-
690b2e1f604a0227068388d3e5b1f1940524c565 10.0.0.102:6379@16379 master - 0 1585053959000 3 connected 12288-16383
-
89f52bfbb8803db19ab0c5a90adc4099df8287f7 10.0.0.100:6379@16379 slave 4fb4c538d5f29255f6212f2eae8a761fbe364a89 0 1585053958149 7 connected
-
86e1881611440012c87fbf3fa98b7b6d79915e25 10.0.0.102:6380@16380 slave 1be5d1aaaa9e9542224554f461694da9cba7c2b8 0 1585053958000 6 connected
-
8c13a2afa76194ef9582bb06675695bfef76b11d 10.0.0.100:6380@16380 myself,slave 690b2e1f604a0227068388d3e5b1f1940524c565 0 1585053954000 4 connected
無需要求每個(gè)master的slot編號(hào)是連續(xù)的,只要每個(gè)master管理的slot的數(shù)量均衡就可以。
-
添加slave
我們剛開始添加10.0.0.103:6379和10.0.0.103:6380,現(xiàn)在他們都是master,應(yīng)該讓10.0.0.103:6380成為10.0.0.103:6379的slave
-
# 首先進(jìn)入10.0.0.103:6380客戶端
-
redis-cli -c -h 10.0.0.103 -p 6380
-
# 然后設(shè)置為10.0.0.103:6379的slave節(jié)點(diǎn)
-
10.0.0.103:6380> cluster replicate 724a8a15f4efe5a01454cb971d7471d6e84279f3
-
OK
-
10.0.0.103:6380> cluster nodes
-
1be5d1aaaa9e9542224554f461694da9cba7c2b8 10.0.0.101:6379@16379 master - 0 1585054332556 2 connected 6827-10922
-
4fb4c538d5f29255f6212f2eae8a761fbe364a89 10.0.0.101:6380@16380 master - 0 1585054332000 7 connected 1365-5460
-
690b2e1f604a0227068388d3e5b1f1940524c565 10.0.0.102:6379@16379 master - 0 1585054332000 3 connected 12288-16383
-
724a8a15f4efe5a01454cb971d7471d6e84279f3 10.0.0.103:6379@16379 master - 0 1585054334000 8 connected 0-1364 5461-6826 10923-12287
-
89f52bfbb8803db19ab0c5a90adc4099df8287f7 10.0.0.100:6379@16379 slave 4fb4c538d5f29255f6212f2eae8a761fbe364a89 0 1585054333565 7 connected
-
8c13a2afa76194ef9582bb06675695bfef76b11d 10.0.0.100:6380@16380 slave 690b2e1f604a0227068388d3e5b1f1940524c565 0 1585054334574 3 connected
-
86e1881611440012c87fbf3fa98b7b6d79915e25 10.0.0.102:6380@16380 slave 1be5d1aaaa9e9542224554f461694da9cba7c2b8 0 1585054332000 2 connected
-
# 10.0.0.103:6380已經(jīng)成為slave
-
ed9b72fffd04b8a7e5ad20afdaf1f53e0eb95011 10.0.0.103:6380@16380 myself,slave 724a8a15f4efe5a01454cb971d7471d6e84279f3 0 1585054333000 0 connected
-
檢查slot的負(fù)載均衡
-
[root@node01 redis]# redis-trib.rb rebalance 10.0.0.100:6379
-
>>> Performing Cluster Check (using node 10.0.0.100:6379)
-
[OK] All nodes agree about slots configuration.
-
>>> Check for open slots...
-
>>> Check slots coverage...
-
[OK] All 16384 slots covered.
-
# 所有master節(jié)點(diǎn)管理的slot數(shù)量的差異在2%之內(nèi),不需要重新均衡!
-
*** No rebalancing needed! All nodes are within the 2.0% threshold.
2. Redis Cluster集群縮容
2.1 縮容原理
- 如果下線的是slave,那么通知其他節(jié)點(diǎn)忘記下線的節(jié)點(diǎn)
- 如果下線的是master,那么將此master的slot遷移到其他master之后,通知其他節(jié)點(diǎn)忘記此master節(jié)點(diǎn)
- 其他節(jié)點(diǎn)都忘記了下線的節(jié)點(diǎn)之后,此節(jié)點(diǎn)就可以正常停止服務(wù)了
2.2 縮容過程
我們?cè)谏厦嫣砑恿?0.0.0.103:6379和10.0.0.103:6380兩個(gè)節(jié)點(diǎn),現(xiàn)在把這兩個(gè)節(jié)點(diǎn)下線
確認(rèn)下線節(jié)點(diǎn)的角色
10.0.0.103:6380> cluster nodes
...
# 10.0.0.103:6380是slave
# 10.0.0.103:6379是master
724a8a15f4efe5a01454cb971d7471d6e84279f3 10.0.0.103:6379@16379 master - 0 1585055101000 8 connected 0-1364 5461-6826 10923-12287
ed9b72fffd04b8a7e5ad20afdaf1f53e0eb95011 10.0.0.103:6380@16380 slave 724a8a15f4efe5a01454cb971d7471d6e84279f3 0 1585055099000 0 connected
下線master節(jié)點(diǎn)的slot遷移到其他master
[root@node01 redis]# redis-trib.rb reshard 10.0.0.100:6379
......
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 1364
What is the receiving node ID? 1be5d1aaaa9e9542224554f461694da9cba7c2b8
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:724a8a15f4efe5a01454cb971d7471d6e84279f3
Source node #2:done
Ready to move 1364 slots.
Source nodes:
M: 724a8a15f4efe5a01454cb971d7471d6e84279f3 10.0.0.103:6379
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
Destination node:
M: 1be5d1aaaa9e9542224554f461694da9cba7c2b8 10.0.0.101:6379
slots:6827-10922 (4096 slots) master
1 additional replica(s)
Resharding plan:
.......
[root@node01 redis]# redis-trib.rb reshard 10.0.0.100:6379
......
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 1364
What is the receiving node ID? 4fb4c538d5f29255f6212f2eae8a761fbe364a89
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:724a8a15f4efe5a01454cb971d7471d6e84279f3
Source node #2:done
.......
[root@node01 redis]# redis-trib.rb reshard 10.0.0.100:6379
......
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 1365
What is the receiving node ID? 690b2e1f604a0227068388d3e5b1f1940524c565
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:724a8a15f4efe5a01454cb971d7471d6e84279f3
Source node #2:done
.......
10.0.0.103:6380> cluster nodes
1be5d1aaaa9e9542224554f461694da9cba7c2b8 10.0.0.101:6379@16379 master - 0 1585056902000 9 connected 0-1363 6827-10922
4fb4c538d5f29255f6212f2eae8a761fbe364a89 10.0.0.101:6380@16380 master - 0 1585056903544 12 connected 2729-6826 10923-12287
690b2e1f604a0227068388d3e5b1f1940524c565 10.0.0.102:6379@16379 master - 0 1585056903000 11 connected 1364-2728 12288-16383
# 10.0.0.103:6379的slot已經(jīng)遷移完成
724a8a15f4efe5a01454cb971d7471d6e84279f3 10.0.0.103:6379@16379 master - 0 1585056903000 10 connected
ed9b72fffd04b8a7e5ad20afdaf1f53e0eb95011 10.0.0.103:6380@16380 myself,slave 4fb4c538d5f29255f6212f2eae8a761fbe364a89 0 1585056898000 0 connected
89f52bfbb8803db19ab0c5a90adc4099df8287f7 10.0.0.100:6379@16379 slave 4fb4c538d5f29255f6212f2eae8a761fbe364a89 0 1585056901000 12 connected
8c13a2afa76194ef9582bb06675695bfef76b11d 10.0.0.100:6380@16380 slave 690b2e1f604a0227068388d3e5b1f1940524c565 0 1585056900000 11 connected
86e1881611440012c87fbf3fa98b7b6d79915e25 10.0.0.102:6380@16380 slave 1be5d1aaaa9e9542224554f461694da9cba7c2b8 0 1585056904551 9 connected
忘記節(jié)點(diǎn)
Redis提供了cluster forget{downNodeId}命令來通知其他節(jié)點(diǎn)忘記下線節(jié)點(diǎn),當(dāng)節(jié)點(diǎn)接收到cluster forget {down NodeId}命令后,會(huì)把nodeId指定的節(jié)點(diǎn)加入到禁用列表中,在禁用列表內(nèi)的節(jié)點(diǎn)不再與其他節(jié)點(diǎn)發(fā)送消息,禁用列表有效期是60秒,超過60秒節(jié)點(diǎn)會(huì)再次參與消息交換。也就是說當(dāng)?shù)谝淮蝔orget命令發(fā)出后,我們有60秒的時(shí)間讓集群內(nèi)的所有節(jié)點(diǎn)忘記下線節(jié)點(diǎn)
線上操作不建議直接使用cluster forget命令下線節(jié)點(diǎn),這需要跟大量節(jié)點(diǎn)進(jìn)行命令交互,建議使用redis- trib.rb del-node {host:port} {downNodeId}命令
另外,先下線slave,再下線master可以防止不必要的數(shù)據(jù)復(fù)制
# 先下線slave 10.0.0.103:6380
[root@node01 redis]# redis-trib.rb del-node 10.0.0.100:6379 ed9b72fffd04b8a7e5ad20afdaf1f53e0eb95011
>>> Removing node ed9b72fffd04b8a7e5ad20afdaf1f53e0eb95011 from cluster 10.0.0.100:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
# 再下線slave 10.0.0.103:6379
[root@node01 redis]# redis-trib.rb del-node 10.0.0.100:6379 724a8a15f4efe5a01454cb971d7471d6e84279f3
>>> Removing node 724a8a15f4efe5a01454cb971d7471d6e84279f3 from cluster 10.0.0.100:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
10.0.0.100:6379> cluster nodes
8c13a2afa76194ef9582bb06675695bfef76b11d 10.0.0.100:6380@16380 slave 690b2e1f604a0227068388d3e5b1f1940524c565 0 1585057049247 11 connected
690b2e1f604a0227068388d3e5b1f1940524c565 10.0.0.102:6379@16379 master - 0 1585057048239 11 connected 1364-2728 12288-16383
4fb4c538d5f29255f6212f2eae8a761fbe364a89 10.0.0.101:6380@16380 master - 0 1585057048000 12 connected 2729-6826 10923-12287
89f52bfbb8803db19ab0c5a90adc4099df8287f7 10.0.0.100:6379@16379 myself,slave 4fb4c538d5f29255f6212f2eae8a761fbe364a89 0 1585057047000 1 connected
86e1881611440012c87fbf3fa98b7b6d79915e25 10.0.0.102:6380@16380 slave 1be5d1aaaa9e9542224554f461694da9cba7c2b8 0 1585057048000 9 connected
1be5d1aaaa9e9542224554f461694da9cba7c2b8 10.0.0.101:6379@16379 master - 0 1585057048000 9 connected 0-1363 6827-10922
redis-trib.rb del-node還可以自動(dòng)停止下線節(jié)點(diǎn)的服務(wù)。
總結(jié)
以上是生活随笔為你收集整理的Redis cluster集群扩容缩容原理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis Cluster 集群模式原理
- 下一篇: Redis cluster集群模式的原理