⌂ Home

CronJobs in Kubernetes

Interactive guide to recurring Jobs, cron schedules, history retention, and recurring workload behavior.

CronJobs do not replace Jobs; they create Jobs on a schedule. The scheduling layer and the execution layer are separate and that distinction matters.

Core Model

Understand the Concept First

Repository YAML Files:
  • k8s/labs/workloads/cronjob.yaml — CronJob that creates Jobs on a cron schedule.
  • k8s/labs/workloads/cronjob-timezone.yaml — CronJob example using time zone scheduling fields where supported.
Recurring work

CronJobs are the right fit for backups, reports, cleanup, and periodic synchronization tasks.

History retention

You can control how many successful and failed Jobs are retained for inspection.

Idempotent design

Scheduled tasks should tolerate repeat runs because retries and overlaps can happen.

Interactive Scheduling

CronJob Timeline & Execution Flow

Interactive Cron Schedule Explorer

*/5 * * * *
Every 5 minutes

Job Creation Timeline

00:00 ✓ Job 00:05 ✓ Job 00:10 ✓ Job 00:15 ✓ Job 00:20 ✓ Job 00:25 ⟳ Next Time

Job Creation from CronJob Template

CronJob schedule: */5 * * * * jobTemplate when schedule matches Job completions: 1 creates Pod Run Success Template defines what to run

Concurrency Policy Comparison

Allow

Job 1 Job 2 Job 3 Multiple jobs run concurrently

Default. Allows multiple Jobs to run at the same time.

Forbid

Job 1 New jobs skipped if one is running

Skips new Job if previous Job is still running.

Replace

Old New Terminates old job starts new one

Terminates current Job and starts new one.

Job History Retention

Successful Jobs (successfulJobsHistoryLimit: 3) Job 1 Job 2 Job 3 Deleted 🗑 Failed Jobs (failedJobsHistoryLimit: 1) Job 4 Deleted 🗑 History Retention Settings successfulJobsHistoryLimit: 3 Keep last 3 successful Jobs for inspection failedJobsHistoryLimit: 1 Keep only 1 failed Job for debugging
CronJobs separate scheduling (when to run) from execution (what to run). The controller creates Jobs on schedule, and each Job manages its own Pod lifecycle and completion tracking.
YAML and Commands

Examples You Can Recognize Quickly

Basic CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello-cronjob
spec:
schedule: "*/5 * * * *"
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
Useful Commands
kubectl get cronjob
kubectl describe cronjob hello-cronjob
kubectl get jobs
Decision Guide

CronJob vs Job

Feature CronJob Job
Trigger Time-based schedule Manual creation or external trigger
Creates Jobs Pods
Use case Recurring tasks One-off batch work
State to monitor Schedule plus history Completion and failure state
When debugging CronJobs, inspect both the CronJob itself and the Jobs it creates.
Use It Well

Practice and Real-World Thinking

Backups

Run scheduled database or file backups at fixed times.

Cleanup

Delete stale files, temporary data, or expired records periodically.

Reporting

Trigger summary reports or email digests at regular intervals.