An Analogy
Imagine visiting a friend’s house. In the past, you might have taken a taxi and given the driver precise, step-by-step directions—for example, “Take a right onto Street B, then left onto Street C, another left onto Street D, and finally stop at the house.” This sequence of detailed instructions illustrates the imperative approach.

Imperative vs Declarative: Kubernetes Perspective
Imperative Approach
The imperative approach in Kubernetes involves executing specific commands to create, update, or delete objects. This method instructs Kubernetes on both what needs to be done and how it should be done. For example, you might run these commands:- If a command partially executes, running it again may require extra checks (e.g., verifying if a resource already exists).
- Updating resources—such as changing the image version—demands explicit re-execution with live adjustments.
- Commands executed interactively are often not persisted, making it challenging for teammates to trace the system’s original state.
kubectl run, kubectl create deployment, kubectl expose, and even editing commands such as kubectl edit or scaling commands are excellent for immediate changes but require careful tracking of the current state.

Declarative Approach
The declarative approach enables you to specify the desired state of your infrastructure through configuration files (typically written in YAML). For example, defining a pod in a file (nginx.yaml) looks like this:Imperative vs Declarative Update Dilemma
Sometimes, you might modify a live object using thekubectl edit command. This command opens a YAML representation of the current state, including additional fields like status, which are absent from your original configuration file. For instance:
-
Initially, you create the object using your YAML file:
-
Later, you edit the deployment:
-
The live object now contains extra status fields. If you later apply the original
nginx.yaml(perhaps with updates), your live edits might be overwritten.
Always update your local configuration files and use commands like
kubectl replace -f nginx.yaml to ensure that your changes are consistently tracked and version-controlled.-
Create the object:
- Modify the local file to implement changes (e.g., update the image version).
-
Update the live object with:
Choosing the Right Approach
| Approach | Ideal Use Case | Example Commands |
|---|---|---|
| Imperative | Quick, one-off tasks such as creating a pod or deployment. | kubectl run --image=nginx nginxkubectl create deployment --image=nginx nginxkubectl expose deployment nginx --port 80 |
| Declarative | Long-term management with version-controlled infrastructure. | kubectl apply -f nginx.yamlkubectl apply -f /path/to/config-files |
-
Imperative Approach:
Use this method for rapid execution when you need to quickly create or modify Kubernetes objects, particularly during certification exams. -
Declarative Approach:
This approach is recommended for complex, long-term management scenarios. It enables a systematic management of configurations via YAML files, ensuring every change is recorded and version-controlled.
Exam Tips
When preparing for Kubernetes certification exams, consider the following strategies:- Use imperative commands for speed when creating simple objects like pods or deployments.
- For modifications or more intricate configurations, adopt the declarative approach by updating configuration files and applying changes using
kubectl applyorkubectl replace. - Always maintain your YAML files in version control to safeguard against unintentional overwrites.