Learn to modify Kubernetes Deployment containers by replacing, adding, and deleting items using JSON 6902 and strategic merge patches while maintaining YAML hierarchy.
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:
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:
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:
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:
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”:
Copy
Ask AI
# api-depl.yamlapiVersion: apps/v1kind: Deploymentmetadata: name: api-deploymentspec: replicas: 1 selector: matchLabels: component: api template: metadata: labels: component: api spec: containers: - name: web image: nginx
The patch file (e.g., label-patch.yaml) adds an additional container:
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:
Copy
Ask AI
apiVersion: apps/v1kind: Deploymentmetadata: name: api-deploymentspec: replicas: 1 selector: matchLabels: component: api template: metadata: labels: component: api spec: containers: - name: web image: nginx - name: database image: mongo
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:
Copy
Ask AI
apiVersion: apps/v1kind: Deploymentmetadata: name: api-deploymentspec: replicas: 1 selector: matchLabels: component: api template: metadata: labels: component: api spec: containers: - name: web image: nginx - name: database image: mongo
The strategic merge patch file (e.g., label-patch.yaml) includes the $patch: delete directive:
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
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!