When customizing Kubernetes resources, you often need to modify lists—such as theDocumentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
containers array in a Deployment. Kustomize supports two patch strategies:
- JSON 6902 patches: precise, index-based operations (
add,replace,remove). - Strategic Merge Patches (SMP): declarative, merge-key based edits.
| Operation | JSON 6902 Patch | Strategic Merge Patch |
|---|---|---|
| Replace item | Targets an explicit index | Matches on name merge key |
| Add item | Uses add with /- or an index | Appends new entries by default |
| Remove item | Uses remove with a specific index | Uses $patch: delete on the merge key |
Base Deployment Example
Every entry in
spec.template.spec.containers is a YAML list item, so it begins with -.1. Replacing a Container
1.1 JSON 6902 Patch
Add to yourkustomization.yaml:
1.2 Strategic Merge Patch
Create a filereplace-container.yaml:
kustomization.yaml:
name: nginx and updates its image.
2. Adding a Container
2.1 JSON 6902 Patch
To append at the end:- with 1, 2, etc.
Using
path: /containers/- always appends. For precise position, specify the index.2.2 Strategic Merge Patch
Fileadd-container.yaml:
kustomization.yaml:
haproxy container is appended automatically.
3. Removing a Container
Assume this deployment:3.1 JSON 6902 Patch
To remove the second container (index 1):web remains.
3.2 Strategic Merge Patch
Createremove-database.yaml:
kustomization.yaml:
name: database.