Opscraft
Kubernetes in depth · Lesson 4 of 11 · 5 min read

Workload controllers

Enlarge diagramDeploymentstateless, interchangeable pods: web, APIsStatefulSetstable names and disks: Kafka, databasesDaemonSetone pod per node: log and metrics agentsJob / CronJobrun to completion, once or on a schedule
Figure 1. Pick the controller by the shape of the work.
Swipe sideways to see all of it, or tap to open it full screen.

Deployment and ReplicaSet

A ReplicaSet keeps N identical pods running. A Deployment manages ReplicaSets to provide rolling updates, rollbacks (revisionHistoryLimit), pausing, and scaling. You almost never create ReplicaSets directly. Use for stateless apps.

StatefulSet

For workloads needing stable identity:

Enlarge diagramkafka-0kafka-0.kafka-headlessPVC data-kafka-0EBS, kept on scale-downkafka-1kafka-1.kafka-headlessPVC data-kafka-1EBS, kept on scale-downkafka-2kafka-2.kafka-headlessPVC data-kafka-2EBS, kept on scale-downCreated in order 0 → 1 → 2; updated in reverse.
Figure 2. A StatefulSet gives each pod a fixed name, its own DNS name and its own disk, which follow it when it is rescheduled.
Swipe sideways to see all of it, or tap to open it full screen.
  • Stable names with ordinals: kafka-0, kafka-1, kafka-2 — recreated pods keep their name.
  • Stable network identity via a headless Service (clusterIP: None): DNS kafka-0.kafka-headless.data.svc.cluster.local.
  • Stable storage: volumeClaimTemplates create a PVC per pod (data-kafka-0) that sticks to that ordinal across rescheduling. PVCs are not deleted when the StatefulSet scales down or is deleted (configurable with persistentVolumeClaimRetentionPolicy).
  • Ordered creation (0, 1, 2 — each Ready before the next) and reverse-order deletion by default; podManagementPolicy: Parallel relaxes it.
  • Rolling updates in reverse ordinal order; partition: N updates only ordinals ≥ N — a built-in canary for stateful apps.

Use for databases, Kafka, ZooKeeper, Elasticsearch — ideally managed by an operator that handles failover, backups and upgrades. On AWS, managed services (RDS, MSK, OpenSearch) often beat running them yourself.

DaemonSet

Runs one pod on every (matching) node — new nodes get one automatically. Used for node agents: log collectors (Fluent Bit), monitoring (node-exporter, Datadog), CNI (aws-node), kube-proxy, CSI node plugins, security agents. Supports RollingUpdate (maxUnavailable, maxSurge) and needs tolerations to run on tainted nodes. Use nodeSelector/affinity to target subsets.

Enlarge diagramNODE 1NODE 2NODE 3NODE 4 (NEW)fluent-bitapp pods…fluent-bitapp pods…fluent-bitapp pods…fluent-bitapp pods…
Figure 3. A DaemonSet runs one copy on every node, including nodes added later.
Swipe sideways to see all of it, or tap to open it full screen.

Job and CronJob

apiVersion: batch/v1
kind: Job
spec:
  completions: 10            # total successful pods needed
  parallelism: 3             # run at most 3 at once
  completionMode: Indexed    # each pod gets JOB_COMPLETION_INDEX (0..9)
  backoffLimit: 4            # retries before marking the Job failed
  activeDeadlineSeconds: 3600
  ttlSecondsAfterFinished: 86400   # auto-delete finished Jobs
  template:
    spec:
      restartPolicy: OnFailure
      containers: [{ name: migrate, image: orders-migrate:1.4.2 }]
apiVersion: batch/v1
kind: CronJob
spec:
  schedule: "30 2 * * *"
  timeZone: "Asia/Kolkata"
  concurrencyPolicy: Forbid          # Allow | Forbid | Replace
  startingDeadlineSeconds: 300       # skip if it couldn't start within 5 min
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 3
  jobTemplate: { spec: { ... } }

concurrencyPolicy: Forbid prevents overlapping runs of long jobs; jobs should be idempotent because they can occasionally run twice or be skipped.

Autoscaling objects

  • HPA (autoscaling/v2) scales replicas on CPU/memory utilisation (relative to requests — no requests, no HPA: it shows <unknown>), custom metrics (Prometheus Adapter) or external metrics (SQS depth via KEDA). Needs metrics-server. Has scale-up/down behaviour policies and stabilisation windows.
  • VPA recommends or sets requests/limits; don't run it in Auto mode together with an HPA on the same CPU/memory metric.
  • KEDA — event-driven autoscaling, including scale to zero.
  • Node autoscaling (Cluster Autoscaler, Karpenter) adds capacity for Pending pods.
Enlarge diagramload risesCPU 85% ofrequestsHPAreplicas 3 → 6pods Pendingno room on nodesKarpenterlaunches a node
Figure 4. Two layers of autoscaling. The HPA adds pods; when those pods do not fit, Karpenter or Cluster Autoscaler adds nodes.
Swipe sideways to see all of it, or tap to open it full screen.