Skip to main content
In this article, you’ll learn how to modify containers within a Kubernetes Deployment by performing operations on list items. We cover how to replace, add, and delete items from the container list using both JSON 6902 patches and strategic merge patches. Every example maintains the correct YAML hierarchy and list indexing to ensure a seamless configuration update. Below is the base deployment configuration used in all examples:
──────────────────────────────

1. Replacing a Container Using a JSON 6902 Patch

In this first example, we update the container’s name and image from “nginx” to “haproxy”. Since the container section is defined as a list, we specifically target the first container (index 0) in the patch path. Remember that list indexes start at zero. The following JSON 6902 patch replaces the container at index 0:
Kustomize will apply this patch to update the first (and only) container of the Deployment.
Ensure you use the correct index when working with lists in your YAML configurations.
──────────────────────────────

2. Replacing a Container Using a Strategic Merge Patch

An alternative approach is to update the container using a strategic merge patch. In this example, we update the image of an existing container. Verify that the Deployment configuration and the patch file refer to the same container name. Note that a minor typo (“ngin” instead of “nginx”) has been corrected for clarity. The base deployment remains as:
The strategic merge patch, defined in a file (e.g., label-patch.yaml), looks like this:
In your kustomization.yaml, reference the patch file:
When merged, this patch updates the container named “nginx”, changing its image to “haproxy”. ──────────────────────────────

3. Adding an Item to a List Using a JSON 6902 Patch

To add another container to your Deployment, use the JSON patch with an “add” operation. The patch path ends with a dash (-) to signal that the new container should be appended to the end of the list. Although indexing (e.g., 0 for the beginning) is possible, appending is achieved easily using the dash notation. The base deployment configuration is as follows:
The JSON 6902 patch to add a new container is:
After applying this patch, the Deployment will include both the original “nginx” container and the newly added “haproxy” container. ──────────────────────────────

4. Adding an Item to a List Using a Strategic Merge Patch

You can also add a container by merging configuration files. In this approach, the original Deployment configuration is combined with a patch file. In this example, the initial Deployment includes a placeholder container named “web” with the image “nginx”:
The patch file (e.g., label-patch.yaml) adds an additional container:
Reference the patch in your kustomization.yaml:
After applying the merge, the Deployment will feature two containers: one named “web” using the image “nginx” and another named “haproxy” using the image “haproxy”.
Using strategic merge patches lets you combine multiple configuration files effortlessly while keeping your Deployment definitions clean and modular.
──────────────────────────────

5. Deleting an Item from a List Using a JSON 6902 Patch

If you need to remove a container from your Deployment, such as one named “database”, a JSON 6902 patch can be used to delete it by specifying its index in the list. In the example below, the “web” container is at index 0, and the “database” container is at index 1. The original configuration:
The JSON 6902 patch to remove the container at index 1 is:
After applying this patch, the “database” container will be removed, leaving only the “web” container in your Deployment. ──────────────────────────────

6. Deleting an Item from a List Using a Strategic Merge Patch

Removing a container using a strategic merge patch is achieved by adding a delete directive in your patch file. In this example, we remove the container with the name “database”. The starting configuration is:
The strategic merge patch file (e.g., label-patch.yaml) includes the $patch: delete directive:
Add this patch to your kustomization.yaml:
After merging, Kustomize removes the container named “database”, leaving only the “web” container in the final configuration.
Double-check list indexes and container names when applying deletion patches to avoid removing the wrong configuration.
──────────────────────────────

Summary

By using JSON 6902 patches and strategic merge patches, you can precisely modify list elements in your Kubernetes configurations. The methods discussed include: These techniques allow you to flexibly manage your container configurations. For further reading on Kubernetes patches, visit the Kubernetes Documentation. By understanding and applying these strategies, maintaining complex deployments becomes more efficient and error-free. Happy patching!

Watch Video

Practice Lab