<p>I am reaching out for ideas on the following topic. Imagine that we have 1 application. This application needs to execute an interval (cron) job. It doesn't make sense to execute however on all instances of the app. It needs to elect just one of X and execute the task, then on the next execution it would again pick one (like load-balancing).</p>
<p>I know that Kubernetes offers <code>Jobs</code> and <code>CronJob</code>s, but those serve only a single purpose of executing a task and then turning off, which would add more complexity to the solution.</p>
<p>An option is of course to have an external coordinator doing this, but I was wondering what is the Kubernetes way to handle such scenario.</p>
<p>Any ideas welcome, thanks.</p>

Daniel Stoyanoff
<p>Job pattern is what you are looking for. There are many examples in official <a href="https://kubernetes.io/docs/tasks/job/" rel="nofollow noreferrer">Kubernetes Documentation/Tasks/Run Jobs</a></p>
<p>Your need seems to match with working with a queue. In this example, as each pod is created, it picks up one unit of work from a task queue, completes it, deletes it from the queue, and exits.</p>
<ol>
<li><strong>Start a message queue service</strong>. In this example, we use RabbitMQ, but you could use another one. In practice you would set up a message queue service once and reuse it for many jobs.</li>
<li><strong>Create a queue, and fill it with messages.</strong> Each message represents one task to be done. In this example, a message is an integer that we will do a lengthy computation on.</li>
<li><strong>Start a Job that works on tasks from the queue</strong>. The Job starts several pods. Each pod takes one task from the message queue, processes it, and repeats until the end of the queue is reached.</li>
</ol>
