redis-trib.rb命令詳解
redis-trib.rb是官方提供的Redis Cluster的管理工具,無(wú)需額外下載,默認(rèn)位于源碼包的src目錄下,但因該工具是用ruby開(kāi)發(fā)的,所以需要準(zhǔn)備相關(guān)的依賴(lài)環(huán)境。
準(zhǔn)備redis-trib.rb的運(yùn)行環(huán)境
wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.gz yum -y install zlib-devel tar xvf ruby-2.5.1.tar.gz cd ruby-2.5.1/ ./configure -prefix=/usr/local/ruby make make install cd /usr/local/ruby/ cp bin/ruby /usr/local/bin cp bin/gem /usr/local/bin
安裝rubygem redis依賴(lài)
wget http://rubygems.org/downloads/redis-3.3.0.gem gem install -l redis-3.3.0.gem
redis-trib.rb支持的操作
# redis-trib.rb help 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.
支持的操作如下:
1. create:創(chuàng)建集群
2. check:檢查集群
3. info:查看集群信息
4. fix:修復(fù)集群
5. reshard:在線遷移slot
6. rebalance:平衡集群節(jié)點(diǎn)slot數(shù)量
7. add-node:添加新節(jié)點(diǎn)
8. del-node:刪除節(jié)點(diǎn)
9. set-timeout:設(shè)置節(jié)點(diǎn)的超時(shí)時(shí)間
10. call:在集群所有節(jié)點(diǎn)上執(zhí)行命令
11. import:將外部redis數(shù)據(jù)導(dǎo)入集群
創(chuàng)建集群
redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384
--replicas參數(shù)指定集群中每個(gè)主節(jié)點(diǎn)配備幾個(gè)從節(jié)點(diǎn),這里設(shè)置為1。
>>> Creating cluster /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 Adding replica 127.0.0.1:6383 to 127.0.0.1:6379 Adding replica 127.0.0.1:6384 to 127.0.0.1:6380 Adding replica 127.0.0.1:6382 to 127.0.0.1:6381 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: bc775f9c4dea40820b82c9451778b1fcd42f92bc 127.0.0.1:6379 slots:0-5460 (5461 slots) master M: 3b27d00d13706a032a92ff6b0a914af272dcaaf2 127.0.0.1:6380 slots:5461-10922 (5462 slots) master M: d874f003257f1fb036bbd856ca605172a1741232 127.0.0.1:6381 slots:10923-16383 (5461 slots) master S: 648eb314863b82aaa676380be7db2ec307f5547d 127.0.0.1:6382 replicates bc775f9c4dea40820b82c9451778b1fcd42f92bc S: 65a6efb441ac44c348f7da8c62e26b888cda7c48 127.0.0.1:6383 replicates 3b27d00d13706a032a92ff6b0a914af272dcaaf2 S: 57bda956485109552547aef6c77fba43d2124abf 127.0.0.1:6384 replicates d874f003257f1fb036bbd856ca605172a1741232 Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join... >>> Performing Cluster Check (using node 127.0.0.1:6379) M: bc775f9c4dea40820b82c9451778b1fcd42f92bc 127.0.0.1:6379 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 648eb314863b82aaa676380be7db2ec307f5547d 127.0.0.1:6382 slots: (0 slots) slave replicates bc775f9c4dea40820b82c9451778b1fcd42f92bc M: 3b27d00d13706a032a92ff6b0a914af272dcaaf2 127.0.0.1:6380 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 57bda956485109552547aef6c77fba43d2124abf 127.0.0.1:6384 slots: (0 slots) slave replicates d874f003257f1fb036bbd856ca605172a1741232 S: 65a6efb441ac44c348f7da8c62e26b888cda7c48 127.0.0.1:6383 slots: (0 slots) slave replicates 3b27d00d13706a032a92ff6b0a914af272dcaaf2 M: d874f003257f1fb036bbd856ca605172a1741232 127.0.0.1:6381 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.
16384個(gè)槽全部被分配,集群創(chuàng)建成功。注意:給redis-trib.rb的節(jié)點(diǎn)地址必須是不包含任何槽/數(shù)據(jù)的節(jié)點(diǎn),否則會(huì)拒絕創(chuàng)建集群。
>>> Creating cluster /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated [ERR] Node 127.0.0.1:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
關(guān)于主從節(jié)點(diǎn)的選擇及槽的分配,其算法如下:
1> 把節(jié)點(diǎn)按照host分類(lèi),這樣保證master節(jié)點(diǎn)能分配到更多的主機(jī)中。
2> 遍歷host列表,從每個(gè)host列表中彈出一個(gè)節(jié)點(diǎn),放入interleaved數(shù)組。直到所有的節(jié)點(diǎn)都彈出為止。
3> 將interleaved數(shù)組中前master個(gè)數(shù)量的節(jié)點(diǎn)保存到masters數(shù)組中。
4> 計(jì)算每個(gè)master節(jié)點(diǎn)負(fù)責(zé)的slot數(shù)量,16384除以master數(shù)量取整,這里記為N。
5> 遍歷masters數(shù)組,每個(gè)master分配N(xiāo)個(gè)slot,最后一個(gè)master,分配剩下的slot。
6> 接下來(lái)為master分配slave,分配算法會(huì)盡量保證master和slave節(jié)點(diǎn)不在同一臺(tái)主機(jī)上。對(duì)于分配完指定slave數(shù)量的節(jié)點(diǎn),還有多余的節(jié)點(diǎn),也會(huì)為這些節(jié)點(diǎn)尋找master。分配算法會(huì)遍歷兩次masters數(shù)組。
7> 第一次遍歷master數(shù)組,在余下的節(jié)點(diǎn)列表找到replicas數(shù)量個(gè)slave。每個(gè)slave為第一個(gè)和master節(jié)點(diǎn)host不一樣的節(jié)點(diǎn),如果沒(méi)有不一樣的節(jié)點(diǎn),則直接取出余下列表的第一個(gè)節(jié)點(diǎn)。
8> 第二次遍歷是分配節(jié)點(diǎn)數(shù)除以replicas不為整數(shù)而多出的一部分節(jié)點(diǎn)。
檢查集群狀態(tài)
redis-trib.rb check 127.0.0.1:6379
指定任意一個(gè)節(jié)點(diǎn)即可。
/usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated >>> Performing Cluster Check (using node 127.0.0.1:6379) M: bc775f9c4dea40820b82c9451778b1fcd42f92bc 127.0.0.1:6379 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 648eb314863b82aaa676380be7db2ec307f5547d 127.0.0.1:6382 slots: (0 slots) slave replicates bc775f9c4dea40820b82c9451778b1fcd42f92bc M: 3b27d00d13706a032a92ff6b0a914af272dcaaf2 127.0.0.1:6380 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 57bda956485109552547aef6c77fba43d2124abf 127.0.0.1:6384 slots: (0 slots) slave replicates d874f003257f1fb036bbd856ca605172a1741232 S: 65a6efb441ac44c348f7da8c62e26b888cda7c48 127.0.0.1:6383 slots: (0 slots) slave replicates 3b27d00d13706a032a92ff6b0a914af272dcaaf2 M: d874f003257f1fb036bbd856ca605172a1741232 127.0.0.1:6381 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.rb info 127.0.0.1:6383
/usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated 127.0.0.1:6380 (3b27d00d...) -> 0 keys | 5462 slots | 1 slaves. 127.0.0.1:6381 (d874f003...) -> 1 keys | 5461 slots | 1 slaves. 127.0.0.1:6379 (bc775f9c...) -> 0 keys | 5461 slots | 1 slaves. [OK] 1 keys in 3 masters. 0.00 keys per slot on average.
修復(fù)集群
目前fix命令能修復(fù)兩種異常,
1. 節(jié)點(diǎn)中存在處于遷移中(importing或migrating狀態(tài))的slot。
2. 節(jié)點(diǎn)中存在未分配的slot。
其它異常不能通過(guò)fix命令修復(fù)。
[root@slowtech conf]# redis-trib.rb fix 127.0.0.1:6379 /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated >>> Performing Cluster Check (using node 127.0.0.1:6379) S: d826c5fd98efa8a17a880e9a90a25f06c88e6ae9 127.0.0.1:6379 slots: (0 slots) slave replicates a8b3d0f9b12d63dab3b7337d602245d96dd55844 S: 55c05d5b0dfea0d52f88548717ddf24975268de6 127.0.0.1:6383 slots: (0 slots) slave replicates a8b3d0f9b12d63dab3b7337d602245d96dd55844 M: f413fb7e6460308b17cdb71442798e1341b56cbc 127.0.0.1:6381 slots:50-16383 (16334 slots) master 2 additional replica(s) S: beba753c5a63607fa66d9ec7427ed9a511ea136e 127.0.0.1:6382 slots: (0 slots) slave replicates f413fb7e6460308b17cdb71442798e1341b56cbc S: 83797d518e56c235272402611477f576973e9d34 127.0.0.1:6384 slots: (0 slots) slave replicates f413fb7e6460308b17cdb71442798e1341b56cbc M: a8b3d0f9b12d63dab3b7337d602245d96dd55844 127.0.0.1:6380 slots:0-49 (50 slots) master 2 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
在線遷移slot
slot計(jì)算方法
slot ( 4096 ) = 16384 / master 個(gè)數(shù)(4)
交互環(huán)境中使用
如,就是將127.0.0.1:6379的slot遷移到3b27d00d13706a032a92ff6b0a914af272dcaaf2 127.0.0.1:6380
redis-trib.rb reshard 127.0.0.1:6379
指定任意一個(gè)節(jié)點(diǎn)即可。
/usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated >>> Performing Cluster Check (using node 127.0.0.1:6379) M: bc775f9c4dea40820b82c9451778b1fcd42f92bc 127.0.0.1:6379 slots:3225-5460 (2236 slots) master 1 additional replica(s) S: 648eb314863b82aaa676380be7db2ec307f5547d 127.0.0.1:6382 slots: (0 slots) slave replicates bc775f9c4dea40820b82c9451778b1fcd42f92bc M: 3b27d00d13706a032a92ff6b0a914af272dcaaf2 127.0.0.1:6380 slots:0-3224,5461-13958 (11723 slots) master 1 additional replica(s) S: 57bda956485109552547aef6c77fba43d2124abf 127.0.0.1:6384 slots: (0 slots) slave replicates d874f003257f1fb036bbd856ca605172a1741232 S: 65a6efb441ac44c348f7da8c62e26b888cda7c48 127.0.0.1:6383 slots: (0 slots) slave replicates 3b27d00d13706a032a92ff6b0a914af272dcaaf2 M: d874f003257f1fb036bbd856ca605172a1741232 127.0.0.1:6381 slots:13959-16383 (2425 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. How many slots do you want to move (from 1 to 16384)? 200 What is the receiving node ID? 3b27d00d13706a032a92ff6b0a914af272dcaaf2 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:
它首先會(huì)提示需要遷移多個(gè)槽,我這里寫(xiě)的是200。
接著它會(huì)提示需要將槽遷移到哪個(gè)節(jié)點(diǎn),這里必須寫(xiě)節(jié)點(diǎn)ID。
緊跟著它會(huì)提示槽從哪些節(jié)點(diǎn)中遷出。
如果指定為all,則待遷移的槽在剩余節(jié)點(diǎn)中平均分配,在這里,127.0.0.1:6379和127.0.0.1:6381各遷移100個(gè)槽出來(lái)。
也可從指定節(jié)點(diǎn)中遷出,這個(gè)時(shí)候,必須指定源節(jié)點(diǎn)的節(jié)點(diǎn)ID,最后以done結(jié)束,如下所示,
Source node #1:bc775f9c4dea40820b82c9451778b1fcd42f92bc Source node #2:done Ready to move 200 slots. Source nodes: M: bc775f9c4dea40820b82c9451778b1fcd42f92bc 127.0.0.1:6379 slots:3225-5460 (2236 slots) master 1 additional replica(s) Destination node: M: 3b27d00d13706a032a92ff6b0a914af272dcaaf2 127.0.0.1:6380 slots:0-3224,5461-13958 (11723 slots) master 1 additional replica(s) Resharding plan: Moving slot 3225 from bc775f9c4dea40820b82c9451778b1fcd42f92bc Moving slot 3226 from bc775f9c4dea40820b82c9451778b1fcd42f92bc Moving slot 3227 from bc775f9c4dea40820b82c9451778b1fcd42f92bc ... Do you want to proceed with the proposed reshard plan (yes/no)? yes Moving slot 3225 from 127.0.0.1:6379 to 127.0.0.1:6380: . Moving slot 3226 from 127.0.0.1:6379 to 127.0.0.1:6380: Moving slot 3227 from 127.0.0.1:6379 to 127.0.0.1:6380: .. Moving slot 3228 from 127.0.0.1:6379 to 127.0.0.1:6380: ...
最后,提示是否繼續(xù)進(jìn)行。
命令行中使用
redis-trib.rb reshard host:port --from <arg> --to <arg> --slots <arg> --yes --timeout <arg> --pipeline <arg>
其中,
host:port:必傳參數(shù),集群內(nèi)任意節(jié)點(diǎn)地址,用來(lái)獲取整個(gè)集群信息。
--from:源節(jié)點(diǎn)id,如果有多個(gè)源節(jié)點(diǎn),使用逗號(hào)分隔,如果是all,則源節(jié)點(diǎn)為集群內(nèi)出目標(biāo)節(jié)點(diǎn)外的其它所有主節(jié)點(diǎn)。
--to:目標(biāo)節(jié)點(diǎn)id,只能填寫(xiě)一個(gè)。
--slots:需要遷移槽的總數(shù)量。
--yes:遷移無(wú)需用戶(hù)手動(dòng)確認(rèn)。
--timeout:控制每次migrate操作的超時(shí)時(shí)間,默認(rèn)為60000毫秒。
--pipeline:控制每次批量遷移鍵的數(shù)量,默認(rèn)為10。
如,
redis-trib.rb reshard --from a8b3d0f9b12d63dab3b7337d602245d96dd55844 --to f413fb7e6460308b17cdb71442798e1341b56cbc --slots 10923 --yes --pipeline 20 127.0.0.1:6383
平衡集群節(jié)點(diǎn)slot數(shù)量
rebalance host:port --weight <arg> --auto-weights --use-empty-masters --timeout <arg> --simulate --pipeline <arg> --threshold <arg>
其中,
--weight <arg>:節(jié)點(diǎn)的權(quán)重,格式為node_id=weight,如果需要為多個(gè)節(jié)點(diǎn)分配權(quán)重的話,需要添加多個(gè)--weight <arg>參數(shù),即--weight b31e3a2e=5 --weight 60b8e3a1=5,node_id可為節(jié)點(diǎn)名稱(chēng)的前綴,只要保證前綴位數(shù)能唯一區(qū)分該節(jié)點(diǎn)即可。沒(méi)有傳遞–weight的節(jié)點(diǎn)的權(quán)重默認(rèn)為1。
--auto-weights:自動(dòng)將每個(gè)節(jié)點(diǎn)的權(quán)重默認(rèn)為1。如果--weight和--auto-weights同時(shí)指定,則--auto-weights會(huì)覆蓋前者。
--threshold <arg>:只有節(jié)點(diǎn)需要遷移的slot閾值超過(guò)threshold,才會(huì)執(zhí)行rebalance操作。
--use-empty-masters:默認(rèn)沒(méi)有分配slot節(jié)點(diǎn)的master是不參與rebalance的。如果要讓其參與rebalance,需添加該參數(shù)。
--timeout <arg>:設(shè)置migrate命令的超時(shí)時(shí)間。
--simulate:設(shè)置該參數(shù),只會(huì)提示用戶(hù)會(huì)遷移哪些slots,而不會(huì)執(zhí)行真正的遷移操作。
--pipeline <arg>:定義cluster getkeysinslot命令一次取出的key數(shù)量,不傳的話使用默認(rèn)值為10。
如,
# redis-trib.rb rebalance --weight a8b3d0f9b12d63dab3b7337d602245d96dd55844=3 --weight f413fb7e6460308b17cdb71442798e1341b56cbc=2 --use-empty-masters 127.0.0.1:6379 /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated >>> Performing Cluster Check (using node 127.0.0.1:6379) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Rebalancing across 2 nodes. Total weight = 5.0 Moving 3824 slots from 127.0.0.1:6380 to 127.0.0.1:6381 #########################################...
刪除節(jié)點(diǎn)
redis-trib.rb del-node host:port node_id
在刪除節(jié)點(diǎn)之前,其對(duì)應(yīng)的槽必須為空,所以,在進(jìn)行節(jié)點(diǎn)刪除動(dòng)作之前,必須使用redis-trib.rb reshard將其遷移出去。
需要注意的是,如果某個(gè)節(jié)點(diǎn)的槽被完全遷移出去,其對(duì)應(yīng)的slave也會(huì)隨著更新,指向遷移的目標(biāo)節(jié)點(diǎn)。
# redis-trib.rb del-node 127.0.0.1:6379 8f7836a9a14fb6638530b42e04f5e58e28de0a6c >>> Removing node 8f7836a9a14fb6638530b42e04f5e58e28de0a6c from cluster 127.0.0.1:6379 /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node.
添加新節(jié)點(diǎn)
redis-trib add-node new_host:new_port existing_host:existing_port --slave --master-id <arg>
其中,
new_host:new_port:待添加的節(jié)點(diǎn),必須確保其為空或不在其它集群中。否則,會(huì)提示以下錯(cuò)誤。
[ERR] Node 127.0.0.1:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
所以,線上建議使用redis-trib.rb添加新節(jié)點(diǎn),因?yàn)槠鋾?huì)對(duì)新節(jié)點(diǎn)的狀態(tài)進(jìn)行檢查。如果手動(dòng)使用cluster meet命令加入已經(jīng)存在于其它集群的節(jié)點(diǎn),會(huì)造成被加入節(jié)點(diǎn)的集群合并到現(xiàn)有集群的情況,從而造成數(shù)據(jù)丟失和錯(cuò)亂,后果非常嚴(yán)重,線上謹(jǐn)慎操作。
existing_host:existing_port:集群中任意一個(gè)節(jié)點(diǎn)的地址。
如果添加的是主節(jié)點(diǎn),只需指定源節(jié)點(diǎn)和目標(biāo)節(jié)點(diǎn)的地址即可。
redis-trib.rb add-node 127.0.0.1:6379 127.0.0.1:6384
如果添加的是從節(jié)點(diǎn),其語(yǔ)法如下,
redis-trib.rb add-node --slave --master-id f413fb7e6460308b17cdb71442798e1341b56cbc 127.0.0.1:6379 127.0.0.1:6384
注意:--slave和--master-id必須寫(xiě)在前面,同樣的參數(shù),如果是下面這樣寫(xiě)法,會(huì)提示錯(cuò)誤,
# redis-trib.rb add-node 127.0.0.1:6379 127.0.0.1:6384 --slave --master-id f413fb7e6460308b17cdb71442798e1341b56cbc[ERR] Wrong number of arguments for specified sub command
添加從節(jié)點(diǎn),可不設(shè)置--master-id,此時(shí)會(huì)隨機(jī)選擇主節(jié)點(diǎn)。
設(shè)置節(jié)點(diǎn)的超時(shí)時(shí)間
redis-trib.rb set-timeout host:port milliseconds
其實(shí)就是批量修改集群各節(jié)點(diǎn)的cluster-node-timeout參數(shù)。
# redis-trib.rb set-timeout 127.0.0.1:6379 20000 /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated >>> Reconfiguring node timeout in every cluster node... *** New timeout set for 127.0.0.1:6379 *** New timeout set for 127.0.0.1:6383 *** New timeout set for 127.0.0.1:6381 *** New timeout set for 127.0.0.1:6382 *** New timeout set for 127.0.0.1:6384 *** New timeout set for 127.0.0.1:6380 >>> New node timeout set. 6 OK, 0 ERR.
在集群所有節(jié)點(diǎn)上執(zhí)行命令
redis-trib.rb call host:port command arg arg .. arg
如,
[root@slowtech conf]# redis-trib.rb call 127.0.0.1:6379 set hello world /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated >>> Calling SET hello world 127.0.0.1:6379: MOVED 866 127.0.0.1:6381 127.0.0.1:6383: MOVED 866 127.0.0.1:6381 127.0.0.1:6381: OK 127.0.0.1:6382: MOVED 866 127.0.0.1:6381 127.0.0.1:6384: MOVED 866 127.0.0.1:6381 127.0.0.1:6380: MOVED 866 127.0.0.1:6381 [root@slowtech conf]# redis-trib.rb call 127.0.0.1:6379 get hello /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated >>> Calling GET hello 127.0.0.1:6379: MOVED 866 127.0.0.1:6381 127.0.0.1:6383: MOVED 866 127.0.0.1:6381 127.0.0.1:6381: world 127.0.0.1:6382: MOVED 866 127.0.0.1:6381 127.0.0.1:6384: MOVED 866 127.0.0.1:6381 127.0.0.1:6380: MOVED 866 127.0.0.1:6381
將外部redis數(shù)據(jù)導(dǎo)入集群
redis-trib.rb import --from 127.0.0.1:6378 127.0.0.1:6379
其內(nèi)部處理流程如下:
1> 通過(guò)load_cluster_info_from_node方法加載集群信息,check_cluster方法檢查集群是否健康。
2> 連接外部redis節(jié)點(diǎn),如果外部節(jié)點(diǎn)開(kāi)啟了cluster_enabled,則提示錯(cuò)誤([ERR] The source node should not be a cluster node.)
3> 通過(guò)scan命令遍歷外部節(jié)點(diǎn),一次獲取1000條數(shù)據(jù)。
4> 遍歷這些key,計(jì)算出key對(duì)應(yīng)的slot。
5> 執(zhí)行migrate命令,源節(jié)點(diǎn)是外部節(jié)點(diǎn),目的節(jié)點(diǎn)是集群slot對(duì)應(yīng)的節(jié)點(diǎn),如果設(shè)置了--copy參數(shù),則傳遞copy參數(shù),其會(huì)保留源節(jié)點(diǎn)的key,如果設(shè)置了--replace,則傳遞replace參數(shù)。如果目標(biāo)節(jié)點(diǎn)中存在同名key,其值會(huì)被覆蓋。兩個(gè)參數(shù)可同時(shí)指定。
6> 不停執(zhí)行scan命令,直到遍歷完所有key。
7> 遷移完成。
[root@slowtech conf]# redis-trib.rb import --from 127.0.0.1:6378 --replace 127.0.0.1:6379 >>> Importing data from 127.0.0.1:6378 to cluster /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated >>> Performing Cluster Check (using node 127.0.0.1:6379) S: d826c5fd98efa8a17a880e9a90a25f06c88e6ae9 127.0.0.1:6379 slots: (0 slots) slave replicates a8b3d0f9b12d63dab3b7337d602245d96dd55844 S: 55c05d5b0dfea0d52f88548717ddf24975268de6 127.0.0.1:6383 slots: (0 slots) slave replicates a8b3d0f9b12d63dab3b7337d602245d96dd55844 M: f413fb7e6460308b17cdb71442798e1341b56cbc 127.0.0.1:6381 slots:50-16383 (16334 slots) master 2 additional replica(s) S: beba753c5a63607fa66d9ec7427ed9a511ea136e 127.0.0.1:6382 slots: (0 slots) slave replicates f413fb7e6460308b17cdb71442798e1341b56cbc S: 83797d518e56c235272402611477f576973e9d34 127.0.0.1:6384 slots: (0 slots) slave replicates f413fb7e6460308b17cdb71442798e1341b56cbc M: a8b3d0f9b12d63dab3b7337d602245d96dd55844 127.0.0.1:6380 slots:0-49 (50 slots) master 2 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Connecting to the source Redis instance *** Importing 1 keys from DB 0 Migrating key5 to 127.0.0.1:6381: OK 從redis-trib.rb到集群加減節(jié)點(diǎn)實(shí)戰(zhàn)-CSDN博客https://blog.csdn.net/qq_40687433/article/details/131234885
從redis-trib.rb到集群加減節(jié)點(diǎn)實(shí)戰(zhàn)-CSDN博客https://blog.csdn.net/qq_40687433/article/details/131234885
到此這篇關(guān)于redis-trib.rb命令詳解的文章就介紹到這了,更多相關(guān)redis-trib.rb命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Redis?zSet實(shí)現(xiàn)滑動(dòng)窗口對(duì)短信進(jìn)行防刷限流的問(wèn)題
這篇文章主要介紹了基于Redis?zSet實(shí)現(xiàn)滑動(dòng)窗口對(duì)短信進(jìn)行防刷限流,主要針對(duì)目前線上短信被腳本惡意盜刷的情況,用Redis實(shí)現(xiàn)滑動(dòng)窗口限流,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-02-02Redis中的配置文件,數(shù)據(jù)持久化,事務(wù)
這篇文章主要介紹了Redis中的配置文件,數(shù)據(jù)持久化,事務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-12-12k8s部署redis集群實(shí)現(xiàn)過(guò)程實(shí)例詳解
這篇文章主要為大家介紹了k8s部署redis集群實(shí)現(xiàn)過(guò)程實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02redis底層數(shù)據(jù)結(jié)構(gòu)之ziplist實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了redis底層數(shù)據(jù)結(jié)構(gòu)之ziplist實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Redis實(shí)戰(zhàn)之百度首頁(yè)新聞熱榜的實(shí)現(xiàn)代碼
這篇文章主要介紹了Redis實(shí)戰(zhàn)之百度首頁(yè)新聞熱榜的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02redis redistemplate序列化對(duì)象配置方式
這篇文章主要介紹了redis redistemplate序列化對(duì)象配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12