Skip to main content
RunBook Academy

KubernetesCV · KustomizeKustomize

Kustomize patches — strategic merge vs JSON 6902 vs images transformer

Advanced⏱ ~17 minkubectlkustomize

What you'll learn

  • Choose between strategic merge and JSON 6902 patches
  • Use the images transformer for image rewrites
  • Apply patches to specific resources via target selectors
  • Apply the operational discipline of choosing the right patch type

Prerequisites

Verified against Kubernetes 1.34.x · kubeadm 1.34.x · kubectl 1.34.x · etcd 3.6.x · CoreDNS 1.11.x · containerd 1.7.x / 2.x · 2026-08-16

Not yet marked complete on this device.

Kustomize supports multiple patch types: strategic merge, JSON 6902, and the images transformer. This lesson walks each, the trade-offs, and the discipline.

Strategic merge patches

# patch-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 5
  template:
    spec:
      containers:
        - name: myapp
          image: myapp:2.0.0
          resources:
            requests:
              cpu: 500m
              memory: 512Mi

Strategic merge patches:

  • Merge by field name (not by position).
  • Lists are merged by element (e.g., containers by name).
  • Fields not in the patch are inherited from the base.
  • Kubernetes-native: the API server uses the same merge strategy.
# kustomization.yaml
patches:
  - path: patch-deployment.yaml
    target:
      kind: Deployment
      name: myapp

The patch is applied to the resource matching the target selector.

JSON 6902 patches

# patch-deployment.yaml
- op: replace
  path: /spec/replicas
  value: 5
- op: replace
  path: /spec/template/spec/containers/0/image
  value: myapp:2.0.0
- op: add
  path: /spec/template/spec/containers/0/env
  value:
    - name: LOG_LEVEL
      value: debug

JSON 6902 patches:

  • Operate on the JSON representation of the resource.
  • Operations: add, remove, replace, move, copy, test.
  • Paths are JSON Pointer notation.
  • Precise: each operation specifies exactly what to change.
patches:
  - path: patch-deployment.yaml
    target:
      version: v1
      kind: Deployment
      name: myapp
    patch: |-
      - op: replace
        path: /spec/replicas
        value: 5

Use JSON 6902 when strategic merge cannot express the change (e.g., inserting an element into a list at a specific position).

Images transformer

images:
  - name: myapp
    newName: myapp
    newTag: "2.0.0"
  - name: postgres
    newName: registry.example.com/postgres
    newTag: "16"

The images transformer rewrites image references without patching the Deployment:

  • name — the original image name (without tag).
  • newName — the new image name.
  • newTag — the new tag.

The transformer finds every container with the original image and rewrites it. This is cleaner than patching the Deployment directly.

Choosing the right patch type

flowchart TD
    A[Patch type] --> B{Image change?}
    B -->|Yes| C[Images transformer]
    B -->|No| D{Field merge?}
    D -->|Yes| E[Strategic merge]
    D -->|No, precise operation| F[JSON 6902]

The choice:

  • Image change. Use the images transformer.
  • Field merge (typical overrides). Use strategic merge.
  • Precise operations (insert, remove, replace at path). Use JSON 6902.

Most patches in a typical overlay are strategic merge. JSON 6902 is for the cases strategic merge cannot handle. The images transformer is for image version changes.

Quiz

Knowledge check · 4 questions

  1. Q1. When is a JSON 6902 patch the right choice over a strategic merge patch?

  2. Q2. Strategic merge patches understand that Kubernetes lists such as `containers` are keyed by name.

  3. Q3. Remove an inherited environment variable from a production overlay when the obvious patch has no effect.

    The base Deployment `checkout` sets `DEBUG=true` in its container's `env`, and production must not run with it. The prod overlay's strategic-merge patch declares the container with `env: []`, but `kubectl kustomize overlays/prod` still renders `DEBUG=true` and the production Pods are writing around 40 GB of debug logs a day.

  4. Q4. Name the two ways to delete one element of a list in a Kustomize patch, and say which of them survives the list being reordered.

Passing score: 75%. Answers are checked in this browser.

The operational discipline

Kustomize patches in production rest on five non-negotiable elements:

  • Use images transformer for image changes. Cleaner than patching.
  • Use strategic merge for typical overrides. Kubernetes-native; predictable.
  • Use JSON 6902 for precise operations. Strategic merge cannot express every change.
  • Test the rendered output. Always kubectl kustomize before applying.
  • Review patches in PRs. Patches are changes; they go through review like any other change.

The patch type is a tool, not a preference. The discipline is to choose the right tool for the task.