What is the difference between imperative and declarative management?
Short answer
Imperative means giving Kubernetes direct commands ("create this", "scale that"). Declarative means writing down the desired state in YAML files and letting Kubernetes make it happen. In real companies, we use declarative YAML stored in Git.
Explained in simple words
- Imperative is like giving a taxi driver turn-by-turn directions: "left here, right there, stop".
- Declarative is like typing the destination into Google Maps: you say where you want to end up, and it works out how to get there — and recalculates if something changes.
Example
Imperative:
kubectl create deployment web --image=nginx:1.27
kubectl scale deployment web --replicas=5
kubectl set image deployment/web nginx=nginx:1.28
Quick, but there's no record of what was done, and it's hard to repeat on another cluster.
Declarative:
# web.yaml (stored in Git)
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 5
selector:
matchLabels: { app: web }
template:
metadata:
labels: { app: web }
spec:
containers:
- name: nginx
image: nginx:1.28
kubectl apply -f web.yaml # create or update to match the file
kubectl diff -f web.yaml # preview what would change
Why companies use declarative
| Imperative | Declarative | |
|---|---|---|
| Record of changes | None | Git history shows who changed what and why |
| Review | Not possible | Pull requests and code review |
| Repeatable | Hard | Apply the same files to dev, staging and prod |
| Works with GitOps | No | Yes — Argo CD or Flux apply files from Git automatically |
What to say in the interview
"Imperative management means running commands like kubectl create or kubectl scale to change the cluster directly. Declarative means describing the desired state in YAML and running kubectl apply, so Kubernetes reconciles the cluster to match. In production we use declarative files stored in Git and usually applied through a GitOps tool like Argo CD, because changes are reviewed, versioned, repeatable and easy to roll back. I use imperative commands only for quick debugging or for generating YAML with --dry-run=client -o yaml."
Follow-up questions you may get
- How do you generate YAML quickly?
kubectl create deployment web --image=nginx --dry-run=client -o yaml > web.yaml - What happens if someone changes the cluster manually in a GitOps setup? Argo CD detects the drift and changes it back to match Git.