Learn to update, add, and remove keys in Kubernetes Deployment configurations using JSON 6902 patches and strategic merge patches with practical examples.
In this article, you’ll learn how to update, add, and remove keys in a Kubernetes Deployment configuration using both JSON 6902 patches and strategic merge patches. Each example starts with a Deployment that contains a label with the key “component” set to “api”. The goal is to modify or update these labels as needed in each scenario.
The patch navigates to the “component” key within the labels dictionary using the path /spec/template/metadata/labels/component and replaces its value with “web”.
The JSON 6902 patch method provides precise control when updating complex configurations. Choose the patch type that best fits your needs.
Alternatively, you can update the label using a strategic merge patch stored in a separate file, for example, label-patch.yaml. Your main Deployment configuration remains unchanged, and your kustomization.yaml is updated as follows:
Copy
Ask AI
kustomization: patches: - label-patch.yaml
Here is an example of the contents for label-patch.yaml:
Copy
Ask AI
apiVersion: apps/v1kind: Deploymentmetadata: name: api-deploymentspec: template: metadata: labels: component: web
Kustomize will merge this patch with the original Deployment configuration, resulting in an updated “component” label.
Suppose you want to add a new label "org" with the value "KodeKloud" while keeping the original "component: api" label. Start with the following Deployment:
Reference this patch file in your kustomization.yaml:
Copy
Ask AI
kustomization: patches: - label-patch.yaml
Kustomize interprets the null value as an instruction to remove the "org" label from the original configuration.
Ensure that you specify the correct path for removal operations to avoid inadvertently deleting other keys in the configuration.
With these examples, you now understand how to update, add, and remove keys in a Kubernetes Deployment configuration using both JSON 6902 patches and strategic merge patches. For more detailed information on Kubernetes configurations and patch strategies, visit the Kubernetes Documentation.