Learn to update, add, and remove keys in Kubernetes deployment configurations using JSON 6902 and strategic merge patches with step-by-step examples.
In this lesson, you’ll learn how to update, add, and remove keys in a Kubernetes deployment configuration using both JSON 6902 patches and strategic merge patches. The step-by-step examples provided below will help you understand the process and decide which patching method best fits your configuration management needs.──────────────────────────────────────────────
──────────────────────────────────────────────Assume you have a deployment configuration with a label component: api that needs to be updated to component: web. First, consider the original deployment configuration:
The patch uses the “replace” operation to update the existing key at the specified path (/spec/template/metadata/labels/component) with the new value web.
──────────────────────────────────────────────You can achieve the same update using a strategic merge patch. Instead of including the patch inline, store it in a separate file named label-patch.yaml and reference it in your kustomization.yaml.In your kustomization.yaml, include:
Copy
Ask AI
patches: - label-patch.yaml
Then, create label-patch.yaml with only the fields that need to be updated:
Copy
Ask AI
apiVersion: apps/v1kind: Deploymentmetadata: name: api-deploymentspec: template: metadata: labels: component: web
Kustomize will merge this patch with the original configuration, updating only the component label value from api to web.──────────────────────────────────────────────
──────────────────────────────────────────────Suppose you want to add a new label org: KodeKloud to the existing deployment configuration. Consider the following original configuration:
In this patch:• The add operation instructs Kubernetes to introduce a new key.
• The path defines where the new label should be inserted (/spec/template/metadata/labels/org).
• The valueKodeKloud is the new label value.After applying the patch, the final configuration will include both labels—component: api and org: KodeKloud:
──────────────────────────────────────────────To add a new label using a strategic merge patch, reference an external patch file. In your kustomization.yaml, include:
Copy
Ask AI
patches: - label-patch.yaml
Then, in the label-patch.yaml file, specify the new key:
Kustomize will merge this patch with the original configuration, resulting in the addition of the new org label.──────────────────────────────────────────────
──────────────────────────────────────────────Consider a deployment that currently has two labels: org: KodeKloud and component: api. Here is the existing configuration:
This patch uses the “remove” operation to delete the org label. After merging, only the component: api label remains.──────────────────────────────────────────────
──────────────────────────────────────────────With a strategic merge patch, you can remove a key by setting its value to null. Starting with the same configuration as above, reference an external patch file in your kustomization.yaml:
Copy
Ask AI
patches: - label-patch.yaml
Then, in label-patch.yaml, mark the org label for removal:
Kustomize merges this patch to remove the org label, leaving only the component: api label in the configuration.──────────────────────────────────────────────
──────────────────────────────────────────────This lesson demonstrated how to update (replace), add, and remove keys in your Kubernetes deployment configurations using both JSON 6902 patches and strategic merge patches. Choose the method that aligns best with your workload and configuration complexity. For further information, explore the Kubernetes Documentation.Use these techniques to maintain clean, efficient deployment configurations in your Kubernetes environment.