redis集群管理

本文详细介绍了如何使用redis-trib.rb工具管理Redis集群,包括添加新节点、添加从节点、重新分片、添加指定从节点及删除节点等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.管理集群

2.添加新节点

3.添加从节点

练习添加主机

练习删除主机

 

环境准备:

1.准备两台新主机,开启redis服务(开启集群功能)

2.修改启动脚本

 

58主机

[root@host58 ~]# redis-cli  -h 192.168.4.57 -p 6357

192.168.4.57:6357> keys *

(empty list or set)

192.168.4.57:6357> cluster info

cluster_state:fail

cluster_slots_assigned:0

cluster_slots_ok:0

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:1

cluster_size:0

cluster_current_epoch:0

cluster_my_epoch:0

cluster_stats_messages_sent:0

cluster_stats_messages_received:0

192.168.4.57:6357> cluster nodes

19b1545610997954ec6370bafac834a54efc02d7 :6357@16357 myself,master - 0 0 0 connected

 

检查(51~56)是否能通信:

[root@host58 ~]# ping -c2 192.168.4.52

 

 

[root@host51 src]# ./redis-trib.rb check 192.168.4.51:6351   //检查集群信息

 

添加master新的主机:

新主机做集群配置,并运行 redis 服务

1.添加从节点

# ./redis-trib.rb add-node --slave --master-id id 值

#从节点 ip: 端口 192.168.4.51:6351

 

[root@host51 src]# ./redis-trib.rb add-node 192.168.4.57:6357 192.168.4.51:6351

>>> Adding node 192.168.4.57:6357 to cluster 192.168.4.51:6351

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: 29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353

   slots: (0 slots) slave

   replicates cd3542ec299850a4a8f2c8cf42874ac06d3f19b6

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   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.

>>> Send CLUSTER MEET to node 192.168.4.57:6357 to make it join the cluster.

[OK] New node added correctly.

 

2.查看集群信息:

[root@host51 src]# ./redis-trib.rb check 192.168.4.51:6351      

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357

   slots: (0 slots) master

   0 additional replica(s)

S: 29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353

   slots: (0 slots) slave

   replicates cd3542ec299850a4a8f2c8cf42874ac06d3f19b6

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   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.

 

 

3.对集群进行重新分片(0~16383)

 

[root@host51 src]# ./redis-trib.rb reshard  192.168.4.51:6351

[root@host51 src]# ./redis-trib.rb add-node 192.168.4.57:6357 192.168.4.51:6351

>>> Adding node 192.168.4.57:6357 to cluster 192.168.4.51:6351

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: 29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353

   slots: (0 slots) slave

   replicates cd3542ec299850a4a8f2c8cf42874ac06d3f19b6

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   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.

>>> Send CLUSTER MEET to node 192.168.4.57:6357 to make it join the cluster.

[OK] New node added correctly.

[root@host51 src]# 

[root@host51 src]# ./redis-trib.rb reshard  192.168.4.51:6351

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357

   slots: (0 slots) master

   0 additional replica(s)

S: 29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353

   slots: (0 slots) slave

   replicates cd3542ec299850a4a8f2c8cf42874ac06d3f19b6

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   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.

How many slots do you want to move (from 1 to 16384)? 4096

What is the receiving node ID? 19b1545610997954ec6370bafac834a54efc02d7

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:all

 

Ready to move 4096 slots.

  Source nodes:

    M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

    M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

    M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

  Destination node:

    M: 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357

   slots: (0 slots) master

   0 additional replica(s)

  Resharding plan:

 

查看集群信息:

[root@host51 src]# ./redis-trib.rb check 192.168.4.51:6351

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

M: 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357

   slots:0-1364,5461-6826,10923-12287 (4096 slots) master

  0 additional replica(s)

S: 29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353

   slots: (0 slots) slave

   replicates cd3542ec299850a4a8f2c8cf42874ac06d3f19b6

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:6827-10922 (4096 slots) master

   1 additional replica(s)

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   slots:12288-16383 (4096 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.

 

 

添加从节点(slave 的主机)

对添加为从节点的主机做如下配置

– 装包 修改配置文件 启动 redis 服务

• 添加从节点

# ./redis-trib.rb add-node --slave --master-id id 值

从节点 ip: 端口 192.168.4.51:6351

 

[root@host58 ~]# ss -ntulp | grep redis

tcp    LISTEN     0      128    192.168.4.58:6358                  *:*                   users:(("redis-server",pid=5313,fd=6))

tcp    LISTEN     0      128    192.168.4.58:16358                 *:*                   users:(("redis-server",pid=5313,fd=8))

 

[root@host58 ~]# redis-cli  -h 192.168.4.58 -p 6358

192.168.4.58:6358> cluster nodes

af602cf973932cd3cb8d3b9cb3d3fa557a3b10cc :6358@16358 myself,master - 0 0 0 connected

192.168.4.58:6358> cluster info

cluster_state:fail

cluster_slots_assigned:0

cluster_slots_ok:0

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:1

cluster_size:0

cluster_current_epoch:0

cluster_my_epoch:0

cluster_stats_messages_sent:0

cluster_stats_messages_received:0

 

[root@host51 src]# ./redis-trib.rb  add-node   --slave 192.168.4.58:6358  192.168.4.51:6351

>>> Adding node 192.168.4.58:6358 to cluster 192.168.4.51:6351

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

M: 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357

   slots:0-1364,5461-6826,10923-12287 (4096 slots) master

   0 additional replica(s)

S: 29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353

   slots: (0 slots) slave

   replicates cd3542ec299850a4a8f2c8cf42874ac06d3f19b6

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:6827-10922 (4096 slots) master

   1 additional replica(s)

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   slots:12288-16383 (4096 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.

Automatically selected master 192.168.4.57:6357

>>> Send CLUSTER MEET to node 192.168.4.58:6358 to make it join the cluster.

Waiting for the cluster to join.

>>> Configure node as replica of 192.168.4.57:6357.

[OK] New node added correctly.

 

192.168.4.58:6358> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:8

cluster_size:4

cluster_current_epoch:9

cluster_my_epoch:9

cluster_stats_messages_ping_sent:86

cluster_stats_messages_pong_sent:86

cluster_stats_messages_meet_sent:6

cluster_stats_messages_sent:178

cluster_stats_messages_ping_received:84

cluster_stats_messages_pong_received:92

cluster_stats_messages_meet_received:2

cluster_stats_messages_received:178

 

[root@host51 src]# ./redis-trib.rb check 192.168.4.51:6351

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

M: 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357

   slots:0-1364,5461-6826,10923-12287 (4096 slots) master

   1 additional replica(s)

S: 29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353

   slots: (0 slots) slave

   replicates cd3542ec299850a4a8f2c8cf42874ac06d3f19b6

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:6827-10922 (4096 slots) master

   1 additional replica(s)

S: af602cf973932cd3cb8d3b9cb3d3fa557a3b10cc 192.168.4.58:6358

   slots: (0 slots) slave

   replicates 19b1545610997954ec6370bafac834a54efc02d7

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   slots:12288-16383 (4096 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.

 

客户端访问:新的主机

[root@host50 ~]# redis-cli -c -h 192.168.4.57 -p 6357

192.168.4.57:6357> keys *

1) "name"

2) "nme"

3) "age"

192.168.4.57:6357> set x 999

-> Redirected to slot [16287] located at 192.168.4.54:6354

OK

 

 

给slave主机是指定master

 

给50主机配置,redis服务,开启集群功能:

[root@host50 ~]# yum -y install  gcc gcc-c++

[root@host50 ~]# ss -ntulp | grep redis

tcp    LISTEN     0      128    192.168.4.50:6350                  *:*                   users:(("redis-server",pid=7350,fd=6))

tcp    LISTEN     0      128    192.168.4.50:16350                 *:*                   users:(("redis-server",pid=7350,fd=8))

 

