收藏起來-Redis 集羣管理常見操作 原

Redis 內置客戶端 redis-cli 通過 --cluster 選項集成了多個 Redis 集羣管理工具, 這些工具可以執行創建集羣、向集羣中添加或移除節點、對集羣實施重分片以及負載均衡等操作, 它們每一個都非常強大:

$ redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
  info           host:port
  fix            host:port
  reshard        host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
  add-node       new_host:new_port existing_host:existing_port
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id
  call           host:port command arg arg .. arg
  set-timeout    host:port milliseconds
  import         host:port
                 --cluster-from <arg>
                 --cluster-copy
                 --cluster-replace
  help

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

本文將對上述工具的其中一些進行介紹, 幫助大家初步地瞭解這些工具的基本用法。

創建集羣的工作可以通過 create 命令來完成, 用戶只需要給定各個節點的地址以及想要設置的從服務器數量即可。 比如說, 通過執行以下命令, 我們可以創建出一個由三個主節點和三個從節點組成的集羣:

$ redis-cli --cluster create 127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005 127.0.0.1:30006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30004 to 127.0.0.1:30001
Adding replica 127.0.0.1:30005 to 127.0.0.1:30002
Adding replica 127.0.0.1:30006 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
M: 4ff303d96f5c7436ce8ce2fa6e306272e82cd454 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
M: 07e230805903e4e1657743a2e4d8811a59e2f32f 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
S: 4788fd4d92387fc5d38a2cd12f0c0d80fc0f6609 127.0.0.1:30004
   replicates 4979f8583676c46039672fb7319e917e4b303707
S: b45a7f4355ea733a3177b89654c10f9c31092e92 127.0.0.1:30005
   replicates 4ff303d96f5c7436ce8ce2fa6e306272e82cd454
S: 7c56ffba63e3758bc4c2e9b6a55caf294bb21650 127.0.0.1:30006
   replicates 07e230805903e4e1657743a2e4d8811a59e2f32f
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:30001)
M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 4788fd4d92387fc5d38a2cd12f0c0d80fc0f6609 127.0.0.1:30004
   slots: (0 slots) slave
   replicates 4979f8583676c46039672fb7319e917e4b303707
S: b45a7f4355ea733a3177b89654c10f9c31092e92 127.0.0.1:30005
   slots: (0 slots) slave
   replicates 4ff303d96f5c7436ce8ce2fa6e306272e82cd454
S: 7c56ffba63e3758bc4c2e9b6a55caf294bb21650 127.0.0.1:30006
   slots: (0 slots) slave
   replicates 07e230805903e4e1657743a2e4d8811a59e2f32f
M: 4ff303d96f5c7436ce8ce2fa6e306272e82cd454 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 07e230805903e4e1657743a2e4d8811a59e2f32f 127.0.0.1:30003
   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.

通過執行 info 命令, 用戶可以看到集羣包含的主從節點數量, 以及集羣中的鍵、槽分佈信息:

$ redis-cli --cluster info 127.0.0.1:30001
127.0.0.1:30001 (4979f858...) -> 1 keys | 5461 slots | 1 slaves.
127.0.0.1:30002 (4ff303d9...) -> 4 keys | 5462 slots | 1 slaves.
127.0.0.1:30003 (07e23080...) -> 3 keys | 5461 slots | 1 slaves.
[OK] 8 keys in 3 masters.
0.00 keys per slot on average.

添加節點/移除節點

在擁有了集羣之後, 用戶就可以通過執行 add-node 命令或者 del-node 命令, 向集羣裏面添加新節點又或者移除已有的節點。

比如說, 通過執行以下命令, 我們可以把節點 30007 添加至集羣當中:在此我向大家推薦一個架構學習交流裙。交流學習裙號:821169538,裏面會分享一些資深架構師錄製的視頻錄像 

redis-cli --cluster add-node 127.0.0.1:30007 127.0.0.1:30001
>>> Adding node 127.0.0.1:30007 to cluster 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
...
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:30007 to make it join the cluster.
[OK] New node added correctly.

又或者通過執行以下命令, 將 ID 爲 e1971e... 的節點 30007 移出集羣:

redis-cli --cluster del-node 127.0.0.1:30001 e1971eef02709cf4698a6fcb09935a910982ab3b
>>> Removing node e1971eef02709cf4698a6fcb09935a910982ab3b from cluster 127.0.0.1:30001
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

通過執行 reshard 命令, 用戶可以將指定數量的槽從一個節點遷移至另一個節點。 比如說, 通過執行以下命令, 我們可以將 ID 爲 4979f... 的節點 30001 目前負責的其中 10 個槽轉交給 ID 爲 1cd87d... 的節點 30007 負責:

