Jobs and CronJobs
Not all workloads run forever. Some need to run once to completion (batch jobs), or run on a schedule (like backups). Kubernetes provides Jobs and CronJobs for these scenarios.
Jobs
A Job creates one or more Pods and ensures that a specified number of them successfully terminate. It is used for tasks like data migration, processing, or any run‑to‑completion work.
Example: A Job that runs a simple command:
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4Create the Job:kubectl apply -f job.yamlCheck Job status:kubectl get jobs
kubectl get podsView logs of the completed Pod:kubectl logs pi-xxxxxCronJobs
A CronJob creates Jobs on a repeating schedule. It is useful for periodic tasks like backups, reports, or cleanup.
Example: Run a job every minute:
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
command: ["echo", "Hello from CronJob"]
restartPolicy: OnFailureCreate the CronJob:kubectl apply -f cronjob.yamlCheck CronJob status:kubectl get cronjobsSee jobs created by the CronJob:kubectl get jobsCleanup
Jobs and CronJobs can accumulate completed Pods. You can set
ttlSecondsAfterFinished to automatically clean up completed Jobs, or delete them manually.Two Minute Drill
- Jobs run Pods to completion, ideal for batch processing.
- CronJobs run Jobs on a schedule using cron syntax.
- Define restartPolicy as
NeverorOnFailurefor Jobs. - Use
kubectl get jobsandkubectl get cronjobsto view them. - Jobs can auto‑cleanup with
ttlSecondsAfterFinished.
Need more clarification?
Drop us an email at career@quipoinfotech.com
