Interactive Lab ยท CKA ยท Beginner

JOBS & CRONJOBS

$ kubectl create job backup --image=alpine -- sh -c 'tar czf /backup/data.tar.gz /data'
🏃 Jobs Run to completion
  • 1A Job creates one or more Pods and ensures they complete successfully. Unlike Deployments, Jobs are meant to run once and finish.
  • 2If a pod fails, the Job retries up to backoffLimit times (default 6). Set restartPolicy: Never or OnFailure. Never use Always in a Job.
  • 3Completed Jobs and their Pods are kept so you can inspect logs. Use ttlSecondsAfterFinished to auto-clean them up.
Job manifest
apiVersion: batch/v1 kind: Job metadata: name: db-backup spec: backoffLimit: 3 ttlSecondsAfterFinished: 300 template: spec: restartPolicy: Never containers: - name: backup image: alpine command: ["sh","-c","echo backup done && exit 0"]
⏰ CronJobs Schedule recurring tasks
  • 1A CronJob creates Jobs on a schedule using standard cron syntax. Perfect for nightly backups, cleanup tasks, report generation.
  • 2successfulJobsHistoryLimit and failedJobsHistoryLimit control how many old Jobs are kept. Default is 3 successful and 1 failed.
  • 3CronJob times are in UTC. Use timeZone field (Kubernetes 1.27+) to specify a local timezone.
CronJob manifest
apiVersion: batch/v1 kind: CronJob metadata: name: nightly-backup spec: schedule: "0 2 * * *" successfulJobsHistoryLimit: 3 failedJobsHistoryLimit: 1 jobTemplate: spec: template: spec: restartPolicy: OnFailure containers: - name: backup image: alpine command: ["sh","-c","echo running backup"]
⚡ Parallelism Run multiple pods at once
  • 1completions is the total number of pods that must succeed for the Job to be complete.
  • 2parallelism is how many pods run simultaneously. Set lower than completions to control concurrency.
  • 3Use case: process 100 queue items. completions=100, parallelism=10 runs 10 at a time until all 100 complete.
Parallel Job
spec: completions: 10 parallelism: 3 template: spec: restartPolicy: Never containers: - name: worker image: alpine command: ["sh","-c","echo processing $JOB_COMPLETION_INDEX"] # Trigger a CronJob manually: kubectl create job manual-run --from=cronjob/nightly-backup
Job fields
backoffLimitMax retries before Job fails (default 6)
completionsTotal pods that must succeed
parallelismMax pods running simultaneously
ttlSecondsAfterFinishedAuto-delete after N seconds
restartPolicyMust be Never or OnFailure
CronJob fields
scheduleCron expression: '0 2 * * *' = 2am daily
suspend: truePause future runs without deleting
successfulJobsHistoryLimitKeep N completed jobs (default 3)
kubectl create job --fromTrigger a CronJob manually now
Done