简体   繁体   中英

Do only one time in multiple Kubernetes pods

I have a general question in optimization in having multiple Kubernetes pods. I have a Java application service A which checks on service B every 5 mins for a condition to do task T . Every 5 mins, if the condition is met, I want task T to be run.

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. But the task T needs only to be performed one time if the condition on service B is met.

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.

Thank you!

May I ask why is it necessary for you to run this apparently periodic service with a 4 pod deployment?

A better idea would be to use a CronJob instead that runs every 5 minutes and performs the task you want it to. 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.

I don't see why you want to have 4 pods of a service that is basically a cron job.

You can find details on CronJobs here .

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.

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.

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. Check Hazelcast , jgroups or Redis , and this list is far from complete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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