
When deploying workloads in Kubernetes, you can choose between two approaches: Imperative and Declarative. This is similar to how developers build applications: either by coding everything manually or by using a framework that handles structure and consistency for them. Let’s look at both in a Kubernetes context.
Imperative deployment (coding from scratch)
Imagine building a website from the ground up, writing raw HTML, CSS, and JavaScript without any libraries or structure. It works, but it’s hard to maintain, scale, or reuse. That’s imperative deployment in Kubernetes. You run commands manually to create or modify resources. Let see an example below:
kubectl run web --image=nginx
This quickly generates a pod named web that runs the nginx image, but the configuration is not saved, so you cannot version or reapply it later.
Declarative deployment (using a framework)
Now picture using a web framework like Django or Laravel. You define your components, routes, or models in a structured way, and the framework takes care of the rest. It’s reusable, scalable, and much easier to maintain. This is declarative deployment. You write a YAML file that defines the desired state of your application, and Kubernetes ensures that the actual state matches it.
Lets create a file called web.yaml and add the below lines:
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: web-container
image: nginx
Save the file and deploy it with:
kubectl apply -f web.yaml
This file can be modified if required and can be deployed again if require. Kubernetes will monitor and maintain this configuration over time.
Which one should you use?
-
Imperative: This approach is great for quick fixes or learning.
-
Declarative: Ideal for production, version control, and infrastructure automation.
In a real-world software project, you’d use a framework for structure and maintainability, similarly, declarative YAML files provide a reliable way to manage Kubernetes workloads over time.
Final tip: convert imperative to declarative
You can generate yaml file which can deploy using declarative approaches from imperative command. Lets see an example by adding –dry-run=client -o yaml to your command!
kubectl run web --image=nginx --dry-run=client -o yaml > web.yaml
This will generate a YAML file called web.yaml from your command that you can edit, version control, and reuse with. Lets explain the parameter used in this command below:
–dry-run: Simulate the command without effecting the cluster. No workload will deploy in the cluster.
client: Check the syntax locally without sending it to the cluster.
-o: What will be the output of the file. In this case the output will be yaml.
web.yaml: File name which can be deployed in the cluster.
Lets deploy it with the below command.
kubectl apply -f web.yaml
The command will deploy the same pod in the cluster using declarative way. For the beginners, you can start imperatively, scale declaratively. Let Kubernetes do the heavy lifting.
Thanks for reading the post. If you enjoyed the post, please share it with your network and let me know your thoughts in the comments.
About the Author: Imtiaz is working in a financial organization in Bangladesh and having experience in system, network and security administration. Feel free to contact with him on LinkedIn or Twitter.