Skip to main content
This lesson shows how to update a key inside a dictionary (specifically: labels on a Pod template) in a Kubernetes resource using both JSON Patch (RFC 6902) and Strategic Merge Patch via Kustomize. You’ll see concise examples that replace, add, and remove a label key on a Deployment and the exact kustomization/patch files required.
When you change labels on a Pod template, consider whether spec.selector.matchLabels also needs to be updated so the Deployment continues to select the updated Pods. Failing to update the selector may cause unexpected Deployment behavior.

Summary: When to use JSON 6902 vs Strategic Merge Patch

Replace a key in a dictionary using JSON 6902

Original Deployment (api-deployment.yaml):
kustomization.yaml with an inline JSON 6902 patch that replaces the component label under the Pod template:
Result (after applying the patch): the Pod template component label becomes web while the Deployment’s spec.selector.matchLabels still contains the original api value (update the selector if you intend to match the new label).

Replace a key in a dictionary using a Strategic Merge Patch

Use an external patch file and reference it from kustomization.yaml. kustomization.yaml:
label-patch.yaml (strategic merge patch — include only identifying fields and the fields to change):
Kustomize will merge label-patch.yaml into the original Deployment, resulting in the Pod template label component: web (again, update spec.selector.matchLabels if you need the selector to match the updated Pods). Resulting fragment:

Add a new key in a dictionary using JSON 6902

Original Deployment (api-deployment.yaml):
kustomization.yaml with an inline JSON 6902 patch that adds a new label org: KodeKloud under the Pod template:
Result: the Pod template includes both component: api and org: KodeKloud.

Add a new key in a dictionary using a Strategic Merge Patch

Using an external patch file referenced by kustomization.yaml: kustomization.yaml:
label-patch.yaml (strategic merge patch):
Kustomize will merge the new label into the existing Pod template metadata, resulting in:

Remove a key from a dictionary using JSON 6902

Original Deployment (api-deployment.yaml) contains two labels:
kustomization.yaml with an inline JSON 6902 patch that removes the org label:
Result: the org label is removed from the Pod template; only component: api remains.

Remove a key from a dictionary using a Strategic Merge Patch

With Strategic Merge Patch you indicate deletion by setting a key to null in the patch file. kustomization.yaml:
label-patch.yaml (strategic merge patch to remove the org label):
Kustomize interprets org: null as “remove this key” and merges accordingly, yielding:

Best practices and tips
  • Use JSON 6902 for precise, targeted operations when you need to guarantee the exact change (add/replace/remove a single field).
  • Use Strategic Merge Patch for declarative, easy-to-read patches that merge cleanly with existing objects.
  • Always re-evaluate spec.selector.matchLabels when changing Pod template labels to ensure Deployment selection remains correct.
  • Keep patch files small and focused — include only the identifying fields and the fields you intend to change.
Links and references

Watch Video