<p><strong>Current Setup</strong></p>
<p>We have kubernetes cluster setup with 3 kubernetes pods which run spring boot application. We run a job every 12 hrs using spring boot scheduler to get some data and cache it.(there is queue setup but I will not go on those details as my query is for the setup before we get to queue)</p>
<p><strong>Problem</strong></p>
<p>Because we have 3 pods and scheduler is at application level , we make 3 calls for data set and each pod gets the response and pod which processes at caches it first becomes the master and other 2 pods replicate the data from that instance.</p>
<p>I see this as a problem because we will increase number of jobs for get more datasets , so this will multiply the number of calls made.</p>
<p>I am not from Devops side and have limited azure knowledge hence I need some help from community</p>
<p><strong>Need</strong></p>
<p>What are the options available to improve this? I want to separate out Cron schedule to run only once and not for each pod
1 - Can I keep cronjob at cluster level , i have read about it here <a href="https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/" rel="noreferrer">https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/</a>
Will this solve a problem?</p>
<p>2 - I googled and found other option is to run a Cronjob which will schedule a job to completion, will that help and not sure what it really means.</p>
<p>Thanks in Advance to taking out time to read it.</p>

<p>Based on my understanding of your problem, it looks like you have following two choices (at least) -</p>
<ol>
<li>If you continue to have scheduling logic within your springboot main app, then you may want to explore something like <a href="https://github.com/lukas-krecan/ShedLock" rel="noreferrer">shedlock</a> 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).</li>
<li>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 <code>cronjob</code> to schedule kubernetes job that internally creates pods and runs your application logic. Benefit of this approach is that you can use native <a href="https://dev.to/drcloudycoder/kubernetes-cronjob-best-practices-4nlk" rel="noreferrer">kubernetes cronjob parameters like concurrency and few others</a> to ensure the job runs only once during scheduled time through single pod.</li>
</ol>
<p>With approach (1), you get to couple your scheduler code with your main app and run them together in same pods.</p>
<p>With approach (2), you'd have to separate your code (that runs in scheduler) from overall application code, <a href="https://spring.io/guides/gs/spring-boot-kubernetes/" rel="noreferrer">containerize it into its own image</a>, and then configure kubernetes cronjob schedule with this new image referring <a href="https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/#example" rel="noreferrer">official guide example</a> and <a href="https://dev.to/drcloudycoder/kubernetes-cronjob-best-practices-4nlk" rel="noreferrer">kubernetes cronjob best practices</a> (<em>authored by me but can find other examples</em>).</p>
<p>Both approaches have their own merits and de-merits, so you can evaluate them to suit your needs best.</p>
