Room 15
JOBS & CRONJOBS
The Task Scheduler
- Some work doesn’t need to run forever - Jobs handle one-off tasks, CronJobs handle recurring ones.
- A CronJob has been computing passwords on a schedule. Find its last output.
📜 Mission Briefing
- Apply
job.yaml- a Job that completes after running a computation. - Wait for the Job to complete:
kubectl get jobs --watch. - Read the Job’s pod logs - it outputs a partial password.
- Apply
cronjob.yaml- a CronJob that runs every minute. - Wait for one execution to complete, then read the CronJob pod’s logs.
- Combine both outputs:
cronjob+99→cronjob99.
Key Commands
| Command | Purpose |
|---|---|
kubectl get jobs |
List Jobs |
kubectl describe job NAME |
Job details |
kubectl get cronjobs |
List CronJobs |
kubectl logs job/NAME |
Logs from Job’s pod |
kubectl create job --from=cronjob/NAME test-run |
Manual trigger |
How Jobs & CronJobs Work
# Job: Runs to completion (one-off task)
apiVersion: batch/v1
kind: Job
metadata:
name: pi-calculator
spec:
completions: 1 # Number of successful completions needed
parallelism: 1 # Max parallel pods
backoffLimit: 4 # Max retries on failure
activeDeadlineSeconds: 100 # Timeout
template:
spec:
restartPolicy: Never # Jobs must be Never or OnFailure
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
---
# CronJob: Runs Jobs on a schedule
apiVersion: batch/v1
kind: CronJob
metadata:
name: backup
spec:
schedule: "*/5 * * * *" # Every 5 minutes (cron syntax)
concurrencyPolicy: Forbid # Allow, Forbid, Replace
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: backup
image: busybox
command: ['sh', '-c', 'echo "Backup done at $(date)"']
# Cron schedule format: minute hour day-of-month month day-of-week
# */5 * * * * = every 5 minutes
# 0 * * * * = every hour
# 0 0 * * * = daily at midnight
# 0 0 * * 0 = weekly on Sunday
# Create a Job imperatively
kubectl create job my-job --image=busybox -- echo "Hello"
# Trigger CronJob manually
kubectl create job --from=cronjob/backup manual-backup
# Delete completed Jobs
kubectl delete jobs --field-selector status.successful=1
Hints
Hint 1
Apply and watch the Job:
Hint 2
Apply the CronJob and wait for execution:
Hint 3
The password is cronjob99.