Redis Cluster集群架构实现
Redis集群簡介
有關redis集群的介紹可以參考下面我摘自redis官網的簡介。
Redis 集群是一個可以在多個 Redis 節點之間進行數據共享的設施(installation)。
Redis 集群不支持那些需要同時處理多個鍵的 Redis 命令, 因為執行這些命令需要在多個 Redis 節點之間移動數據, 并且在高負載的情況下, 這些命令將降低 Redis 集群的性能, 并導致不可預測的行為。
Redis 集群通過分區(partition)來提供一定程度的可用性(availability): 即使集群中有一部分節點失效或者無法進行通訊, 集群也可以繼續處理命令請求。
Redis 集群提供了以下兩個好處:
- 將數據自動切分(split)到多個節點的能力。
- 當集群中的一部分節點失效或者無法進行通訊時, 仍然可以繼續處理命令請求的能力。? --摘自redis官網
總而言之,redis集群實現了數據的共享以及去中心化。
?
Redis集群實現
實現redis集群,現在采用一臺服務器安裝6個redis實例,有關如果安裝多實例的詳細講解請參考我上篇博客《Redis主從復制、多實例、高可用(三)--技術流le》
6個多實例,三個為主節點,三個為從節點
環境:
CentOS Linux release 7.5.1804 (Core)?
..redis-4.0.11
?
創建多實例
第一步:創建多實例數據目錄
root@le ~]# mkdir /le
[root@le ~]# cd /le
[root@le le]# mkdir 6379 6380 6381 6382 6383 6384
[root@le le]# ls
6379? 6380? 6381? 6382? 6383? 6384
?
第二步:上傳redis安裝包
這里使用的是redis-4.0.11的版本,可以在redis官方網站進行下載https://redis.io/
[root@le ~]# rz
?
第三步:解壓安裝包
[root@le ~]# tar xf redis-4.0.11.tar.gz
?
第四步:復制解壓安裝包的配置文件到6379目錄下
[root@le ~]# cp redis-4.0.11/redis.conf /le/6379/
?
第五步:移動redis解壓包至/usr/local/redis下
[root@le ~]# mv redis-4.0.11 /usr/local/redis
?
第六步:編譯安裝
[root@le ~]# cd /usr/local/redis
[root@le redis]# make && make install
[root@le ~]# ln /usr/local/redis/src/ /bin -s
?
第七步:修改配置文件
?
[root@le ~]# grep -E -v "^#|^$" /le/6379/redis.conf
bind 10.220.5.137?????? #綁定本機ip地址
protected-mode yes
port 6379?????????????? #監聽端口
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes?????????? #后臺運行
supervised no
pidfile /le/6379/redis_6379.pid? #pid文件保存位置
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /le/6379/??????????? #rdb文件保存位置
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
cluster-enabled yes??? #開啟集群
cluster-config-file nodes-6379.conf? #集群文件名稱
cluster-node-timeout 15000?????????? #集群超時時間
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
?
?
第八步:把上面這個修改過的文件復制到其他的實例數據目錄之下
[root@le ~]# cp /le/6379/redis.conf /le/6380/
[root@le ~]# cp /le/6379/redis.conf /le/6381/
[root@le ~]# cp /le/6379/redis.conf /le/6382/
[root@le ~]# cp /le/6379/redis.conf /le/6383/
[root@le ~]# cp /le/6379/redis.conf /le/6384/
?
第九步:修改配置文件
只要使用sed即可進行修改
[root@le ~]# sed -i 's/6379/6380/g' /le/6380/redis.conf
[root@le ~]# sed -i 's/6379/6381/g' /le/6381/redis.conf
[root@le ~]# sed -i 's/6379/6382/g' /le/6382/redis.conf
[root@le ~]# sed -i 's/6379/6383/g' /le/6383/redis.conf
[root@le ~]# sed -i 's/6379/6384/g' /le/6384/redis.conf
?
第十步:啟動各個實例
?
[root@le ~]# redis-server /le/6380/redis.conf
[root@le ~]# redis-server /le/6381/redis.conf
[root@le ~]# redis-server /le/6382/redis.conf
[root@le ~]# redis-server /le/6383/redis.conf
[root@le ~]# redis-server /le/6384/redis.conf
[root@le ~]# ss -tnl
State????? Recv-Q Send-Q?????????????????? Local Address:Port????????????????????????????????? Peer Address:Port?????????????
LISTEN??? ?0????? 128?????????????????????? 10.220.5.137:16380??????????????????????????????????????????? *:*?????????????????
LISTEN???? 0????? 128?????????????????????? 10.220.5.137:16381??????????????????????????????????????????? *:*?????????????????
LISTEN??? ?0????? 128?????????????????????? 10.220.5.137:16382??????????????????????????????????????????? *:*?????????????????
LISTEN???? 0????? 128?????????????????????? 10.220.5.137:16383??????????????????????????????????????????? *:*?????????????????
LISTEN??? ?0????? 128?????????????????????? 10.220.5.137:16384??????????????????????????????????????????? *:*?????????????????
LISTEN???? 0????? 128????????????????????????????????? *:10050??????????????????????????????????????????? *:*?????????????????
LISTEN??? ?0????? 128????????????????????????????????? *:10051??????????????????????????????????????????? *:*?????????????????
LISTEN???? 0????? 128?????????????????????? 10.220.5.137:6379???????????????????????????????????????????? *:*?????????????????
LISTEN??? ?0????? 128?????????????????????? 10.220.5.137:6380???????????????????????????????????????????? *:*?????????????????
LISTEN???? 0????? 128?????????????????????? 10.220.5.137:6381???????????????????????????????????????????? *:*?????????????????
LISTEN??? ?0????? 128?????????????????????? 10.220.5.137:6382???????????????????????????????????????????? *:*?????????????????
LISTEN???? 0????? 128?????????????????????? 10.220.5.137:6383???????????????????????????????????????????? *:*?????????????????
LISTEN??? ?0????? 128????????????????????????????????? *:111????????????????????????????????????????????? *:*?????????????????
LISTEN???? 0????? 128?????????????????????? 10.220.5.137:6384???????????????????????????????????????????? *:*?????????????????
LISTEN??? ?0????? 128????????????????????????????????? *:22?????????????????????????????????????????????? *:*?????????????????
LISTEN???? 0????? 128?????????????????????? 10.220.5.137:16379??????????????????????????????????????????? *:*?????????????????
LISTEN??? ?0????? 128???????????????????????????????? :::10050?????????????????????????????????????????? :::*?????????????????
LISTEN???? 0????? 128???????????????????????????????? :::10051?????????????????????????????????????????? :::*?????????????????
LISTEN??? ?0????? 70????????????????????????????????? :::3306??????????????????????????????????????????? :::*?????????????????
LISTEN???? 0????? 128???????????????????????????????? :::111???????????????????????????????????????????? :::*?????????????????
LISTEN??? ?0????? 128???????????????????????????????? :::80????????????????????????????????????????????? :::*?????????????????
LISTEN???? 0????? 128???????????????????????????????? :::22????????????????????????????????????????????? :::*?????
?
?
安裝ruby2.3
實現redis cluster功能,依賴redis-trib.rb,而這個工具是依賴一個ruby開發工具包的,所以需要安裝ruby環境,并安裝依賴包
?
第一步:上傳解壓安裝包
[root@le ~]# rz
[root@le ~]# tar xf ruby-2.3.5.tar.gz
?
第二步:編譯安裝
建議虛擬機內存至少1個G以上
[root@le ~]# cd ruby-2.3.5/
[root@le ruby-2.3.5]# ./configure --prefix=/usr/local/ruby && make && make install
?
第三步:安裝redis-trib.rb的依賴
?
[root@le ~]# ln -s /usr/local/ruby/bin/gem /bin
[root@le ~]# 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
Done installing documentation for redis after 0 seconds
1 gem installed
[root@le ~]# ln -s /usr/local/ruby/bin/ruby? /bin
?
?
獲取集群幫助
?只需要輸入redis-trib.rb回車即可
?
[root@le ~]# redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>
create????????? host1:port1 ... hostN:portN
????????????????? --replicas <arg>
? check?????????? host:port
? info??????????? host:port
? fix???????????? host:port
????????????????? --timeout <arg>
? reshard???????? host:port
????????????????? --from <arg>
????????????????? --to <arg>
????????????????? --slots <arg>
????????????????? --yes
????????????????? --timeout <arg>
????????????????? --pipeline <arg>
? rebalance?????? host:port
????????????????? --weight <arg>
????????????????? --auto-weights
????????????????? --use-empty-masters
????????????????? --timeout <arg>
????????????????? --simulate
????????????????? --pipeline <arg>
????????????????? --threshold <arg>
? add-node??????? new_host:new_port existing_host:existing_port
????????????????? --slave
????????????????? --master-id <arg>
? del-node??????? host:port node_id
? set-timeout???? host:port milliseconds
? call??????????? host:port command arg arg .. arg
? import????????? host:port
????????????????? --from <arg>
????????????????? --copy
????????????????? --replace
? help???? ???????(show this help)
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
?
?
創建redis集群
命令的意義如下:
- 給定?redis-trib.rb?程序的命令是?create?, 這表示我們希望創建一個新的集群。
- 選項?--replicas?1?表示我們希望為集群中的每個主節點創建一個從節點。
- 之后跟著的其他參數則是實例的地址列表, 我們希望程序使用這些地址所指示的實例來創建新集群。
簡單來說, 以上命令的意思就是讓?redis-trib?程序創建一個包含三個主節點和三個從節點的集群。
接著,?redis-trib?會打印出一份預想中的配置給你看, 如果你覺得沒問題的話, 就可以輸入?yes?,?redis-trib?就會將這份配置應用到集群當中:
?
[root@le ~]# redis-trib.rb create --replicas 1 10.220.5.137:6379 10.220.5.137:6380 10.220.5.137:6381 10.220.5.137:6382 10.220.5.137:6383 10.220.5.137:6384
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.220.5.137:6379
10.220.5.137:6380
10.220.5.137:6381
Adding replica 10.220.5.137:6383 to 10.220.5.137:6379
Adding replica 10.220.5.137:6384 to 10.220.5.137:6380
Adding replica 10.220.5.137:6382 to 10.220.5.137:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 57753754f4c89054ab14e8ec517604c4fc4c8ed5 10.220.5.137:6379
?? slots:0-5460 (5461 slots) master
M: 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d 10.220.5.137:6380
?? slots:5461-10922 (5462 slots) master
M: ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8 10.220.5.137:6381
?? slots:10923-16383 (5461 slots) master
S: 1bc40ffc4c54099f8cf47efdeb82a9f42e717bf6 10.220.5.137:6382
?? replicates 57753754f4c89054ab14e8ec517604c4fc4c8ed5
S: 640bb3a62565a0bb74980852fee7e8636cf238bc 10.220.5.137:6383
?? replicates 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d
S: b8214c9e4b617c7992e71a2fa07a4a64cfeddf58 10.220.5.137:6384
?? replicates ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8
Can I set the above configuration? (type 'yes' to accept): yes?? #輸入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 10.220.5.137:6379)
M: 57753754f4c89054ab14e8ec517604c4fc4c8ed5 10.220.5.137:6379
?? slots:0-5460 (5461 slots) master
?? 1 additional replica(s)
M: ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8 10.220.5.137:6381
?? slots:10923-16383 (5461 slots) master
?? 1 additional replica(s)
M: 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d 10.220.5.137:6380
?? slots:5461-10922 (5462 slots) master
?? 1 additional replica(s)
S: 640bb3a62565a0bb74980852fee7e8636cf238bc 10.220.5.137:6383
?? slots: (0 slots) slave
?? replicates 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d
S: b8214c9e4b617c7992e71a2fa07a4a64cfeddf58 10.220.5.137:6384
?? slots: (0 slots) slave
?? replicates ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8
S: 1bc40ffc4c54099f8cf47efdeb82a9f42e717bf6 10.220.5.137:6382
?? slots: (0 slots) slave
?? replicates 57753754f4c89054ab14e8ec517604c4fc4c8ed5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
?
?
Redis集群測試
測試 Redis 集群比較簡單的辦法就是使用?redis-rb-cluster?或者?redis-cli?, 接下來將使用?redis-cli?為例來進行演示:
?
第一步:登錄集群中
一定要加個-c,后面輸入哪個端口都可以,這就是redis的區中心化思想
[root@le ~]# redis-cli -c -h 10.220.5.137 -p 6379
?
第二步:創建key
可以發現創建的key被分配到了不同的節點
?
10.220.5.137:6379> keys *
(empty list or set)
10.220.5.137:6379> set name le
-> Redirected to slot [5798] located at 10.220.5.137:6380
OK
10.220.5.137:6380> set addr jiangsu
-> Redirected to slot [12790] located at 10.220.5.137:6381
OK
10.220.5.137:6381> set tel 123445
-> Redirected to slot [7485] located at 10.220.5.137:6380
OK
10.220.5.137:6380> set ege 25
OK
10.220.5.137:6380> set gender male
-> Redirected to slot [15355] located at 10.220.5.137:6381
OK
10.220.5.137:6381> keys *
1) "addr"
2) "gender"
?
?
第三步:獲取key
在端口6381的節點之上沒有tel這個key,但是我們仍然可以使用get tel獲取到值,這就實現了redis集群的數據共享
10.220.5.137:6381> keys *
1) "addr"
2) "gender"
10.220.5.137:6381> get tel
-> Redirected to slot [7485] located at 10.220.5.137:6380
"123445"
?
在集群中添加新的節點
第一步:創建一個新的實例
?
[root@le ~]# mkdir /le/6385
[root@le ~]# cp /le/6379/redis.conf /le/6385/
[root@le ~]# sed -i 's/6379/6385/g' /le/6385/redis.conf
[root@le ~]# redis-server /le/6385/redis.conf
10461:C 15 Nov 21:21:11.646 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
10461:C 15 Nov 21:21:11.646 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=10461, just started
10461:C 15 Nov 21:21:11.646 # Configuration loaded
?
?
第二步:添加該節點到集群中
命令中的?add-node?表示我們要讓?redis-trib?將一個節點添加到集群里面,?add-node?之后跟著的是新節點的 IP 地址和端口號, 再之后跟著的是集群中任意一個已存在節點的 IP 地址和端口號, 這里我們使用的是?10.220.5.137:6379?。
?
[root@le ~]# redis-trib.rb add-node 10.220.5.137:6385 10.220.5.137:6379
>>> Adding node 10.220.5.137:6385 to cluster 10.220.5.137:6379
>>> Performing Cluster Check (using node 10.220.5.137:6379)
M: 57753754f4c89054ab14e8ec517604c4fc4c8ed5 10.220.5.137:6379
?? slots:0-5460 (5461 slots) master
?? 1 additional replica(s)
M: ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8 10.220.5.137:6381
?? slots:10923-16383 (5461 slots) master
?? 1 additional replica(s)
M: 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d 10.220.5.137:6380
?? slots:5461-10922 (5462 slots) master
?? 1 additional replica(s)
S: 640bb3a62565a0bb74980852fee7e8636cf238bc 10.220.5.137:6383
?? slots: (0 slots) slave
?? replicates 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d
S: b8214c9e4b617c7992e71a2fa07a4a64cfeddf58 10.220.5.137:6384
?? slots: (0 slots) slave
?? replicates ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8
S: 1bc40ffc4c54099f8cf47efdeb82a9f42e717bf6 10.220.5.137:6382
?? slots: (0 slots) slave
?? replicates 57753754f4c89054ab14e8ec517604c4fc4c8ed5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 10.220.5.137:6385 to make it join the cluster.
[OK] New node added correctly.
?
?
第三步:查看
通過?cluster?nodes?命令, 我們可以確認新節點10.220.5.137:6385?已經被添加到集群里面了
?
10.220.5.137:6379> CLUSTER nodes
57753754f4c89054ab14e8ec517604c4fc4c8ed5 10.220.5.137:6379@16379 myself,master - 0 1542288244000 1 connected 0-5460
daf9464ef45d0c73e1ee18f7cc7ddbc7d9719f2d 10.220.5.137:6385@16385 master - 0 1542288242824 0 connected
ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8 10.220.5.137:6381@16381 master - 0 1542288244000 3 connected 10923-16383
3a9aa9592afc594c7e4206cc82ffb37d46a5b23d 10.220.5.137:6380@16380 master - 0 1542288242000 2 connected 5461-10922
640bb3a62565a0bb74980852fee7e8636cf238bc 10.220.5.137:6383@16383 slave 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d 0 1542288242000 5 connected
b8214c9e4b617c7992e71a2fa07a4a64cfeddf58 10.220.5.137:6384@16384 slave ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8 0 1542288244852 6 connected
1bc40ffc4c54099f8cf47efdeb82a9f42e717bf6 10.220.5.137:6382@16382 slave 57753754f4c89054ab14e8ec517604c4fc4c8ed5 0 1542288243844 4 connected
?
?
第四步:使添加的新節點為從節點
?如果我們打算讓新節點成為?10.220.5.137:6379?的從節點, 那么我們只要用客戶端連接上新節點, 然后執行以下命令就可以了
[root@le ~]# redis-cli -c -h 10.220.5.137 -p 6385
10.220.5.137:6385> CLUSTER REPLICATE 57753754f4c89054ab14e8ec517604c4fc4c8ed5
OK
其中57753754f4c89054ab14e8ec517604c4fc4c8ed5是主節點10.220.5.137:6379的id
?
對redis中的數據做重新分片
?
[root@le ~]# redis-trib.rb reshard 10.220.5.137:6379
?
你只需要指定集群中其中一個節點的地址,?redis-trib?就會自動找到集群中的其他節點。
?
How many slots do you want to move (from 1 to 16384)? 1000
我們將打算移動的槽數量設置為?1000?個。
?
What is the receiving node ID? 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d
定目標需要使用節點的 ID , 而不是 IP 地址和端口。 比如說, 我們打算使用集群的第一個主節點來作為目標, 它的 IP 地址和端口是?10.220.5.137:6380?, 而節點 ID 則是3a9aa9592afc594c7e4206cc82ffb37d46a5b23d?, 那么我們應該向?redis-trib?提供節點的 ID?
?
Source node #1:ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8
接著,?redis-trib?會向你詢問重新分片的源節點(source node), 也即是, 要從哪個節點中取出?1000?個哈希槽, 并將這些槽移動到目標節點上面。
?
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes
輸入?yes?并使用按下回車之后,?redis-trib?就會正式開始執行重新分片操作, 將指定的哈希槽從源節點一個個地移動到目標節點上面
?
?
Moving slot 12905 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12906 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12907 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12908 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12909 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12910 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12911 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12912 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12913 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12914 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12915 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12916 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12917 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12918 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12919 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12920 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12921 from 10.220.5.137:6381 to 10.220.5.137:6380:
Moving slot 12922 from 10.220.5.137:6381 to 10.220.5.137:6380:
?
在重新分片操作執行完畢之后, 可以使用以下命令來檢查集群是否正常
?
[root@le ~]# redis-trib.rb check 10.220.5.137:6379
>>> Performing Cluster Check (using node 10.220.5.137:6379)
M: 57753754f4c89054ab14e8ec517604c4fc4c8ed5 10.220.5.137:6379
?? slots:0-5460 (5461 slots) master
?? 2 additional replica(s)
S: daf9464ef45d0c73e1ee18f7cc7ddbc7d9719f2d 10.220.5.137:6385
?? slots: (0 slots) slave
?? replicates 57753754f4c89054ab14e8ec517604c4fc4c8ed5
M: ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8 10.220.5.137:6381
?? slots:12923-16383 (3461 slots) master
?? 1 additional replica(s)
M: 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d 10.220.5.137:6380
?? slots:5461-12922 (7462 slots) master
?? 1 additional replica(s)
S: 640bb3a62565a0bb74980852fee7e8636cf238bc 10.220.5.137:6383
?? slots: (0 slots) slave
?? replicates 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d
S: b8214c9e4b617c7992e71a2fa07a4a64cfeddf58 10.220.5.137:6384
?? slots: (0 slots) slave
?? replicates ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8
S: 1bc40ffc4c54099f8cf47efdeb82a9f42e717bf6 10.220.5.137:6382
?? slots: (0 slots) slave
?? replicates 57753754f4c89054ab14e8ec517604c4fc4c8ed5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
?
?
刪除節點
如果節點中有slot,那么需要先將slot?執行reshard給其他節點,然后才能執行刪除操作
1. 刪除空slot
刪除10.220.5.137:6385
?
[root@le ~]# redis-trib.rb check 10.220.5.137:6379
>>> Performing Cluster Check (using node 10.220.5.137:6379)
M: 57753754f4c89054ab14e8ec517604c4fc4c8ed5 10.220.5.137:6379
?? slots:0-5460 (5461 slots) master
?? 2 additional replica(s)
S: daf9464ef45d0c73e1ee18f7cc7ddbc7d9719f2d 10.220.5.137:6385
?? slots: (0 slots) slave
?? replicates 57753754f4c89054ab14e8ec517604c4fc4c8ed5
M: ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8 10.220.5.137:6381
?? slots:12923-16383 (3461 slots) master
?? 1 additional replica(s)
...
?
執行如下命令
[root@le ~]# redis-trib.rb del-node 10.220.5.137:6379 daf9464ef45d0c73e1ee18f7cc7ddbc7d9719f2d
>>> Removing node daf9464ef45d0c73e1ee18f7cc7ddbc7d9719f2d from cluster 10.220.5.137:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
查看發現6385節點已經被移除
?
[root@le ~]# redis-trib.rb check 10.220.5.137:6379
>>> Performing Cluster Check (using node 10.220.5.137:6379)
M: 57753754f4c89054ab14e8ec517604c4fc4c8ed5 10.220.5.137:6379
?? slots:0-5460 (5461 slots) master
?? 1 additional replica(s)
M: ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8 10.220.5.137:6381
?? slots:12923-16383 (3461 slots) master
?? 1 additional replica(s)
M: 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d 10.220.5.137:6380
?? slots:5461-12922 (7462 slots) master
?? 1 additional replica(s)
S: 640bb3a62565a0bb74980852fee7e8636cf238bc 10.220.5.137:6383
?? slots: (0 slots) slave
?? replicates 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d
S: b8214c9e4b617c7992e71a2fa07a4a64cfeddf58 10.220.5.137:6384
?? slots: (0 slots) slave
?? replicates ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8
S: 1bc40ffc4c54099f8cf47efdeb82a9f42e717bf6 10.220.5.137:6382
?? slots: (0 slots) slave
?? replicates 57753754f4c89054ab14e8ec517604c4fc4c8ed5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
?
?
2.刪除帶有slot的節點
提示有數據報錯
[root@le ~]# redis-trib.rb del-node 10.220.5.137:6379 ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8
>>> Removing node ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8 from cluster 10.220.5.137:6379
[ERR] Node 10.220.5.137:6381 is not empty! Reshard data away and try again.
需要重新分片
?
[root@le ~]# redis-trib.rb reshard 10.220.5.137:6379
>>> Performing Cluster Check (using node 10.220.5.137:6379)
M: 57753754f4c89054ab14e8ec517604c4fc4c8ed5 10.220.5.137:6379
?? slots:0-5460 (5461 slots) master
?? 1 additional replica(s)
M: ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8 10.220.5.137:6381
?? slots:12923-16383 (3461 slots) master
?? 1 additional replica(s)
M: 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d 10.220.5.137:6380
?? slots:5461-12922 (7462 slots) master
?? 1 additional replica(s)
S: 640bb3a62565a0bb74980852fee7e8636cf238bc 10.220.5.137:6383
?? slots: (0 slots) slave
?? replicates 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d
S: b8214c9e4b617c7992e71a2fa07a4a64cfeddf58 10.220.5.137:6384
?? slots: (0 slots) slave
?? replicates ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8
S: 1bc40ffc4c54099f8cf47efdeb82a9f42e717bf6 10.220.5.137:6382
?? slots: (0 slots) slave
?? replicates 57753754f4c89054ab14e8ec517604c4fc4c8ed5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 3461
What is the receiving node ID? 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d
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:ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8
Source node #2:done
?
再檢查slot已經為空
?
>>> Performing Cluster Check (using node 10.220.5.137:6379)
M: 57753754f4c89054ab14e8ec517604c4fc4c8ed5 10.220.5.137:6379
?? slots:0-5460 (5461 slots) master
?? 1 additional replica(s)
M: ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8 10.220.5.137:6381? #已經為空
?? slots: (0 slots) master
?? 0 additional replica(s)
M: 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d 10.220.5.137:6380
?? slots:5461-16383 (10923 slots) master
?? 2 additional replica(s)
S: 640bb3a62565a0bb74980852fee7e8636cf238bc 10.220.5.137:6383
?? slots: (0 slots) slave
?? replicates 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d
S: b8214c9e4b617c7992e71a2fa07a4a64cfeddf58 10.220.5.137:6384
?? slots: (0 slots) slave
?? replicates 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d
S: 1bc40ffc4c54099f8cf47efdeb82a9f42e717bf6 10.220.5.137:6382
?? slots: (0 slots) slave
?? replicates 57753754f4c89054ab14e8ec517604c4fc4c8ed5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
?
再執行刪除操作
[root@le ~]# redis-trib.rb del-node 10.220.5.137:6379 ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8
>>> Removing node ffadc66b51b6c87ad3c5a33dc9ec39f984bb80e8 from cluster 10.220.5.137:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
再次查看。6381節點已經被刪除
?
[root@le ~]# redis-trib.rb check 10.220.5.137:6379
>>> Performing Cluster Check (using node 10.220.5.137:6379)
M: 57753754f4c89054ab14e8ec517604c4fc4c8ed5 10.220.5.137:6379
?? slots:0-5460 (5461 slots) master
?? 1 additional replica(s)
M: 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d 10.220.5.137:6380
?? slots:5461-16383 (10923 slots) master
?? 2 additional replica(s)
S: 640bb3a62565a0bb74980852fee7e8636cf238bc 10.220.5.137:6383
?? slots: (0 slots) slave
?? replicates 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d
S: b8214c9e4b617c7992e71a2fa07a4a64cfeddf58 10.220.5.137:6384
?? slots: (0 slots) slave
?? replicates 3a9aa9592afc594c7e4206cc82ffb37d46a5b23d
S: 1bc40ffc4c54099f8cf47efdeb82a9f42e717bf6 10.220.5.137:6382
?? slots: (0 slots) slave
?? replicates 57753754f4c89054ab14e8ec517604c4fc4c8ed5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/kesz/p/10960657.html
總結
以上是生活随笔為你收集整理的Redis Cluster集群架构实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 15 FFT及其框图实现
- 下一篇: 在博客园的博客插入代码块(高亮、行号、缩