๐ Kubernetes Pod Creation: Imperative vs Declarative Approach (Complete Beginner to CKA Guide)
Level: Beginner โ Intermediate
Category: Kubernetes | DevOps | CKA | Docker
Author: Sumit Bera
๐ Introduction
Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications.
Before learning Deployments, Services, ReplicaSets, or Ingress, you should first understand Pods, because every application in Kubernetes runs inside a Pod.
There are two methods to create Kubernetes resources:
- โก Imperative Approach
- ๐ Declarative Approach
Every Kubernetes Administrator should know both methods because they are frequently used in interviews, the CKA exam, and real-world production environments.
๐ฆ What is a Pod?
A Pod is the smallest deployable unit in Kubernetes.
A Pod can contain:
- One container (most common)
- Multiple containers (sidecar pattern)
Example
+--------------------------------+
| Pod |
| |
| +--------------------------+ |
| | NGINX Container | |
| +--------------------------+ |
| |
+--------------------------------+
๐ Two Ways to Create Pods
| Imperative Approach | Declarative Approach |
|---|---|
| Command-based | YAML-based |
| Quick | Production Ready |
| Temporary | Version Controlled |
| Easy for Labs | Easy for Teams |
| Good for CKA | Good for Production |
โก Method 1 โ Imperative Approach
What is Imperative?
In the Imperative approach, you directly tell Kubernetes what to do.
Think of it like giving instructions.
Example:
Create a Pod.
Kubernetes immediately creates it.
Create a Pod
kubectl run pod1 --image=nginx
Output
pod/pod1 created
Check the Pod
kubectl get pods
Output
NAME READY STATUS RESTARTS AGE
pod1 1/1 Running 0 10s
Create Pod with Specific Version
kubectl run pod2 --image=nginx:1.14.2
Check
kubectl get pods
Delete Pod
kubectl delete pod pod1
Output
pod "pod1" deleted
Generate YAML without Creating the Pod
This is one of the most useful commands for CKA.
kubectl run pod3 \
--image=nginx \
--dry-run=client \
-o yaml > pod3.yaml
Nothing is created.
Only the YAML file is generated.
Open the YAML
vim pod3.yaml
Generated YAML
apiVersion: v1
kind: Pod
metadata:
labels:
run: pod3
name: pod3
spec:
containers:
- image: nginx
name: pod3
Create Pod Using YAML
kubectl create -f pod3.yaml
Verify
kubectl get pods
๐ Method 2 โ Declarative Approach
What is Declarative?
Instead of telling Kubernetes what to do every time,
you describe the desired state in a YAML file.
Kubernetes continuously works to keep the cluster matching that desired state.
This is the preferred method in production.
Create YAML File
Create a file named
pod-basic.yaml
YAML File
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
type: webserver
annotations:
owner: sumit
description: Kubernetes Demo Pod
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Create Pod
kubectl create -f pod-basic.yaml
Verify
kubectl get pods
Describe Pod
kubectl describe pod nginx-pod
View YAML
kubectl get pod nginx-pod -o yaml
Update Image
Change
image: nginx:1.14.2
to
image: nginx:latest
Save the file.
Apply Changes
kubectl apply -f pod-basic.yaml
Output
pod/nginx-pod configured
Verify
kubectl describe pod nginx-pod | grep -i image
Output
Image: nginx:latest
Delete Resource
kubectl delete -f pod-basic.yaml
Understanding Labels
Labels organize Kubernetes resources.
Example
labels:
app: nginx
env: production
tier: frontend
Add Label
kubectl label pod pod1 type=webserver
Show Labels
kubectl get pods --show-labels
Output
NAME LABELS
pod1 run=pod1,type=webserver
Update Label
kubectl label pod pod1 type=database --overwrite
Delete Label
kubectl label pod pod1 type-
Select Pods Using Labels
kubectl get pods -l type=webserver
Understanding Annotations
Annotations store metadata.
Unlike labels,
Annotations are not used for selecting Pods.
Example
annotations:
owner: sumit
email: [email protected]
description: My First Kubernetes Pod
Add Annotation
kubectl annotate pod pod1 owner=sumit
Add Description
kubectl annotate pod pod1 description="My First Pod in Kubernetes"
Add Email
kubectl annotate pod pod1 [email protected]
View Annotations
kubectl describe pod pod1 | grep -i annotations -A5
Example
Annotations:
owner: sumit
email: [email protected]
description: My First Pod in Kubernetes
Remove Annotation
kubectl annotate pod pod1 description-
Export Existing Pod
kubectl get pod pod1 -o yaml > pod1.yaml
Open
vim pod1.yaml
Modify the YAML.
Apply
kubectl apply -f pod1.yaml
kubectl create vs kubectl apply
| kubectl create | kubectl apply |
|---|---|
| Creates resource | Creates or updates |
| First time only | Continuous updates |
| Fails if resource exists | Updates existing resource |
| Good for initial creation | Best for production |
| No configuration tracking | Tracks changes using last-applied-configuration annotation |
Imperative vs Declarative
| Feature | Imperative | Declarative |
|---|---|---|
| Uses Commands | โ | โ |
| Uses YAML | โ | โ |
| Easy for Beginners | โ | โ |
| Fast | โญโญโญโญโญ | โญโญโญ |
| Version Control | โ | โ |
| Git Friendly | โ | โ |
| CI/CD Ready | โ | โ |
| Production | โ | โ |
| Infrastructure as Code | โ | โ |
| Best Practice | Testing | Production |
Common Commands
kubectl get pods
kubectl describe pod pod1
kubectl delete pod pod1
kubectl edit pod pod1
kubectl apply -f pod.yaml
kubectl create -f pod.yaml
kubectl get pod pod1 -o yaml
kubectl label pod pod1 type=webserver
kubectl annotate pod pod1 owner=sumit
kubectl get pods --show-labels
CKA Exam Tips
โ Learn both Imperative and Declarative methods.
โ
Memorize kubectl run, create, apply, edit, and describe.
โ Practice creating Pods from YAML.
โ Understand Labels and Annotations.
โ
Know the difference between create and apply.
โ Practice exporting and modifying YAML files.

Best Practices
- Use Imperative for:
- Quick testing
- Labs
- CKA exam speed
- Troubleshooting
- Use Declarative for:
- Production
- Git repositories
- CI/CD pipelines
- Infrastructure as Code
- Team collaboration
Final Thoughts
Both approaches are essential for every Kubernetes engineer.
- Imperative is fast and ideal for learning, testing, and exam scenarios.
- Declarative is the industry standard for production because it supports version control, repeatability, automation, and Infrastructure as Code.
If you’re preparing for the CKA exam or working in a DevOps role, mastering both methods will make you more confident in managing Kubernetes clusters efficiently.
