patch

patching a resource.

Update field(s) of a resource using strategic merge patch, a JSON merge patch, or a JSON patch.

JSON and YAML formats are accepted.

Command

  1. $ kubectl patch (-f FILENAME | TYPE NAME) -p PATCH

Example

Current State

  1. $ kubectl get deployments
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. nginx 2/2 2 2 24m

Command

  1. kubectl patch deployment nginx -p '{"spec":{"replicas":1}}'
  2. deployment.apps/nginx patched

This will reduce the number of replicas for nginx from 2 to 1.

Output

  1. $ kubectl get deployments
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. nginx 1/1 1 1 26m

More Examples

  1. # Partially update a node using a strategic merge patch. Specify the patch as JSON.
  2. kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'
  1. # Partially update a node using a strategic merge patch. Specify the patch as YAML.
  2. kubectl patch node k8s-node-1 -p $'spec:\n unschedulable: true'
  1. # Partially update a node identified by the type and name specified in "node.json" using strategic merge patch.
  2. kubectl patch -f node.json -p '{"spec":{"unschedulable":true}}'
  1. # Update a container's image; spec.containers[*].name is required because it's a merge key.
  2. kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
  1. # Update a container's image using a json patch with positional arrays.
  2. kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"newimage"}]'