[root@host51 src]# ./redis-trib.rb check 192.168.4.51:6351

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

M: 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357

   slots:0-1364,5461-6826,10923-12287 (4096 slots) master

   1 additional replica(s)

S: 29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353

   slots: (0 slots) slave

   replicates cd3542ec299850a4a8f2c8cf42874ac06d3f19b6

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:6827-10922 (4096 slots) master

  1 additional replica(s)

S: af602cf973932cd3cb8d3b9cb3d3fa557a3b10cc 192.168.4.58:6358

   slots: (0 slots) slave

   replicates 19b1545610997954ec6370bafac834a54efc02d7

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   slots:12288-16383 (4096 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.

 

 

删除slave主机53

• 从节点主机没有槽位范围,直接执行移除命令即可

– redis-trib.rb del-node 192.168.4.51:6351 被移除

主机的 ID

 

]#redis-trib.rb del-node 192.168.4.51:6351 9c507832f99b9af535

63646a06c5b0525e8fcb4a

 

[root@host51 src]# ./redis-trib.rb  del-node 192.168.4.51:6351 29023faf64c53fedbe7d8861ec548d3b12c9e821

>>> Removing node 29023faf64c53fedbe7d8861ec548d3b12c9e821 from cluster 192.168.4.51:6351

>>> Sending CLUSTER FORGET messages to the cluster...

>>> SHUTDOWN the node.

 

[root@host51 src]# ./redis-trib.rb check 192.168.4.51:6351

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

M: 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357

   slots:0-1364,5461-6826,10923-12287 (4096 slots) master

   1 additional replica(s)

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:6827-10922 (4096 slots) master

   1 additional replica(s)

S: af602cf973932cd3cb8d3b9cb3d3fa557a3b10cc 192.168.4.58:6358

   slots: (0 slots) slave

   replicates 19b1545610997954ec6370bafac834a54efc02d7

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   slots:12288-16383 (4096 slots) master

   0 additional replica(s)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

删除master主机 57

[root@host51 src]# ./redis-trib.rb  del-node 192.168.4.51:6351   19b1545610997954ec6370bafac834a54efc02d7

>>> Removing node 19b1545610997954ec6370bafac834a54efc02d7 from cluster 192.168.4.51:6351

[ERR] Node 192.168.4.57:6357 is not empty! Reshard data away and try again.

因为hasha槽还在占用

 

1.释放hasha槽

 

[root@host51 src]# ./redis-trib.rb  reshard 192.168.4.51:6351

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

M: 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357

   slots:0-1364,5461-6826,10923-12287 (4096 slots) master

   0 additional replica(s)

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:6827-10922 (4096 slots) master

   1 additional replica(s)

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   slots:12288-16383 (4096 slots) master

   0 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)? 4096

What is the receiving node ID? c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4  //给的主机

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:19b1545610997954ec6370bafac834a54efc02d7   //拿出来的主机

Source node #2:done

 

Ready to move 4096 slots.

  Source nodes:

    M: 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357

   slots:0-1364,5461-6826,10923-12287 (4096 slots) master

   0 additional replica(s)

  Destination node:

    M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:6827-10922 (4096 slots) master

   1 additional replica(s)

 

 

[root@host51 src]# ./redis-trib.rb check 192.168.4.51:6351

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

M: 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357

   slots: (0 slots) master

   0 additional replica(s)

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

  slots:0-1364,5461-12287 (8192 slots) master

   1 additional replica(s)

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   slots:12288-16383 (4096 slots) master

   0 additional replica(s)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

删除主机:

[root@host51 src]# ./redis-trib.rb del-node 192.168.4.51:6351 19b1545610997954ec6370bafac834a54efc02d7

>>> Removing node 19b1545610997954ec6370bafac834a54efc02d7 from cluster 192.168.4.51:6351

>>> Sending CLUSTER FORGET messages to the cluster...

>>> SHUTDOWN the node.

 

