yamlbeginner

Kubernetes CronJob — Scheduled Tasks

Create Kubernetes CronJobs for scheduled tasks with history limits, concurrency control, and deadlines.

yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: daily-report
  namespace: production
spec:
  schedule: '0 6 * * *'  # 6 AM UTC daily
  timeZone: 'America/New_York'
  concurrencyPolicy: Forbid  # skip if previous still running
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 5
  startingDeadlineSeconds: 300  # skip if 5min late
  jobTemplate:
    spec:
      backoffLimit: 2
      activeDeadlineSeconds: 3600  # kill if runs > 1hr
      template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: report-generator
              image: myapp/report-gen:latest
              command: ['python', 'generate_report.py']
              env:
                - name: DB_URL
                  valueFrom:
                    secretKeyRef:
                      name: db-credentials
                      key: url
              resources:
                requests:
                  cpu: 200m
                  memory: 256Mi
                limits:
                  cpu: '1'
                  memory: 1Gi

Sponsored

DigitalOcean

Use Cases

  • Scheduling daily data exports or reports
  • Running cleanup tasks on a cron schedule
  • Database maintenance jobs in Kubernetes

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.