Understanding the Concepts with an Analogy
Imagine you want to visit a friend’s house on Street B. In the past, you might have hired a taxi and provided detailed, step-by-step directions—turn right onto Street B, then left onto Street C, and so on. This instruction-by-instruction guidance is similar to the imperative approach in Kubernetes.

Imperative Approach Explained
The imperative approach involves issuing explicit commands, which include both the desired configuration and the detailed steps needed to achieve it. For example, consider a series of commands to provision and configure a server:- Provision a VM.
- Install and configure NGINX (set the port to 8080 and define the web files’ path).
- Download application source code.
- Start the NGINX server.
Imperative Methods in Detail
There are two primary methods within the imperative paradigm:-
Direct Commands:
Commands likekubectl run,kubectl create, andkubectl exposeenable quick object creation or modification without the overhead of editing YAML files. These commands are especially useful during certification exams where speed is critical. However, for complex configurations such as multi-container pods, they have limitations. Additionally, since these commands reside only in your shell history, they may not provide a clear record of how the objects were created. -
Transient Changes with Commands:
Commands such askubectl editallow you to modify a live object directly. However, note that these changes are temporary; they update the running object in the cluster but do not alter your local YAML configuration. This can lead to inconsistencies if local configuration files are used for future modifications.
Working with Object Configuration Files
Using YAML configuration files (or manifest files) is the foundation of the declarative approach. This method offers several advantages:- The configuration is fully documented in the YAML file.
- It can be stored in version-controlled repositories such as Git.
- Changes are tracked and reproducible through a controlled change process.
kubectl edit, Kubernetes opens a temporary YAML representation of the live object. This representation may include additional runtime fields such as status conditions:
kubectl edit do not update your local YAML file. A more reliable process is to edit your local YAML file directly:
Running
kubectl create -f nginx.yaml on an object that already exists will produce an error. Always check the object’s existence before creation.Declarative Approach with kubectl apply
The declarative approach is based on describing the desired state of your cluster in configuration files. When you usekubectl apply, Kubernetes:
- Creates the object if it doesn’t already exist.
- Compares the current state with the declared desired state, then updates the object to match.
nginx.yaml file, simply run:
Exam Tips for Kubernetes Management
For exam scenarios that require quick operations, the imperative commands can be time saving. For example, to create a pod or deployment with a specific image, you can use commands like:kubectl apply. This method allows you to iterate quickly and safely correct any mistakes directly in your configuration files.
While imperative commands are efficient for simple tasks, maintaining configuration files in a version-controlled repository is key for managing larger and more complex clusters.
Conclusion
Understanding the differences between imperative and declarative approaches in Kubernetes is crucial for efficient cluster management. Use the method that best suits your needs: imperative commands for rapid testing and declarative configuration files for consistency and repeatability. For more details on Kubernetes management techniques, visit the Kubernetes Documentation.