case-cron

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.

  • Kubernetes
  • CronJob
  • Prometheus
  • alerting
  • Alertmanager

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 Failed state are sitting in the namespace.
  • The KubeJobFailed alert doesn't clear until those old Jobs are deleted.

Why does it happen?

Two settings together create this situation:

  1. 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 to Failed instead of retrying.
  2. failedJobsHistoryLimit: 3 (default 1): failed Job records stay in the namespace. kube-state-metrics keeps seeing them as kube_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.
  • backoffLimit isn'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 backoffLimit 0 on frequently running CronJobs?
  • [ ] Is failedJobsHistoryLimit larger 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)?

Organisation and environment details have been removed; command and configuration examples are generalised. Test in your own environment first.

This note is the long form of one of the case files on this site. Drafted with Claude. Also available in Turkish.

ls ~/notes

More notes

All notes

Got an infrastructure question, a project or a route tip?

Email is the fastest way. If I'm on the road, I'll reply from the next Wi-Fi.

Send an email