在Kubernetes中有多个相同的有状态应用实例 – 不是数据库 – 如何进行管理?

huangapple go评论153阅读模式
英文:

few instances of same stateful application - not database - in Kubernetes - how is it managed?

问题

我有我的主应用程序,它具有自己的唯一状态,让我们称之为应用程序 A。
这个应用程序 A 启动了一些进程,进行一些解析工作,然后将其收集起来,应该发送到 Kubernetes 集群外部的数据库服务器。

我想在不同的 Pod 中运行几个副本这个应用程序 A。然而,每个实例都是唯一的,不能被替换,因为它有自己的状态。这意味着每个客户端必须只与启动通信的相同实例进行 HTTP 请求通信。

  1. 如何在 Kubernetes 中实现这一点?
  2. 我是否需要定义 StatefulSet 组件?
  3. 如何确保每个客户端(来自集群外部)每次都与启动通信的相同实例进行通信,以获取该对象的状态,例如获取其状态?
  4. 如果 Pod 停止运行,我不希望恢复。这是否可能?
英文:

I have my main application which has its own unique state, let's call it Application A.
This application A starts a few processes which does some parsing work and then it collects it and should send it to a database server outside of the Kuberentes cluster.

I would like to run a few copies of this application A in different pods. however, each instance is unique and cannot be replaced as it has its own state. it means that each client has to talk only with the same instance it started the communication with http requests.

  1. How can it be done in Kubernetes?
  2. do I need to define StatefulSet component?
  3. how do I manage that each client (from outside the cluster) will talk every time with the same instance he started communication on the same object id ? for example to get status on that object.
  4. in case the pod die I don't want to recover. is that possible?

答案1

得分: 2

1: 是的,有点

2: 不一定,但可能会简化一些事情

3: 如果您使用Ingress,您可以使用不同的方法来维护后端亲和性,例如基于Cookie、基于源IP等(nginx示例:https://kubernetes.github.io/ingress-nginx/examples/affinity/cookie/)

4: 您可能希望将restartPolicy设置为Never

总之,这听起来确实不是一个好主意。您应该允许共享状态(例如Redis),或者使用StatefulSet,并能够在本地存储加载相同状态的情况下重新启动。您需要记住,即使使用最优化的设置,类似这样的事情也可能会出问题(例如,当支持的Pod关闭时切换到不同的Pod,由于集群扩展而进行的节点重新调度等)。

英文:

1: yes, sort of

2: not necessarily, but might simplify some things

3: if you use ingress, you can use different methods to maintain backend affinity ie. cookie based, source IP based etc. (nginx example: https://kubernetes.github.io/ingress-nginx/examples/affinity/cookie/)

4: you might want to set restartPolicy to Never

With all that said, this really sounds like a bad idea. You should either allow shared state (ie. redis), or statefulset with ability to restart with the same state loaded from local storage. You need to remember that even with the most optimal setup things like this can break (ie. switch to different pod when a backing pod went down, node rescheduling due to cluster scaling etc.)

答案2

得分: 1

对于第4个问题,你只需设置container-restart-policy。我使用了这个标志来创建一个具有此特性的Pod:--restart=Never

在我看来,这不是Kubernetes的问题。您可能在其他环境中遇到这种情况。关键是使用粘性会话来使所有请求具有亲和性。您可能需要在您的Ingress控制器文档中搜索这种设置。例如Nginx Ingress

英文:

For the number 4 question. You only need to set up the container-restart-policy. I used this flag to create a pod with this feature: --restart=Never

IMHO, It is not a Kubernetes problem. You could have this scenario in other environments. The idea is to use sticky sessions to have an affinity for all your request. You probably need to search for this setup in your ingress controller documentation. E.g Nginx Ingress

huangapple
  • 本文由 发表于 2023年1月9日 17:16:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定