Learn how to update, add, and remove keys (e.g., labels) in a Kubernetes Deployment using Kustomize. We’ll demonstrate both JSON 6902 patches and Strategic Merge Patches with clear examples and best practices.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.
Overview
Kustomize supports two main patch mechanisms:| Patch Type | Syntax | Use Case | Reference |
|---|---|---|---|
| JSON 6902 Patch | JSON Pointer | Precise add, replace, remove ops | JSON Patch (RFC6902) |
| Strategic Merge Patch | YAML merge | Declarative updates, merges by key | Strategic Merge Patch |
Use JSON 6902 when you need fine-grained control. Choose Strategic Merge for simpler, declarative label or annotation updates.
Base Deployment Configuration
Save this asbase/api-deployment.yaml:
kustomization.yaml should include:
1. Replacing a Label
1.1 JSON 6902 Patch
Add this section underpatches in kustomization.yaml:
- op: replace – updates the existing
componentlabel. - path – JSON Pointer to
/spec/template/metadata/labels/component. - value: web – new label value.
1.2 Strategic Merge Patch
Reference an external YAML file inkustomization.yaml:
label-replace.yaml:
component to web.
2. Adding a Label
2.1 JSON 6902 Patch
Inkustomization.yaml:
- op: add – inserts a new key
org. - value: kodekloud – label value added under
.spec.template.metadata.labels.
2.2 Strategic Merge Patch
Reference a file inkustomization.yaml:
label-add.yaml:
component: api and org: kodekloud.
3. Removing a Label
Assume the Deployment now has:3.1 JSON 6902 Patch
Inkustomization.yaml:
- op: remove – deletes the
orgkey.
3.2 Strategic Merge Patch
Reference inkustomization.yaml:
label-remove.yaml:
Setting
org: null instructs Kustomize to remove that label key.