Jobs & CronJobs
Key Takeaways for AI & Readers
- Batch Processing: Kubernetes Jobs are designed for tasks that run to completion, rather than long-running services.
- Job Configuration: Jobs ensure a specified number of Pods succeed, offering controls for completions, parallelism, and retry limits.
- Scheduled Tasks: CronJobs automate the creation of Jobs on a recurring schedule, mimicking traditional cron functionality within the cluster.
- Use Cases: Jobs are ideal for one-off tasks like migrations, while CronJobs suit routine operations like backups or reports.
While Deployments and StatefulSets are designed for long-running processes (services), Kubernetes also has first-class support for batch tasks.
1. Jobs
A Job creates one or more Pods and ensures that a specified number of them successfully terminate. As pods successfully complete, the Job tracks the successful completions.
Use Cases
- One-off database migrations.
- Batch processing (e.g., rendering a video frame).
- System maintenance tasks.
Key Fields
.spec.completions: How many pods need to succeed..spec.parallelism: How many pods can run at the same time..spec.backoffLimit: How many times to retry before marking the Job as failed.
2. CronJobs
Schedule: */5 * * * *
Time: 0
Jobs are ephemeral. They run, complete, and then the Pod stops.
A CronJob creates a Job on a repeating schedule. It is essentially a crontab for your cluster.
Use Cases
- Daily database backups.
- Sending digest emails.
- Cleaning up old temporary files.
Schedule Syntax
It uses standard Cron syntax:
"*/1 * * * *" (Run every minute)