再次查看集群信息:

[root@host51 src]# ./redis-trib.rb check 192.168.4.51:6351>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:0-1364,5461-12287 (8192 slots) master

   1 additional replica(s)

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   slots:12288-16383 (4096 slots) master

   0 additional replica(s)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

 

把修复好的master主机添回到集群中:

踢出集群的主机,自动关闭redis服务,需要把redis的服务从新开启,并查看集群信息和节点信息

 

[root@host57 ~]# ss -ntulp | grep redis

[root@host57 ~]# /etc/init.d/redis_6379  start

Starting Redis server...

[root@host57 ~]# ss -ntulp | grep redis

tcp    LISTEN     0      128    192.168.4.57:6357                  *:*                   users:(("redis-server",pid=9441,fd=6))

tcp    LISTEN     0      128    192.168.4.57:16357                 *:*                   users:(("redis-server",pid=9441,fd=8))

[root@host57 ~]# redis-cli -h 192.168.4.57 -p 6357

192.168.4.57:6357> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:10

cluster_my_epoch:9

cluster_stats_messages_ping_sent:89

cluster_stats_messages_sent:89

cluster_stats_messages_pong_received:89

cluster_stats_messages_received:89

192.168.4.57:6357> 

192.168.4.57:6357> cluster nodes

cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354@16354 master - 0 1532939768184 8 connected 12288-16383

c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352@16352 master - 0 1532939766000 10 connected 0-1364 5461-12287

bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355@16355 slave 07df1de074025884c615cc2b0e1331742ca77419 0 1532939767000 1 connected

07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351@16351 master - 0 1532939768000 1 connected 1365-5460

a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356@16356 slave c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 0 1532939767179 10 connected

19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357@16357 myself,master - 0 1532939459251 9 connected

 

[root@host58 ~]# ss -ntulp | grep redis

[root@host58 ~]# /etc/init.d/redis_6379  start

Starting Redis server...

[root@host58 ~]# ss -ntulp | grep redis

tcp    LISTEN     0      128    192.168.4.58:6358                  *:*                   users:(("redis-server",pid=9175,fd=6))

tcp    LISTEN     0      128    192.168.4.58:16358                 *:*                   users:(("redis-server",pid=9175,fd=8))

[root@host58 ~]# redis-cli  -h 192.168.4.58 -p 6358

192.168.4.58:6358> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:7

cluster_size:3

cluster_current_epoch:10

cluster_my_epoch:10

cluster_stats_messages_ping_sent:60

cluster_stats_messages_sent:60

cluster_stats_messages_pong_received:60

cluster_stats_messages_received:60

192.168.4.58:6358> 

192.168.4.58:6358> cluster nodes  //查看节点信息

af602cf973932cd3cb8d3b9cb3d3fa557a3b10cc 192.168.4.58:6358@16358 myself,slave c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 0 1532939659026 0 connected

c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352@16352 master - 0 1532939740723 10 connected 0-1364 5461-12287

bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355@16355 slave 07df1de074025884c615cc2b0e1331742ca77419 0 1532939739218 1 connected

19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357@16357 master - 0 1532939739218 9 connected

07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351@16351 master - 0 1532939740000 1 connected 1365-5460

a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356@16356 slave c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 0 1532939740000 10 connected

cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354@16354 master - 0 1532939740222 8 connected 12288-16383

 

3.1 把修复的master的redis 服务器再次添加到集群中

 

[root@host51 src]# ./redis-trib.rb add-node 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.51:6351

>>> Adding node 19b1545610997954ec6370bafac834a54efc02d7 to cluster 192.168.4.51:6351

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:0-1364,5461-12287 (8192 slots) master

   1 additional replica(s)

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   slots:12288-16383 (4096 slots) master

   0 additional replica(s)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

Invalid IP or Port (given as 19b1545610997954ec6370bafac834a54efc02d7) - use IP:Port format

##报错信息,无效的ip和端口

 

