Docs / Kubernetes & Orchestration / How to Set Up a Kubernetes CronJob

How to Set Up a Kubernetes CronJob

By Admin · Mar 2, 2026 · Updated Apr 23, 2026 · 29 views · 2 min read

How to Set Up a Kubernetes CronJob

CronJobs in Kubernetes run tasks on a recurring schedule, just like traditional cron on Linux. They are ideal for backups, report generation, data cleanup, and other periodic tasks on your Breeze cluster. Each scheduled run creates a Job, which in turn creates one or more pods.

Basic CronJob Manifest

apiVersion: batch/v1
kind: CronJob
metadata:
  name: db-backup
  namespace: production
spec:
  schedule: "0 2 * * *"
  timeZone: "America/New_York"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 5
  failedJobsHistoryLimit: 3
  jobTemplate:
    spec:
      backoffLimit: 2
      activeDeadlineSeconds: 3600
      template:
        spec:
          restartPolicy: OnFailure
          containers:
          - name: backup
            image: postgres:16
            command:
            - /bin/sh
            - -c
            - |
              pg_dump -h $DB_HOST -U $DB_USER $DB_NAME | gzip > /backups/db-$(date +%Y%m%d-%H%M%S).sql.gz
              echo "Backup completed successfully"
            env:
            - name: DB_HOST
              value: postgres-headless
            - name: DB_USER
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: username
            - name: DB_NAME
              value: production
            - name: PGPASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: password
            volumeMounts:
            - name: backup-storage
              mountPath: /backups
          volumes:
          - name: backup-storage
            persistentVolumeClaim:
              claimName: backup-pvc

Schedule Syntax

CronJob uses standard cron syntax with five fields:

# ┌───────────── minute (0-59)
# │ ┌───────────── hour (0-23)
# │ │ ┌───────────── day of month (1-31)
# │ │ │ ┌───────────── month (1-12)
# │ │ │ │ ┌───────────── day of week (0-6, Sun=0)
# │ │ │ │ │
# * * * * *

"*/15 * * * *"    # Every 15 minutes
"0 */6 * * *"     # Every 6 hours
"30 1 * * 1-5"    # Mon-Fri at 01:30
"0 0 1 * *"       # First day of each month at midnight

Concurrency Policies

  • Allow (default) — multiple Jobs can run simultaneously
  • Forbid — skip the new run if the previous Job has not finished
  • Replace — cancel the running Job and start a new one

Managing CronJobs

# List CronJobs
kubectl get cronjobs -n production

# View recent Job history
kubectl get jobs -n production --sort-by=.status.startTime

# Manually trigger a run
kubectl create job --from=cronjob/db-backup manual-backup-001 -n production

# Suspend a CronJob (prevent future runs)
kubectl patch cronjob db-backup -n production -p '{"spec":{"suspend":true}}'

# View logs from the most recent Job pod
kubectl logs job/manual-backup-001 -n production

Best Practices

  • Always set activeDeadlineSeconds to prevent runaway jobs consuming Breeze resources
  • Use concurrencyPolicy: Forbid for jobs that should not overlap (like database backups)
  • Set appropriate backoffLimit to control retry behavior on failure
  • Use the timeZone field (Kubernetes 1.27+) to avoid confusion with UTC scheduling
  • Keep history limits reasonable to avoid accumulating completed Job objects

Was this article helpful?