Kubernetes Pods and Deployments
Understand Kubernetes Pods as the smallest deployable unit, and Deployments as the declarative way to manage replicated, rolling-updated workloads.
What you'll learn
- Explain what a Pod is and why it exists as the basic unit
- Create, inspect, and delete Pods imperatively and declaratively
- Understand ReplicaSets and why Deployments manage them
- Perform rolling updates and rollbacks with Deployments
- Use kubectl to inspect pod status, logs, and execute into containers
Prerequisites
Relevant for certifications
What is a Pod?
A Pod is the smallest deployable unit in Kubernetes. It wraps one or more containers that share:
- The same network namespace (same IP, same ports)
- The same storage volumes
- The same lifecycle
Pod
├── Container 1 (app)
├── Container 2 (sidecar — e.g., log shipper)
└── Shared volume (/logs)
Tip
In practice, most Pods contain a single container. Multi-container Pods are used for sidecar patterns (logging, proxies like Envoy/Istio).
Creating Pods
Imperatively (for quick testing)
# Run a pod and attach to it
kubectl run nginx --image=nginx --port=80
# Run and immediately open a shell
kubectl run -it debugger --image=busybox --restart=Never -- sh
# Generate YAML without creating (dry-run)
kubectl run nginx --image=nginx --dry-run=client -o yaml
Declaratively (YAML manifest)
# pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
environment: dev
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "250m"
kubectl apply -f pod.yaml
kubectl get pods
kubectl describe pod nginx-pod
kubectl logs nginx-pod
kubectl exec -it nginx-pod -- bash
kubectl delete pod nginx-pod
Pod Lifecycle
Pending → Running → Succeeded / Failed
↕
(Restart if restartPolicy allows)
| Phase | Meaning |
|---|---|
| Pending | Scheduled but containers not yet started |
| Running | At least one container running |
| Succeeded | All containers exited with 0 (Jobs) |
| Failed | At least one container exited non-zero |
| Unknown | Node communication lost |
Useful kubectl Commands
# Inspect
kubectl get pods # All pods in current namespace
kubectl get pods -n kube-system # Pods in kube-system namespace
kubectl get pods -o wide # Shows node assignment + IP
kubectl get pods --watch # Live updates
kubectl describe pod <name> # Detailed events and config
kubectl logs <pod> # Container logs
kubectl logs <pod> -c <container> # Specific container logs
kubectl logs <pod> --previous # Logs from crashed container
# Execute
kubectl exec -it <pod> -- bash # Attach a shell
kubectl exec <pod> -- cat /etc/hosts # Run a single command
# Troubleshooting
kubectl get events --sort-by=.lastTimestamp
kubectl describe pod <name> # Look at Events: section
ReplicaSets
A ReplicaSet ensures a specified number of identical pod replicas are running at all times.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
Warning
Rarely create ReplicaSets directly. Always use Deployments — they manage ReplicaSets for you and add rolling update + rollback capabilities.
Deployments
A Deployment is the recommended way to manage stateless applications. It:
- Creates a ReplicaSet
- Manages rolling updates
- Supports rollbacks
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # 1 extra pod during update
maxUnavailable: 0 # 0 pods down during update
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
kubectl apply -f deployment.yaml
kubectl get deployment nginx-deployment
kubectl get replicasets # See the RS created by the deployment
kubectl get pods -l app=nginx # Pods managed by deployment
Rolling Updates
# Update the image (triggers rolling update)
kubectl set image deployment/nginx-deployment nginx=nginx:1.26
# Watch the rollout
kubectl rollout status deployment/nginx-deployment
# Pause a rollout
kubectl rollout pause deployment/nginx-deployment
# Resume
kubectl rollout resume deployment/nginx-deployment
During a rolling update, Kubernetes:
- Creates a new ReplicaSet with the new version
- Scales up the new RS while scaling down the old one
- Old RS remains at 0 replicas (for rollback)
Rollbacks
# View rollout history
kubectl rollout history deployment/nginx-deployment
# Roll back to previous version
kubectl rollout undo deployment/nginx-deployment
# Roll back to a specific revision
kubectl rollout undo deployment/nginx-deployment --to-revision=2
Scaling
# Scale imperatively
kubectl scale deployment/nginx-deployment --replicas=5
# Scale via YAML (edit and apply)
kubectl edit deployment nginx-deployment
# Change spec.replicas and save
# Auto-scaling (requires metrics server)
kubectl autoscale deployment nginx-deployment --min=2 --max=10 --cpu-percent=70
Common Interview Questions
Q: What is the difference between a Pod and a Deployment? A Pod is the basic unit of execution — a single instance of your app. A Deployment manages multiple Pods via a ReplicaSet, ensuring the desired number are running and enabling rolling updates and rollbacks.
Q: What happens when you delete a Pod managed by a Deployment? The ReplicaSet detects a pod is missing and creates a new one to maintain the desired replica count. You cannot permanently delete a managed Pod without deleting the Deployment or scaling to 0.
Q: What is maxSurge and maxUnavailable in a Deployment?
maxSurge is how many extra pods above the desired count can exist during a rolling update. maxUnavailable is how many pods below the desired count are tolerated. Together they control the speed and safety of the update.
Q: How do you roll back a Deployment?
kubectl rollout undo deployment/<name>
kubectl rollout undo deployment/<name> --to-revision=<n>
Common Mistakes
- Editing a Pod spec directly — most fields are immutable on running pods. Delete and recreate, or use a Deployment
- Not setting resource requests/limits — without these, a Pod can consume all node resources (noisy neighbour problem)
- Using
latestimage tag — makes it impossible to know which version is running and breaks rollbacks - Forgetting the selector must match template labels — if they don't match, the Deployment controller won't find its pods
What to Learn Next
- Kubernetes Services — exposing pods with ClusterIP, NodePort, LoadBalancer
- Kubernetes ConfigMaps and Secrets — environment configuration
- Kubernetes Persistent Storage — PVs, PVCs, StorageClasses