在启动期间,只有一个Pod处理任务。

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

Have only 1 pod process a task during startup

问题

我有多个Pod进行部署。要求是每当应用程序启动时,我希望执行一段代码。但我只希望一个Pod执行此代码。如何实现这一目标?

目前我有一种方法-在我的数据库中设置一个标志,并让我的Pod读取该标志,首先读取并锁定该标志的Pod将处理该任务。

我的方法有什么缺点吗?有没有更好的方法来实现这个目标?

英文:

I have my deployment scaled across multiple pods. The requirement is that whenever the app starts up I want a piece of code to be executed. But I just want one pod to execute this code. How can this be achieved?

Right now I have a way - I set a flag in my DB and have my pods read the flag, which ever reads and locks the flag first will have to process the task.

Are there any drawbacks in my approach ? Is there any better way to do this?

答案1

得分: 1

我相信这是正确的方法。所有的Pod都需要有一种方式来了解是否有其他人正在处理任务,并且通过数据库是最好的选择。唯一的缺点是,如果在接收任务的Pod无法更新标志状态,那么会发生什么情况?

我能想到的另一个选择是将消息发布到消息队列(一个在Pod之外维护的队列,云平台上的队列?)。这个想法是,每当一个Pod启动时,它们会检查队列并处理其中的消息。这与你所提到的数据库方法类似。

英文:

I believe this is right approach. All pods need to have a way to understand if someone else is processing the task and doing through DB is best option. The only drawback is what if while the pod which picks it up, couldnt update the flag status. What would happen in that case?

The other option that i could think of is perhaps publishing a message to a message queue (a queue maintained outside of your pods, cloud platforms?). The idea is whenever a pod comes alive, they will check the queue and process it. Its similar to what database approach you have.

huangapple
  • 本文由 发表于 2022年5月13日 02:25:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/72220473.html
匿名

发表评论

匿名网友

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

确定