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:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
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: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:label-patch.yaml), looks like this:
kustomization.yaml, reference the patch file:
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: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”:label-patch.yaml) adds an additional container:
kustomization.yaml:
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: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:label-patch.yaml) includes the $patch: delete directive:
kustomization.yaml:
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:| Operation | Patch Type | Use Case | Example File Reference |
|---|---|---|---|
| Replace | JSON 6902/Strategic | Update container name and image | See Sections 1 & 2 |
| Add | JSON 6902/Strategic | Append new container to the Deployment list | See Sections 3 & 4 |
| Delete | JSON 6902/Strategic | Remove a container based on index or name | See Sections 5 & 6 |