What if you could launch or fix your entire app with just one simple file?
Creating Deployments with YAML in Kubernetes - Why You Should Know This
Imagine you have a website that needs to run on several computers to handle many visitors. You try to start each copy by typing commands on each computer one by one.
This manual way is slow and tiring. You might forget a step or make a typo. If you want to change something, you have to repeat the slow process again. It's easy to make mistakes that break your website.
Using YAML files to create deployments lets you write down exactly how your website should run. Then, with one command, Kubernetes reads the file and sets up all the copies correctly and quickly. It's like giving clear instructions to a robot that never forgets.
kubectl create deployment myapp --image=myimage
kubectl scale deployment myapp --replicas=3apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myimageYou can quickly and safely create, update, or fix your applications on many computers with a simple text file.
A company launches a new online store. They use a YAML deployment file to run many copies of their store app. When traffic grows, they just change the file to add more copies instantly.
Manual setup is slow and error-prone.
YAML files give clear, repeatable instructions.
Deployments become fast, safe, and easy to manage.
Practice
Solution
Step 1: Understand Deployment role
A Deployment manages the lifecycle of pods, ensuring the desired number of replicas run and updates happen smoothly.Step 2: Compare options
Options B, C, and D describe other Kubernetes resources (PersistentVolume, Service, NetworkPolicy), not Deployment.Final Answer:
To manage and update multiple copies of an application smoothly -> Option AQuick Check:
Deployment = Manage app replicas and updates [OK]
- Confusing Deployment with Service or PersistentVolume
- Thinking Deployment exposes apps externally
- Assuming Deployment manages network policies
Solution
Step 1: Identify correct replicas field
The correct field to specify pod count in Deployment YAML isreplicas.Step 2: Check other options
Fields likereplicaCount,count, orpodReplicasare invalid and will cause errors.Final Answer:
replicas: 3 -> Option AQuick Check:
Use 'replicas' to set pod count [OK]
- Using incorrect field names like replicaCount
- Misspelling 'replicas' or using camelCase
- Placing replicas under wrong YAML level
selector:
matchLabels:
app: myapp
tier: frontendSolution
Step 1: Understand matchLabels selector
ThematchLabelsrequires all listed labels to match exactly for pod selection.Step 2: Analyze given labels
Bothapp: myappandtier: frontendmust be present on pods to match.Final Answer:
Pods with labels app=myapp and tier=frontend -> Option DQuick Check:
matchLabels means all labels must match [OK]
- Assuming matchLabels matches any one label
- Confusing matchLabels with matchExpressions
- Ignoring one of the labels in selector
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp
spec:
replicas: 2
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: webapp
image: nginxSolution
Step 1: Check selector and template labels
The selectormatchLabels: app: webappdoes not match templatelabels: app: frontend. Labels must match exactly or Deployment creation fails.Step 2: Verify other fields
Ports are optional for pod creation; apiVersion is correct; image tag defaults to latest.Final Answer:
Selector labels do not match template labels -> Option CQuick Check:
Selector must match template labels [OK]
- Assuming container ports are required for pod creation
- Thinking image tag is mandatory
- Confusing apiVersion for Deployment
Solution
Step 1: Understand Deployment spec structure
Thereplicasfield is directly underspec. The pod template is underspec.template, which must includemetadata.labelsandspec.containers.Step 2: Check each option's correctness
spec: replicas: 5 template: spec: containers: - name: nginx image: nginx:1.21missesmetadata.labelsunder template, which is required for selector matching.spec: replicas: 5 containers: - name: nginx image: nginx:1.21places containers directly under spec, which is invalid.spec: template: spec: containers: - name: nginx image: nginx:1.21 replicas: 5places replicas after template, which is allowed but missing labels in template metadata.spec: replicas: 5 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.21correctly includes replicas, template metadata labels, and container spec with updated image.Final Answer:
spec: replicas: 5, template with metadata.labels app: nginx, containers image: nginx:1.21 -> Option BQuick Check:
replicas under spec, labels in template metadata, containers in template spec [OK]
- Omitting labels in pod template metadata
- Placing containers outside template spec
- Misplacing replicas field
Ready to prove it?
Pass the test and this pattern moves unknown
