liveBook · Manning

Composition

As explained in Kubernetes for Developers

Object composition in Kubernetes is a pattern in which a higher-order resource embeds the specification of a lower-level resource.

Suppose you need an application to keep running through updates and node failures. A Pod describes its containers, but a Pod created directly is not managed by a controller that will reschedule it after eviction. A Deployment embeds a Pod template and manages the Pods it creates. This embedding of one object’s specification inside a higher-order object is Kubernetes object composition. section 3.2.4

The same pattern serves different jobs:

  • A Deployment embeds a Pod template. Its controller creates the requested replicas, replaces Pods during updates, and reschedules evicted Pods. section 3.2.4
  • A Job embeds a Pod template for a task that should finish. The embedded PodSpec can use container settings such as environment variables; the Job also sets a backoffLimit for attempts. section 10.2.1
  • A CronJob embeds a Job template, which embeds a Pod template. Its schedule field determines when to create Jobs. section 10.2.2

Read nested YAML from the outside in: identify the higher-order resource, find its template, then find the embedded object’s spec. A PodSpec keeps its Pod capabilities when embedded, although the surrounding resource may rule out combinations that do not fit its purpose. For example, a Job can use a Pod restartPolicy of Never or OnFailure, but not Always. section 3.2.4 section 10.2.1

Listing 10.10 Chapter10/10.2.2_CronJob/cronjob_addwork.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: addwork
spec:
  schedule: "*/5 * * * *"   #1
  jobTemplate:
    spec:                   #2
      backoffLimit: 2
      template:
        spec:
          containers:
          - name: pi-container
            image: docker.io/wdenniss/pi_worker:2
            command: ["python3",  "add_tasks.py"]
            env:
            - name: REDIS_HOST
              value: redis-0.redis-service
            - name: PYTHONUNBUFFERED
              value: "1"
          restartPolicy: Never 

The CronJob manifest makes the nesting concrete: schedule sits on the CronJob, backoffLimit sits in jobTemplate.spec, and containers sit in the Job’s embedded Pod spec. section 10.2.2

How Kubernetes for Developers covers it

How deep it goes
Explains template nesting through runnable manifests and controller behavior, while directing readers to API documentation for additional embedded fields.
Examples
Shows a standalone Pod, a one-off Job running a container command, and a CronJob scheduling that Job.
What it assumes
Introduces PodSpec through a concrete Deployment and standalone Pod; assumes enough Kubernetes YAML familiarity to read object manifests.
What you can do after
Build it from scratch by placing a PodSpec in a managed template and locating each setting at its proper nesting level.
How it connects
Connects PodSpec and controllers with Deployments, Jobs, CronJobs, restartPolicy, and backoffLimit.

Also explained in

Compare how 11 books explain composition →

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.