CronJob for Scheduled Batch Processing

Create a Kubernetes CronJob manifest for scheduled tasks like database cleanup, report generation, or data synchronization with configurable schedule and concurrency.

Scheduling

Detailed Explanation

Kubernetes CronJobs

CronJobs create Jobs on a schedule, similar to cron on Linux. They are ideal for periodic tasks like database maintenance, report generation, cache warming, and data synchronization.

Key Configuration

apiVersion: batch/v1
kind: CronJob
metadata:
  name: db-cleanup
  labels:
    app: "db-cleanup"
spec:
  schedule: "0 2 * * *"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: cleanup
              image: my-cleanup-script:latest
              env:
                - name: DATABASE_URL
                  value: "postgres://admin:changeme@postgres:5432/mydb"
              resources:
                requests:
                  cpu: "100m"
                  memory: "128Mi"
                limits:
                  cpu: "500m"
                  memory: "256Mi"

Schedule Syntax

The schedule field uses standard cron syntax with 5 fields:

Field Values Example
Minute 0-59 0 (at minute 0)
Hour 0-23 2 (at 2 AM)
Day of month 1-31 * (every day)
Month 1-12 * (every month)
Day of week 0-6 * (every day)

Common schedules:

  • 0 2 * * * — Daily at 2 AM
  • */15 * * * * — Every 15 minutes
  • 0 0 * * 0 — Weekly on Sunday at midnight
  • 0 0 1 * * — Monthly on the 1st at midnight

Concurrency Policy

  • Allow (default): Multiple jobs can run simultaneously
  • Forbid: Skip the new job if the previous one is still running
  • Replace: Cancel the running job and start a new one

For database cleanup, Forbid prevents overlapping cleanup operations that could cause locks or conflicts.

Restart Policy

CronJob pods must use restartPolicy: OnFailure or restartPolicy: Never. The default Always policy is not allowed because completed jobs should not restart indefinitely.

Use Case

Scheduling periodic tasks in Kubernetes such as database vacuum, log rotation, backup creation, report generation, or syncing data between systems on a regular schedule.

Try It — K8s Pod Spec Builder

Open full tool