Creating Kubernetes Deployments
Write a Deployment manifest, apply it, inspect rollout status, scale replicas, and understand selector mechanics.
Full Deployment Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
Key Fields to Notice
replicas
How many pod instances you want.
selector.matchLabels
This tells the Deployment which pods belong to it.
template
This is the pod blueprint used for each replica.
Apply the Deployment
kubectl apply -f deployment.yaml
Inspect the Deployment
kubectl get deployment
kubectl rollout status deployment/web
kubectl get rs
kubectl get pods -l app=web
Scale the Deployment
kubectl scale deployment web --replicas=5
Kubernetes then updates the ReplicaSet and creates more pods.
Why Label Selectors Matter
The selector must match the labels on the pod template. If they do not match, the Deployment cannot manage the pods correctly.
| Field | Why it matters |
|---|---|
selector.matchLabels | Defines ownership logic |
template.metadata.labels | Must match the selector |
Mental Model
A Deployment is not the pod itself. It is the recipe and control logic for keeping the right number of matching pods alive.
Scaling Meaning
What does `kubectl scale deployment web --replicas=5` do?
Selector Match
Why must the Deployment selector match the pod template labels?