Workloads: Deployments, rollouts and jobs · 5 MIN READ
Deployments, ReplicaSets and Pods: the controller chain behind every replica
Create a Deployment, trace its ReplicaSet and Pods through labels and owner references, read the READY, UP-TO-DATE and AVAILABLE columns, and watch the controllers scale and replace Pods.
A Pod you create by hand is gone for good when someone deletes it or its node dies. A Deployment declares how many identical Pods you want and what they look like; a ReplicaSet keeps that many running, and the Deployment manages ReplicaSets so that the template can change safely. Knowing which object owns which is what lets you read kubectl output and fix a stuck workload.
kubernetes · A MAP OF THE IDEA
- Write the Deployment manifest
- Controller creates a ReplicaSet
- ReplicaSet creates the Pods
- Scale and heal through it
Useful when
- Deleted or lost Pods are replaced automatically, on any node.
- Scaling is one field change that never restarts running Pods.
Watch out
- Edits to a ReplicaSet are reverted by its Deployment.
- Overlapping selectors make controllers fight over Pods.
Implementation & operation cost: One Deployment manifest per stateless component, kept in version control with stable selector labels.
Three objects, two controllers
The Deployment stores desired state: a replica count, a label selector and a Pod template. The Deployment controller, running in kube-controller-manager, creates one ReplicaSet per distinct template and decides how many replicas each should have. The ReplicaSet controller then creates or deletes Pods until the number of live Pods matching its selector equals its replicas field. Every Pod carries an ownerReference to its ReplicaSet, and every ReplicaSet one to its Deployment; the garbage collector follows these links when you delete the Deployment. Controllers are level-triggered: they compare desired with observed state on every change, so a missed event is corrected on the next pass rather than lost. You almost never create ReplicaSets yourself; you change the Deployment and let the chain converge.
A Deployment manifest
spec.selector tells the Deployment which Pods are its own, and it must match spec.template.metadata.labels or the API server rejects the object. In apps/v1 the selector is immutable after creation, so choose stable labels such as app: web rather than anything containing a version. replicas defaults to 1. Everything under template is the Pod that will be stamped out; containerPort is informational, but listing it documents the contract that Services and probes rely on. Keep this file in version control and change it there, so that the cluster is always something you can recreate from Git.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
labels:
app: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: gopherforge/web:v1
ports:
- containerPort: 8080Three label sets appear. The label on the Deployment itself only lets you select Deployments, for example kubectl get deploy -l app=web. The selector app: web decides which Pods the Deployment claims, and the template label app: web is stamped on every Pod its ReplicaSet creates. If the last two disagree, the API server refuses the Deployment because the selector does not match the template labels.
pod-template-hash ties a ReplicaSet to one template
The Deployment controller hashes the Pod template and adds the result as the pod-template-hash label to the ReplicaSet's selector, to its template and therefore to every Pod it creates; the ReplicaSet is named web-<hash>. Two ReplicaSets of the same Deployment therefore never select each other's Pods, although both also match app=web. Any change under template, such as the image, an environment variable or a template annotation, produces a new hash and a new ReplicaSet: that is a rollout. replicas is not part of the template, which is why scaling never starts a rollout. Never set pod-template-hash yourself, and never give two controllers overlapping selectors: the documentation warns that such controllers fight with each other and do not behave correctly.
Reading kubectl get deploy and get rs
READY is the number of ready Pods over the desired replica count. UP-TO-DATE counts Pods created from the current template, so it lags during a rollout. AVAILABLE counts Pods that have stayed ready for at least minReadySeconds, zero by default; it is what the Available condition and rollout decisions use. For a ReplicaSet, DESIRED is its replicas field, CURRENT the Pods it currently owns and READY the ready ones among them. A Deployment showing 3/3 with UP-TO-DATE 1 is in the middle of a rollout; 2/3 with UP-TO-DATE 3 means a Pod is missing or not ready and the ReplicaSet is dealing with it. kubectl describe deploy adds the Conditions and the ScalingReplicaSet events that record what the controller decided and when.
Scaling and self-healing
kubectl scale sets replicas on the Deployment, which passes the number to its current ReplicaSet. When a Pod disappears because it was deleted, evicted or lost with its node, the ReplicaSet sees fewer matching Pods than desired and creates a replacement with a new name, a new UID and usually a new IP; nothing is restarted in place. That is why clients reach Pods through a Service, never by Pod IP. Changing a ReplicaSet directly does not stick: the Deployment controller owns its replicas field and puts it back. When scaling down, the ReplicaSet removes unscheduled and not-ready Pods first, then prefers Pods on nodes that run more replicas and newer Pods; the controller.kubernetes.io/pod-deletion-cost annotation lets you influence the choice.
Try it in the lab terminal
Create a fresh single-node cluster, apply the manifest and follow the chain from Deployment to ReplicaSet to Pods through labels and owner references. Then delete one Pod, scale the Deployment to five replicas, and try to scale its ReplicaSet behind the Deployment's back. The $(...) substitutions pick the first Pod or ReplicaSet name, so you never copy random suffixes by hand. Watch the AGE column: replacements are new Pods, not restarted ones.
kind create cluster
cat > web.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
labels:
app: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: gopherforge/web:v1
ports:
- containerPort: 8080
EOF
kubectl apply -f web.yaml
kubectl get deploy web
kubectl rollout status deploy/web
kubectl get deploy,rs,pods --show-labels
kubectl get pods -o custom-columns=NAME:.metadata.name,OWNER:.metadata.ownerReferences[0].name
kubectl delete $(kubectl get pods -l app=web -o name | head -n 1)
kubectl get deploy web
kubectl get pods
kubectl scale deploy web --replicas=5
kubectl rollout status deploy/web
kubectl scale $(kubectl get rs -l app=web -o name) --replicas=1
kubectl get rs
kubectl describe deploy webRight after apply, get deploy shows web 0/3 3 0: three Pods exist from the current template, none ready yet. After the rollout, ReplicaSet web-<hash> and its three Pods all carry app=web,pod-template-hash=<hash>, and the OWNER column names that ReplicaSet. Deleting web-<hash>-<id> drops the Deployment to 2/3 3 2 while a replacement with a new suffix is ContainerCreating. Scaling the ReplicaSet to 1 prints scaled, yet get rs still shows DESIRED 5, and the last event in describe reads Scaled up replica set web-<hash> from 1 to 5. On a real cluster a few Pods may be replaced during that race.
TAKE THIS WITH YOU
A Deployment owns one ReplicaSet per Pod template, and each ReplicaSet owns Pods that it replaces whenever they disappear. Change and scale the Deployment, never its ReplicaSets, and read READY, UP-TO-DATE and AVAILABLE to tell a rollout from a missing Pod.
Check your understanding
You change only replicas from 3 to 6 in a Deployment manifest and apply it. What do the controllers do?
Go to the source
Progress stays in this browser. No account needed.