redis-trib.rb命令詳解

redis-trib.rb命令詳解
redis-trib.rb是官方提供的Redis Cluster的管理工具,無需額外下載,默認(rèn)位于源碼包的src目錄下,但因該工具是用ruby開發(fā)的,所以需要準(zhǔn)備相關(guān)的依賴環(huán)境。

成都創(chuàng)新互聯(lián)公司2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站設(shè)計、成都網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元濰坊做網(wǎng)站,已為上家服務(wù),為濰坊各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108

官網(wǎng):https://redis.io/documentation
中文官網(wǎng):http://www.redis.cn/documentation.html

1 redis-trib.rb支持的操作

[root@node1 src]# ./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.
[root@node1 src]#

支持的操作如下:

  • create:創(chuàng)建集群
  • check:檢查集群
  • info:查看集群信息
  • fix:修復(fù)集群
  • reshard:在線遷移slot
  • rebalance:平衡集群節(jié)點slot數(shù)量
  • add-node:將新節(jié)點加入集群
  • del-node:從集群中刪除節(jié)點
  • set-timeout:設(shè)置集群節(jié)點間心跳連接的超時時間
  • call:在集群全部節(jié)點上執(zhí)行命令
  • import:將外部redis數(shù)據(jù)導(dǎo)入集群

對于check,fix,reshard,del-node,set-timeout,您可以指定集群中任何工作節(jié)點的主機和端口。

2 創(chuàng)建集群

用戶無需指定哪臺節(jié)點為master,哪臺節(jié)點為slave,因為redis內(nèi)部算法幫我們實現(xiàn)了,也可以先創(chuàng)建主節(jié)點,然后再指定從節(jié)點;

  • create
  • --replicas # 可選參數(shù),replicas表示每個master需要有幾個slave。

  • 只有master節(jié)點的創(chuàng)建方式
./redis-trib.rb create 192.168.1.101:7000 192.168.1.102:7000 192.168.1.103:7000 192.168.1.104:7000 192.168.1.105:7000 192.168.1.106:7000
  • 使用 --replicas 1 創(chuàng)建 每個master帶一個 slave 指令
./redis-trib.rb create --replicas 1 192.168.1.101:7000 192.168.1.102:7000 192.168.1.103:7000 192.168.1.104:7000 192.168.1.105:7000 192.168.1.106:7000

16384個槽全部被分配,集群創(chuàng)建成功。注意:給redis-trib.rb的節(jié)點地址必須是不包含任何槽/數(shù)據(jù)的節(jié)點,否則會拒絕創(chuàng)建集群。

關(guān)于主從節(jié)點的選擇及槽的分配,其算法如下:

把節(jié)點按照host分類,這樣保證master節(jié)點能分配到更多的主機中。
遍歷host列表,從每個host列表中彈出一個節(jié)點,放入interleaved數(shù)組。直到所有的節(jié)點都彈出為止。
將interleaved數(shù)組中前master個數(shù)量的節(jié)點保存到masters數(shù)組中。
計算每個master節(jié)點負(fù)責(zé)的slot數(shù)量,16384除以master數(shù)量取整,這里記為N。
遍歷masters數(shù)組,每個master分配N個slot,最后一個master,分配剩下的slot。
接下來為master分配slave,分配算法會盡量保證master和slave節(jié)點不在同一臺主機上。對于分配完指定slave數(shù)量的節(jié)點,還有多余的節(jié)點,也會為這些節(jié)點尋找master。分配算法會遍歷兩次masters數(shù)組。
第一次遍歷master數(shù)組,在余下的節(jié)點列表找到replicas數(shù)量個slave。每個slave為第一個和master節(jié)點host不一樣的節(jié)點,如果沒有不一樣的節(jié)點,則直接取出余下列表的第一個節(jié)點。
第二次遍歷是分配節(jié)點數(shù)除以replicas不為整數(shù)而多出的一部分節(jié)點。

3 檢查集群情況:check

指定任意一個節(jié)點即可。

./redis-trib.rb check 192.168.1.101:7000

顯示:

[root@NUC-2 src]# ./redis-trib.rb check 192.168.1.101:7000
>>> Performing Cluster Check (using node 192.168.1.101:7000)
S: afaa82815a7fc2d0e19ffa664677dde03aa8ab36 192.168.1.101:7000
   slots: (0 slots) slave
   replicates 845674c71b1f43f9297501903e616140b2a0a1f6
