redis集羣高級操作

視頻課程

https://www.bilibili.com/video/av57628808?t=2968

將節點增加到redis集羣中

學會使用命令幫助行解決問題

root@ubuntu-130:/opt/redis/7007#  ./redis-cli  --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
                 --cluster-search-multiple-owners
  info           host:port
  fix            host:port
                 --cluster-search-multiple-owners
  reshard        host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  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.

將7006節點增加到集羣中

root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster add-node 192.168.13.130:7006 192.168.13.130:7003

先查看集羣節點信息,7006端口已經在集羣中了

root@ubuntu-130:/opt/redis/7007# ./redis-cli -h 192.168.13.130 -p 7006 -c
192.168.13.130:7006> CLUSTER nodes
eab93120449b1ef90dfd082941b2cdacc76890c2 192.168.13.130:7004@17004 slave 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 0 1570091944000 2 connected
7a7d5a7b766f3baa018d240b8b8d6868b34bb56a 192.168.13.130:7005@17005 slave 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 0 1570091945229 3 connected
9696373588ef54337542627daa48d30a9d3808f5 192.168.13.130:7006@17006 myself,master - 0 1570091942000 0 connected
d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000@17000 master - 0 1570091943000 1 connected 0-5460
0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001@17001 master - 0 1570091944000 2 connected 5461-10922
604fe9ed88ad20584821ae39f14223faa5b21286 192.168.13.130:7003@17003 slave d2612e7669a28dfeaf03a90b661b8e2bc966af64 0 1570091944215 1 connected
3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002@17002 master - 0 1570091943207 3 connected 10923-16383

增加7007節點,並將它作爲7006的slaver

root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster add-node 192.168.13.130:7007 192.168.13.130:7003 --cluster-slave --cl
>>> Adding node 192.168.13.130:7007 to cluster 192.168.13.130:7003
>>> Performing Cluster Check (using node 192.168.13.130:7003)
S: 604fe9ed88ad20584821ae39f14223faa5b21286 192.168.13.130:7003
   slots: (0 slots) slave
   replicates d2612e7669a28dfeaf03a90b661b8e2bc966af64
M: d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 9696373588ef54337542627daa48d30a9d3808f5 192.168.13.130:7006
   slots: (0 slots) master
S: 7a7d5a7b766f3baa018d240b8b8d6868b34bb56a 192.168.13.130:7005
   slots: (0 slots) slave
   replicates 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c
M: 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eab93120449b1ef90dfd082941b2cdacc76890c2 192.168.13.130:7004
   slots: (0 slots) slave
   replicates 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.13.130:7007 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 192.168.13.130:7006.
[OK] New node added correctly.
root@ubuntu-130:/opt/redis/7007#

 

查看節點信息可以知道,7007已經變更爲7006的slaver了

槽位遷移 (擴容集羣)

重新分配槽位

./redis-cli --cluster reshard 192.168.13.130:7000
>>> Performing Cluster Check (using node 192.168.13.130:7000)
M: d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: eab93120449b1ef90dfd082941b2cdacc76890c2 192.168.13.130:7004
   slots: (0 slots) slave
   replicates 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
M: 9696373588ef54337542627daa48d30a9d3808f5 192.168.13.130:7006
   slots: (0 slots) master
   1 additional replica(s)
S: 18adc88658b01a2f991d6f5ecc8bd6f7a1fe6a6e 192.168.13.130:7007
   slots: (0 slots) slave
   replicates 9696373588ef54337542627daa48d30a9d3808f5
M: 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 7a7d5a7b766f3baa018d240b8b8d6868b34bb56a 192.168.13.130:7005
   slots: (0 slots) slave
   replicates 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c
M: 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 604fe9ed88ad20584821ae39f14223faa5b21286 192.168.13.130:7003
   slots: (0 slots) slave
   replicates d2612e7669a28dfeaf03a90b661b8e2bc966af64
