简体   繁体   English

如何重新连接Redis集群节点?

[英]How to reconnect Redis cluster nodes?

I have a Redis cluster of 6 nodes, running in my Kubernetes cluster as a stateful set. 我有一个由6个节点组成的Redis集群,作为状态集在我的Kubernetes集群中运行。 Since it was for testing and not yet on production, all of the Redis nodes were on the same machine. 由于它是用于测试且尚未投入生产,因此所有Redis节点都在同一台机器上。 Of course, the machine failed, and all of Redis' nodes crashed immediately. 当然,机器出现故障,Redis的所有节点立即崩溃。 When the machine was back alive the pods were recreated and was given different cluster ips, therefore they couldn't re-connect with each other. 当计算机恢复运行时,将重新创建Pod,并为其分配了不同的群集ip,因此它们无法相互重新连接。

I need to find a solution for a disaster case such as this. 我需要找到针对此类灾难案例的解决方案。 Let's assume all the nodes were reassigned with different ips, how can I configure the nodes to get to other ips? 假设所有节点都被重新分配了不同的ip,如何配置这些节点以获取其他ip?

The slaves are easy to reset with the CLUSTER RESET command, but the masters contain slots and data that shouldn't be deleted. 使用CLUSTER RESET命令可以很容易地重置从站,但是主站包含不应删除的插槽和数据。

Should I manually re-write nodes.conf? 我应该手动重写nodes.conf吗? I'm afraid this will make it even worse? 恐怕会使情况变得更糟? I there a known method to deal with it? 我有已知的处理方法吗?

Thanks! 谢谢!

Found a solution: 找到一个解决方案:

The first step is to change the current pod ip in nodes.conf when the pod is starting. 第一步是在Pod启动时更改node.conf中的当前Pod ip。 You can achieve that with this script 您可以使用此脚本来实现

#!/bin/sh
    CLUSTER_CONFIG="/data/nodes.conf"
    if [ -f ${CLUSTER_CONFIG} ]; then
      if [ -z "${POD_IP}" ]; then
        echo "Unable to determine Pod IP address!"
        exit 1
      fi
      echo "Updating my IP to ${POD_IP} in ${CLUSTER_CONFIG}"
      sed -i.bak -e "/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/${POD_IP}/" ${CLUSTER_CONFIG}
    fi
    exec "$@"

You should start any pod by calling this script and passing to it the original redis-server start command. 您应该通过调用此脚本并将原始的redis-server start命令传递给该脚本来启动任何pod。

Now every pod in the cluster has the correct IP of itself set. 现在,群集中的每个Pod都已设置了正确的IP。

  1. Make sure the cluster's pods are stable and don't crash. 确保群集的Pod稳定且不会崩溃。
  2. Manually edit nodes.conf in one of the pods. 在其中一个窗格中手动编辑nodes.conf。 Set the correct IPs instead of the deprecated ones. 设置正确的IP而不是已弃用的IP。
  3. Restart the pod you've edit with redis-cli shutdown . 使用redis-cli shutdown重新启动您已编辑的pod。 Kubernetes will set up a new pod for it. Kubernetes将为此设置一个新的pod。 The new pod's IP will be set by the script I added above. 新容器的IP将由我在上面添加的脚本设置。

In my opinion you shouldn't rely on Pods' internal IP address at all, when referencing your Redis cluster in any place within your application. 我认为,在应用程序中任何位置引用Redis集群时,您都不应完全依赖Pods的内部IP地址。 Pods are mortal, this means they are designed to crash. 豆荚是致命的,这意味着它们被设计成会崩溃的。 So when node dies, they are destroyed too. 因此,当节点死亡时,它们也会被破坏。 When node resurrects, PODs are recreated with new IP addresses. 当节点复活时,将使用新的IP地址重新创建POD。

The proper way to target your PODs would be via their DNS names (as explained here ), if you created your Redis cluster as a Stateful application. 定位您荚正确的方法是通过他们的DNS名称(如解释在这里 ),如果您创建的Redis集群有状态应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM