Skip to content

Commit 10e1919

Browse files
committed
cronjob: move conceptual contents to concepts topic
1 parent 883bccd commit 10e1919

File tree

2 files changed

+109
-136
lines changed

2 files changed

+109
-136
lines changed

content/en/docs/concepts/workloads/controllers/cron-jobs.md

Lines changed: 108 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,10 @@ weight: 80
1414

1515
A _CronJob_ creates {{< glossary_tooltip term_id="job" text="Jobs" >}} on a repeating schedule.
1616

17-
One CronJob object is like one line of a _crontab_ (cron table) file. It runs a job periodically
18-
on a given schedule, written in [Cron](https://en.wikipedia.org/wiki/Cron) format.
17+
One CronJob object is like one line of a _crontab_ (cron table) file on a Unix system. It runs a job periodically on a given schedule, written in [Cron](https://en.wikipedia.org/wiki/Cron) format.
1918

20-
{{< caution >}}
21-
All **CronJob** `schedule:` times are based on the timezone of the
22-
{{< glossary_tooltip term_id="kube-controller-manager" text="kube-controller-manager" >}}.
23-
24-
If your control plane runs the kube-controller-manager in Pods or bare
25-
containers, the timezone set for the kube-controller-manager container determines the timezone
26-
that the cron job controller uses.
27-
{{< /caution >}}
28-
29-
{{< caution >}}
30-
The [v1 CronJob API](/docs/reference/kubernetes-api/workload-resources/cron-job-v1/)
31-
does not officially support setting timezone as explained above.
32-
33-
Setting variables such as `CRON_TZ` or `TZ` is not officially supported by the Kubernetes project.
34-
`CRON_TZ` or `TZ` is an implementation detail of the internal library being used
35-
for parsing and calculating the next Job creation time. Any usage of it is not
36-
recommended in a production cluster.
37-
{{< /caution >}}
19+
Cron jobs have limitations and idiosyncrasies.
20+
For example, in certain circumstances, a single cron job can create multiple jobs. See the [limitations](#cron-job-limitations) below.
3821

3922
When the control plane creates new Jobs and (indirectly) Pods for a CronJob, the `.metadata.name`
4023
of the CronJob is part of the basis for naming those Pods. The name of a CronJob must be a valid
@@ -56,6 +39,12 @@ report generation, and so on. Each of those tasks should be configured to recur
5639
indefinitely (for example: once a day / week / month); you can define the point
5740
in time within that interval when the job should start.
5841

42+
{{< note >}}
43+
If you modify a CronJob, the changes you make will apply to new jobs that start to run after your modification
44+
is complete. Jobs (and their Pods) that have already started continue to run without changes.
45+
That is, the CronJob does _not_ update existing jobs, even if those remain running.
46+
{{< /note >}}
47+
5948
### Example
6049

6150
This example CronJob manifest prints the current time and a hello message every minute:
@@ -65,7 +54,8 @@ This example CronJob manifest prints the current time and a hello message every
6554
([Running Automated Tasks with a CronJob](/docs/tasks/job/automated-tasks-with-cron-jobs/)
6655
takes you through this example in more detail).
6756

68-
### Cron schedule syntax
57+
### Schedule
58+
The `.spec.schedule` is a required field and follows the [Cron](https://en.wikipedia.org/wiki/Cron) syntax below:
6959

7060
```
7161
# ┌───────────── minute (0 - 59)
@@ -79,6 +69,24 @@ takes you through this example in more detail).
7969
# * * * * *
8070
```
8171

72+
For example, `0 0 13 * 5` states that the task must be started every Friday at midnight, as well as on the 13th of each month at midnight.
73+
74+
The format also includes extended "Vixie cron" step values. As explained in the
75+
[FreeBSD manual](https://www.freebsd.org/cgi/man.cgi?crontab%285%29):
76+
77+
> Step values can be used in conjunction with ranges. Following a range
78+
> with `/<number>` specifies skips of the number's value through the
79+
> range. For example, `0-23/2` can be used in the hours field to specify
80+
> command execution every other hour (the alternative in the V7 standard is
81+
> `0,2,4,6,8,10,12,14,16,18,20,22`). Steps are also permitted after an
82+
> asterisk, so if you want to say "every two hours", just use `*/2`.
83+
84+
{{< note >}}
85+
A question mark (`?`) in the schedule has the same meaning as an asterisk `*`, that is,
86+
it stands for any of available value for a given field.
87+
{{< /note >}}
88+
89+
Other than the standard syntax, some macros like `@monthly` can also be used:
8290

8391
| Entry | Description | Equivalent to |
8492
| ------------- | ------------- |------------- |
@@ -88,14 +96,86 @@ takes you through this example in more detail).
8896
| @daily (or @midnight) | Run once a day at midnight | 0 0 * * * |
8997
| @hourly | Run once an hour at the beginning of the hour | 0 * * * * |
9098

99+
{{< caution >}}
100+
All **CronJob** `schedule:` times are based on the timezone of the
101+
{{< glossary_tooltip term_id="kube-controller-manager" text="kube-controller-manager" >}}.
91102

103+
If your control plane runs the kube-controller-manager in Pods or bare
104+
containers, the timezone set for the kube-controller-manager container determines the timezone
105+
that the CronJob controller uses.
106+
{{< /caution >}}
92107

93-
For example, the line below states that the task must be started every Friday at midnight, as well as on the 13th of each month at midnight:
108+
{{< caution >}}
109+
The [v1 CronJob API](/docs/reference/kubernetes-api/workload-resources/cron-job-v1/)
110+
does not officially support setting timezone as explained above.
94111

95-
`0 0 13 * 5`
112+
Setting variables such as `CRON_TZ` or `TZ` is not officially supported by the Kubernetes project.
113+
`CRON_TZ` or `TZ` is an implementation detail of the internal library being used
114+
for parsing and calculating the next Job creation time. Any usage of it is not
115+
recommended in a production cluster.
116+
{{< /caution >}}
96117

97118
To generate CronJob schedule expressions, you can also use web tools like [crontab.guru](https://crontab.guru/).
98119

120+
### Job Template
121+
122+
The `.spec.jobTemplate` is the template for the job, and it is required.
123+
It has exactly the same schema as a [Job](/docs/concepts/workloads/controllers/job/), except that
124+
it is nested and does not have an `apiVersion` or `kind`.
125+
For information about writing a job `.spec`, see [Writing a Job Spec](/docs/concepts/workloads/controllers/job/#writing-a-job-spec).
126+
127+
### Starting Deadline
128+
129+
The `.spec.startingDeadlineSeconds` field is optional.
130+
It stands for the deadline in seconds for starting the job if it misses its scheduled time for any reason.
131+
After the deadline, the CronJob does not start the job.
132+
Jobs that do not meet their deadline in this way count as failed jobs.
133+
If this field is not specified, the jobs have no deadline.
134+
135+
If the `.spec.startingDeadlineSeconds` field is set (not null), the CronJob
136+
controller measures the time between when a job is expected to be created and
137+
now. If the difference is higher than that limit, it will skip this execution.
138+
139+
For example, if it is set to `200`, it allows a job to be created for up to 200
140+
seconds after the actual schedule.
141+
142+
### Concurrency Policy
143+
144+
The `.spec.concurrencyPolicy` field is also optional.
145+
It specifies how to treat concurrent executions of a job that is created by this CronJob.
146+
The spec may specify only one of the following concurrency policies:
147+
148+
* `Allow` (default): The CronJob allows concurrently running jobs
149+
* `Forbid`: The CronJob does not allow concurrent runs; if it is time for a new job run and the
150+
previous job run hasn't finished yet, the CronJob skips the new job run
151+
* `Replace`: If it is time for a new job run and the previous job run hasn't finished yet, the
152+
CronJob replaces the currently running job run with a new job run
153+
154+
Note that concurrency policy only applies to the jobs created by the same cron job.
155+
If there are multiple CronJobs, their respective jobs are always allowed to run concurrently.
156+
157+
### Suspend
158+
159+
The `.spec.suspend` field is also optional.
160+
If it is set to `true`, all subsequent executions are suspended.
161+
This setting does not apply to already started executions.
162+
Defaults to false.
163+
164+
{{< caution >}}
165+
Executions that are suspended during their scheduled time count as missed jobs.
166+
When `.spec.suspend` changes from `true` to `false` on an existing CronJob without a
167+
[starting deadline](#starting-deadline), the missed jobs are scheduled immediately.
168+
{{< /caution >}}
169+
170+
### Jobs History Limits
171+
172+
The `.spec.successfulJobsHistoryLimit` and `.spec.failedJobsHistoryLimit` fields are optional.
173+
These fields specify how many completed and failed jobs should be kept.
174+
By default, they are set to 3 and 1 respectively. Setting a limit to `0` corresponds to keeping
175+
none of the corresponding kind of jobs after they finish.
176+
177+
For another way to clean up jobs automatically, see [Clean up finished jobs automatically](/docs/concepts/workloads/controllers/job/#clean-up-finished-jobs-automatically).
178+
99179
## Time zones
100180

101181
For CronJobs with no time zone specified, the kube-controller-manager interprets schedules relative to its local time zone.
@@ -107,14 +187,14 @@ you can specify a time zone for a CronJob (if you don't enable that feature gate
107187
Kubernetes that does not have experimental time zone support, all CronJobs in your cluster have an unspecified
108188
timezone).
109189

110-
When you have the feature enabled, you can set `spec.timeZone` to the name of a valid [time zone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). For example, setting
111-
`spec.timeZone: "Etc/UTC"` instructs Kubernetes to interpret the schedule relative to Coordinated Universal Time.
190+
When you have the feature enabled, you can set `.spec.timeZone` to the name of a valid [time zone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). For example, setting
191+
`.spec.timeZone: "Etc/UTC"` instructs Kubernetes to interpret the schedule relative to Coordinated Universal Time.
112192

113193
A time zone database from the Go standard library is included in the binaries and used as a fallback in case an external database is not available on the system.
114194

115195
## CronJob limitations {#cron-job-limitations}
116196

117-
A cron job creates a job object _about_ once per execution time of its schedule. We say "about" because there
197+
A CronJob creates a job object _about_ once per execution time of its schedule. We say "about" because there
118198
are certain circumstances where two jobs might be created, or no job might be created. We attempt to make these rare,
119199
but do not completely prevent them. Therefore, jobs should be _idempotent_.
120200

@@ -167,13 +247,11 @@ and set this flag to `false`. For example:
167247
* Learn about [Pods](/docs/concepts/workloads/pods/) and
168248
[Jobs](/docs/concepts/workloads/controllers/job/), two concepts
169249
that CronJobs rely upon.
170-
* Read about the [format](https://pkg.go.dev/github.com/robfig/cron/v3#hdr-CRON_Expression_Format)
250+
* Read about the detailed [format](https://pkg.go.dev/github.com/robfig/cron/v3#hdr-CRON_Expression_Format)
171251
of CronJob `.spec.schedule` fields.
172252
* For instructions on creating and working with CronJobs, and for an example
173253
of a CronJob manifest,
174254
see [Running automated tasks with CronJobs](/docs/tasks/job/automated-tasks-with-cron-jobs/).
175-
* For instructions to clean up failed or completed jobs automatically,
176-
see [Clean up Jobs automatically](/docs/concepts/workloads/controllers/job/#clean-up-finished-jobs-automatically)
177255
* `CronJob` is part of the Kubernetes REST API.
178256
Read the {{< api-reference page="workload-resources/cron-job-v1" >}}
179-
object definition to understand the API for Kubernetes cron jobs.
257+
reference doc for more details.

content/en/docs/tasks/job/automated-tasks-with-cron-jobs.md

Lines changed: 1 addition & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,7 @@ weight: 10
99

1010
<!-- overview -->
1111

12-
You can use a {{< glossary_tooltip text="CronJob" term_id="cronjob" >}} to run {{< glossary_tooltip text="Jobs" term_id="job" >}}
13-
on a time-based schedule.
14-
These automated jobs run like [Cron](https://en.wikipedia.org/wiki/Cron) tasks on a Linux or UNIX system.
15-
16-
Cron jobs are useful for creating periodic and recurring tasks, like running backups or sending emails.
17-
Cron jobs can also schedule individual tasks for a specific time, such as if you want to schedule a job for a low activity period.
18-
19-
Cron jobs have limitations and idiosyncrasies.
20-
For example, in certain circumstances, a single cron job can create multiple jobs.
21-
Therefore, jobs should be idempotent.
22-
23-
For more limitations, see [CronJobs](/docs/concepts/workloads/controllers/cron-jobs).
12+
This page shows how to run automated tasks using Kubernetes {{< glossary_tooltip text="CronJob" term_id="cronjob" >}} object.
2413

2514
## {{% heading "prerequisites" %}}
2615

@@ -123,97 +112,3 @@ kubectl delete cronjob hello
123112

124113
Deleting the cron job removes all the jobs and pods it created and stops it from creating additional jobs.
125114
You can read more about removing jobs in [garbage collection](/docs/concepts/architecture/garbage-collection/).
126-
127-
## Writing a CronJob Spec {#writing-a-cron-job-spec}
128-
129-
As with all other Kubernetes objects, a CronJob must have `apiVersion`, `kind`, and `metadata` fields.
130-
For more information about working with Kubernetes objects and their
131-
{{< glossary_tooltip text="manifests" term_id="manifest" >}}, see the
132-
[managing resources](/docs/concepts/cluster-administration/manage-deployment/),
133-
and [using kubectl to manage resources](/docs/concepts/overview/working-with-objects/object-management/) documents.
134-
135-
Each manifest for a CronJob also needs a [`.spec`](/docs/concepts/overview/working-with-objects/kubernetes-objects/#object-spec-and-status) section.
136-
137-
{{< note >}}
138-
If you modify a CronJob, the changes you make will apply to new jobs that start to run after your modification
139-
is complete. Jobs (and their Pods) that have already started continue to run without changes.
140-
That is, the CronJob does _not_ update existing jobs, even if those remain running.
141-
{{< /note >}}
142-
143-
### Schedule
144-
145-
The `.spec.schedule` is a required field of the `.spec`.
146-
It takes a [Cron](https://en.wikipedia.org/wiki/Cron) format string, such as `0 * * * *` or `@hourly`,
147-
as schedule time of its jobs to be created and executed.
148-
149-
The format also includes extended "Vixie cron" step values. As explained in the
150-
[FreeBSD manual](https://www.freebsd.org/cgi/man.cgi?crontab%285%29):
151-
152-
> Step values can be used in conjunction with ranges. Following a range
153-
> with `/<number>` specifies skips of the number's value through the
154-
> range. For example, `0-23/2` can be used in the hours field to specify
155-
> command execution every other hour (the alternative in the V7 standard is
156-
> `0,2,4,6,8,10,12,14,16,18,20,22`). Steps are also permitted after an
157-
> asterisk, so if you want to say "every two hours", just use `*/2`.
158-
159-
{{< note >}}
160-
A question mark (`?`) in the schedule has the same meaning as an asterisk `*`, that is,
161-
it stands for any of available value for a given field.
162-
{{< /note >}}
163-
164-
### Job Template
165-
166-
The `.spec.jobTemplate` is the template for the job, and it is required.
167-
It has exactly the same schema as a [Job](/docs/concepts/workloads/controllers/job/), except that
168-
it is nested and does not have an `apiVersion` or `kind`.
169-
For information about writing a job `.spec`, see [Writing a Job Spec](/docs/concepts/workloads/controllers/job/#writing-a-job-spec).
170-
171-
### Starting Deadline
172-
173-
The `.spec.startingDeadlineSeconds` field is optional.
174-
It stands for the deadline in seconds for starting the job if it misses its scheduled time for any reason.
175-
After the deadline, the cron job does not start the job.
176-
Jobs that do not meet their deadline in this way count as failed jobs.
177-
If this field is not specified, the jobs have no deadline.
178-
179-
If the `.spec.startingDeadlineSeconds` field is set (not null), the CronJob
180-
controller measures the time between when a job is expected to be created and
181-
now. If the difference is higher than that limit, it will skip this execution.
182-
183-
For example, if it is set to `200`, it allows a job to be created for up to 200
184-
seconds after the actual schedule.
185-
186-
### Concurrency Policy
187-
188-
The `.spec.concurrencyPolicy` field is also optional.
189-
It specifies how to treat concurrent executions of a job that is created by this cron job.
190-
The spec may specify only one of the following concurrency policies:
191-
192-
* `Allow` (default): The cron job allows concurrently running jobs
193-
* `Forbid`: The cron job does not allow concurrent runs; if it is time for a new job run and the
194-
previous job run hasn't finished yet, the cron job skips the new job run
195-
* `Replace`: If it is time for a new job run and the previous job run hasn't finished yet, the
196-
cron job replaces the currently running job run with a new job run
197-
198-
Note that concurrency policy only applies to the jobs created by the same cron job.
199-
If there are multiple cron jobs, their respective jobs are always allowed to run concurrently.
200-
201-
### Suspend
202-
203-
The `.spec.suspend` field is also optional.
204-
If it is set to `true`, all subsequent executions are suspended.
205-
This setting does not apply to already started executions.
206-
Defaults to false.
207-
208-
{{< caution >}}
209-
Executions that are suspended during their scheduled time count as missed jobs.
210-
When `.spec.suspend` changes from `true` to `false` on an existing cron job without a
211-
[starting deadline](#starting-deadline), the missed jobs are scheduled immediately.
212-
{{< /caution >}}
213-
214-
### Jobs History Limits
215-
216-
The `.spec.successfulJobsHistoryLimit` and `.spec.failedJobsHistoryLimit` fields are optional.
217-
These fields specify how many completed and failed jobs should be kept.
218-
By default, they are set to 3 and 1 respectively. Setting a limit to `0` corresponds to keeping
219-
none of the corresponding kind of jobs after they finish.

0 commit comments

Comments
 (0)