apply
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 edit (RESOURCE/NAME | -f FILENAME) [flags] Common flags
| Flag | Purpose |
|---|---|
| -o, --output | Format opened in the editor: yaml (default) or json. |
| --windows-line-endings | Use CRLF line endings on Windows editors. |
| --save-config | Record the applied config annotation for future apply. |
| --subresource | Edit a subresource such as status (1.24+). |
| -n, --namespace | Namespace of the resource. |
Examples
kubectl edit deploy web Open the live Deployment YAML in your editor.
KUBE_EDITOR=code kubectl edit svc api Use VS Code as the editor for this session.
kubectl edit configmap app -n prod Interactively tweak a ConfigMap in production.
Gotcha
Changes are lost if the resourceVersion drifts before you save - kubectl re-opens the editor. Editing status fields requires --subresource=status.
Related commands
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 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 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.