0 / 15 lessons — 0%
Lesson 12 / 15

Workload types: StatefulSets, Jobs, CronJobs & DaemonSets

A Deployment assumes every replica is interchangeable — kill pod 2 of 3, and a new, differently-named pod takes its place, nobody cares which. That assumption breaks for a handful of real workloads, which is why Kubernetes has three more specialized workload types.

StatefulSets — for apps that care about identity

A database cluster's node 0 often needs to stay node 0 — same name, same storage, even across restarts. A StatefulSet gives each replica a stable, predictable name (db-0, db-1, db-2) and its own PersistentVolumeClaim that follows it across rescheduling, plus ordered, one-at-a-time startup and shutdown.

StatefulSet: db db-0 pvc: data-db-0 db-1 pvc: data-db-1 db-2 pvc: data-db-2 names and storage stick to each replica, even after a restart
Unlike a Deployment, each StatefulSet pod keeps its own name and its own storage.

Jobs & CronJobs — run to completion, not forever

A Deployment's pods are meant to run forever. A Job runs a pod until it finishes successfully, then stops — perfect for a one-off migration script or batch task. A CronJob is a Job that fires on a schedule, using standard cron syntax.

# job.yaml — run a database migration once, to completion apiVersion: batch/v1 kind: Job metadata: name: db-migrate spec: template: spec: containers: - name: migrate image: myregistry/web:1.5.0 command: ["npm", "run", "migrate"] restartPolicy: Never # cronjob.yaml — same idea, but nightly at 2am apiVersion: batch/v1 kind: CronJob metadata: name: nightly-report spec: schedule: "0 2 * * *" jobTemplate: spec: template: spec: containers: - name: report image: myregistry/report:1.0 restartPolicy: OnFailure

DaemonSets — exactly one pod per node

Some things need to run on every node, no more, no less — a log shipper, a monitoring agent, a network plugin. A DaemonSet guarantees exactly one copy of its pod on each matching node, and automatically adds a copy when a new node joins the cluster.

Workload typeUse it when
DeploymentStateless, interchangeable replicas — most web apps and APIs
StatefulSetEach replica needs a stable identity and its own storage — databases, message queues
JobA task that runs once and finishes — migrations, batch processing
CronJobA Job on a recurring schedule — nightly reports, cleanup tasks
DaemonSetExactly one pod per node — log shippers, node-level monitoring
Try it yourselfRun kubectl get daemonsets -A on any real cluster you have access to — most clusters already run a DaemonSet or two for networking or monitoring, even if nobody on the team created it by hand.