<p>I am trying to find a solution to run a cron job in a Kubernetes-deployed app without unwanted duplicates. Let me describe my scenario, to give you a little bit of context.</p>
<p>I want to schedule jobs that execute once at a specified date. More precisely: creating such a job can happen anytime and its execution date will be known only at that time. The job that needs to be done is always the same, but it needs parametrization.</p>
<p>My application is running inside a Kubernetes cluster, and I cannot assume that there always will be only one instance of it running at the any moment in time. Therefore, creating the said job will lead to multiple executions of it due to the fact that all of my application instances will spawn it. However, I want to guarantee that a job runs exactly <strong>once</strong> in the whole cluster.</p>
<p>I tried to find solutions for this problem and came up with the following ideas.</p>
<ul>
<li><p><em>Create a local file and check if it is already there when starting a new job. If it is there, cancel the job.</em></p>
<p>Not possible in my case, since the duplicate jobs might run on other machines!</p>
</li>
<li><p><em>Utilize the <a href="https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/" rel="nofollow noreferrer">Kubernetes CronJob API</a>.</em></p>
<p>I cannot use this feature because I have to create cron jobs dynamically from inside my application. I cannot change the cluster configuration from a pod running inside that cluster. Maybe there is a way, but it seems to me there have to be a better solution than giving the application access to the cluster it is running in.</p>
</li>
</ul>
<p>Would you please be as kind as to give me any directions at which I might find a solution?</p>
<p>I am using a managed Kubernetes Cluster on <a href="https://www.digitalocean.com/" rel="nofollow noreferrer">Digital Ocean</a> (Client Version: v1.22.4, Server Version: v1.21.5).</p>

Alexander Grass
<p>After thinking about a solution for a rather long time I found it.</p>
<p>The solution is to take the scheduling of the jobs to a central place. It is as easy as building a job web service that exposes endpoints to create jobs. An instance of a backend creating a job at this service will also provide a callback endpoint in the request which the job web service will call at the execution date and time.</p>
<p>The endpoint in my case links back to the calling backend server which carries the logic to be executed. It would be rather tedious to make the job service execute the logic directly since there are a lot of dependencies involved in the job. I keep a separate database in my job service just to store information about whom to call and how. Addressing the <em>startup after crash</em> problem becomes trivial since there is only one instance of the job web service and it can just re-create the jobs normally after retrieving them from the database in case the service crashed.</p>
<p>Do not forget to take care of failing jobs. If your backends are not reachable for some reason to take the callback, there must be some reconciliation mechanism in place that will prevent this failure from staying unnoticed.</p>
<p>A little note I want to add: In case you also want to scale the job service horizontally you run into very similar problems again. However, if you think about what is the actual work to be done in that service, you realize that it is very lightweight. I am not sure if horizontal scaling is ever a requirement, since it is only doing requests at specified times and is not executing heavy work.</p>
