Sub topic 8
Handling overlapping runs
This chapter assumes you can write a basic CronJob and Job manifest, apply it with kubectl, and read Pod logs. A CronJob creates a new Job at each scheduled time. If a run takes longer than the interval between schedules, two runs can overlap. The concurrencyPolicy field decides what happens then.
Why overlap happens
Suppose a CronJob runs every 5 minutes, and one run takes 7 minutes. At minute 5 the first Job is still running. The controller must either start a second Job, skip the new run, or stop the old one. Without a decision, two Jobs could write to the same database table or file at the same time.
The three policies
concurrencyPolicy accepts three values:
| Value | Behavior when the previous run is still active | Typical use |
|---|---|---|
| `Allow` (default) | Starts the new Job. Runs overlap. | Independent tasks that are safe to run in parallel |
| `Forbid` | Skips the new run. The skipped run is counted as missed. | Tasks that must not overlap, such as report generation or syncs |
| `Replace` | Deletes the running Job and starts a new one. | Tasks where only the latest run matters |
Under Replace, the old Job's Pods are terminated mid-work. Use it only if stopping a run partway is harmless.
Example: Forbid
This CronJob generates a report every night. If the previous run is still active, the new run is skipped.
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-report
spec:
schedule: "30 2 * * *"
timeZone: "Etc/UTC"
concurrencyPolicy: Forbid
startingDeadlineSeconds: 600
jobTemplate:
spec:
backoffLimit: 2
template:
spec:
restartPolicy: OnFailure
containers:
- name: report
image: busybox:1.36
command: ["sh", "-c", "date; echo generating report"]To see the policy in action, change the command to sleep 120 and the schedule to * * * * *. Then watch the Jobs:
kubectl get jobs --watchWith Forbid, only one Job runs at a time, and some minutes produce no new Job. With Allow, a new Job appears every minute, and several run together.
Common errors
- Assuming Forbid queues the run. It does not. The skipped run is gone. The next run happens at the next scheduled time. If you need the work done, the next run must be able to catch up.
- Leaving the default on a non-parallel-safe task.
Allowis the default. A slow run can pile up Jobs and consume cluster resources, which makes later runs slower still. - Using Replace with side effects. A killed run may have written half its output. Design the task so a partial run can be repeated safely.
- Ignoring the deadline. With
Forbid,startingDeadlineSecondsand the number of missed runs interact. A CronJob that cannot start runs for a long time may stop being scheduled, which the next chapter covers.
Choosing a policy
- Ask whether two runs can safely run at once. If yes,
Allowis fine. - If they cannot, ask whether the newer run should win. If yes, use
Replace. If the old run should finish first, useForbid. - Whichever you pick, set
activeDeadlineSecondson the Job template so a hung run cannot blockForbidforever.
Exercise
A CronJob runs every 10 minutes and sometimes takes 15 minutes. It writes a summary file that must never be written by two Pods at once, and a skipped run is acceptable. Which concurrencyPolicy should you use, and what should you add to the Job template to avoid a stuck run blocking the schedule?
Show answer
Use Forbid, so the new run is skipped while the previous one is active. Add activeDeadlineSeconds to the Job spec so a hung run is stopped and later runs can start.