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 6902pathto append to the list. - Strategic merge patches match list items by merge key (commonly
namefor containers).
Quick comparison
Example base Deployment
Use this base Deployment asapi-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 thereplace operation. The example below replaces the container at index 0 with a new container named haproxy.
kustomization.yaml (using patchesJson6902):
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 (commonlyname) and merge fields. This is ideal when you want to update a field (like image) without replacing the whole container object.
kustomization.yaml:
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, useop: 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:
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.
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 aweb container; the patch introduces a new haproxy container, and Kustomize will merge them.
Base Deployment example (different base with a web container):
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, useop: remove and specify the exact index to remove. The example below removes the second container (index 1) from a two-container base.
Base Deployment:
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):
- The
$patch: deletedirective tells Kustomize to remove the container matchingname: databasefrom the final merged resource. - If the
databasecontainer is present in the base, Kustomize removes it during the merge.
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.
- Kustomize docs: https://kubectl.docs.kubernetes.io/references/kustomize/
- JSON Patch (RFC 6902): https://tools.ietf.org/html/rfc6902
- Kubernetes Deployments: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/