M: b7cada75939d960f8ca98aad875b8f2e49020b19 192.168.1.102:7000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: f1946135289c8c76bb9817213cbb1730d9a0b052 192.168.1.103:7000
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 9f9a36f2da46bd18f9df785900b5a8e3c53b5ca3 192.168.1.106:7000
   slots: (0 slots) slave
   replicates b7cada75939d960f8ca98aad875b8f2e49020b19
M: 845674c71b1f43f9297501903e616140b2a0a1f6 192.168.1.105:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 15fc1bd754a2e02ec571e23c7d81a80c7322a100 192.168.1.104:7000
   slots: (0 slots) slave
   replicates f1946135289c8c76bb9817213cbb1730d9a0b052
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@NUC-2 src]#

4 查看集群信息:info

[root@NUC-2 src]# ./redis-trib.rb info 192.168.20.61:7000
192.168.20.62:7000 (b7cada75...) -> 2 keys | 5462 slots | 1 slaves.
192.168.20.63:7000 (f1946135...) -> 2 keys | 5461 slots | 1 slaves.
192.168.20.65:7000 (845674c7...) -> 4 keys | 5461 slots | 1 slaves.
[OK] 8 keys in 3 masters.
0.00 keys per slot on average.
[root@NUC-2 src]#

5 修復(fù)集群

目前fix命令能修復(fù)兩種異常:

  1. 節(jié)點中存在處于遷移中(importing或migrating狀態(tài))的slot。
  2. 節(jié)點中存在未分配的slot。
    其它異常不能通過fix命令修復(fù)。
[root@NUC-2 src]# ./redis-trib.rb fix 192.168.20.61:7000
>>> Performing Cluster Check (using node 192.168.20.61:7000)
S: afaa82815a7fc2d0e19ffa664677dde03aa8ab36 192.168.20.61:7000
   slots: (0 slots) slave
   replicates 845674c71b1f43f9297501903e616140b2a0a1f6
M: b7cada75939d960f8ca98aad875b8f2e49020b19 192.168.20.62:7000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: f1946135289c8c76bb9817213cbb1730d9a0b052 192.168.20.63:7000
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 9f9a36f2da46bd18f9df785900b5a8e3c53b5ca3 192.168.20.66:7000
   slots: (0 slots) slave
   replicates b7cada75939d960f8ca98aad875b8f2e49020b19
M: 845674c71b1f43f9297501903e616140b2a0a1f6 192.168.20.65:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 15fc1bd754a2e02ec571e23c7d81a80c7322a100 192.168.20.64:7000
   slots: (0 slots) slave
   replicates f1946135289c8c76bb9817213cbb1730d9a0b052
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@NUC-2 src]#

6 在線遷移:slot

格式:

redis-trib.rb reshard host:port --from <arg> --to <arg> --slots <arg> --yes --timeout <arg> --pipeline <arg>

選項:

  • reshard
  • host:port:必傳參數(shù),集群內(nèi)任意節(jié)點地址,用來獲取整個集群信息。
  • --from <arg>:需要從哪些源節(jié)點上遷移slot,可從多個源節(jié)點完成遷移,以逗號隔開,傳遞的是節(jié)點的node id,還可以直接傳遞--from all,這樣源節(jié)點就是集群的所有節(jié)點,不傳遞該參數(shù)的話,則會在遷移過程中提示用戶輸入。
  • --to <arg>:slot需要遷移的目的節(jié)點的node id,目的節(jié)點只能填寫一個,不傳遞該參數(shù)的話,則會在遷移過程中提示用戶輸入。
  • --slots <arg>:需要遷移的slot數(shù)量,不傳遞該參數(shù)的話,則會在遷移過程中提示用戶輸入。
  • --yes:設(shè)置該參數(shù),可以在打印執(zhí)行reshard計劃的時候,提示用戶輸入yes確認(rèn)后再執(zhí)行reshard。
  • --timeout <arg>:控制每次migrate操作的超時時間,默認(rèn)為60000毫秒。
  • --pipeline <arg>:定義cluster getkeysinslot命令一次取出的key數(shù)量,不傳的話使用默認(rèn)值為10。

例:

./redis-trib.rb reshard --from all --to 845674c71b1f43f9297501903e616140b2a0a1f6 --slots 11 

7 平衡集群節(jié)點slot數(shù)量

選項:

rebalance       host:port
                  --weight <arg>
                  --auto-weights
                  --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>

