Day 7/40 - Pod In Kubernetes Explained | Imperative VS Declarative Way | YAML Tutorial

Overview

This video is Day 7 of the 40-day CKA preparation course. It introduces the Pod, the smallest deployable unit in Kubernetes, and covers the two primary ways of interacting with a cluster: Imperative (via kubectl commands) and Declarative (via YAML manifests). The video also includes a YAML tutorial for beginners since all Kubernetes manifests are written in YAML.

Source Details

Key Takeaways

1. What is a Pod?

  • A Pod is the smallest and simplest unit in the Kubernetes object model.
  • A Pod represents a single instance of a running process in your cluster.
  • A Pod can contain one or more containers that share:
    • Network namespace (same IP address and port space)
    • Storage volumes (shared filesystems)
    • Linux namespaces (ipc, uts)
  • Containers in the same Pod are always co-located and co-scheduled on the same node.
  • Best practice: Generally use 1 container per Pod, except for sidecar patterns (e.g., logging, proxy, git sync).

2. Imperative Way (kubectl commands)

Directly create and manage resources using kubectl without writing YAML files.

CommandPurpose
kubectl run nginx --image=nginxCreate a Pod named nginx using the nginx image
kubectl get podsList all pods in the current namespace
kubectl get pods -o wideList pods with additional details (IP, node)
kubectl describe pod nginxShow detailed information about the nginx pod
kubectl delete pod nginxDelete the nginx pod
kubectl exec -it nginx -- /bin/shGet an interactive shell inside the nginx container
kubectl logs nginxView logs of the nginx pod

Advantages:

  • Fast for quick experiments and debugging
  • No need to write YAML files
  • Great for learning and one-off tasks

Disadvantages:

  • Not reproducible (no version control)
  • Difficult to review and audit changes
  • Not suitable for complex multi-resource deployments
  • Cannot easily rollback or track history

3. Declarative Way (YAML manifests)

Define the desired state in YAML files and use kubectl apply to create or update resources.

Basic Pod YAML:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: web
spec:
  containers:
  - name: nginx-container
    image: nginx:latest
    ports:
    - containerPort: 80

Key YAML fields explained:

FieldDescription
apiVersionAPI version for the object (e.g., v1 for Pod)
kindType of Kubernetes object (Pod, Deployment, Service, etc.)
metadataData that helps identify the object (name, namespace, labels, annotations)
specDesired state specification for the object
spec.containersList of containers to run in the Pod
spec.containers[].nameName of the container
spec.containers[].imageDocker image to use
spec.containers[].portsPorts to expose from the container

Declarative commands:

# Create a pod from YAML
kubectl apply -f pod.yaml
 
# Update a pod (modify YAML then re-apply)
kubectl apply -f pod.yaml
 
# Delete a pod using the YAML file
kubectl delete -f pod.yaml

Advantages:

  • Infrastructure as Code — version controlled, auditable, reproducible
  • Self-documenting
  • Easy to review and collaborate via Git
  • Supports GitOps workflows (ArgoCD, Flux)
  • Can define complex multi-resource deployments

Disadvantages:

  • Steeper learning curve (YAML syntax, Kubernetes API)
  • Slower for quick experiments

4. YAML Tutorial for Beginners

Basic YAML syntax rules:

  • Indentation matters — use spaces, not tabs
  • Key-value pairs: key: value
  • Lists: items prefixed with - (dash and space)
  • Nested objects: indent child elements under parent

Example YAML structure:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: web
    env: production
spec:
  containers:
  - name: nginx
    image: nginx:1.25
    ports:
    - containerPort: 80
    - containerPort: 443
    env:
    - name: MY_VAR
      value: "hello"
  restartPolicy: Always

Important YAML tips for Kubernetes:

  • Always validate YAML syntax before applying: kubectl apply --dry-run=client -f file.yaml
  • Use kubectl explain pod.spec to see available fields and their documentation
  • Labels in metadata.labels are key-value pairs used for selectors and organization

5. Multi-Container Pod Example

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
  - name: sidecar
    image: busybox
    command: ['sh', '-c', 'echo Hello from sidecar && sleep 3600']

6. Pod Lifecycle Phases

PhaseDescription
PendingPod accepted by cluster, containers not yet running
RunningPod bound to node, at least one container running
SucceededAll containers terminated successfully
FailedAll containers terminated, at least one failed
UnknownState cannot be determined

7. Why This Matters for CKA

  • The CKA exam heavily tests both imperative and declarative knowledge.
  • You must be comfortable with kubectl run, kubectl create, and writing YAML manifests from memory.
  • Understanding Pods is foundational — everything in Kubernetes (Deployments, ReplicaSets, DaemonSets) ultimately manages Pods.
  • The exam may ask you to create, debug, and modify Pods quickly under time pressure.

Cross-References


Ingested: 2026-05-21