Choosing a Workload (CKAD)
Deployment, StatefulSet, DaemonSet, Job or CronJob: what each one promises about your pods, and how to pick.
An interactive Kubernetes lesson: 22 steps, about 35 minutes, on a live simulation in your browser.
Lesson K8S-01 ended with a rule: a bare pod that goes away stays away, so in practice a controller creates your pods. Kubernetes has five of them for workloads, and they are not interchangeable.
The shop needs all five. An api that answers requests. A Postgres database. A log agent on every machine. A schema migration that runs once per release. A sales report every night. What separates them is one question: when a pod goes away, what exactly must come back?
What you will learn
Stateless: Deployment
- Five things to run, five shapes: Choosing a workload resource means answering: when a pod goes away, what exactly must come back?
- Deployment: interchangeable pods: A Deployment keeps N interchangeable pods running. Any pod can be replaced by any other, in any order.
Identity and storage: StatefulSet
- StatefulSet: a number, a name, a disk: A StatefulSet gives each pod a number, and the number owns a name, a DNS record and a volume.
- Delete db-1: Delete a StatefulSet pod and the same identity returns: same name, same DNS name, same PersistentVolumeClaim.
- Scale the database down: A StatefulSet scales down from the highest ordinal and leaves the PersistentVolumeClaims behind. Storage goes only when you delete the claim.
- Break it: one replica blocks the rest: OrderedReady: a StatefulSet works through its ordinals one at a time and stops at the first pod that is not Ready.
One per node: DaemonSet
- DaemonSet: one per node: A DaemonSet has no replica count. Its size is the number of nodes that qualify.
- A node joins the cluster: A DaemonSet follows the nodes: a new node gets a pod, a removed node loses one. Other controllers never rebalance by themselves.
- Cover the control plane too
Run to completion: Job
- Job: run until it succeeds: A Job's goal is a number of successful exits, not a number of running pods.
- completions and parallelism: completions is how many successes the Job needs. parallelism is how many pods may work on it at once.
- Break it: a Job that cannot succeed: A failed pod makes a Job try again, up to backoffLimit times. After that the Job itself is Failed and stays that way.
- A deadline, and cleaning up: backoffLimit bounds the retries, activeDeadlineSeconds bounds the time, ttlSecondsAfterFinished removes the finished Job.
On a schedule: CronJob
- CronJob: a Job on a schedule: A CronJob creates Jobs on a schedule, and each Job creates pods. Three layers: CronJob, Job, pod.
- Reading a schedule: Five fields: minute, hour, day of month, month, day of week. Read a schedule from left to right, smallest unit first.
- A run that overlaps the next: concurrencyPolicy: Allow lets runs overlap, Forbid skips the new run, Replace cancels the old one.
- Pause it, or run it now: suspend stops a CronJob from creating Jobs. kubectl create job --from runs its template once, right now.
Choosing, at exam speed
- Which one? Three questions: Does it finish? Job or CronJob. One per node? DaemonSet. Own identity or disk? StatefulSet. Otherwise, Deployment.
- Drill: create a CronJob
- Drill: run a CronJob now
Recap & playground
- Cheat sheet
- Playground