☸️ Day 8: Imperative vs Declarative + YAML + Pod Manifest

Video Overview

This note covers:

  • Imperative vs Declarative approaches
  • YAML fundamentals (VERY IMPORTANT)
  • Pod manifest creation
  • kubectl commands for pods

πŸ“Œ YAML + Declarative = Core of Kubernetes


demo

βš™οΈ Concept 1: Imperative vs Declarative

Important

Two ways to configure any system (including Kubernetes)


⚑ Imperative Approach

Info

Tell the system HOW to do something

kubectl run mypod --image=nginx
  • Step-by-step instructions
  • Quick tasks
  • Not scalable

πŸ“œ Declarative Approach

Info

Tell the system WHAT you want

kubectl apply -f pod.yaml
  • Define desired state
  • Kubernetes ensures it

🧠 Key Difference

ApproachFocusUse Case
ImperativeHowQuick tasks
DeclarativeWhatProduction setups

πŸš€ Why Declarative is Preferred

Success

1️⃣ Idempotency

  • Apply same file multiple times β†’ same result

2️⃣ Version Control

  • Store configs in Git
  • Track changes
  • Team collaboration

3️⃣ Simplicity

  • Human-readable configs
  • Easy to understand

πŸ“„ Concept 2: What is YAML?

Important

YAML = Human-readable data format used in Kubernetes


🧠 Key Features:

  • Easy to read & write
  • Uses indentation (spaces ❗)
  • Less verbose than JSON/XML

⚠️ Rules:

  • ❌ No tabs (only spaces)
  • Maintain consistent indentation
  • Key-value structure

🧩 Concept 3: YAML Data Types


πŸ”Ή 1. Scalar (Basic Values)

Example

name: "Prakash"
age: 21
height: 5.9
isEngineer: true
paymentDue: null
  • String, Integer, Float, Boolean, Null

πŸ”Ή 2. Dictionary (Map)

Example

address:
  flat: "X001"
  city: "Mumbai"
  country: "India"
  • Collection of key-value pairs

πŸ”Ή 3. List (Array)

Example

hobbies:
  - singing
  - dancing
  - traveling

πŸ”₯ Advanced YAML Concepts


πŸ” Nested Dictionary

contact:
  phone: "123456"
  email: "test@example.com"

πŸ“š Nested List

sports:
  - cricket
  - football

🧠 List with Dictionary

education:
  degrees:
    - degree: "Bachelors"
      field: "Computer Science"
    - degree: "Masters"
      field: "Data Science"

πŸ’Ό List + Nested Dictionary

work:
  - company: "ABC Corp"
    role: "DevOps Engineer"
  - company: "XYZ Inc"
    role: "Consultant"

🧠 Golden Rule

Quote

YAML = Key : Value β†’ Everything is built from this


πŸ“¦ Concept 4: Kubernetes Pod Manifest

Important

Every K8s object has 4 main fields:

🧠 KAMS Trick

  • A β†’ apiVersion
  • K β†’ kind
  • M β†’ metadata
  • S β†’ spec

🧾 Example Pod YAML

Example

apiVersion: v1
kind: Pod
 
metadata:
  name: mypod-2
  labels:
    app: nginx
    env: dev
 
spec:
  containers:
    - name: nginx-container
      image: nginx
      ports:
        - containerPort: 80
 
    - name: sidecar-container
      image: busybox
      args: ["sleep", "3600"]

🧠 Understanding Structure

Important

  • spec β†’ Dictionary
  • containers β†’ List
  • Each container β†’ Dictionary
  • ports β†’ List
  • args β†’ List

πŸ” YAML Separator

Info

---
  • Used to separate multiple objects in one file

βš™οΈ Concept 5: kubectl Commands


πŸ“Œ Create Pod (Imperative)

kubectl run mypod --image=nginx

πŸ“Œ Apply YAML (Declarative)

kubectl apply -f pod.yaml

πŸ“Œ Get Pods

kubectl get pods
kubectl get pods -o wide

πŸ“Œ Describe Pod

kubectl describe pod mypod-2

πŸ“Œ Logs

kubectl logs mypod-2
kubectl logs mypod-2 -c sidecar-container

πŸ“Œ Execute Command

kubectl exec mypod-2 -- ls
kubectl exec -it mypod-2 -- bash

πŸ“Œ Delete Pod

kubectl delete pod mypod-2

⚠️ Important Observations

Warning

  • Multiple containers β†’ 2/2 READY
  • Single container β†’ 1/1 READY

🧠 Behind the Scenes

Info

  • Scheduler assigns node
  • Kubelet:
    • Pulls image
    • Starts container
    • Configures networking

🧠 Final Takeaways

Summary

  • Declarative > Imperative
  • YAML = backbone of Kubernetes
  • Pod = smallest unit
  • kubectl = interaction tool

πŸ“Œ One-Line Summary

Quote

Write YAML β†’ Apply β†’ Kubernetes handles everything ☸️