The kubectl patch
command in Kubernetes is used to update fields of a resource using strategic merge patch, JSON patch, or merge patch. It allows for making precise modifications to existing resources without requiring a full replacement.
Here are several examples demonstrating the usage of kubectl patch
:
Example 1: Patching a Deployment to update its image:
kubectl patch deployment nginx-deployment -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:1.19.10"}]}}}}'
This command updates the image of the container named ‘nginx’ in the ‘nginx-deployment’ Deployment to version 1.19.10 of nginx.
To verify, you can use:
kubectl get deployment nginx-deployment -o=jsonpath='{.spec.template.spec.containers[0].image}'
Expected output: nginx:1.19.10
Example 2: Patching a Service to change its type to NodePort:
kubectl patch service my-service -p '{"spec":{"type":"NodePort"}}'
This command modifies the type of the ‘my-service’ Service to NodePort.
To verify, you can use:
kubectl get service my-service -o=jsonpath='{.spec.type}'
Expected output: NodePort
Example 3: Applying a strategic merge patch to update labels:
kubectl patch pod mypod --type='json' -p='[{"op": "replace", "path": "/metadata/labels", "value": {"new-label": "value"}}]'
This updates the labels of the ‘mypod’ Pod to include a new label named ‘new-label’.
To verify, you can use:
kubectl get pod mypod -o=jsonpath='{.metadata.labels.new-label}'
Expected output: value
Example 4: Patching a ConfigMap to add a new data entry:
kubectl patch configmap my-config --type='json' -p='[{"op": "add", "path": "/data/new-key", "value": "new-value"}]'
This adds a new entry ‘new-key: new-value’ to the ‘my-config’ ConfigMap.
To verify, you can use:
kubectl get configmap my-config -o=jsonpath='{.data.new-key}'
Expected output: new-value
Example 5: Patching a Namespace to update its annotations:
kubectl patch namespace my-namespace -p '{"metadata":{"annotations":{"new-annotation":"value"}}}'
This updates the annotations of the ‘my-namespace’ Namespace to include a new annotation ‘new-annotation’.
To verify, you can use:
kubectl get namespace my-namespace -o=jsonpath='{.metadata.annotations.new-annotation}'
Expected output: value
Example 6: Patching a StatefulSet to modify its replicas:
kubectl patch statefulset my-statefulset -p '{"spec":{"replicas":3}}'
This adjusts the number of replicas for the ‘my-statefulset’ StatefulSet to 3.
To verify, you can use:
kubectl get statefulset my-statefulset -o=jsonpath='{.spec.replicas}'
Expected output: 3
Example 7: Patching a PersistentVolumeClaim to update storage size:
kubectl patch pvc my-pvc -p '{"spec":{"resources":{"requests":{"storage":"10Gi"}}}}'
This command updates the storage request of the PersistentVolumeClaim ‘my-pvc’ to 10Gi.
To verify, you can use:
kubectl get pvc my-pvc -o=jsonpath='{.spec.resources.requests.storage}'
Expected output: 10Gi
Example 8: Patching a ServiceAccount to add a new secret:
kubectl patch serviceaccount my-sa -p '{"secrets": [{"name": "new-secret"}]}'
This adds a new secret ‘new-secret’ to the ‘my-sa’ ServiceAccount.
To verify, you can use:
kubectl get serviceaccount my-sa -o=jsonpath='{.secrets[?(@.name=="new-secret")].name}'
Expected output: new-secret
Example 9: Patching a Job to update its backoffLimit:
kubectl patch job my-job -p '{"spec":{"backoffLimit":5}}'
This updates the backoffLimit of the ‘my-job’ Job to 5.
To verify, you can use:
kubectl get job my-job -o=jsonpath='{.spec.backoffLimit}'
Expected output: 5
Example 10: Patching a NetworkPolicy to update its podSelector:
kubectl patch networkpolicy my-network-policy -p '{"spec":{"podSelector":{"matchLabels":{"role":"frontend"}}}}'
This updates the podSelector of the ‘my-network-policy’ NetworkPolicy to select pods labeled with ‘role: frontend’.
To verify, you can use:
kubectl get networkpolicy my-network-policy -o=jsonpath='{.spec.podSelector.matchLabels.role}'
Expected output: frontend
Also check similar articles.
Applying Kubernetes Configurations with kubectl apply
Diffing Kubernetes Configurations with kubectl diff
Listing Kubernetes Events with kubectl events
Troubleshooting Kubernetes with kubectl debug
Managing Kubernetes Authorization with kubectl auth
Discussion about this post