What is kubectl, and what's the difference between imperative and declarative usage?
Quick Answer
kubectl is the CLI that talks to the Kubernetes API. Imperative commands (kubectl run/create/scale) tell the cluster to do something now; declarative usage (kubectl apply -f) submits desired-state manifests that Kubernetes reconciles and that you keep in version control.
Detailed Answer
kubectl is a thin client over the REST API — every command becomes an API call the apiserver authenticates and admits. Imperative style issues one-off actions: kubectl run, kubectl create, kubectl scale, kubectl delete. It's fast for experiments but leaves no source of truth. Declarative style is the production way: you write YAML manifests describing desired state and run kubectl apply -f (or GitOps tools like Argo CD/Flux apply them for you). Apply is idempotent and does a three-way merge, so re-applying is safe and diffs are meaningful. The rule of thumb: imperative for quick debugging/learning, declarative (manifests in Git) for anything real, because it's reviewable, reproducible, and auditable.
Code Example
# Imperative — quick, no record kubectl create deploy web --image=nginx --replicas=3 # Declarative — the production way (manifest in Git) kubectl apply -f deploy.yaml kubectl diff -f deploy.yaml # preview changes before applying
Interview Tip
The keyword is 'declarative + version control + idempotent apply.' Mentioning GitOps (Argo CD/Flux) as the natural extension signals modern practice.