Core Kubernetes · 04
Deployments and Scaling
1.What is a Deployment ?
A Deployment keeps N identical Pods running for a stateless app. It never creates Pods itself: it owns ReplicaSets, and they own the Pods.
- One ReplicaSet per Pod template version, named
web-<pod-template-hash>. - Old ones stay at 0 replicas as revision history (see Rollouts and Probes).
- Links live in
metadata.. Edit the Deployment, not its ReplicaSets.ownerReferences
2.Labels and Selectors
Labels are key/value tags, a selector is a query over them. A ReplicaSet finds its Pods by selector, never by name.
- Key : optional prefix (DNS subdomain, 253 max),
/, name (63 max).kubernetes.io/andk8s.io/are reserved. - Value : 63 max, may be empty.
- Match :
=,==,!=, or set-basedin,notin, exists (key), not exists (!key). - YAML : matchLabels and matchExpressions, all ANDed. No OR.
A selector the template does not satisfy is rejected. In apps/v1 it is never defaulted and is immutable.
3.A Deployment YAML
apiVersion: apps/v1
kind: Deployment
metadata: {name: web}
spec:
replicas: 3
selector: {matchLabels: {app: web}}
template:
metadata: {labels: {app: web}}
spec:
containers:
- name: web
image: nginx:1.29
resources: {requests: {cpu: 100m}}- selectorMust match template labels (extras ok). Immutable.
- replicasDefault 1. Leave out under an HPA.
- templateA change makes (or reuses) a ReplicaSet.
| What broke | Who fixes it | You see |
|---|---|---|
| Container crashed | kubelet, in place, with backoff | RESTARTS climbs, CrashLoopBackOff |
| Pod deleted or evicted | ReplicaSet makes a new Pod | New name, new IP |
| Node failed | ReplicaSet, after node grace + 300s toleration | Nothing for about 6 min, then Pods on other nodes |
| Stray Pod matches | ReplicaSet adopts it, deletes extras | Back to replicas |
4.Which workload controller ?
| Controller | Use it for | Gives you | Watch out |
|---|---|---|---|
| Deployment apps/v1 | Stateless web apps, APIs, workers | Interchangeable Pods, declarative updates | Pod names are random. Use a StatefulSet for stable ones |
| StatefulSet apps/v1 | Databases, queues, quorum systems | db-0, db-1, ... : stable DNS, own PVC, in order | You make the headless Service. PVCs stay |
| DaemonSet apps/v1 | Log shippers, node exporters, CNI agents | One Pod per eligible node | No HPA. Control-plane nodes need tolerations |
| Job batch/v1 | Migrations, one-off batch runs | Runs to completion, backoffLimit 6 | restartPolicy Never or OnFailure |
| CronJob batch/v1 | Backups, nightly reports | A new Job per schedule tick | Runs may overlap (Allow). Use spec.timeZone |
1.Scaling by hand
Scaling only edits spec.replicas. No rollout, no new revision.
$ kubectl scale deployment/web --replicas=5
Undone by an apply that sets replicas, or an HPA.
- Not yet on a node
- Pending, Unknown
- Not ready
- Lower deletion cost
- Doubled up on a node
- Ready for less time
- More restarts
- Newer
The first rule that differs decides (v1.37). The cost is the controller. annotation: the one sure lever.
2.HorizontalPodAutoscaler
An HPA (autoscaling/v2) edits replicas for you. Every 15s its controller reads metrics and writes the target's /scale.
4 Pods at 90% CPU, target 70% : ceil(4 x 90 / 70) = ceil(5.14) = 6, kept within min..max.
- Within 10% of target : no change. Several metrics : the biggest answer wins.
- Utilization is a % of the requests. No request, no action.
metrics-server is an add-on that kubeadm does not install, see metrics-server. Custom or external metrics need an adapter, such as KEDA.
3.HPA manifest and defaults
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: {name: web}
spec:
scaleTargetRef: {apiVersion: apps/v1,
kind: Deployment, name: web}
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target: {type: Utilization,
averageUtilization: 70}| Field | Default | Note |
|---|---|---|
| minReplicas | 1 | 0 needs an Object or External metric (beta) |
| maxReplicas | required | At least minReplicas |
| metrics | 80% CPU | Highest result wins |
| scaleUp | no wait | +100% or +4 Pods per 15s |
| scaleDown | 300s | Highest pick in that window |
| tolerance | 10% | Per-HPA field GA in v1.37 |
| sync | 15s | kube-controller-manager |
replicas or each apply resets it (removing it later can drop to 1 once). Set requests.cpu : 100m, so 70% is 70m.4.Commands
| Command | What it does |
|---|---|
| kubectl create deployment web --image=nginx:1.29 --replicas=3 | Create one with 3 Pods |
| kubectl get deploy,rs,pods -l app=web --show-labels | The whole chain, with pod-template-hash |
| kubectl get pods -l 'env in (prod, qa)' | Set-based selector query |
| kubectl scale --current-replicas=2 --replicas=3 deployment/web | Scale only if it is still at 2 |
| kubectl autoscale deployment web --min=2 --max=10 --cpu=70% | Make an HPA (--cpu-percent is deprecated) |
| kubectl get hpa web --watch | Current vs target metric, replicas |
| kubectl describe hpa web | Conditions (ScalingActive, ...) and events |
| kubectl delete deployment web --cascade=orphan | Remove it, keep ReplicaSets and Pods |