The
-
Ensure that you have
kubectl installed and properly configured to connect to your Kubernetes cluster. -
Identify the resource you want to patch. You can use the
kubectl get command to list the resources in your cluster and find the one you want to modify. For example, to list all deployments, you can run:kubectl get deployments
-
Determine the specific field(s) you want to modify in the resource. You will need to provide the JSON or YAML patch document that describes the changes you want to make. The patch document should follow the JSON Patch or Strategic Merge Patch format.
- JSON Patch: Use the
json format for the patch document. It specifies the operations to be performed on the resource. - Strategic Merge Patch: Use the
strategic-merge format for the patch document. It allows you to specify only the fields you want to change, and the rest of the resource remains unchanged.
- JSON Patch: Use the
-
Open your terminal or command prompt and run the following command:
kubectl patch {{resource_type}} {{resource_name}} -p '{{patch_document}}'
Replace
{{resource_type}} with the type of the resource you want to patch (e.g.,deployment ,service ,pod ). Replace{{resource_name}} with the name of the specific resource you want to patch. Replace{{patch_document}} with the JSON or YAML patch document.For example, to patch a deployment named
my-deployment with a JSON Patch document, you can run:kubectl patch deployment my-deployment -p '{"spec": {"replicas": 3}}'
Or, to patch a deployment named
my-deployment with a Strategic Merge Patch document, you can run:kubectl patch deployment my-deployment -p '{"spec": {"template": {"spec": {"containers": [{"name": "my-container", "image": "new-image"}]}}}}' --type=merge
-
After running the
kubectl patch command, you will see the output indicating that the resource has been patched.For example:
deployment.apps/my-deployment patched
This output confirms that the deployment resource has been successfully patched.
-
You can verify the changes by inspecting the resource using the
kubectl get orkubectl describe commands. For example:kubectl get deployment my-deployment kubectl describe deployment my-deployment
These commands will display the current state of the patched resource, allowing you to confirm the modifications.
The
Note: It’s important to carefully review the patch document and verify the changes after applying them to ensure that the resource is in the desired state.