Sub topic 18
When a Deployment is not the right controller
This chapter assumes you can write a Deployment manifest, know that a Deployment manages ReplicaSets, and have used kubectl get and kubectl describe. Deployments suit many workloads, but not all of them. Choosing the wrong controller causes problems that are hard to fix later.
What a Deployment assumes
A Deployment treats its Pods as interchangeable. Any Pod can be replaced by any other Pod. Pods get random names such as web-6d4cf56db6-x2k9p, share the same Pod template, and can run on any node. Rolling updates and scaling depend on this assumption.
If your workload breaks that assumption, use a different controller.
The alternatives
| Need | Controller | What it gives you |
|---|---|---|
| Stable identity and storage per replica | StatefulSet | Fixed names (`db-0`, `db-1`), ordered startup, one PersistentVolumeClaim per Pod |
| Exactly one Pod on every node (or a subset) | DaemonSet | A Pod is added when a node joins and removed when it leaves |
| A task that runs to completion | Job | Retries until a set number of Pods succeed, then stops |
| A task that runs on a schedule | CronJob | Creates a Job at each scheduled time |
StatefulSet
Use a StatefulSet for databases, message brokers and other systems where each replica has its own identity or data. If db-1 is deleted, the replacement is also called db-1 and reattaches to the same volume. A Deployment cannot do this. Its replacement Pod gets a new name, and all replicas normally share one volume claim.
DaemonSet
Use a DaemonSet for node-level agents such as log collectors, metrics exporters and network plugins. You do not set a replica count. The count follows the number of matching nodes. A Deployment with replicas: 3 gives you three Pods, but the scheduler may place two on the same node and leave other nodes without one.
Job and CronJob
A Deployment restarts Pods that exit, because it expects them to run forever. A batch task that finishes would be restarted again and again. Use a Job instead. It treats a successful exit as completion.
A quick comparison
apiVersion: batch/v1
kind: Job
metadata:
name: report
spec:
completions: 1
backoffLimit: 3
template:
spec:
restartPolicy: Never
containers:
- name: report
image: busybox:1.36
command: ["sh", "-c", "echo generating report && sleep 5"]This Job runs one Pod to completion and retries up to three times on failure. Compare it with a Deployment: it has no replicas field for steady state, and restartPolicy must be Never or OnFailure. A Deployment's Pod template only allows Always.
Decision checklist
- Does the Pod run until you stop it? If not, use a Job or CronJob.
- Do you need one Pod per node? If so, use a DaemonSet.
- Does each replica need its own name or disk? If so, use a StatefulSet.
- Otherwise, use a Deployment.
Exercise
You run a log-shipping agent that must run on every node, including nodes added later. Which controller should you use, and why is a Deployment with many replicas a poor fit?
Show answer
Use a DaemonSet. It places one Pod on each matching node and adds a Pod automatically when a node joins. A Deployment only keeps a fixed number of Pods and does not tie them to nodes, so some nodes could have none while others have several.
Exercise
A Job's Pod template sets restartPolicy: Always. What happens when you apply it?
Show answer
The API server rejects it. A Job requires restartPolicy to be Never or OnFailure, because a Job expects its Pods to finish.