<p>I have a Kubernetes Cron Job for running a scheduled task every 5 minutes. I want to make sure that when a new pod is created at next schedule time, the earlier pod should have been terminated. The earlier pod should get terminated before creation of new. Can Kubernetes terminate the earlier pod before creation of new?</p>

<p>My yaml is:</p>

<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: my-scheduled
spec:
  schedule: "*/5 * * * *"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cmm-callout
            env:
              - name: SCHEDULED
                value: "true"
            livenessProbe:
              httpGet:
                path: /myapp/status
                port: 7070
                scheme: HTTPS
              initialDelaySeconds: 120
              timeoutSeconds: 30
              periodSeconds: 120                
            image: gcr.io/projectid/folder/my-app:9.0.8000.34
          restartPolicy: Never
</code></pre>

<p>How can I make sure the earlier pod is terminated before new is created?</p>

user5155835
<p>If i understood your case correctly (the earlier pod should have been terminated  before creation of new one).</p>

<p><strong>1</strong>. Please use <strong>spec.jobTemplate.spec.activeDeadlineSeconds</strong> instead.</p>

<p>By setting this parameter once a Job reaches <strong>activeDeadlineSeconds</strong> - all of running Pods will be terminated and the Job status will become type: Failed with reason DeadlineExceeded.</p>

<p>example:</p>

<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      activeDeadlineSeconds: 60
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster &amp;&amp; sleep 420
          restartPolicy: Never
</code></pre>

<p><strong>2</strong>. The second solution is to set-up <strong>concurrencyPolicy</strong>. and replace the currently running job with a new job.</p>

<p>example:</p>

<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/2 * * * *"
  concurrencyPolicy: Replace
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster &amp;&amp; sleep 420
          restartPolicy: Never
</code></pre>

<p>Resources:</p>

<ul>
<li><p><a href="https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/#job-termination-and-cleanup" rel="noreferrer">Job Termination</a></p></li>
<li><p><a href="https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#concurrency-policy" rel="noreferrer">Concurrency Policy</a></p></li>
</ul>
