DevOps & Kubernetes (50 Qs)•Part 2: Kubernetes Architecture & Core Workloads (11 - 25)
Deployments vs. ReplicaSets vs. StatefulSets: When to use StatefulSets and how headless services work.
Workload Controllers Architecture
Kubernetes encapsulates container management into higher-level declarative controllers tailored for stateless versus stateful systems.
scss
Stateless Architecture (Deployment)
[ Deployment ] ---> [ ReplicaSet ] ---> [ Pod-abc1 ] (Interchangeable, random IDs)
---> [ Pod-xyz2 ] (Dynamic IP, shared PV or stateless)
Stateful Architecture (StatefulSet)
[ StatefulSet ] ---> [ Pod-0 ] <---> [ PVC-0 ] <---> [ Dedicated PV 0 ] (Sticky Storage)
---> [ Pod-1 ] <---> [ PVC-1 ] <---> [ Dedicated PV 1 ]
---> [ Pod-2 ] <---> [ PVC-2 ] <---> [ Dedicated PV 2 ]
^
| (Ordered Deployment: 0 -> 1 -> 2)
[ Headless Service (clusterIP: None) ]
DNS: pod-0.db-service.default.svc.cluster.local
Key Differences
| Feature | Deployment | StatefulSet |
|---|---|---|
| Identity | Ephemeral, random hash suffixes (e.g., api-7bf89d-4kx2p). | Deterministic, sequential index (e.g., redis-0, redis-1). |
| Storage | Pods share the same PVC or use ephemeral storage. | Each Pod gets its own dedicated PVC via volumeClaimTemplates. |
| Startup / Teardown | Scaled in parallel. | Scaled sequentially (0 → 1 → 2; teardown 2 → 1 → 0). |
| Networking | Standard load-balanced ClusterIP service. | Headless Service (clusterIP: None) providing direct A-records for each Pod. |
Headless Service Blueprint for Distributed Clusters (Kafka/ZooKeeper/Cassandra)
yaml
apiVersion: v1
kind: Service
metadata:
name: postgres-cluster
spec:
clusterIP: None # Headless: returns Pod IPs directly in DNS queries
selector:
app: postgres
ports:
- port: 5432
name: postgres