說明

  • rebalance
  • host:port:這個是必傳參數(shù),用來從一個節(jié)點獲取整個集群信息,相當(dāng)于獲取集群信息的入口。
  • --weight <arg>:節(jié)點的權(quán)重,格式為node_id=weight,如果需要為多個節(jié)點分配權(quán)重的話,需要添加多個--weight <arg>參數(shù),即--weight b31e3a2e=5 --weight 60b8e3a1=5,node_id可為節(jié)點名稱的前綴,只要保證前綴位數(shù)能唯一區(qū)分該節(jié)點即可。沒有傳遞–weight的節(jié)點的權(quán)重默認(rèn)為1。
  • --auto-weights:這個參數(shù)在rebalance流程中并未用到。
  • --threshold <arg>:只有節(jié)點需要遷移的slot閾值超過threshold,才會執(zhí)行rebalance操作。具體計算方法可以參考下面的rebalance命令流程的第四步。
  • --use-empty-masters:rebalance是否考慮沒有節(jié)點的master,默認(rèn)沒有分配slot節(jié)點的master是不參與rebalance的,設(shè)置--use-empty-masters可以讓沒有分配slot的節(jié)點參與rebalance。
  • --timeout <arg>:設(shè)置migrate命令的超時時間。
  • --simulate:設(shè)置該參數(shù),可以模擬rebalance操作,提示用戶會遷移哪些slots,而不會真正執(zhí)行遷移操作。
  • --pipeline <arg>:與reshar的pipeline參數(shù)一樣,定義cluster getkeysinslot命令一次取出的key數(shù)量,不傳的話使用默認(rèn)值為10。

例:

redis-trib.rb rebalance --weight 845674c71b1f43f9297501903e616140b2a0a1f6=3 --weight b7cada75939d960f8ca98aad875b8f2e49020b19=2 --use-empty-masters  192.168.20.61:7000

增加一個主節(jié)點

./redis-trib.rb add-node 192.168.1.107:7000 192.168.1.108:7000

# 添加成功,但是并沒有指定 slot ,所以必須遷移slot節(jié)點
./redis-trib.rb reshard 192.168.1.108:7000

# 提示一 :How many slots do you want to move (from 1 to 16384)?
為了平衡每個master管理的slot的個數(shù),所以輸入 16384/master  的數(shù)量。如這里為4 那么就是 16384/4 = 4096個。
輸入 4096

# 提示二:What is the receiving node ID?(接受的node ID是多少)
            890d2c8d989cce50e5fa48e37cd35738887f3f7d # 107的ID

# 提示三: 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.
                  (要從哪個節(jié)點中獲取lost ?)
不打算從特定的節(jié)點上取出指定數(shù)量的哈希槽, 那么可以輸入 all
否則輸入某個節(jié)點的 node ID

# 檢查是否成功
./redis-trib.rb check 192.168.1.108:7000

添加新節(jié)點

格式:

redis-trib add-node new_host:new_port existing_host:existing_port --slave --master-id <arg>

其中,
new_host:new_port:待添加的節(jié)點,必須確保其為空或不在其它集群中。否則,會提示以下錯誤。

[ERR] Node 192.168.1.101:7000 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é)點,因為其會對新節(jié)點的狀態(tài)進(jìn)行檢查。如果手動使用cluster meet命令加入已經(jīng)存在于其它集群的節(jié)點,會造成被加入節(jié)點的集群合并到現(xiàn)有集群的情況,從而造成數(shù)據(jù)丟失和錯亂,后果非常嚴(yán)重,線上謹(jǐn)慎操作。

existing_host:existing_port:集群中任意一個節(jié)點的地址。

如果添加的是主節(jié)點,只需指定源節(jié)點和目標(biāo)節(jié)點的地址即可。

redis-trib.rb add-node 192.168.1.101:7000 127.0.0.1:6384

如果添加的是從節(jié)點,其語法如下,

redis-trib.rb add-node --slave --master-id f413fb7e6460308b17cdb71442798e1341b56cbc 192.168.1.101:7000 127.0.0.1:6384

注意:--slave和--master-id必須寫在前面,同樣的參數(shù),如果是下面這樣寫法,會提示錯誤,

# redis-trib.rb add-node 192.168.1.101:7000 127.0.0.1:6384 --slave --master-id f413fb7e6460308b17cdb71442798e1341b56cbc
[ERR] Wrong number of arguments for specified sub command

添加從節(jié)點,可不設(shè)置--master-id,此時會隨機選擇主節(jié)點。

  • 從節(jié)點會自動匹配主節(jié)點
