Spring boot scheduler running cron job for each pod Spring boot scheduler running cron job for each pod kubernetes kubernetes

Spring boot scheduler running cron job for each pod


Based on my understanding of your problem, it looks like you have following two choices (at least) -

  1. If you continue to have scheduling logic within your springboot main app, then you may want to explore something like shedlock that helps make sure your scheduled job through app code executes only once via an external lock provider like MySQL, Redis, etc. when the app code is running on multiple nodes (or kubernetes pods in your case).
  2. If you can separate out the scheduler specific app code into its own executable process (i.e. that code can run in separate set of pods than your main application code pods), then you can levarage kubernetes cronjob to schedule kubernetes job that internally creates pods and runs your application logic. Benefit of this approach is that you can use native kubernetes cronjob parameters like concurrency and few others to ensure the job runs only once during scheduled time through single pod.

With approach (1), you get to couple your scheduler code with your main app and run them together in same pods.

With approach (2), you'd have to separate your code (that runs in scheduler) from overall application code, containerize it into its own image, and then configure kubernetes cronjob schedule with this new image referring official guide example and kubernetes cronjob best practices (authored by me but can find other examples).

Both approaches have their own merits and de-merits, so you can evaluate them to suit your needs best.