误用redis cluster forget删除节点,如何把节点加回来?【详细步骤】

问题

如果你在使用cluster forget操作的时候,误删了某个节点。该如何恢复呢?

本例中演示的集群有3个master节点

$ redis-cli -c -h 192.168.1.196 -p 6379 -a xxxxxx cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
bfc47617d26387e800c1ff1b714d25175caaaa60 192.168.1.196:6579@16579 master - 0 1640674351105 6 connected 6554-12014
74c8b8101bc810e82d98926c9d0f60b8f1b5d163 192.168.1.196:6479@16479 master - 0 1640674349099 8 connected 3277-6553 12560-13106 14746-16383
d1851a905c4daf870dc9ca7c28c3ebe29585af07 192.168.1.196:6379@16379 myself,master - 0 1640674350000 7 connected 0-3276 12015-12559 13107-14745

先用cluster forget操作删掉一个:

$ redis-cli -c -h 192.168.1.196 -p 6379 -a xxxxx cluster forget bfc47617d26387e800c1ff1b714d25175caaaa60
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK

现在只剩下2个节点:

$ redis-cli -c -h 192.168.1.196 -p 6379 -a xxxxx cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
74c8b8101bc810e82d98926c9d0f60b8f1b5d163 192.168.1.196:6479@16479 master - 0 1640674488571 8 connected 3277-6553 12560-13106 14746-16383
d1851a905c4daf870dc9ca7c28c3ebe29585af07 192.168.1.196:6379@16379 myself,master - 0 1640674486000 7 connected 0-3276 12015-12559 13107-14745

整个集群都不好了:

$ redis-cli --cluster check 192.168.1.196:6379 -a xxxxxx
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.196:6379 (d1851a90...) -> 41303 keys | 5461 slots | 0 slaves.
192.168.1.196:6479 (74c8b810...) -> 41365 keys | 5462 slots | 0 slaves.
[OK] 82668 keys in 2 masters.
5.05 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.196:6379)
M: d1851a905c4daf870dc9ca7c28c3ebe29585af07 192.168.1.196:6379
   slots:[0-3276],[12015-12559],[13107-14745] (5461 slots) master
M: 74c8b8101bc810e82d98926c9d0f60b8f1b5d163 192.168.1.196:6479
   slots:[3277-6553],[12560-13106],[14746-16383] (5462 slots) master
[ERR] Nodes don't agree about configuration!
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.

解决

可以使用cluster meet 命令

$ redis-cli -c -h 192.168.1.196 -p 6379 -a xxxxxx cluster meet 192.168.1.196 6579
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK

命令参数说明:

参数说明
-c集群模式登录
-h 192.168.1.196任意节点ip地址
-p 6379登录节点的端口
-a xxxxxx密码
cluster meetcluster meet 命令
192.168.1.196 6579要增加的节点的ip地址和端口

检查

$ redis-cli --cluster check 192.168.1.196:6379 -a xxxxxx
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.196:6379 (d1851a90...) -> 41303 keys | 5461 slots | 0 slaves.
192.168.1.196:6579 (bfc47617...) -> 41090 keys | 5461 slots | 0 slaves.
192.168.1.196:6479 (74c8b810...) -> 41365 keys | 5462 slots | 0 slaves.
[OK] 123758 keys in 3 masters.
7.55 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.196:6379)
M: d1851a905c4daf870dc9ca7c28c3ebe29585af07 192.168.1.196:6379
   slots:[0-3276],[12015-12559],[13107-14745] (5461 slots) master
M: bfc47617d26387e800c1ff1b714d25175caaaa60 192.168.1.196:6579
   slots:[6554-12014] (5461 slots) master
M: 74c8b8101bc810e82d98926c9d0f60b8f1b5d163 192.168.1.196:6479
   slots:[3277-6553],[12560-13106],[14746-16383] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

被删掉的节点又回来了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redis ClusterRedis 的分布式解决方案,可以将数据分散存储在不同的节点上,以提高性能和可扩展性。在 Redis Cluster 中,每个节点负责一部分槽位(slot)的存储和处理。 要新增一个节点,并进行槽位的分配,我们需要执行以下步骤: 1. 启动一个新的 Redis 节点,配置文件中要指定集群模式,并确保其他节点也都开启了集群模式。 2. 在一个已经运行的节点上执行 CLUSTER MEET 命令,将新节点加入到集群中。命令格式为 CLUSTER MEET <ip> <port>,其中 <ip> 和 <port> 分别是新节点的 IP 地址和端口号。 3. 新节点加入集群后,其他节点会自动感知到这个变化,它们会进行重新的槽位分配。每个节点会负责同时管理一些旧槽位和新槽位。 4. 为了保持集群的平衡,可以使用 CLUSTER REBALANCE 命令来手动执行槽位的重新分配。这个命令会将不均匀分布的槽位重新分配给节点,使得集群中每个节点管理的槽位数尽量接近。 要删除一个节点,并重新分配其槽位,我们需要执行以下步骤: 1. 首先,将要删除节点设置为故障状态。可以使用 CLUSTER FORGET 命令从其他节点中移除它。命令格式为 CLUSTER FORGET <node_id>,其中 <node_id> 是要删除节点的 ID。 2. 接下来,进行槽位的重新分配。Redis Cluster 会自动进行槽位的再平衡。每个存活的节点都会参与槽位的重新分配,以保持集群的平衡。 需要注意的是,新增和删除节点时,Redis Cluster 会自动进行槽位的重新分配,不需要手动操作。在节点加入或离开集群后,其他存活节点会按照一定的规则重新分配槽位,以确保数据的平衡和高可用性。如果发生数据迁移,Redis Cluster 会自动处理数据的同步和恢复。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值