[英]Do only one time in multiple Kubernetes pods
I have a general question in optimization in having multiple Kubernetes pods.在拥有多个 Kubernetes pod 的优化方面,我有一个一般性问题。 I have a Java application service A
which checks on service B
every 5 mins for a condition to do task T
.我有一个 Java 应用程序service A
每 5 分钟检查一次service B
是否有条件执行task T
。 Every 5 mins, if the condition is met, I want task T
to be run.每 5 分钟,如果满足条件,我希望task T
运行。
However, service A
currently run with 4 pods, and each of them are checking service B
separately, and performing task T
if condition is met.但是, service A
目前运行 4 个 pod,每个 pod 都在单独检查service B
,如果满足条件,则执行task T
But the task T
needs only to be performed one time if the condition on service B
is met.但是,如果service B
的条件满足,则task T
只需执行一次。
I am wondering if there's a way for different pods of service A
to coordinate and only do task T
one time every 5 mins (if condition is true), instead of 4 times done by each pod.我想知道是否有办法让service A
的不同 pod 进行协调,并且每 5 分钟只执行一次task T
(如果条件为真),而不是每个 pod 执行 4 次。
Thank you!谢谢!
May I ask why is it necessary for you to run this apparently periodic service with a 4 pod deployment?我可以问一下,为什么您必须使用 4 pod 部署来运行这个明显的周期性服务?
A better idea would be to use a CronJob
instead that runs every 5 minutes and performs the task you want it to.一个更好的主意是使用CronJob
代替,它每 5 分钟运行一次并执行您希望它执行的任务。 Every time the CronJob
is to run it will spawn a pod, which will check the condition on Service B
, run the task if needed and decommission itself.每次CronJob
运行时,它都会生成一个 pod,它将检查Service B
上的条件,如果需要运行任务并自行停用。
I don't see why you want to have 4 pods of a service that is basically a cron job.我不明白您为什么要拥有 4 个基本上是 cron 作业的服务 pod。
You can find details on CronJobs
here .您可以在此处找到有关CronJobs
的详细信息。
While I absolutely support zerO's answer, I believe that there could be a situation where multiple pods check for a condition and then try to act.虽然我绝对支持 zerO 的回答,但我相信可能存在多个 pod 检查某个条件然后尝试采取行动的情况。
In this case you need to combine two conditions: Only if service B says something needs to be done AND task T is not already running a pod may trigger it.在这种情况下,您需要结合两个条件:只有当服务 B 说需要做某事并且任务 T 尚未运行 pod 时才可能触发它。
Now the question would be how all pods have a common view whether one of them is running task T. This could be information in a shared filesystem, a database or some semaphores.现在的问题是,所有 pod 是否有一个共同的视图是否其中一个正在运行任务 T。这可能是共享文件系统、数据库或某些信号量中的信息。 Check Hazelcast , jgroups or Redis , and this list is far from complete.检查Hazelcast 、 jgroups或Redis ,这个列表还远未完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.