$ redis-cli --cluster reshard 127.0.0.1:30001 --cluster-from 4979f8583676c46039672fb7319e917e4b303707 --cluster-to 1cd87d132101893b7aa2b81cf333b2f7be9e1b75 --cluster-slots 10
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 4788fd4d92387fc5d38a2cd12f0c0d80fc0f6609 127.0.0.1:30004
   slots: (0 slots) slave
   replicates 4979f8583676c46039672fb7319e917e4b303707
S: b45a7f4355ea733a3177b89654c10f9c31092e92 127.0.0.1:30005
   slots: (0 slots) slave
   replicates 4ff303d96f5c7436ce8ce2fa6e306272e82cd454
M: 1cd87d132101893b7aa2b81cf333b2f7be9e1b75 127.0.0.1:30007
   slots: (0 slots) master
S: 7c56ffba63e3758bc4c2e9b6a55caf294bb21650 127.0.0.1:30006
   slots: (0 slots) slave
   replicates 07e230805903e4e1657743a2e4d8811a59e2f32f
M: 4ff303d96f5c7436ce8ce2fa6e306272e82cd454 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 07e230805903e4e1657743a2e4d8811a59e2f32f 127.0.0.1:30003
   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.

Ready to move 10 slots.
  Source nodes:
    M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
  Destination node:
    M: 1cd87d132101893b7aa2b81cf333b2f7be9e1b75 127.0.0.1:30007
       slots: (0 slots) master
  Resharding plan:
    Moving slot 0 from 4979f8583676c46039672fb7319e917e4b303707
    Moving slot 1 from 4979f8583676c46039672fb7319e917e4b303707
    Moving slot 2 from 4979f8583676c46039672fb7319e917e4b303707
    Moving slot 3 from 4979f8583676c46039672fb7319e917e4b303707
    Moving slot 4 from 4979f8583676c46039672fb7319e917e4b303707
    Moving slot 5 from 4979f8583676c46039672fb7319e917e4b303707
    Moving slot 6 from 4979f8583676c46039672fb7319e917e4b303707
    Moving slot 7 from 4979f8583676c46039672fb7319e917e4b303707
    Moving slot 8 from 4979f8583676c46039672fb7319e917e4b303707
    Moving slot 9 from 4979f8583676c46039672fb7319e917e4b303707
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 0 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 1 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 2 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 3 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 4 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 5 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 6 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 7 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 8 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 9 from 127.0.0.1:30001 to 127.0.0.1:30007:

當集羣中各個節點的槽配置趨於不均衡的時候, 各個節點負擔的載荷就可能會出現區別, 並導致某些節點超負荷運行, 而某些節點則長時間空置。 爲了解決這個問題, 用戶可以通過執行 rebalance 命令, 讓各個節點負責的槽數量重新回到平均狀態。

比如說, 對於以下這個由三個節點組成, 並且三個節點分別負責 2000 、 11384 和 3000 個槽的集羣來說:

$ redis-cli --cluster info 127.0.0.1:30001
127.0.0.1:30001 (61d1f17b...) -> 0 keys | 2000 slots | 1 slaves.
127.0.0.1:30002 (101fbae9...) -> 0 keys | 11384 slots | 1 slaves.
127.0.0.1:30003 (31822e0a...) -> 0 keys | 3000 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

我們可以通過執行以下命令來重新分配各個節點負責的槽:

$ redis-cli --cluster rebalance 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 3 nodes. Total weight = 3.00
Moving 3462 slots from 127.0.0.1:30002 to 127.0.0.1:30001
#####...#####
Moving 2461 slots from 127.0.0.1:30002 to 127.0.0.1:30003
#####...#####

在複雜均衡操作執行完畢之後, 集羣各個節點負責的槽數量將趨於平均, 這樣它們各自負擔的載荷也會重新回到平均狀態:

$ redis-cli --cluster info 127.0.0.1:30001
127.0.0.1:30001 (61d1f17b...) -> 0 keys | 5462 slots | 1 slaves.
127.0.0.1:30002 (101fbae9...) -> 0 keys | 5461 slots | 1 slaves.
127.0.0.1:30003 (31822e0a...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

在集羣的所有節點中執行命令

如果用戶想要對集羣中的所有節點執行同一個命令, 那麼可以通過 call 命令來完成。 比如說, 通過執行以下命令, 用戶就可以讓集羣中的每個節點都執行 DBSIZE 命令, 從而得到各個節點各自的數據庫大小:

$ redis-cli --cluster call 127.0.0.1:30001 DBSIZE
>>> Calling DBSIZE
127.0.0.1:30001: (integer) 0

127.0.0.1:30004: (integer) 0

127.0.0.1:30005: (integer) 3

127.0.0.1:30006: (integer) 0

127.0.0.1:30002: (integer) 3

127.0.0.1:30003: (integer) 0

好的, 關於 redis-cli --cluster 選項的簡單介紹就到此結束了, 希望這些介紹可以幫助大家更好地瞭解到這些命令。 大家有興趣的話, 也可以以此爲起點, 更加深入地學習這些命令。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章