What is the difference between Declarative and Imperative in Kubernetes, and why is Declarative in production environments?
The Imperative mode defines the exact commands and steps for how to reach the goal (e.g. create, modify, delete), while the Declarative mode defines the desired state in YAML configuration files and leaves it up to Kubernetes to continually match the actual state to it.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
In Imperative mode, you tell the system what to do step by step:
# Imperative approach
kubectl create web deployment --image=nginx:1.25
kubectl scale deployment web --replicas=3If a server crashes or you rerun the command, it may fail because the resource already exists or not automatically recover when a Configuration Drift occurs.
While in declarative mode, the desired state is defined inside the manifest file:
apiVersion: apps/v1
kind:Deployment
metadata:
name: web
spec:
copies: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
-name: nginx
image: nginx:1.25Then you apply it using:
kubectl apply -f web-deployment.yamlThe Control Plane saves the configuration in etcd, and Reconciliation Loops constantly measure the difference between the Actual State and the Desired State and take corrective actions automatically.
- Rely on kubectl run or kubectl expose commands directly in production instead of tracking YAML files in a Git repository (GitOps).
- Confusion between kubectl apply (declarative) and kubectl create (procedural).
- How does kubectl apply handle Three-Way Merge when modifying an already existing object?
- Where is the last-applied-configuration stored in the object?
Explain how the Reconciliation Loop works in Kubernetes Controller Manager.
The normalization loop is an endless iterative loop that operates in the form Observe -> Analyze/Diff -> Act, where controllers monitor the actual state of the block via the API Server, compare it with the desired state stored in etcd, and perform the required operations to eliminate any deviation.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
The Kubernetes architecture is based on the philosophy of Level-Triggered and not just Edge-Triggered. This means that the system not only reacts to change events as they occur, but also regularly checks whether the current level matches the desired level:
- Observe: The Controller listens to changes to its registered objects via the
Watch APIavailable fromkube-apiserver. - Analyze / Diff: Compares
spec(desired state) withstatus(actual reported state). - Act: If there is a difference (eg: Deployment requests 5 capsules and only 3 are available), the Controller sends requests to
kube-apiserverto create the remaining capsules.
┌──────────────────────────────┐
│ kube-apiserver (etcd) │
└───────┬──────────────▲───────┘
│ Watch │ Create/Update
▼ │
┌──────────────────────────────┐
│ Controller Manager │
│ Observe ──> Diff ──> Act │
└──────────────────────────────┘If kube-controller-manager crashes, existing containers remain running on Worker Nodes without interruption, but the cluster loses its ability to self-heal and reschedule collapsed pods until Controller Manager comes back online.
- Belief that Controllers connect directly to the etcd database, while all communications pass exclusively through kube-apiserver.
- Ignoring the concept of Event-Driven Control, Kubernetes relies on Informers and Watch mechanisms to avoid examining resources via costly polling.
- What is the role of SharedInformer and List Cache in improving controller performance?
- What happens if Controller Manager temporarily crashes? Do running containers stop?
Why did Kubernetes choose to use a Pod as a smaller atomic operating unit instead of running a container directly?
Because Pod allows integrated containers that share the same Network Namespace/IP and shared storage volumes and run concurrently on the same physical node, enabling advanced architectural patterns such as Sidecar, Init Containers, and Ambassador.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
A Pod is an abstract shell that collects one or more containers:
- Shared Network Namespace): All Pod containers share a single IP address and a single local port. The underlying container and the monitoring application (Sidecar) communicate directly via
localhost:PORT. - Shared Storage Volumes: One container can write logs or configuration files to
emptyDirand another container reads them or ships them directly to the log server. - Lifecycle co-ordination: All Pod containers are scheduled together on the same node (Atomic Co-scheduling), and two containers of the same Pod cannot be separated on two different nodes.
CRI first creates a Pause Container to reserve the Network Namespace and IP address, and then application containers join this shared space via Linux Namespaces.
- Place independent, functionally unrelated applications within the same Pod instead of separating them into separate Deployments.
- The belief that containers within the same Pod are communicating via an external network address, while they are communicating via localhost at extremely high speed.
- What is the role of the pause container (or infra container) in initializing Pod namespaces?
- How do containers within a Pod share shared memory (IPC Shared Memory) and storage space?