If you already run containers in production, the next thing you need to schedule is batch work: nightly backups, hourly data syncs, weekly report emails, certificate renewals. To run cron jobs reliably on Kubernetes, skip the classic server cron. You declare a CronJob resource and let the control plane handle the clock. That sounds simple, and the happy path is. The production failures, however, come from timezones, overlapping runs, and silent failures that nobody notices until Monday morning. This guide covers the manifest anatomy, the schedule gotchas that break jobs in the real world, how to detect failures fast, and when CronJobs are the wrong tool.
CronJob anatomy: how to run cron jobs reliably on Kubernetes
A CronJob is a thin wrapper. On the declared schedule, the controller creates a Job, and the Job creates Pods that run your container once. You get retries, parallelism, and deadlines from the Job spec, plus scheduling from the CronJob spec. If you are still mapping core concepts, this practical Kubernetes guide covers controllers and Pods in plain terms. Here is a complete, production-shaped manifest:
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-db-backup
namespace: batch-jobs
spec:
schedule: "0 2 * * *"
timeZone: "Asia/Jakarta"
concurrencyPolicy: Forbid
startingDeadlineSeconds: 120
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 5
jobTemplate:
spec:
backoffLimit: 3
activeDeadlineSeconds: 3600
template:
spec:
restartPolicy: OnFailure
containers:
- name: backup
image: registry.example.com/backup:1.4.2
resources:
requests:
cpu: "250m"
memory: "512Mi"
envFrom:
- secretRef:
name: db-credentials
Three fields deserve attention from day one. backoffLimit: 3 means the Job retries up to three times on failure, so transient database hiccups recover without paging anyone. activeDeadlineSeconds: 3600 kills a Pod that hangs past an hour, so a stuck backup cannot hold a lock forever. restartPolicy: OnFailure is mandatory inside a Job template; Kubernetes rejects the manifest otherwise. Put batch workloads in their own namespace, because namespaces isolate noisy batch jobs from serving traffic and make quota enforcement easy.
Timezones and schedule gotchas that break production jobs
CronJob schedules use standard five-field cron format. The classic mistakes are assuming the cluster timezone and forgetting what happens during daylight saving transitions. Kubernetes interprets schedule in UTC unless you set timeZone. A job declared as 0 9 * * * without a timezone runs at 09:00 UTC, which is 16:00 in Jakarta and 02:00 in Los Angeles. Getting the kubernetes cronjob timezone setting right is the difference between a 2 a.m. backup and a 2 a.m. page. Since Kubernetes 1.27 the timeZone field is stable, so always set it explicitly. A missed 2 a.m. backup window caused by a UTC assumption is one of the most common on-call tickets in batch systems.
Other gotchas: */5 means every five minutes starting at minute 0, while 0/5 behaves the same in this implementation; neither means “offset by five”. A startingDeadlineSeconds: 120 tells the controller to skip a schedule if it could not start the job within two minutes of its slot, which protects against a thundering herd after a controller outage instead of running ten stale jobs at once. And watch the shortest safe cadence. The controller evaluates schedules every 10 seconds by default, and schedules shorter than that can behave erratically. If you need sub-minute execution, CronJobs are not the tool.
Control overlapping runs with concurrencyPolicy and deadlines
When you run cron jobs on Kubernetes in real environments, a job sometimes takes longer than its interval. The kubernetes cronjob concurrency policy setting decides what happens next, and there are three options. Allow starts a new Job even if the old one is still running, which is fine for idempotent reads but dangerous for anything that writes. Forbid skips the new schedule while the old Job is active, the safest default for backups and migrations. Replace kills the running Job and starts a fresh one, which suits lightweight polls where freshness beats completion.
Pair the policy with deadlines. startingDeadlineSeconds bounds how late a job may start. activeDeadlineSeconds in the Job template bounds how long the Pods may run. Without the latter, a job that deadlocks on a network call can sit “active” forever, blocking every future schedule under Forbid. Set both, and set them tighter than you think: a backup that normally takes 20 minutes should have an active deadline of 60 minutes, not 12 hours. History limits also matter. successfulJobsHistoryLimit and failedJobsHistoryLimit cap retained Job objects so etcd does not grow unbounded on a busy cluster.
Know when jobs fail: monitoring that actually works
The scariest CronJob failure mode is silence. A failed Pod, an image pull error, or a missed schedule produces no user-facing symptom until someone asks where the report went. Check .status.lastScheduleTime and .status.lastSuccessfulTime with a query or in your dashboard to see when each job last ran. For alerting, Prometheus exposes kube_cronjob_status_active, kube_job_status_failed, and kube_cronjob_spec_suspend, so you can alert on failed Jobs, jobs that have not produced a successful run in N hours, or a CronJob someone suspended and forgot. If you have not picked a stack yet, this Prometheus vs Zabbix vs Checkmk comparison maps the trade-offs.
Add a dead man’s switch for the critical ones. Have the job itself POST to an external heartbeat endpoint at completion, like a Healthchecks.io check or Better Stack heartbeat, and alert when the heartbeat goes missing for more than one interval. Kubernetes-native metrics catch Pods that failed; only a completion heartbeat catches a job the scheduler never started. A solid kubernetes cronjob monitoring setup needs both layers, not just one. One more operational tip: give batch jobs modest, explicit resource requests. An unbounded backup Pod competing for CPU during peak hours can starve serving Pods, and autoscaling reacts to the spike by adding nodes you did not plan to pay for.
When CronJob is not the right tool
CronJobs handle single, independent executions well. They fall short when jobs need ordering, branching, retries with custom logic, or human approval. For multi-step pipelines, Argo Workflows or Tekton model dependencies as first-class concepts instead of shell scripts calling kubectl wait. For event-driven work, KEDA’s ScaledJob triggers jobs from queue depth or metrics, which beats polling on a timer. For workflows that span services with sagas and compensation, Temporal or a workflow engine fits better than cron-shaped thinking.
The boundary rule I use: if the logic fits in one container and one schedule line, a CronJob is the simplest correct answer. If the schedule line needs to coordinate with anything else, move up a level. And for scheduling CI runs rather than cluster batch work, a GitHub Actions pipeline with scheduled triggers keeps build-time jobs next to the code that defines them instead of inside the cluster.
Quick reliability checklist
- Pin the container image to a digest or immutable tag, never
:latest, so scheduled runs stay reproducible. - Set
timeZoneexplicitly on every CronJob and document it in the manifest comment. - Use
concurrencyPolicy: Forbidas the default for any job that writes state. - Set
startingDeadlineSecondsandactiveDeadlineSecondson every job. - Alert on failed Jobs and on missing completion heartbeats, not just Pod restarts.
- Keep history limits small and clean up test CronJobs, stale jobs clutter
kubectl get jobsfast.
Get these five habits in place and Kubernetes CronJobs become one of the quietest parts of your platform: they run, they retry, they alert when they cannot, and they stay out of your on-call rotation the rest of the time.