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 ApproachDeclarative Approach
Command-basedYAML-based
QuickProduction Ready
TemporaryVersion Controlled
Easy for LabsEasy for Teams
Good for CKAGood 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 createkubectl apply
Creates resourceCreates or updates
First time onlyContinuous updates
Fails if resource existsUpdates existing resource
Good for initial creationBest for production
No configuration trackingTracks changes using last-applied-configuration annotation

Imperative vs Declarative

FeatureImperativeDeclarative
Uses Commandsโœ…โŒ
Uses YAMLโŒโœ…
Easy for Beginnersโœ…โœ…
Fastโญโญโญโญโญโญโญโญ
Version ControlโŒโœ…
Git FriendlyโŒโœ…
CI/CD ReadyโŒโœ…
ProductionโŒโœ…
Infrastructure as CodeโŒโœ…
Best PracticeTestingProduction

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.


Related posts:-

Total Page Visits: 12 - Today Page Visits: 12

Leave a Reply

Your email address will not be published. Required fields are marked *