Skip to main content
The same patching techniques you use to remove, replace, and add keys in dictionaries also apply to YAML lists—such as the spec.template.spec.containers list in a Kubernetes Deployment. List fields are ordered and each item has a 0-based index. Kustomize supports two common patch styles for list operations:
  • JSON 6902 patches: index-based operations (precise control, insert/remove at a specific position).
  • Strategic merge patches: merge-key-based operations (match list items by a key such as name).
List basics:
  • JSON 6902 patches act on list indices (0-based).
  • Use - at the end of a JSON 6902 path to append to the list.
  • Strategic merge patches match list items by merge key (commonly name for containers).

Quick comparison


Example base Deployment

Use this base Deployment as api-depl.yaml for the examples below.

Replace a container (JSON 6902)

To replace an entire container at a specific index, use a JSON 6902 patch and the replace operation. The example below replaces the container at index 0 with a new container named haproxy. kustomization.yaml (using patchesJson6902):
Result: the container at index 0 is replaced with the haproxy container (both name and image changed).
Using index-based (JSON 6902) operations can be fragile if prior patches or changes reorder the list. Prefer strategic merge when you only need to update specific fields by name.

Replace a container’s image (strategic merge patch)

Strategic merge patches match list items by their merge key (commonly name) and merge fields. This is ideal when you want to update a field (like image) without replacing the whole container object. kustomization.yaml:
label-patch.yaml (strategic merge patch):
When Kustomize performs the strategic merge, it finds the containers item with name: nginx and updates the image field. The container retains its name but now uses the haproxy image.

Add a container (JSON 6902)

To append or insert a new container using JSON 6902, use op: add and the list path. Use a trailing - to append to the end of the list, or specify an index to insert at a particular position. kustomization.yaml:
Notes:
  • path: /spec/template/spec/containers/- appends the new container to the end of the list.
  • To insert at the front, use /spec/template/spec/containers/0; to insert as the second item, use /spec/template/spec/containers/1, etc.
Result: the Deployment will have both the original nginx container and the new haproxy container.

Add a container (strategic merge patch)

Strategic merge patches can also add new list items when the merge key differs. In this example the base Deployment includes a web container; the patch introduces a new haproxy container, and Kustomize will merge them. Base Deployment example (different base with a web container):
kustomization.yaml:
label-patch.yaml:
Result: both web and haproxy containers will be present because the patch adds a new container with a distinct name.

Remove a container (JSON 6902)

To remove an item from a list using JSON 6902, use op: remove and specify the exact index to remove. The example below removes the second container (index 1) from a two-container base. Base Deployment:
kustomization.yaml:
Result: the database container (index 1) is removed, leaving only the web container.

Remove a container (strategic merge patch)

With strategic merge patches, deletion is expressed using the $patch: delete directive. Provide the merge key (name) to identify which list item to delete. Base Deployment (two containers):
kustomization.yaml:
label-patch.yaml (strategic merge patch instructing deletion):
Explanation:
  • The $patch: delete directive tells Kustomize to remove the container matching name: database from the final merged resource.
  • If the database container is present in the base, Kustomize removes it during the merge.
Result: the database container is removed; only web remains.
These examples illustrate common list operations in Kustomize:
  • Use JSON 6902 patches when you need index-level operations (replace at position, insert at specific index, or remove by index).
  • Use strategic merge patches when you want to match items by key (like name) to add, modify, or delete entries more robustly.
Links and references:

Watch Video

Practice Lab