CKA Certification Course - Certified Kubernetes Administrator
Patches Dictionary
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.
──────────────────────────────────────────────
Updating a Key with a JSON 6902 Patch
──────────────────────────────────────────────
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:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 1
selector:
matchLabels:
component: api
template:
metadata:
labels:
component: api
spec:
containers:
- name: nginx
image: nginx
To update this label using a JSON 6902 patch, add the following patch to your kustomization.yaml:
patches:
- target:
kind: Deployment
name: api-deployment
patch: |-
- op: replace
path: /spec/template/metadata/labels/component
value: web
How It Works
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.
──────────────────────────────────────────────
Updating a Key with a Strategic Merge Patch
──────────────────────────────────────────────
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:
patches:
- label-patch.yaml
Then, create label-patch.yaml with only the fields that need to be updated:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
template:
metadata:
labels:
component: web
Kustomize will merge this patch with the original configuration, updating only the component label value from api to web.
──────────────────────────────────────────────
Adding a New Key Using a JSON 6902 Patch
──────────────────────────────────────────────
Suppose you want to add a new label org: KodeKloud to the existing deployment configuration. Consider the following original configuration:
# api-depl.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 1
selector:
matchLabels:
component: api
template:
metadata:
labels:
component: api
spec:
containers:
- name: nginx
image: nginx
Now, add the following JSON 6902 patch in your kustomization.yaml:
# kustomization.yaml
patches:
- target:
kind: Deployment
name: api-deployment
patch: |-
- op: add
path: /spec/template/metadata/labels/org
value: KodeKloud
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 value KodeKloud is the new label value.
After applying the patch, the final configuration will include both labels—component: api and org: KodeKloud:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 1
selector:
matchLabels:
component: api
template:
metadata:
labels:
component: api
org: KodeKloud
spec:
containers:
- name: nginx
image: nginx
──────────────────────────────────────────────
Adding a New Key with a Strategic Merge Patch
──────────────────────────────────────────────
To add a new label using a strategic merge patch, reference an external patch file. In your kustomization.yaml, include:
patches:
- label-patch.yaml
Then, in the label-patch.yaml file, specify the new key:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
template:
metadata:
labels:
org: kodekloud
Kustomize will merge this patch with the original configuration, resulting in the addition of the new org label.
──────────────────────────────────────────────
Removing a Key Using a JSON 6902 Patch
──────────────────────────────────────────────
Consider a deployment that currently has two labels: org: KodeKloud and component: api. Here is the existing configuration:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 1
selector:
matchLabels:
component: api
template:
metadata:
labels:
org: KodeKloud
component: api
spec:
containers:
- name: nginx
image: nginx
To remove the org label, apply the following JSON 6902 patch:
patches:
- target:
kind: Deployment
name: api-deployment
patch: |-
- op: remove
path: /spec/template/metadata/labels/org
This patch uses the "remove" operation to delete the org label. After merging, only the component: api label remains.
──────────────────────────────────────────────
Removing a Key with a Strategic Merge Patch
──────────────────────────────────────────────
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:
patches:
- label-patch.yaml
Then, in label-patch.yaml, mark the org label for removal:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
template:
metadata:
labels:
org: null
Kustomize merges this patch to remove the org label, leaving only the component: api label in the configuration.
──────────────────────────────────────────────
Conclusion
──────────────────────────────────────────────
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.
Watch Video
Watch video content