kubectl edit -- In-Place Resource Editing
kubectl edit opens a resource's live YAML in your terminal editor, lets you make changes, and applies them when you save and close the file.
Basic Usage
# Edit a deployment
kubectl edit deployment my-app
# Edit a service
kubectl edit service my-service
# Edit a configmap
kubectl edit configmap app-config
# Edit a resource in a specific namespace
kubectl edit deployment my-app -n production
When you run the command, kubectl fetches the resource's current YAML, opens it in your editor, and waits. When you save and exit, kubectl sends the changes to the API server.
Setting Your Editor
kubectl uses the editor defined in the KUBE_EDITOR environment variable. If that is not set, it falls back to EDITOR, and then to vi:
# Use VS Code
export KUBE_EDITOR="code --wait"
# Use nano
export KUBE_EDITOR="nano"
# Use vim (default on most systems)
export KUBE_EDITOR="vim"
# Add to your .bashrc or .zshrc to persist
echo 'export KUBE_EDITOR="code --wait"' >> ~/.zshrc
What Can Be Edited?
Most fields can be edited on a live resource, but some fields are immutable after creation:
metadata.name-- cannot be changedspec.selectoron a Deployment -- cannot be changedspec.clusterIPon a Service -- cannot be changed- Pod
spec.containers[*].name-- cannot be changed
If you try to edit an immutable field, the API server rejects the change and kubectl reopens the editor with an error message.
What Happens When You Save
When you save the file and close the editor:
- kubectl compares your edits against the live resource
- If there are changes, it sends an update request to the API server
- The API server validates the changes
- If valid, the resource is updated
- If invalid, kubectl shows the error and reopens the editor
If you close the editor without making changes, nothing happens.
When to Use edit vs apply
Use kubectl edit for quick, interactive changes during development or debugging. Use kubectl apply -f for production changes that should be version controlled.
# Quick debugging fix
kubectl edit deployment my-app
# Change replicas from 1 to 3, save, done
# Production change (better approach)
# Edit deployment.yaml in your repo, commit, then apply
kubectl apply -f deployment.yaml
Key Takeaways
kubectl editopens live resources in your terminal editor- Set
KUBE_EDITORto use your preferred editor - Some fields like name and selector are immutable after creation
- Changes are validated by the API server before being applied
- Use
editfor quick fixes; useapplyfor production changes