Skip to content
Rahul Shishodiaon GitHub LinkedIn profile

Jobs and CronJobs

Jobs

  • Job: run workload until N successful completions; Pod stops when work finishes
  • Completed Jobs/Pods not auto-deleted: delete manually or use ttlSecondsAfterFinished
  • Pod template inside spec.template: same fields as Pod
  • restartPolicy: only OnFailure or Never (not Always)
Job typecompletionsparallelism
Non-parallel11
Parallel fixed count≥1≥1
Worker queueunset≥1
PolicyOn failure
OnFailureRestart container in same Pod
NeverStart new Pod
  • backoffLimit: retries before failed (default 6)

Job YAML skeleton

apiVersion: batch/v1
kind: Job
metadata:
  name: counter
spec:
  completions: 1
  parallelism: 1
  backoffLimit: 6
  ttlSecondsAfterFinished: 100
  template:
    spec:
      restartPolicy: Never
      containers:
      - name: counter
        image: nginx:1.25.1
        command: ["/bin/sh", "-c", "echo done"]

CronJobs

  • CronJob → creates Job → creates Pod on schedule
  • spec.schedule: cron expression (* * * * * = every minute)
  • spec.jobTemplate: Job template (not Pod directly)
  • Default history: 3 successful Jobs, 1 failed Job retained
apiVersion: batch/v1
kind: CronJob
metadata:
  name: current-date
spec:
  schedule: "*/2 * * * *"
  successfulJobsHistoryLimit: 7
  failedJobsHistoryLimit: 3
  concurrencyPolicy: Forbid    # skip if previous still running
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
          - name: current-date
            image: nginx:1.25.1
            args: ["/bin/sh", "-c", 'echo $(date)']

Cron schedule shortcuts

ScheduleMeaning
0 2 * * *2 AM daily
*/5 * * * *every 5 min
*/30 * * * *every 30 min
0 3 * * 1Monday 3 AM
0 9-17 * * 1-5hourly from 9-17 on weekdays

kubectl

  • kubectl create job <n> --image=<img> -- <cmd>
  • kubectl get jobs: watch COMPLETIONS (e.g. 1/1)
  • kubectl logs <pod-from-job>
  • kubectl create cronjob <n> --schedule="* * * * *" --image=<img> -- <cmd>
  • kubectl get cronjobs / kubectl get jobs,po
  • kubectl create job <n> --from=cronjob/<cronjob>: trigger now
kubectl create cronjob backup \
  --image=busybox \
  --schedule="0 2 * * *" \
  -- sh -c 'echo backup'

kubectl create job backup-now --from=cronjob/backup
kubectl patch cronjob backup -p '{"spec":{"suspend":true}}'
kubectl patch cronjob backup -p '{"spec":{"suspend":false}}'

Key fields

FieldObject
completions, parallelism, backoffLimit, ttlSecondsAfterFinishedJob
schedule, jobTemplate, successfulJobsHistoryLimit, failedJobsHistoryLimitCronJob

Exam tips

  • Job Pod restartPolicy must be OnFailure or Never
  • CronJob Pods prefixed with CronJob name: use kubectl get jobs,po to trace
  • Exam clusters use UTC for schedules unless stated otherwise
  • If no Jobs appear, check .spec.suspend, schedule syntax, and describe cronjob