Kubectl patch: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=
* https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#patch
* https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#patch
* https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/
* https://kubernetes.io/docs/reference/using-api/server-side-apply
=Internal=
=Internal=
* [[Kubectl#Commands|kubectl]]
* [[Kubectl#Commands|kubectl]]
* [[Kubernetes Strategic Merge Patch]]
* [[JSON Merge Patch]]
* [[JSON Patch]]


=Overview=
=Overview=
<code>kubectl patch</code> supports [[Kubernetes Strategic Merge Patch|strategic merge patch]].
<code>kubectl patch</code> supports the following modes: [[Kubernetes Strategic Merge Patch|strategic merge patch]] ("strategic"), [[JSON_Merge_Patch|JSON merge patch]] ("merge") and [[JSON_Patch|JSON patch]] ("json").
 
=Caveats=
⚠️There are situations when the patch apparently succeeds, the <code>kubectl patch</code> command exits with 0 and if the resource is queried immediately, the patch seems to be applied, but when the resource is queried a while later (15 secs), it becomes apparent that the "patch" did not take. This happened while trying to unbind a PV from the PVC while the PVC is still active.


=Usage Examples=
=Usage Examples=
==Delete a Field==
==JSON Patch Usage Examples==
===<span id='Delete_a_Field'></span>Delete a Field with a JSON Patch===
Unbind a PVC from a PV by deleting the PV's <code>claimRef</code>:
Unbind a PVC from a PV by deleting the PV's <code>claimRef</code>:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
kubectl patch pv my-pv --type=json -p='[{"op": "remove", "path": "/spec/claimRef"}]'
kubectl patch pv my-pv --type=json --patch='[{"op": "remove", "path": "/spec/claimRef"}]'
</syntaxhighlight>
===Update the Image on a Deployment===
Updates the <code>image</code> field of the first container in the containers array.
<syntaxhighlight lang='bash'>
kubectl patch deployment my-deployment --type=json --patch='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value": "docker.io/ovidiufeodorov/httpd:test_2"}]'
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 18:15, 30 July 2021

External

Internal

Overview

kubectl patch supports the following modes: strategic merge patch ("strategic"), JSON merge patch ("merge") and JSON patch ("json").

Caveats

⚠️There are situations when the patch apparently succeeds, the kubectl patch command exits with 0 and if the resource is queried immediately, the patch seems to be applied, but when the resource is queried a while later (15 secs), it becomes apparent that the "patch" did not take. This happened while trying to unbind a PV from the PVC while the PVC is still active.

Usage Examples

JSON Patch Usage Examples

Delete a Field with a JSON Patch

Unbind a PVC from a PV by deleting the PV's claimRef:

kubectl patch pv my-pv --type=json --patch='[{"op": "remove", "path": "/spec/claimRef"}]'

Update the Image on a Deployment

Updates the image field of the first container in the containers array.

kubectl patch deployment my-deployment --type=json --patch='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value": "docker.io/ovidiufeodorov/httpd:test_2"}]'