apply
kubectl patch
Applies a partial update to a resource using strategic-merge, JSON-merge, or JSON-patch semantics. It is the surgical tool for changing a single field without rewriting the whole manifest.
kubectl patch (TYPE NAME | -f FILENAME) [-p PATCH] [--type strategic|merge|json] [flags] Common flags
| Flag | Purpose |
|---|---|
| -p, --patch | Inline patch document (JSON or YAML). |
| --patch-file | Read the patch from a file instead of the CLI. |
| --type | strategic (default for built-ins), merge, or json (RFC 6902). |
| --subresource | Target a subresource such as status or scale (1.24+). |
| --dry-run | none, client, or server to preview the change. |
Examples
kubectl patch deploy web -p '{"spec":{"replicas":5}}' Strategic-merge patch to change replica count.
kubectl patch svc api --type=json -p='[{"op":"replace","path":"/spec/type","value":"ClusterIP"}]' JSON Patch to flip Service type.
kubectl patch node worker-1 -p '{"spec":{"unschedulable":true}}' Cordon a node via patch.
kubectl patch pod web --subresource=status -p '{"status":{"phase":"Failed"}}' Patch a status subresource (rarely needed).
Gotcha
Strategic merge only works on built-in types; CRDs default to JSON-merge. Quoting matters on Windows PowerShell - prefer --patch-file for multi-line patches.
Related commands
kubectl apply
Declaratively creates or updates resources from a manifest file, directory, URL, or kustomization. Tracks the last-applied configuration so subsequent applies compute a three-way merge patch rather than replacing the object.
kubectl replace
Replaces a resource with the complete definition from a file, overwriting the entire spec. With --force it deletes and recreates the resource, which is useful for immutable fields.
kubectl edit
Fetches a live resource, opens it in your $EDITOR, and applies the diff on save. Convenient for one-off tweaks; not recommended for production changes that should live in Git.
kubectl create
Imperatively creates a resource from a file or from built-in generators (deployment, configmap, secret, job, etc.). Unlike apply, it fails if the resource already exists and does not record a last-applied annotation.