π― Kubernetes Jobs & CronJobs: Your Personal Task Assistants!
Analogy Throughout: Imagine Kubernetes is like a restaurant kitchen. Jobs are like special orders that get made once and done. CronJobs are like daily specials that automatically appear on the menu every day at the same time!
π³ What is a Job?
Think of a Job like asking someone to bake you ONE birthday cake.
- You give them the recipe (the task)
- They make the cake (run the task)
- When itβs done, theyβre finished!
- They donβt keep making cakes forever
In Kubernetes terms: A Job creates one or more Pods, runs them until they successfully complete, then stops. Itβs perfect for tasks that need to run once and finish.
Real-Life Examples of Jobs:
- ποΈ Backing up a database
- π§ Sending a batch of emails
- π’ Processing a file of numbers
- π§Ή Cleaning up old data
π Basic Job Example
Hereβs the simplest Job - like ordering one cupcake:
apiVersion: batch/v1
kind: Job
metadata:
name: say-hello
spec:
template:
spec:
containers:
- name: hello
image: busybox
command: ["echo", "Hello!"]
restartPolicy: Never
What happens:
- Kubernetes creates a Pod
- Pod says βHello!β
- Pod completes successfully
- Job is done! β
π§ Job Configuration Patterns
Pattern 1: Run Multiple Times (Completions)
Story: You need to pack 5 gift boxes. Each box needs one person to pack it.
apiVersion: batch/v1
kind: Job
metadata:
name: pack-gifts
spec:
completions: 5
template:
spec:
containers:
- name: packer
image: busybox
command: ["echo", "Box packed!"]
restartPolicy: Never
completions: 5= βDo this task 5 timesβ- Kubernetes runs 5 Pods, one after another
- Job is complete when ALL 5 finish
Pattern 2: Work Faster with Parallelism
Story: Now you have 5 friends helping pack boxes at the SAME TIME!
apiVersion: batch/v1
kind: Job
metadata:
name: fast-packing
spec:
completions: 10
parallelism: 3
template:
spec:
containers:
- name: packer
image: busybox
command: ["echo", "Box packed!"]
restartPolicy: Never
completions: 10= Need 10 boxes packedparallelism: 3= 3 friends work together- Result: Much faster! π
graph TD A[Job: Pack 10 Boxes] --> B[Pod 1] A --> C[Pod 2] A --> D[Pod 3] B --> E[Wait for finish] C --> E D --> E E --> F[Start next 3 Pods] F --> G[Until all 10 done β ]
Pattern 3: Handle Failures with backoffLimit
Story: What if a friend drops a box? How many tries do we give them?
apiVersion: batch/v1
kind: Job
metadata:
name: careful-job
spec:
backoffLimit: 4
template:
spec:
containers:
- name: worker
image: busybox
command: ["sh", "-c", "exit 1"]
restartPolicy: Never
backoffLimit: 4= βTry up to 4 times if it failsβ- After 4 failures, Job gives up
- Default is 6 tries
Pattern 4: Time Limits with activeDeadlineSeconds
Story: βYou have 5 minutes to finish your homework!β
apiVersion: batch/v1
kind: Job
metadata:
name: timed-job
spec:
activeDeadlineSeconds: 300
template:
spec:
containers:
- name: worker
image: busybox
command: ["sleep", "600"]
restartPolicy: Never
activeDeadlineSeconds: 300= 5 minutes max- If not done in time, Kubernetes stops it
- Great for preventing stuck tasks!
Pattern 5: Clean Up with ttlSecondsAfterFinished
Story: After eating cake, clean up the dishes automatically!
apiVersion: batch/v1
kind: Job
metadata:
name: auto-cleanup
spec:
ttlSecondsAfterFinished: 100
template:
spec:
containers:
- name: worker
image: busybox
command: ["echo", "Done!"]
restartPolicy: Never
ttlSecondsAfterFinished: 100= Delete Job 100 seconds after it finishes- Keeps your cluster clean! π§Ή
β° What is a CronJob?
Story: Remember the restaurant analogy? A CronJob is like having βTaco Tuesdayβ - every Tuesday at noon, tacos automatically appear!
A CronJob is a Job that runs on a schedule. It uses a special time format called βcron expression.β
The Cron Expression Explained
ββββββββββββββ minute (0-59)
β ββββββββββββββ hour (0-23)
β β ββββββββββββββ day of month (1-31)
β β β ββββββββββββββ month (1-12)
β β β β ββββββββββββββ day of week (0-6)
β β β β β (Sunday = 0)
* * * * *
Common Examples:
| Expression | Meaning |
|---|---|
*/5 * * * * |
Every 5 minutes |
0 * * * * |
Every hour |
0 9 * * * |
9 AM daily |
0 0 * * 0 |
Midnight on Sunday |
0 0 1 * * |
First of each month |
π Basic CronJob Example
Story: Send a βGood morning!β message every day at 8 AM:
apiVersion: batch/v1
kind: CronJob
metadata:
name: morning-greeting
spec:
schedule: "0 8 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: greeter
image: busybox
command: ["echo", "Good morning!"]
restartPolicy: Never
What happens:
- Every day at 8:00 AMβ¦
- Kubernetes creates a Job
- Job says βGood morning!β
- Job completes
- Tomorrow, repeat! π
βοΈ CronJob Configuration
Handling Overlaps: concurrencyPolicy
Story: What if yesterdayβs cleaning isnβt done, but todayβs cleaning time arrives?
apiVersion: batch/v1
kind: CronJob
metadata:
name: cleanup
spec:
schedule: "0 * * * *"
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
spec:
containers:
- name: cleaner
image: busybox
command: ["sleep", "3600"]
restartPolicy: Never
Three Options:
| Policy | What It Does |
|---|---|
Allow |
Let them overlap (default) |
Forbid |
Skip new if old still running |
Replace |
Stop old, start new |
graph TD A[New Job Time!] --> B{Old Job Running?} B -->|Allow| C[Start new anyway] B -->|Forbid| D[Skip this time] B -->|Replace| E[Stop old, start new]
Starting Deadline: startingDeadlineSeconds
Story: βIf my alarm doesnβt wake me up within 5 minutes of 7 AM, just skip breakfast.β
apiVersion: batch/v1
kind: CronJob
metadata:
name: morning-task
spec:
schedule: "0 7 * * *"
startingDeadlineSeconds: 300
jobTemplate:
spec:
template:
spec:
containers:
- name: worker
image: busybox
command: ["echo", "Morning task!"]
restartPolicy: Never
startingDeadlineSeconds: 300= 5 minute grace period- If Kubernetes is busy and canβt start the job within 5 minutes of 7 AM, skip it
- Prevents running very late jobs
Keeping History: successfulJobsHistoryLimit & failedJobsHistoryLimit
Story: Keep the last 3 report cards, throw away older ones!
apiVersion: batch/v1
kind: CronJob
metadata:
name: reports
spec:
schedule: "0 0 * * *"
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
containers:
- name: reporter
image: busybox
command: ["echo", "Daily report"]
restartPolicy: Never
successfulJobsHistoryLimit: 3= Keep last 3 successful jobsfailedJobsHistoryLimit: 1= Keep only 1 failed job- Default: 3 successful, 1 failed
Pause a CronJob: suspend
Story: Going on vacation? Put the newspaper delivery on hold!
apiVersion: batch/v1
kind: CronJob
metadata:
name: daily-backup
spec:
schedule: "0 2 * * *"
suspend: true
jobTemplate:
spec:
template:
spec:
containers:
- name: backup
image: busybox
command: ["echo", "Backing up..."]
restartPolicy: Never
suspend: true= CronJob wonβt create new Jobs- Great for maintenance windows!
- Set to
falseto resume
π― Complete CronJob Example
Hereβs a production-ready CronJob with all the bells and whistles:
apiVersion: batch/v1
kind: CronJob
metadata:
name: database-backup
spec:
schedule: "0 2 * * *"
concurrencyPolicy: Forbid
startingDeadlineSeconds: 600
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 2
suspend: false
jobTemplate:
spec:
backoffLimit: 3
activeDeadlineSeconds: 1800
template:
spec:
containers:
- name: backup
image: backup-tool:latest
command: ["backup.sh"]
restartPolicy: OnFailure
What this does:
- β° Runs at 2 AM every day
- π« Wonβt overlap with previous backup
- β±οΈ Must start within 10 minutes of scheduled time
- π Retries up to 3 times if it fails
- β³ Must finish within 30 minutes
- π Keeps last 3 successful, 2 failed jobs
π Key Takeaways
| Concept | What It Means |
|---|---|
| Job | One-time task that runs to completion |
| completions | How many times to run the task |
| parallelism | How many pods run at once |
| backoffLimit | Number of retries on failure |
| CronJob | Scheduled Job that repeats |
| schedule | When to run (cron format) |
| concurrencyPolicy | What to do if jobs overlap |
| suspend | Pause/resume the schedule |
π You Did It!
You now understand:
- β What Jobs are and when to use them
- β How to configure Jobs for different patterns
- β What CronJobs are and how scheduling works
- β All the important CronJob settings
Remember: Jobs are like hiring someone for a specific task. CronJobs are like setting up automatic recurring appointments. Both are super useful for running tasks in Kubernetes!
π Go forth and schedule amazing things!