Jobs and CronJobs
Overview of Jobs and CronJobs
Jobs and CronJobs are Kubernetes resources designed to manage short-lived, one-off tasks that run to completion.
- A job is a resource object used to control batch tasks. Jobs start and terminate at specific times, unlike long-running services such as Deployments and StatefulSets, which run continuously unless terminated. Pods managed by a job are automatically removed after successfully completing their tasks, based on the specified settings.
- A CronJob runs a job periodically on a specified schedule. A CronJob object is similar to a line in a crontab file in Linux.
The run-to-completion feature of workloads makes them particularly suitable for one-off tasks, such as continuous integration (CI) pipelines.
Creating a Job
apiVersion: batch/v1 kind: Job metadata: name: pi-with-timeout spec: completions: 50 # The total number of pods that must complete successfully for the job to be considered complete parallelism: 5 # The number of pods that can run concurrently. The default value is 1. backoffLimit: 5 # The maximum number of retries for a pod if it fails activeDeadlineSeconds: 100 # The number of seconds after which the job and all its pods will be terminated template: # Define the pods for the job. spec: containers: - name: pi image: perl command: - perl - "-Mbignum=bpi" - "-wle" - print bpi(2000) restartPolicy: Never
Based on the completions and Parallelism settings, jobs can be classified into several types.
Job Type |
Description |
Example |
---|---|---|
One-off job |
The job runs a single pod to completion. |
Database migration |
Non-parallel job with a fixed completion count |
The job runs one pod until the specified completion count is reached. |
Processing a queue of tasks |
Parallel job with a fixed completion count |
The job runs multiple pods in parallel until the specified completion count is reached. |
Processing a task queue concurrently |
Parallel job |
The job runs one or more pods in parallel until one pod completes successfully. |
Processing a task queue concurrently |
Creating a CronJob
A CronJob is a scheduled job. A CronJob runs a job periodically on a specified schedule, and the job creates pods.
apiVersion: batch/v1 kind: CronJob metadata: name: cronjob-example spec: schedule: "0,15,30,45 * * * *" # Schedule for the CronJob jobTemplate: # Define the job. spec: template: spec: restartPolicy: OnFailure containers: - name: pi image: perl command: - perl - "-Mbignum=bpi" - "-wle" - print bpi(2000)
The format of the CronJob is as follows:
- Minute
- Hour
- Day of month
- Month
- Day of week
Take 0,15,30,45 * * * * as an example. In this example, commas separate the minutes (0, 15, 30, 45). The first asterisk (*) indicates any hour, the second asterisk indicates any day, the third asterisk indicates any month, and the fourth asterisk indicates any day of the week.
If you want to run the job every 30 minutes on the first day of each month, set this parameter to 0,30 * 1 * *. If you want to run the job at 03:00 a.m. every Sunday, set this parameter to 0 3 * * 0.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.