简体   繁体   English

在多个 Kubernetes pod 中只执行一次

[英]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.检查HazelcastjgroupsRedis ,这个列表还远未完成。

暂无
暂无

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

相关问题 在 Kubernetes 中,我怎样才能有一种访问模式来允许一个 pod 一次写入而多个 pod 只读? - In Kubernetes, how can i have an access mode to allow one pod at a time to write and many pods to read only? Java,多个线程,一次仅执行一个 - Java, multiple threads with only one executing at a time 如何在 Spring 请求映射中接受多个可选参数,但一次只接受一个? - How do I accept multiple optional parameters in a Spring request mapping, but only one at a time? Kube.netes Pod 的资源监控 - Resource monitoring for Kubernetes Pods SharedIndexInformer (Fabric8 kubernetes-client) 在集群中运行时仅监视其自己命名空间的 Pod - SharedIndexInformer (Fabric8 kubernetes-client) watches only pods of its own namespace when run in the cluster Kubernetes Java Pods套接字通信 - Kubernetes Java Pods Socket communication 如果 Kubernetes 上存在多个 pod,则从单个 pod 运行 Spring 中的计划 Cron 方法 - Run Scheduled Cron method in Spring from single pod if multiple pods exist on Kubernetes 如何使用 fabric8 kubernetes java 客户端从部署的 pod 中读取文件? - How do I read a file from deployment's pods using fabric8 kubernetes java client? 一次只执行一个线程,如何同时执行多个任务? - At a time only one thread is executed, how to execute multiple tasks simultaneously? 在 java 的 do while 循环中只执行一次指令 - execute an instruction only one time in a do while loop in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM