kubectl patch: Command Reference for CI/CD
Surgically update one field from a script: pick the right patch type.
kubectl patch applies a partial update and is the scriptable alternative to edit. Choosing the correct --type is what makes it behave as expected. This reference compares the three patch types and shows a CI example.
Common flags and usage
- --type=strategic (default): Kubernetes list-aware merge for core types
- --type=merge: plain RFC 7386 merge; replaces lists wholesale (use for CRDs)
- --type=json: RFC 6902 op list (add/replace/remove) at explicit paths
- -p '<patch>': inline patch body
- --patch-file <file>: read the patch from a file
Example
shell
kubectl patch deploy/web --type=json \
-p '[{"op":"replace","path":"/spec/replicas","value":4}]'
kubectl patch svc/web --type=merge \
-p '{"spec":{"type":"LoadBalancer"}}'In CI
Strategic-merge patches misbehave on custom resources, which lack merge metadata, so use --type=merge or --type=json for CRDs. A JSON-patch op against a path that does not exist fails server-side; add the parent first. Prefer apply for whole-object declarative changes and patch only for narrow, scripted edits.
Key takeaways
- Wrong --type is the top patch mistake; CRDs need merge or json.
- json patches edit explicit paths; the path must already exist for replace.
- For whole-object declarative changes, prefer apply over patch.
Related guides
kubectl Cheat Sheet: Commands for Everyday KubernetesA kubectl cheat sheet - get, describe, logs, apply, rollout, debug, and context commands for daily Kubernetes…
kubectl apply: Command Reference for CI/CDReference for kubectl apply: declarative create-or-update from manifests, server-side apply, dry-run validati…
kubectl set image: Command Reference for CI/CDReference for kubectl set image: promote a Deployment to a new image tag in one command, why immutable tags m…
kubectl scale: Command Reference for CI/CDReference for kubectl scale: set replica counts, conditional scaling with --current-replicas, why an HPA figh…