<p>I have a deployment of my backend app which has 6 replicas. In the code of the app, I have a node.js cronjob running every minute. But the problem is that because I have 6 replicas, it runs 6 times in parallel. I want to send env variable to exactly only one Pod in the deployment to ensure that only 1 Pod performs the cronjob. Then in the app code I will check this using the <code>process.env.SHOULD_PROCESS_CRONJOBS == 1</code>.</p>
<p>If this is not the right way to achieve what I need, can you inform me on how to handle cronjobs using nodejs in distributed environment like K8S?</p>

Raz Buchnik
<p>Let Kubernetes schedule a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/" rel="nofollow noreferrer">CronJob</a> in addition to the application</p>
<pre><code>apiVersion: batch/v1
kind: CronJob
metadata:
  name: app-cron
spec:
  schedule: &quot;* * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: my-app-cron
            image: same-as-app
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from scheduled job
          restartPolicy: OnFailure
</code></pre>
<p>If you don't want kubernetes supervising the cron scheduling, then another option is to split out the application that schedules the cron into two Deployments. Only use 1 replica for the container that has environment variable:</p>
<pre><code>      env:
      - name: SHOULD_PROCESS_CRONJOBS
        value: &quot;1&quot;
</code></pre>
