liveBook · Manning

Cronjob

As explained in Kubernetes for Developers

A Kubernetes CronJob is a workload resource that creates Jobs on a recurring schedule.

A CronJob creates Jobs on a recurring schedule. Use it for repeatable batch work such as daily cleanup, without requiring developers to remember a complex manual command. section 10.2.2 section 10.5

Its structure is nested: the CronJob contains a Job template, and that Job contains a Pod template. This composition lets the scheduled workload use Job and Pod fields while Kubernetes creates ordinary Pods from the templates. section 10.2.2

Choose the workload type according to how the background work should run:

  • Use a Deployment for a continuously running task queue, commonly coordinated with a queue such as Redis. section 10.5
  • Use a Job for one-off work, static-queue processing, or batch work that terminates when complete. section 10.5
  • Use a CronJob when Kubernetes should create those Jobs at recurring intervals. section 10.2.2 section 10.5

Run a worker every five minutes

Define a CronJob named addwork with schedule "*/5 * * * *". Its Job template runs the pi_worker:2 image with add_tasks.py, passes REDIS_HOST and PYTHONUNBUFFERED environment variables, and uses restartPolicy: Never. After creation, kubectl get cronjob,job,pods shows the scheduled Job and a Completed Pod.

How Kubernetes for Developers covers it

How deep it goes
Explains nested CronJob, Job, and Pod templates with a complete manifest, but does not provide exhaustive API reference.
Examples
Demonstrates a five-minute worker schedule, kubectl creation, and resulting Job and Completed Pod.
What it assumes
Assumes Jobs and Pods while clarifying how their templates nest inside a CronJob.
What you can do after
Build it from scratch with a batch/v1 manifest and verify scheduled Jobs and Pods using kubectl.
How it connects
Connects CronJob with Kubernetes Job, Pod, Deployment, background queues, and Redis coordination.

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 for Developers. The section links point to the parts of the book it draws on.