Loading

Quipoin Menu

Learn • Practice • Grow

kubernetes / Jobs and CronJobs
tutorial

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: 4
Create the Job:
kubectl apply -f job.yaml
Check Job status:
kubectl get jobs
kubectl get pods
View logs of the completed Pod:
kubectl logs pi-xxxxx

CronJobs

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: OnFailure
Create the CronJob:
kubectl apply -f cronjob.yaml
Check CronJob status:
kubectl get cronjobs
See jobs created by the CronJob:
kubectl get jobs

Cleanup

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 Never or OnFailure for Jobs.
  • Use kubectl get jobs and kubectl get cronjobs to view them.
  • Jobs can auto‑cleanup with ttlSecondsAfterFinished.

Need more clarification?

Drop us an email at career@quipoinfotech.com