192.168.4.57:6357> cluster  reset

OK

192.168.4.57:6357> cluster info

cluster_state:fail

cluster_slots_assigned:0

cluster_slots_ok:0

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:1

cluster_size:0

cluster_current_epoch:10

cluster_my_epoch:9

cluster_stats_messages_ping_sent:1430

cluster_stats_messages_pong_sent:230

cluster_stats_messages_sent:1660

cluster_stats_messages_ping_received:230

cluster_stats_messages_pong_received:1430

cluster_stats_messages_received:1660

192.168.4.57:6357> cluster nodes

19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357@16357 myself,master - 0 1532939459251 9 connected

192.168.4.57:6357> keys *

(empty list or set)

 

把修复好的主机,添回集群

[root@host51 src]# ./redis-trib.rb add-node 192.168.4.57:6357  192.168.4.51:6351

>>> Adding node 192.168.4.57:6357 to cluster 192.168.4.51:6351

>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:0-1364,5461-12287 (8192 slots) master

   1 additional replica(s)

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   slots:12288-16383 (4096 slots) master

   0 additional replica(s)

[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.4.57:6357 to make it join the cluster.

[OK] New node added correctly.

 

 

[root@host51 src]# ./redis-trib.rb check 192.168.4.51:6351>>> Performing Cluster Check (using node 192.168.4.51:6351)

M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

M: 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.57:6357

  slots: (0 slots) master

   0 additional replica(s)

S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355

   slots: (0 slots) slave

   replicates 07df1de074025884c615cc2b0e1331742ca77419

M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352

   slots:0-1364,5461-12287 (8192 slots) master

   1 additional replica(s)

S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356

   slots: (0 slots) slave

   replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4

M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354

   slots:12288-16383 (4096 slots) master

   0 additional replica(s)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

重新分配hasha槽

[root@host51 src]# ./redis-trib.rb  reshard 192.168.4.51:6351

 

把修复好的slave从库从新添加到集群中

[root@host58 ~]# /etc/init.d/redis_6379 start

Starting Redis server...

[root@host58 ~]# redis-cli  -h 192.168.4.58 -p 6358

192.168.4.58:6358> cluster reset

OK

 

 

[root@host51 src]# ./redis-trib.rb  add-node --slave --master-id 19b1545610997954ec6370bafac834a54efc02d7 192.168.4.58:6358 192.168.4.51:6351

 

查看主机信息:

192.168.4.58:6358> INFO  replication

# Replication

role:slave

master_host:192.168.4.57

master_port:6357

master_link_status:up

master_last_io_seconds_ago:3

master_sync_in_progress:0

slave_repl_offset:1428

slave_priority:100

slave_read_only:1

connected_slaves:0

master_replid:068ad72ce1a3c819371e47d565bb438d83d99f19

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:1428

second_repl_offset:-1

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:1428

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

redis-cluster-tool 是一个非常便利的 Redis 集群管理工具。help        Usage: redis-cluster-tool [-?hVds] [-v verbosity level] [-o output file]                  [-c conf file] [-a addr] [-i interval]                  [-p pid file] [-C command] [-r redis role]                  [-t thread number] [-b buffer size]    Options:      -h, --help             : this help      -V, --version          : show version and exit      -d, --daemonize        : run as a daemon      -s, --simple           : show the output not in detail      -v, --verbosity=N      : set logging level (default: 5, min: 0, max: 11)      -o, --output=S         : set logging file (default: stderr)      -c, --conf-file=S      : set configuration file (default: conf/rct.yml)      -a, --addr=S           : set redis cluster address (default: 127.0.0.1:6379)      -i, --interval=N       : set interval in msec (default: 1000 msec)      -p, --pid-file=S       : set pid file (default: off)      -C, --command=S        : set command to execute (default: cluster_state)      -r, --role=S           : set the role of the nodes that command to execute on (default: all, you can input: all, master or slave)      -t, --thread=N         : set how many threads to run the job(default: 8)      -b, --buffer=S         : set buffer size to run the job (default: 1048576 byte, unit:G/M/K)        Commands:        cluster_state                 :Show the cluster state.        cluster_used_memory           :Show the cluster used memory.        cluster_keys_num              :Show the cluster holds keys num.        slots_state                   :Show the slots state.        node_slot_num                 :Show the node hold slots number.        new_nodes_name                :Show the new nodes name that not covered slots.        cluster_rebalance             :Show the cluster how to rebalance.        flushall                      :Flush all the cluster.        cluster_config_get            :Get config from every node in the cluster and check consistency.        cluster_config_set            :Set config to every node in the cluster.        cluster_config_rewrite        :Rewrite every node config to echo node for the cluster.        node_list                     :List the nodes            del_keys                      :Delete keys in the cluster. The keys must match a given glob-style pattern.(This command not block the redis)ExampleGet the cluster state:        $redis-cluster-tool -a 127.0.0.1:34501 -C cluster_state -r master    master[127.0.0.1:34504] cluster_state is ok     master[127.0.0.1:34501] cluster_state is ok     master[127.0.0.1:34502] cluster_state is ok     master[127.0.0.1:34503] cluster_state is ok     all nodes cluster_state is ok    Get the cluster used memory:    $redis-cluster-tool -a 127.0.0.1:34501 -C cluster_used_memory -r master    master[127.0.0.1:34504] used 195 M 25%    master[127.0.0.1:34501] used 195 M 25%    master[127.0.0.1:34502] used 195 M 25%    master[127.0.0.1:34503] used 195 M 25%    cluster used 780 MRebalance the cluster slots:    $redis-cluster-tool -a 127.0.0.1:34501 -C cluster_rebalance    --from e1a4ba9922555bfc961f987213e3d4e6659c9316 --to 785862477453bc6b91765ffba0b5bc803052d70a --slots 2048    --from 437c719f50dc9d0745032f3b280ce7ecc40792ac --to cb8299390ce53cefb2352db34976dd768708bf64 --slots 2048    --from a497fc619d4f6c93bd4afb85f3f8a148a3f35adb --to a0cf6c1f12d295cd80f5811afab713cdc858ea30 --slots 2048    --from 0bdef694d08cb3daab9aac518d3ad6f8035ec896 --to 471eaf98ff43ba9a0aadd9579f5af1609239c5b7 --slots 2048Then you can use "redis-trib.rb reshard --yes --from e1a4ba9922555bfc961f987213e3d4e6659c9316 --to 785862477453bc6b91765ffba0b5bc803052d70a --slots 2048 127.0.0.1:34501" to rebalance the cluster slots     Flushall the cluster:    $redis-cluster-tool -a 127.0.0.1:34501 -C flushall -s    Do you really want to execute the "flushall"?    please input "yes" or "no" :        yes    OKGet a config from every node in cluster:    $redis-cluster-tool -a 127.0.0.1:34501 -C "cluster_config_get maxmemory" -r master    master[127.0.0.1:34501] config maxmemory is 1048576000 (1000MB)    master[127.0.0.1:34502] config maxmemory is 1048576000 (1000MB)    master[127.0.0.1:34503] config maxmemory is 1048576000 (1000MB)    master[127.0.0.1:34504] config maxmemory is 1048576000 (1000MB)    All nodes config are Consistent    cluster total maxmemory: 4194304000 (4000MB)Set a config from every node in cluster:    $redis-cluster-tool -a 127.0.0.1:34501 -C "cluster_config_set maxmemory 10000000" -s    Do you really want to execute the "cluster_config_set"?    please input "yes" or "no" :    yes        OKDelete keys in the cluster:    $redis-cluster-tool -a 127.0.0.1:34501 -C "del_keys abc*"    Do you really want to execute the "del_keys"?    please input "yes" or "no" :    yes    delete keys job is running...    delete keys job finished, deleted: 999999 keys, used: 4 s 标签:redis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维螺丝钉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值