liveBook · Manning

Cronjob

As explained in Kubernetes in Action, Second Edition

A CronJob controller is a Kubernetes controller that creates Jobs from a CronJob's template according to its schedule.

A CronJob runs a finite task at a scheduled time or at regular intervals. A Job starts its work when created; a CronJob adds a schedule and a Job template. At each scheduled time, the CronJob controller creates a Job from that template, and the Job controller creates the pods that do the work. This continues until the CronJob is deleted. section 18.2

Aggregate responses every minute

A daily statistics task can use `apiVersion: batch/v1`, `kind: CronJob`, `spec.schedule: "* * * * *"`, and `spec.jobTemplate` containing the Job specification. Apply its manifest, then run `kubectl get cj` (`cj` is short for CronJob). Before the next minute, `ACTIVE` may be 0 and `LAST SCHEDULE` empty; afterward, they show a running Job and its scheduling time. Add a label to the Job template if you want to find its Jobs with `kubectl get jobs -l app=aggregate-responses-today`: the controller does not add a CronJob-identifying label for you. section 18.2.1

The schedule uses five crontab fields: minute, hour, day of month, month, and day of week. An asterisk matches any value, so `* * * * *` means every minute; `0 3 * * *` means 03:00 each day. Values, ranges, lists, and steps provide more precise schedules. You can also use shortcuts such as `@hourly` or `@daily`, and set `spec.timeZone`, for example to `CET`. Take care when restricting both day fields: a match on either day of month or day of week can trigger a run. section 18.2.2

Table 18.4 Cron examples
Schedule
Explanation
* * * * *
Every minute (at every minute of every hour, regardless of month, day of the month, or day of the week).
15 * * * *
Fifteen minutes after every hour.
0 0 * 1-3 *
Every day at midnight, but only from January to March.
*/5 18 * * *
Every five minutes between 18:00 (6 PM) and 18:59 (6:59 PM).
* * 7 5 *
Every minute on May 7.
0,30 3 7 5 *
At 3:00AM and 3:30AM on May 7.
0 0 * * 1-5
At 0:00 AM every weekday (Monday through Friday).

Compare concrete crontab expressions with the times at which they trigger. section 18.2.2

To pause future runs, use `kubectl patch cj NAME -p '{"spec":{"suspend":true}}'`; change the value to `false` to resume. Suspending does not stop Jobs already running. Use `kubectl get cj` to check the suspension state, active Job count, and last scheduled run. section 18.2.3 section 18.2.1

Several runs can overlap if a task takes longer than its schedule interval. The default `concurrencyPolicy: Allow` permits this; `Forbid` skips a run while a previous Job remains active; `Replace` deletes the active Job and starts a new one. A replaced Job's pods may still be terminating gracefully. section 18.2.6

Table 18.5 Supported concurrency policies
Value
Description
Allow
Multiple Jobs are allowed to run at the same time. This is the default setting.
Forbid
Concurrent runs are prohibited. If the previous run is still active when a new run is to be scheduled, the CronJob controller records a JobAlreadyActive event and skips creating a new Job.
Replace
The active Job is canceled and replaced by a new one. The CronJob controller cancels the active Job by deleting the Job object. The Job controller then deletes the pods, but they’re allowed to terminate gracefully. This means that two Jobs are still running at the same time, but one of them is being terminated.

Compare the three concurrency policies before choosing what should happen when runs overlap. section 18.2.6

CronJob creation is approximately on schedule, not guaranteed to the second. Set `startingDeadlineSeconds` when a late run would be undesirable: if the controller misses that deadline, it skips creating the Job and reports a missed-schedule event. Without a deadline, an extended controller outage can lead to catch-up Jobs; if more than 100 runs were missed, the controller creates none and reports an event. section 18.2.5

Finished Jobs do not accumulate indefinitely. By default, a CronJob retains three successful Jobs and one failed Job, together with their pods for log inspection; `successfulJobsHistoryLimit` and `failedJobsHistoryLimit` change those counts. Deleting the CronJob also deletes its Jobs and pods. To remove only the schedule while preserving existing Jobs, use `kubectl delete cj NAME --cascade=orphan`. section 18.2.4 section 18.2.7

How Kubernetes in Action, Second Edition covers it

How deep it goes
Works through a manifest, crontab schedules, controller behavior, and operational controls, without descending into controller implementation.
Examples
Demonstrates a minute-by-minute statistics Job with a YAML manifest and kubectl status, Job, and pod listings.
What it assumes
Assumes familiarity with Job manifests but introduces crontab's five schedule fields for readers who have not used them.
What you can do after
Operate/configure it to schedule Jobs, inspect runs, manage overlap and missed starts, and suspend or delete the schedule.
How it connects
Connects CronJobs to Jobs, pods, controllers, crontab schedules, and concurrency policies.

Also explained in

Compare how 2 books explain cronjob →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.