./redis-trib.rb add-node --slave 127.0.0.1:7007 127.0.0.1:7000
  • 增加從節(jié)點的時候指定主節(jié)點。
./redis-trib.rb add-node --slave --master-id 890d2c8d989cce50e5fa48e37cd35738887f3f7d 192.168.66.3:7008 192.168.66.2:7000

刪除節(jié)點

格式

redis-trib.rb del-node host:port node_id
參數(shù):
  • del-node:刪除節(jié)點的指令;
  • host:port:從該節(jié)點獲取集群信息;
  • node_id:需要刪除的節(jié)點id。

在刪除節(jié)點之前,其對應(yīng)的槽必須為空,所以,在進(jìn)行節(jié)點刪除動作之前,必須使用redis-trib.rb reshard將其遷移出去。

需要注意的是,如果某個節(jié)點的槽被完全遷移出去,其對應(yīng)的slave也會隨著更新,指向遷移的目標(biāo)節(jié)點。

./redis-trib.rb del-node 192.168.66.2:7000 d5f6d1d17426bd564a6e309f32d0f5b96962fe53

設(shè)置節(jié)點的超時時間

redis-trib.rb set-timeout host:port milliseconds

其實就是批量修改集群各節(jié)點的cluster-node-timeout參數(shù)。

# redis-trib.rb set-timeout 192.168.1.101:7000 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 192.168.1.101:7000
*** New timeout set for 192.168.1.102:7000
*** New timeout set for 192.168.1.103:7000
*** New timeout set for 192.168.1.104:7000
*** New timeout set for 192.168.1.105:7000
*** New timeout set for 192.168.1.106:7000
>>> New node timeout set. 6 OK, 0 ERR.

將外部redis數(shù)據(jù)導(dǎo)入集群

redis-trib.rb import --from 127.0.0.1:6378 127.0.0.1:6379

其內(nèi)部處理流程如下:

1> 通過load_cluster_info_from_node方法加載集群信息,check_cluster方法檢查集群是否健康。

2> 連接外部redis節(jié)點,如果外部節(jié)點開啟了cluster_enabled,則提示錯誤([ERR] The source node should not be a cluster node.)

3> 通過scan命令遍歷外部節(jié)點,一次獲取1000條數(shù)據(jù)。

4> 遍歷這些key,計算出key對應(yīng)的slot。

5> 執(zhí)行migrate命令,源節(jié)點是外部節(jié)點,目的節(jié)點是集群slot對應(yīng)的節(jié)點,如果設(shè)置了--copy參數(shù),則傳遞copy參數(shù),其會保留源節(jié)點的key,如果設(shè)置了--replace,則傳遞replace參數(shù)。如果目標(biāo)節(jié)點中存在同名key,其值會被覆蓋。兩個參數(shù)可同時指定。

6> 不停執(zhí)行scan命令,直到遍歷完所有key。

7> 遷移完成。

宕機情況

當(dāng)某個從節(jié)點掛掉之后,對于redis集群來說幾乎沒有什么影響,相當(dāng)于這個從節(jié)點對應(yīng)的 主節(jié)點少了一個備份而已。
當(dāng)某一個主節(jié)點掛掉之后,redis 會從這個 主節(jié)點 的 多個從節(jié)點 中推選一個出來,擔(dān)當(dāng)master的工作,并且把之前依附在
主節(jié)點的從節(jié)點調(diào)整依附到新的master上。如果新任的master也掛掉并且他沒有從節(jié)點了,那么這個集群也真正的掛掉了。

集群創(chuàng)建時 replicas 參數(shù) 指定情況。

使用 --replicas 1 參數(shù)時,如果節(jié)點數(shù)量少于六個。
報錯

*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 5 nodes and 1 replicas per node.
*** At least 6 nodes are required.

使用 --replicas 1 參數(shù)時,如果節(jié)點數(shù)量 大于六個,且為單數(shù)時。
這樣會造成某個master擁有兩個salve

參考

https://www.cnblogs.com/ivictor/p/9768010.html

文章標(biāo)題:redis-trib.rb命令詳解
文章分享:http://bm7419.com/article20/iihhco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航關(guān)鍵詞優(yōu)化、企業(yè)網(wǎng)站制作、網(wǎng)站內(nèi)鏈、動態(tài)網(wǎng)站、微信小程序

廣告

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

h5響應(yīng)式網(wǎng)站建設(shè)