Sub topic 5
Cleaning up finished Jobs
This chapter assumes you can write a basic Job manifest, apply it with kubectl, and read Pod logs. Here you will see why finished Jobs pile up and how ttlSecondsAfterFinished removes them for you.
Why finished Jobs stay around
When a Job finishes, Kubernetes does not delete it. The Job object and its Pods remain in the cluster in a Completed or Failed state. This is deliberate. You can still run kubectl logs and kubectl describe to see what happened.
The cost shows up over time. Each leftover Job and Pod is an object stored in etcd, the cluster's database. Thousands of them slow down listing commands and add load to the control plane. A CronJob that runs every minute can create over a thousand Jobs a day.
Automatic cleanup with ttlSecondsAfterFinished
The ttlSecondsAfterFinished field sets how many seconds a Job is kept after it finishes, whether it succeeded or failed. When that time passes, the TTL controller deletes the Job. Deleting the Job also deletes its Pods, so their logs go with them.
apiVersion: batch/v1
kind: Job
metadata:
name: hello
spec:
backoffLimit: 3
activeDeadlineSeconds: 300
ttlSecondsAfterFinished: 600
template:
spec:
restartPolicy: Never
containers:
- name: hello
image: busybox:1.36
command: ["sh", "-c", "echo Hello from a Job && sleep 5"]This Job is removed 600 seconds (10 minutes) after it completes or fails.
The timer starts when the Job reaches a finished state. A Job that is still running is never deleted by the TTL.
Choosing a TTL value
The value is a trade-off between tidiness and debugging time.
| Value | Effect |
|---|---|
| Not set | The Job is never deleted automatically. |
| `0` | The Job is deleted right after it finishes. Logs are lost almost at once. |
| `600` to `86400` | The Job stays long enough to inspect, then disappears. |
Setting 0 is risky for failed Jobs, because you may have no time to read the logs. If you use short TTLs, ship logs to a central logging system first.
Cleanup in a CronJob
A CronJob does not clean up the Jobs it creates by default beyond its history limits. You can set the TTL in the jobTemplate, so each Job it creates carries the field:
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-report
spec:
schedule: "30 2 * * *"
jobTemplate:
spec:
ttlSecondsAfterFinished: 86400
template:
spec:
restartPolicy: OnFailure
containers:
- name: report
image: busybox:1.36
command: ["sh", "-c", "date; echo generating report"]Each run is deleted one day after it finishes. A later chapter covers the CronJob's own history limits, which work alongside this field.
Common mistakes
- Putting the field under
template.specinstead of the Job'sspec. It belongs directly under the Job'sspec. - Expecting a TTL to stop a stuck Job. The TTL only applies after a Job finishes. Use
activeDeadlineSecondsto bound a running Job. - Setting
0and then wondering where the logs went.
Cleaning up by hand
If a Job has no TTL, delete it yourself:
kubectl delete job hello
kubectl get jobsThe Pods are removed with it. Add --cascade=orphan only if you want to keep the Pods.
Exercise
A Job finishes with status Failed and has ttlSecondsAfterFinished: 0. You run kubectl logs job/<name> a minute later and get "not found". Why, and what would you change?
Show answer
With a TTL of 0 the Job and its Pods are deleted as soon as it finishes, even when it failed, so the logs are gone. Set a larger value, such as 3600, so there is time to inspect the failure, or send logs to a central system.
Exercise
Where in a CronJob manifest do you put ttlSecondsAfterFinished so each created Job is cleaned up?
Show answer
Under spec.jobTemplate.spec, at the same level as backoffLimit.