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
- Channel: Tech Tutorials with Piyush
- Playlist: 40 Days of Kubernetes (CKA Full Course)
- Companion Repository: piyushsachdeva/CKA-2024
- Challenge:
#40daysofKubernetes
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.
| Command | Purpose |
|---|---|
kubectl run nginx --image=nginx | Create a Pod named nginx using the nginx image |
kubectl get pods | List all pods in the current namespace |
kubectl get pods -o wide | List pods with additional details (IP, node) |
kubectl describe pod nginx | Show detailed information about the nginx pod |
kubectl delete pod nginx | Delete the nginx pod |
kubectl exec -it nginx -- /bin/sh | Get an interactive shell inside the nginx container |
kubectl logs nginx | View 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: 80Key YAML fields explained:
| Field | Description |
|---|---|
apiVersion | API version for the object (e.g., v1 for Pod) |
kind | Type of Kubernetes object (Pod, Deployment, Service, etc.) |
metadata | Data that helps identify the object (name, namespace, labels, annotations) |
spec | Desired state specification for the object |
spec.containers | List of containers to run in the Pod |
spec.containers[].name | Name of the container |
spec.containers[].image | Docker image to use |
spec.containers[].ports | Ports 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.yamlAdvantages:
- 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: AlwaysImportant YAML tips for Kubernetes:
- Always validate YAML syntax before applying:
kubectl apply --dry-run=client -f file.yaml - Use
kubectl explain pod.specto see available fields and their documentation - Labels in
metadata.labelsare 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
| Phase | Description |
|---|---|
| Pending | Pod accepted by cluster, containers not yet running |
| Running | Pod bound to node, at least one container running |
| Succeeded | All containers terminated successfully |
| Failed | All containers terminated, at least one failed |
| Unknown | State 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
- Pod Fundamentals
- Kubernetes Architecture
- Kind Cluster Setup
- CKA Certification Overview
- CKA Progress Tracker
Ingested: 2026-05-21