What You Will Learn
Intermediate
- What Jobs and CronJobs are
- When to use them
- How to create them
A Job runs a Pod until it completes successfully. A CronJob runs Jobs on a schedule (like cron). Perfect for batch processing, backups, and cleanup.
job.yamlyaml
apiVersion: batch/v1
kind: Job
metadata:
name: db-migration
spec:
completions: 1 # Run once
backoffLimit: 3 # Retry 3 times on failure
template:
spec:
containers:
- name: migrate
image: my-app:latest
command: ["python", "migrate.py"]
restartPolicy: OnFailurecronjob.yamlyaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: daily-backup
spec:
schedule: "0 2 * * *" # Daily at 2 AM
jobTemplate:
spec:
template:
spec:
containers:
- name: backup
image: backup-tool:latest
command: ["./backup.sh"]
restartPolicy: OnFailureCronJob schedule format
Same as Linux cron: minute hour day month weekday. "0 2 * * *" = 2 AM daily. "*/5 * * * *" = every 5 minutes.Practical Exercise
Create a Job that prints hello and exits
Apply: kubectl apply -f job.yaml
Check: kubectl get jobs
Check: kubectl logs job/db-migration
Create a CronJob that runs every minute
Key Takeaways
- Job: runs a Pod until completion.
- CronJob: schedules Jobs on a cron schedule.
- Use for: migrations, backups, batch processing, cleanup.
- backoffLimit: retry count on failure.
- restartPolicy: OnFailure or Never for Jobs.
Comments
Comments
Post a Comment