Skip to main content
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.

Overview

Kustomize supports two main patch mechanisms:
Use JSON 6902 when you need fine-grained control. Choose Strategic Merge for simpler, declarative label or annotation updates.

Base Deployment Configuration

Save this as base/api-deployment.yaml:
In the same directory, your kustomization.yaml should include:

1. Replacing a Label

1.1 JSON 6902 Patch

Add this section under patches in kustomization.yaml:
  • op: replace – updates the existing component label.
  • path – JSON Pointer to /spec/template/metadata/labels/component.
  • value: web – new label value.

1.2 Strategic Merge Patch

Reference an external YAML file in kustomization.yaml:
Create label-replace.yaml:
Kustomize merges only the provided fields, updating component to web.

2. Adding a Label

2.1 JSON 6902 Patch

In kustomization.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 in kustomization.yaml:
Create label-add.yaml:
After applying, labels will include both component: api and org: kodekloud.

3. Removing a Label

Assume the Deployment now has:

3.1 JSON 6902 Patch

In kustomization.yaml:
  • op: remove – deletes the org key.

3.2 Strategic Merge Patch

Reference in kustomization.yaml:
Create label-remove.yaml:
Setting org: null instructs Kustomize to remove that label key.

Watch Video