Tech Interview RoomStudy guides

Kubernetes

When do you use a Deployment, StatefulSet, DaemonSet, Job or CronJob?

Intermediate · 2 min read · Lesson 2 of 7 in Kubernetes

  • Pod: the smallest unit; one or more containers sharing a network namespace (same IP) and volumes. Never create bare pods for applications; nothing recreates them.
  • Deployment → ReplicaSet → Pods: stateless services (REST APIs, consumers). Interchangeable replicas, rolling updates and rollbacks (kubectl rollout undo), horizontal scaling.
  • StatefulSet: stateful workloads needing stable identity: stable names (kafka-0, kafka-1), stable per-pod storage through volumeClaimTemplates that follows the pod, ordered deployment and scaling. Used for databases, Kafka, ZooKeeper, Elasticsearch. Usually paired with a headless Service for per-pod DNS. In practice, prefer managed databases or a mature operator.
  • DaemonSet: one pod per node (or per selected node) for node-level agents: log collectors (Fluent Bit), monitoring (node-exporter), CNI and CSI components, security agents.
  • Job: run to completion with retries (backoffLimit), parallelism and completions; for migrations, batch processing, one-off tasks.
  • CronJob: Jobs on a schedule; set concurrencyPolicy: Forbid to avoid overlapping runs and make the work idempotent because a run can be missed or happen twice.

Rolling update knobs (Deployment)

maxSurge (extra pods allowed above the desired count) and maxUnavailable (pods allowed to be down). For zero-downtime with full capacity: maxSurge: 25%, maxUnavailable: 0, plus correct readiness probes. minReadySeconds and progressDeadlineSeconds guard against bad releases.

Example

yaml
apiVersion: apps/v1
kind: Deployment
metadata: { name: orders }
spec:
  replicas: 4
  strategy:
    type: RollingUpdate
    rollingUpdate: { maxSurge: 1, maxUnavailable: 0 }
  selector: { matchLabels: { app: orders } }
  template:
    metadata: { labels: { app: orders } }
    spec:
      topologySpreadConstraints:
        - { maxSkew: 1, topologyKey: topology.kubernetes.io/zone, whenUnsatisfiable: ScheduleAnyway,
            labelSelector: { matchLabels: { app: orders } } }
      containers:
        - name: app
          image: registry.example.com/orders:3f2a91c
          ports: [{ containerPort: 8080 }]

Follow-up questions interviewers ask

Try answering each one before you open it.

How would you run Kafka or PostgreSQL on Kubernetes?

With a mature operator (Strimzi for Kafka, CloudNativePG or Crunchy for PostgreSQL), which builds on StatefulSets and adds the domain knowledge: rolling upgrades, failover, backups, scaling, certificates. Use storage classes with appropriate performance and anti-affinity across zones. Many teams still choose managed services (MSK, Cloud SQL, RDS) to avoid owning that operational burden.