Kubernetes workload types: which one to use
Deployments, StatefulSets, DaemonSets, Jobs and CronJobs, and the question that picks between them.
You rarely create bare pods: a controller creates them and replaces them when they fail. The right controller depends on two questions: should the work keep running or finish, and do the copies need their own identity?
Keep running, all copies the same: Deployment (rolling updates, rollbacks, scaling). Keep running, each copy with its own name and disk: StatefulSet. One copy on every node: DaemonSet. Run to completion: Job. Run to completion on a schedule: CronJob.
Inside a pod you can have more than one container. Init containers run first, in order, and must finish (wait for a database, run a migration). Sidecar containers run alongside the app for its whole life (log shipping, proxies), and Kubernetes now starts them before the app and stops them after it.
Cost. Free to learn on kind.
Use it when
- you're deciding how to run a new service or task.
- a task that should finish keeps getting restarted.
- you need an agent on every node.
How it works
- Deployment
- Stateless apps; ReplicaSets underneath handle versions.
- StatefulSet
- Ordered, named pods with their own claims.
- DaemonSet
- One pod per node, including new nodes.
- Job
- Retries until success, with completions and parallelism.
- CronJob
- A schedule that creates Jobs, with overlap rules.
Options
- Init container
- Setup that must finish before the app starts.
- Sidecar container
- A helper that runs for the app's whole life.
- Separate deployment
- When the helper scales or changes on its own schedule.
Example. Kestwick Hotels runs its booking API as a Deployment, its search index as a StatefulSet, its log shipper as a DaemonSet, its nightly finance export as a CronJob, and its database migration as an init container in the API's pods.
Don't confuse it with
- Bare pod
- Nothing replaces it if it fails or its node goes; use a controller.
- ReplicaSet
- What a Deployment manages for you; you rarely create one yourself.
Words to know
- Controller
- A loop that keeps real state matching the desired state.
- Init container
- A container that runs to completion before the app's containers start.
- Sidecar
- A helper container that runs next to the app container.
Where to look
- kubectl get deploy,sts,ds,job,cronjob
Practise it
- Run DaemonSets, Jobs and CronJobsHow-to, Free
- Keep data with volumes, claims and a StatefulSetHow-to, Free
- Read a pod.yaml and follow a pod's lifecycleHow-to, Free
Read it in the guide, next to the tasks and labs.
Open in DevOps, Hands On