A Job template is the Job configuration embedded in a CronJob. The CronJob controller uses it to create a new Job whenever its schedule fires; the Job controller then creates the Pod or Pods. section 18.2
A Job scheduled every minute
A CronJob for aggregating the day's Quiz responses places its Job definition under spec.jobTemplate and uses schedule "* * * * *". After the next minute begins, kubectl get cj shows one active Job. Labels added to the Job template and its nested Pod template make the resulting Jobs and Pods easy to find. section 18.2.1
Listing 18.17 A CronJob that runs a Job every minute
apiVersion: batch/v1 #1
kind: CronJob #1
metadata:
name: aggregate-responses-every-minute
spec:
schedule: "* * * * *" #2
jobTemplate: #3
metadata: #3
labels: #3
app: aggregate-responses-today #3
spec: #3
template: #3
metadata: #3
labels: #3
app: aggregate-responses-today #3
spec: #3
restartPolicy: OnFailure #3
containers: #3
- name: updater #3
image: mongo:5 #3
command: #3
- mongosh #3
- mongodb+srv://quiz-pods.kiada.svc.cluster.local/kiada?tls=false #3
- --quiet #3
- --file #3
- /script.js #3
volumeMounts: #3
- name: script #3
subPath: script.js #3
mountPath: /script.js #3
volumes: #3
- name: script #3
configMap: #3
name: aggregate-responses-today #3
Inspect the manifest to see the CronJob's two main configuration areas: schedule and jobTemplate, including metadata and the nested Pod template. section 18.2.1
The template participates in the scheduled workload lifecycle:
- The CronJob controller creates a Job from the template at each scheduled time. section 18.2
- The Job controller creates the Pods from that Job, as it does for a manually created Job. section 18.2
- Add your own labels when you need to list Jobs created by a particular CronJob. section 18.2.1
