CKA Certification Course - Certified Kubernetes Administrator
2025 Updates Kustomize Basics
Patches list
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:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 1
selector:
matchLabels:
component: api
template:
metadata:
labels:
component: api
spec:
containers:
- name: nginx
image: nginx
──────────────────────────────
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:
patches:
- target:
kind: Deployment
name: api-deployment
patch: |-
- op: replace
path: /spec/template/spec/containers/0
value:
name: haproxy
image: haproxy
Kustomize will apply this patch to update the first (and only) container of the Deployment.
Tip
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:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 1
selector:
matchLabels:
component: api
template:
metadata:
labels:
component: api
spec:
containers:
- name: nginx
image: nginx
The strategic merge patch, defined in a file (e.g., label-patch.yaml
), looks like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
template:
spec:
containers:
- name: nginx
image: haproxy
In your kustomization.yaml
, reference the patch file:
patches:
- label-patch.yaml
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:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 1
selector:
matchLabels:
component: api
template:
metadata:
labels:
component: api
spec:
containers:
- name: nginx
image: nginx
The JSON 6902 patch to add a new container is:
patches:
- target:
kind: Deployment
name: api-deployment
patch: |-
- op: add
path: /spec/template/spec/containers/-
value:
name: haproxy
image: haproxy
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":
# api-depl.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
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:
# label-patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
template:
spec:
containers:
- name: haproxy
image: haproxy
Reference the patch in your kustomization.yaml
:
# kustomization.yaml
patches:
- label-patch.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".
Hint
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:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
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:
patches:
- target:
kind: Deployment
name: api-deployment
patch: |-
- op: remove
path: /spec/template/spec/containers/1
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:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
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:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
template:
spec:
containers:
- $patch: delete
name: database
Add this patch to your kustomization.yaml
:
patches:
- label-patch.yaml
After merging, Kustomize removes the container named "database", leaving only the "web" container in the final configuration.
Warning
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 |
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
Watch video content
Practice Lab
Practice lab