Learning Kubernetes From Scratch (Part 9) – Jobs
By Newt / March 28, 2026 / No Comments / Kubernetes
🧠 Understanding Kubernetes Jobs
At some point, every system needs to run one-off tasks or scheduled background work — things like backups, data processing, or cleanup scripts.
This is exactly where Kubernetes Jobs and CronJobs come in.
In this guide, we’ll build both — properly, cleanly, and in a way you can actually reuse in real projects.
⚙️ 1. One-Time Job (Pi Calculator)
Let’s start simple. We’ll create a Job that calculates Pi. This isn’t about maths — it’s about understanding how Kubernetes runs finite tasks and tracks their completion.
📄 File: 01-simple-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pi-calculator
spec:
# Controls how many retries Kubernetes will attempt if the job fails
backoffLimit: 4
template:
spec:
containers:
- name: pi
# Using a Perl image to calculate Pi
image: perl:5.34
# This command calculates Pi to 2000 decimal places
command:
- "perl"
- "-Mbignum=bpi"
- "-wle"
- "print bpi(2000)"
# Important: Job should NOT restart once completed
restartPolicy: Never
Let’s break down what’s actually happening here 👇
- 📦 Kubernetes creates a pod to run this task
- 🧮 The container executes the Pi calculation
- ✅ Once done, the pod exits and the Job is marked Completed
- 🔁 If it fails, Kubernetes retries (up to 4 times)
🚀 Run the Job
kubectl apply -f 01-simple-job.yaml
👀 Check Job Status
kubectl get jobs
📜 View Output
kubectl logs job/pi-calculator
You should see a massive number — that’s Pi calculated inside your cluster 🎯
⏰ 2. Scheduled Jobs (CronJobs)
Now let’s level up.
A CronJob is just a Job that runs on a schedule — very similar to Linux cron.
📄 File: 02-cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: backup-job
spec:
# Cron format: MIN HOUR DAY MONTH DAY-OF-WEEK
# This runs every day at 2 AM
schedule: "0 2 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: backup
image: busybox
# Simulating a backup task with a timestamp
command:
- "/bin/sh"
- "-c"
- "echo Performing backup at $(date)"
# Restart ONLY if it fails
restartPolicy: OnFailure
This is where things get interesting 👇
- ⏰ Kubernetes schedules the job automatically
- 📦 Each run creates a new Job + Pod
- 📊 You can track every execution
0 2 * * * = Run at 02:00 every day
🚀 Deploy CronJob
kubectl apply -f 02-cronjob.yaml
👀 Check CronJob
kubectl get cronjob backup-job
🧪 Test It Immediately (Best Practice)
kubectl create job --from=cronjob/backup-job backup-test
📜 View Output
kubectl logs job/backup-test
You’ll see a timestamp printed — simulating a real backup run 🗂️
⚖️ Jobs vs CronJobs
| Feature | Job | CronJob |
|---|---|---|
| Execution | Runs once | Runs on schedule |
| Use Case | Batch tasks | Recurring tasks |
| Lifecycle | Completes and stops | Keeps creating jobs |
🧹 Cleanup Commands
kubectl delete job pi-calculator kubectl delete cronjob backup-job kubectl delete job backup-test
Always clean up after testing — Jobs can pile up quickly and clutter your cluster.
🧠 Final Thoughts
Jobs and CronJobs are one of those features that seem simple… but become critical in real-world systems.
- 🔄 Automating backups
- 📊 Running analytics
- 🧹 Cleaning old data
- ⚙️ Running maintenance scripts
- Persistent storage (for backups)
- Secrets (for credentials)
- Monitoring (to track failures)
Once you understand this properly, you’ve unlocked a huge part of how real DevOps systems operate behind the scenes.
