Kubernetes Workloads
Learn how to manage applications using Kubernetes controllers.1. Deployments
The standard way to run stateless applications (web servers, APIs).- ReplicaSet: Manages the number of pod copies.
- Rolling Updates: Updates pods without downtime.
- Rollbacks: Revert to previous versions if something goes wrong.
Managing Deployments
2. StatefulSets
Used for stateful applications (Databases, Kafka, Zookeeper).- Stable Network ID:
web-0,web-1(not random). - Stable Storage: PersistentVolumeClaims are kept even if pods are deleted.
- Ordered Deployment: 0 -> 1 -> 2.
- Ordered Termination: 2 -> 1 -> 0.
3. DaemonSets
Ensures that all (or some) Nodes run a copy of a Pod.- Use cases: Log collectors (Fluentd), Monitoring agents (Prometheus Node Exporter).
4. Jobs & CronJobs
Job
Runs a pod until it completes successfully (exit code 0).- Use cases: Batch processing, database migrations.
CronJob
Creates Jobs on a schedule (like Linux cron).Key Takeaways
| Workload | Use Case | Example |
|---|---|---|
| Deployment | Stateless apps | Web servers, APIs |
| StatefulSet | Stateful apps | Databases, Kafka |
| DaemonSet | Node agents | Logging, Monitoring |
| Job | One-off tasks | DB Migration |
| CronJob | Scheduled tasks | Backups |
5. Horizontal Pod Autoscaler (HPA)
Automatically scales the number of pods based on observed CPU/memory utilization or custom metrics.HPA vs VPA
| Feature | HPA | VPA |
|---|---|---|
| What it scales | Number of pods | Resources per pod |
| Best for | Stateless apps | Stateful apps, right-sizing |
| Can run together? | Yes, but don’t use same metric | Use different metrics |
6. Pod Disruption Budgets (PDB)
Ensures a minimum number of pods remain available during voluntary disruptions (e.g., node drains, cluster upgrades).7. Deployment Strategies Deep Dive
Rolling Update (Default)
Updates pods incrementally, ensuring availability.Recreate
Kills all existing pods before creating new ones.- Use Case: When you cannot run two versions simultaneously (e.g., database schema conflicts).
Blue-Green Deployment (Manual with Services)
Run two identical environments. Switch traffic instantly.Canary Deployment
Route a small percentage of traffic to the new version.- Requires Ingress controller or Service Mesh (Istio) for traffic splitting.
8. Pod Affinity & Anti-Affinity
Control which nodes pods are scheduled on based on labels.Node Affinity
Schedule pods on nodes with specific labels.Pod Anti-Affinity
Spread pods across nodes/zones to avoid single points of failure.9. Taints & Tolerations
Taints are applied to nodes to repel pods. Tolerations allow pods to be scheduled on tainted nodes.| Effect | Behavior |
|---|---|
| NoSchedule | New pods won’t be scheduled |
| PreferNoSchedule | Soft version - avoid if possible |
| NoExecute | Evict existing pods + no new scheduling |
Interview Questions & Answers
What is the difference between a Deployment and a StatefulSet?
What is the difference between a Deployment and a StatefulSet?
| Aspect | Deployment | StatefulSet |
|---|---|---|
| Pod Names | Random (app-abc123) | Ordered (app-0, app-1) |
| Storage | Shared or ephemeral | Dedicated PVC per pod |
| Scaling Order | Parallel | Sequential (0→1→2) |
| Use Case | Stateless apps | Databases, Kafka |
How does a ReplicaSet differ from a Deployment?
How does a ReplicaSet differ from a Deployment?
- ReplicaSet ensures a specified number of pod replicas are running
- Deployment manages ReplicaSets and provides:
- Rolling updates
- Rollback capability
- Update history
- You almost never create ReplicaSets directly; use Deployments instead.
What happens when you update a Deployment?
What happens when you update a Deployment?
- A new ReplicaSet is created with the updated pod template
- New pods are created in the new ReplicaSet
- Old pods are terminated from the old ReplicaSet (respecting
maxUnavailable) - Old ReplicaSet is kept for rollback (scaled to 0)
How would you ensure pods are spread across availability zones?
How would you ensure pods are spread across availability zones?
Use Pod Anti-Affinity with Or use Pod Topology Spread Constraints for more control.
topologyKey:What is the purpose of init containers?
What is the purpose of init containers?
Init containers run before app containers and are used for:
- Wait for dependencies (database, service)
- Clone git repos
- Run database migrations
- Generate config files
How do you debug a CrashLoopBackOff?
How do you debug a CrashLoopBackOff?
- Check logs:
kubectl logs <pod> --previous - Describe pod:
kubectl describe pod <pod>(check Events) - Check resources: Is the container OOMKilled?
- Exec into container:
kubectl exec -it <pod> -- sh(if it starts briefly) - Override command: Create a debug pod with
command: ["/bin/sleep", "infinity"]
Common Pitfalls
Next: Kubernetes Services →