We have a Java application deployed in open shift container platform. There are 4 identical stateless pods running the application. In our application , a scheduler runs every 15 mins that process some data. I need to perform the scheduler run only in one of the pod. As doing it in all the pods leads to duplicate operations. How can I achieve this? I am looking for some best and simplest way to achieve this without complicating much. Please suggest me some options.
2 Answers
Does it really need to run in one of the pods? Because as you are discovering, that's somewhat problematic. By definition, the pods are identical and so to go down that path you you are going to have to use some kind of external coordinator like a database or PV.*
On the other hand if you use something like a CronJob, you can let OpenShift do the work for you regarding both the scheduling and the "only happen once". You'll also get better visibility, logging, and troubleshooting. You can probably even re-use your image, perhaps with just a different CMD.
*Technically this isn't true, you could use some kind of consensus mechanism within your pods, but that's a lot of complexity if you just want a cron job.

- 4,396
- 1
- 19
- 29
Use distributed locks.If the lock fails, other pods are executing tasks.

- 16
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 26 '22 at 11:01
-
I want only one pod to execute a specific task , not multiple pods. – user3615185 Jan 27 '22 at 01:28
-
If the lock fails, exit the task.This ensures that only one pod is actually performing tasks at a time.Or you can use the distributed task framework. – hututu Jan 27 '22 at 02:25
-
Thank you for the detail. Can you please give some more details on distributed lock. Is it specific to open shift container. Any reference link would be much appreciated – user3615185 Jan 27 '22 at 15:14