Skip to content
docktionary

Snippets / Kubernetes

CronJob

Runs a container on a cron schedule, with retry and history limits.

cronjobbatch
cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: nightly-report
spec:
  # Standard 5-field cron syntax, in the cluster's configured timezone --
  # here, 2am every day.
  schedule: "0 2 * * *"
  # Without these, Kubernetes keeps every finished Job/Pod around
  # forever; this caps how many completed and failed runs to keep for
  # debugging.
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      # Retries the Job up to 2 more times on failure before giving up,
      # each retry a fresh Pod.
      backoffLimit: 2
      template:
        spec:
          # OnFailure restarts just the container in place; a CronJob
          # should essentially never use "Always" here.
          restartPolicy: OnFailure
          containers:
            - name: report
              image: docktionary/app:1.4.0
              command: ["npm", "run", "nightly-report"]