A CronJob that failed once keeps the alert firing for days
A CronJob that fails on a momentary DNS error doesn't clear the KubeJobFailed alert, even when the following runs succeed. A lasting fix with the backoffLimit and history settings.
There was a retry job that ran every minute. Every now and then it failed on its own, and the next minute it ran successfully again. From the application's point of view, there was no problem. But on the monitoring screen, the KubeJobFailed alert stayed lit for days, and after a while nobody looked at it anymore. That was the real danger.
Symptoms
- The CronJob's last successful run was a few minutes ago.
- A few old Jobs in
Failedstate are sitting in the namespace. - The
KubeJobFailedalert doesn't clear until those old Jobs are deleted.
Why does it happen?
Two settings together create this situation:
backoffLimit: 0: the pod inside the Job gives up on the first error. If DNS can't answer for a few seconds while the job connects to a service, the Job goes straight toFailedinstead of retrying.failedJobsHistoryLimit: 3(default 1): failed Job records stay in the namespace. kube-state-metrics keeps seeing them askube_job_status_failed. And the Prometheus rule fires with "there's a failed Job".
The end result: a glitch lasting a few seconds stays on as an alert until someone deletes it by hand.
Fix
Give transient glitches a short retry allowance, and don't let old failures pile up:
spec:
failedJobsHistoryLimit: 1
jobTemplate:
spec:
backoffLimit: 2 # retries after 10 s, then 20 s
activeDeadlineSeconds: 240 # cap the total time so it doesn't overlap the next run
To fix the existing CronJob in place:
kubectl -n <ns> patch cronjob <ad> --type merge -p \
'{"spec":{"failedJobsHistoryLimit":1,"jobTemplate":{"spec":{"backoffLimit":2}}}}'
Once failedJobsHistoryLimit is lowered, Kubernetes cleans up the extra Failed Jobs itself. The alert clears within a few minutes.
Watch out
- If the CI pipeline only updates the image (
kubectl set image), this setting won't be overwritten. If the manifest comes from Helm or GitOps, make the change at the source, or it will revert on the first deploy. backoffLimitisn't for hiding problems; it's for errors that last seconds. If the job keeps failing, retries only add noise.- The lasting improvement belongs in the application: a short retry and a sensible timeout when connecting.
Takeaway
When an alert shows not a real problem but a problem from the past, the team stops looking at it. An alert going quiet, and firing again on a real error, matters as much as the alert itself.
Checklist
- [ ] Is
backoffLimit0 on frequently running CronJobs? - [ ] Is
failedJobsHistoryLimitlarger than it needs to be? - [ ] Does the alert rule look only at the result of the latest run?
- [ ] Was the setting written at the source (Helm/GitOps)?