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,但该切换时间段客户端写入的数据还是会丢失)

发布了135 篇原创文章 · 获赞 16 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/nmjhehe/article/details/102002643