[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)? 4096
#這裏指定給那個nodeID分配
What is the receiving node ID? 9696373588ef54337542627daa48d30a9d3808f5
#下面指定分配策略
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: d2612e7669a28dfeaf03a90b661b8e2bc966af64
Source node #2: 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c
Source node #3: 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
Source node #4: done
#下面是遷移計劃
Ready to move 4096 slots.
  Source nodes:
    M: d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    M: 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    M: 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
  Destination node:
    M: 9696373588ef54337542627daa48d30a9d3808f5 192.168.13.130:7006
       slots: (0 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 5461 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5462 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5463 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5464 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5465 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5466 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5467 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5468 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
Moving slot 12282 from 192.168.13.130:7002 to 192.168.13.130:7006:
Moving slot 12283 from 192.168.13.130:7002 to 192.168.13.130:7006:
Moving slot 12284 from 192.168.13.130:7002 to 192.168.13.130:7006:
Moving slot 12285 from 192.168.13.130:7002 to 192.168.13.130:7006:
Moving slot 12286 from 192.168.13.130:7002 to 192.168.13.130:7006:
Moving slot 12287 from 192.168.13.130:7002 to 192.168.13.130:7006:

遷移計劃是將7000的0~1364槽位,7001的5461-6826,7002的10923-12287遷移到7006節點

槽位遷回:

將7006的0-1364的1065個槽位遷移到7000節點上

root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster reshard 192.168.13.130:7000 --cluster-from 9696373588ef54337542627daa48d30a9d3808f5 --cluster-to d2612e7669a28dfeaf03a90b661b8e2bc966af64 --cluster-slots 1365
>>> Performing Cluster Check (using node 192.168.13.130:7000)
M: d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: eab93120449b1ef90dfd082941b2cdacc76890c2 192.168.13.130:7004
   slots: (0 slots) slave
   replicates 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
M: 9696373588ef54337542627daa48d30a9d3808f5 192.168.13.130:7006
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
S: 18adc88658b01a2f991d6f5ecc8bd6f7a1fe6a6e 192.168.13.130:7007
   slots: (0 slots) slave
   replicates 9696373588ef54337542627daa48d30a9d3808f5
M: 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 7a7d5a7b766f3baa018d240b8b8d6868b34bb56a 192.168.13.130:7005
   slots: (0 slots) slave
   replicates 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c
M: 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 604fe9ed88ad20584821ae39f14223faa5b21286 192.168.13.130:7003
   slots: (0 slots) slave
   replicates d2612e7669a28dfeaf03a90b661b8e2bc966af64
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Ready to move 1365 slots.
  Source nodes:
    M: 9696373588ef54337542627daa48d30a9d3808f5 192.168.13.130:7006
       slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
       1 additional replica(s)
  Destination node:
    M: d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000
       slots:[1365-5460] (4096 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 0 from 9696373588ef54337542627daa48d30a9d3808f5
    Moving slot 1 from 9696373588ef54337542627daa48d30a9d3808f5
    Moving slot 2 from 9696373588ef54337542627daa48d30a9d3808f5
    Moving slot 3 from 9696373588ef54337542627daa48d30a9d3808f5
    Moving slot 4 from 9696373588ef54337542627daa48d30a9d3808f5
    Moving slot 5 from 9696373588ef54337542627daa48d30a9d3808f5

查看遷回情況

刪除節點

查看7006沒有槽位後,就可以刪除節點了,否則會刪除時失敗

oot@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster del-node 192.168.13.130:7000 18adc88658b01a2f991d6f5ecc8bd6f7a1fe6a6e >>> Removing node 18adc88658b01a2f991d6f5ecc8bd6f7a1fe6a6e from cluster 192.168.13.130:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster del-node 192.168.13.130:7000 9696373588ef54337542627daa48d30a9d3808f5 >>> Removing node 9696373588ef54337542627daa48d30a9d3808f5 from cluster 192.168.13.130:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
root@ubuntu-130:/opt/redis/7007# ./redis-cli -h 192.168.13.130 -p 7000 -c
192.168.13.130:7000> CLUSTER nodes
eab93120449b1ef90dfd082941b2cdacc76890c2 192.168.13.130:7004@17004 slave 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 0 15700955 15366 10 connected
3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002@17002 master - 0 1570095516375 11 connected 10923-16383
7a7d5a7b766f3baa018d240b8b8d6868b34bb56a 192.168.13.130:7005@17005 slave 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 0 15700955 14000 11 connected
0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001@17001 master - 0 1570095514335 10 connected 5461-10922
d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000@17000 myself,master - 0 1570095515000 9 connected 0-5460
604fe9ed88ad20584821ae39f14223faa5b21286 192.168.13.130:7003@17003 slave d2612e7669a28dfeaf03a90b661b8e2bc966af64 0 15700955 15000 9 connected

刪除節點7006和7007

建議先刪除slaver節點7007再刪除7006,否則當刪除7006會將7007變爲主節點的(槽位數據都會自動遷移)

root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster del-node 192.168.13.130:7000 18adc88658b01a2f991d6f5ecc8bd6f7a1fe6a6e >>> Removing node 18adc88658b01a2f991d6f5ecc8bd6f7a1fe6a6e from cluster 192.168.13.130:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster del-node 192.168.13.130:7000 9696373588ef54337542627daa48d30a9d3808f5 >>> Removing node 9696373588ef54337542627daa48d30a9d3808f5 from cluster 192.168.13.130:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
root@ubuntu-130:/opt/redis/7007# ./redis-cli -h 192.168.13.130 -p 7000 -c
192.168.13.130:7000> CLUSTER nodes
eab93120449b1ef90dfd082941b2cdacc76890c2 192.168.13.130:7004@17004 slave 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 0 15700955 15366 10 connected
3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002@17002 master - 0 1570095516375 11 connected 10923-16383
7a7d5a7b766f3baa018d240b8b8d6868b34bb56a 192.168.13.130:7005@17005 slave 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 0 15700955 14000 11 connected
0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001@17001 master - 0 1570095514335 10 connected 5461-10922
d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000@17000 myself,master - 0 1570095515000 9 connected 0-5460
604fe9ed88ad20584821ae39f14223faa5b21286 192.168.13.130:7003@17003 slave d2612e7669a28dfeaf03a90b661b8e2bc966af64 0 15700955 15000 9 connected

 

學會利用help幫助工具

root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
                 --cluster-search-multiple-owners
  info           host:port
  fix            host:port
                 --cluster-search-multiple-owners
  reshard        host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  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

jedis操作redis集羣原理

 

redis的99.9%集羣可用原理分析:

因爲java的jedisCluster創建redis集羣時,jedisCluster會爲每一個節點創建jedisPool,當node1掛了,jedisCluster會寫入失敗,雖然node1的slaver會轉成master,但該切換時間段客戶端寫入的數據還是會丟失)

 

 

 

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