Sub topic 10
Job history and suspending a CronJob
This chapter assumes you can write a CronJob manifest, you know that a CronJob creates a Job for each run, and you can read Pod logs with kubectl. It covers two controls that keep a CronJob tidy and safe to operate: history limits and suspension.
Why history needs a limit
Each time a CronJob fires, it creates a Job, and that Job creates Pods. If nothing removes them, finished Jobs and Pods pile up. A CronJob that runs every minute adds 1,440 Jobs a day. That clutters kubectl get jobs and puts load on the API server.
Two fields on the CronJob spec control how many finished Jobs it keeps:
| Field | Default | What it limits |
|---|---|---|
| `successfulJobsHistoryLimit` | 3 | Finished Jobs that succeeded |
| `failedJobsHistoryLimit` | 1 | Finished Jobs that failed |
When a new Job finishes and the count goes over the limit, the CronJob controller deletes the oldest finished Job. Deleting a Job also deletes its Pods, so their logs go too.
Setting a limit to 0 keeps nothing. That saves space, but you lose the logs you would need to debug a failure.
History limits and ttlSecondsAfterFinished
Two mechanisms can delete a finished Job. History limits are applied by the CronJob, and they count Jobs. ttlSecondsAfterFinished is set in the jobTemplate and is applied per Job, and it counts time. Both can be active at once. A Job disappears when either rule removes it first.
If you set a TTL of 60 seconds and a history limit of 3, the TTL wins in practice. The Job is gone a minute after it finishes, so the history never fills. Choose one rule as your main cleanup and use the other only as a backstop.
Suspending a CronJob
Suspending stops a CronJob from creating new Jobs without deleting it. Set spec.suspend to true:
kubectl patch cronjob nightly-report -p '{"spec":{"suspend":true}}'
kubectl get cronjob nightly-reportThe SUSPEND column now shows True. To resume:
kubectl patch cronjob nightly-report -p '{"spec":{"suspend":false}}'Suspending is useful during an incident, a database migration, or a maintenance window. You keep the schedule, the template, and the history.
Suspension affects only future runs. A Job that is already running keeps running. To stop it, delete that Job.
A CronJob that uses both controls
apiVersion: batch/v1
kind: CronJob
metadata:
name: cleanup-task
spec:
schedule: "*/15 * * * *"
suspend: false
successfulJobsHistoryLimit: 2
failedJobsHistoryLimit: 2
startingDeadlineSeconds: 300
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: cleanup
image: busybox:1.36
command: ["sh", "-c", "date; echo cleaning up"]You can also create the CronJob already suspended by setting suspend: true. This lets you review it before the first run, and then resume it when ready.
How the controller trims history
Common mistakes
- Setting both history limits to
0and then having no logs after a failure. - Expecting
suspend: trueto stop a Job that is already running. - Forgetting that a suspended CronJob keeps accumulating missed schedules.
- Combining a short TTL with high history limits and wondering why the history is always empty.
Exercise
A CronJob runs every 5 minutes. You set suspend: true for an hour, and you did not set startingDeadlineSeconds. After you resume, how many catch-up Jobs can the controller start for the missed runs, and what happens to a Job that was running when you suspended?
Show answer
The controller starts at most one catch-up Job, for the most recent missed time, not one for each of the 12 missed runs. The Job that was already running when you suspended continues to completion, because suspending a CronJob only stops new Jobs from being created.
Exercise
Your failed Jobs disappear within a minute, so you cannot read their logs. failedJobsHistoryLimit is 5. What is the most likely cause, and how do you fix it?
Show answer
The jobTemplate probably sets a short ttlSecondsAfterFinished, which deletes each Job shortly after it finishes regardless of the history limit. Raise the TTL, or remove it and rely on failedJobsHistoryLimit.