liveBook · Manning

Job template

As explained in Kubernetes in Action, Second Edition

A Job template is the configuration embedded in a CronJob that defines the Jobs it creates.

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

How Kubernetes in Action, Second Edition covers it

How deep it goes
Explains lifecycle, manifest placement, labels, and inspection, while treating Job manifest details as previously learned.
Examples
Uses a per-minute Quiz-response aggregation CronJob with labels on the Job and nested Pod templates.
What it assumes
Assumes familiarity with writing a Job manifest and optionally with crontab schedule syntax.
What you can do after
Operate/configure a CronJob by defining its jobTemplate, scheduling it, and selecting its generated Jobs with labels.
How it connects
Connects CronJob, Job controller, Jobs, Pods, schedules, and labels through a generated-workload lifecycle.

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.