Deployment YAML
- Docker & Kubernetes course
- Intermediate
- Lesson 26/82
- 4 min
Learn the Deployment YAML structure.
Deployment YAML
A Deployment's spec has three important parts: replicas, the selector determining which pods belong to it, and the template, the mould for new pods. The labels in the template must match the selector:
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
Resource requests and limits
In production, resources are written for each container. requests are the guaranteed amount the scheduler reserves; limits are the ceiling that can't be exceeded (a container exceeding its memory limit is OOMKilled):
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 256Mi100m = 0.1 CPU core; Mi = mebibytes.
Practice Write the deployment YAML
Write a Deployment YAML: name web, replicas: 3; selector and template label app: web; container named nginx, image nginx:1.27, containerPort: 80.
Starter code
# buraya yazSample solution
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: 80Expected output
deployment.apps/web created
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 If selector.matchLabels is app: web but the template label is app: api, what happens?
- It works fine
- The selector is fixed automatically
- The API server rejects the object Correct answer
- Pods are created but not counted
Explanation: You get a "selector does not match template labels" error.
2 Write the field defining the mould for new pods.
spec:
replicas: 3
selector:
matchLabels:
app: web
____:
metadata:
labels:
app: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
Explanation: template is a pod definition (metadata + spec).
3 Match.
| Term | Meaning |
|---|---|
replicas |
Replica count |
selector |
Which pods are mine |
template |
Pod mould |
requests |
Guaranteed resources |