📖 Scenario: You are working as a DevOps engineer. Your team needs to deploy a simple web application on Kubernetes. You will create a Deployment YAML file step-by-step to manage the application pods.
🎯 Goal: Build a Kubernetes Deployment YAML file that defines a Deployment named webapp-deployment running 3 replicas of the nginx:1.21 container.
📋 What You'll Learn
Create a Deployment YAML with apiVersion, kind, and metadata
Add a spec section with replicas and selector
Define the pod template with container name and image
Output the complete YAML content
💡 Why This Matters
🌍 Real World
Kubernetes Deployments are used to manage and scale containerized applications in production environments.
💼 Career
Understanding how to write Deployment YAML files is essential for DevOps engineers and site reliability engineers working with Kubernetes.
Progress0 / 4 steps
1
Create the Deployment header and metadata
Create a YAML file starting with apiVersion: apps/v1, kind: Deployment, and metadata with name: webapp-deployment.
Kubernetes
Hint
Start by specifying the API version, the kind of resource, and the deployment name under metadata.
2
Add replicas and selector to the spec
Add a spec section with replicas: 3 and a selector matching app: webapp under matchLabels.
Kubernetes
Hint
The spec defines how many pods to run and how to select them using labels.
3
Define the pod template with container details
Under spec.template, add metadata.labels with app: webapp. Then add spec.containers with one container named nginx-container using the image nginx:1.21.
Kubernetes
Hint
The pod template defines the pods that will be created. Labels must match the selector. Containers need a name and image.
4
Output the complete Deployment YAML
Print the entire YAML content stored in a variable called deployment_yaml.
Kubernetes
Hint
Use print(deployment_yaml) to show the full YAML content.
Practice
(1/5)
1. What is the main purpose of a Deployment in Kubernetes when defined using a YAML file?
easy
A. To manage and update multiple copies of an application smoothly
B. To store data persistently across pods
C. To expose an application to the internet
D. To define network policies between pods
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 A
Quick Check:
Deployment = Manage app replicas and updates [OK]
Hint: Deployments control app copies and updates, not storage or networking [OK]
Common Mistakes:
Confusing Deployment with Service or PersistentVolume
Thinking Deployment exposes apps externally
Assuming Deployment manages network policies
2. Which of the following YAML snippets correctly sets the number of pod replicas to 3 in a Deployment?
easy
A. replicas: 3
B. replicaCount: 3
C. count: 3
D. podReplicas: 3
Solution
Step 1: Identify correct replicas field
The correct field to specify pod count in Deployment YAML is replicas.
Step 2: Check other options
Fields like replicaCount, count, or podReplicas are invalid and will cause errors.
Final Answer:
replicas: 3 -> Option A
Quick Check:
Use 'replicas' to set pod count [OK]
Hint: Use 'replicas' exactly to set pod count in Deployment YAML [OK]
Common Mistakes:
Using incorrect field names like replicaCount
Misspelling 'replicas' or using camelCase
Placing replicas under wrong YAML level
3. Given this Deployment YAML snippet, what will be the label selector used to match pods?
selector:
matchLabels:
app: myapp
tier: frontend
medium
A. Pods with any label app or tier
B. Pods with label app=myapp only
C. Pods with label tier=frontend only
D. Pods with labels app=myapp and tier=frontend
Solution
Step 1: Understand matchLabels selector
The matchLabels requires all listed labels to match exactly for pod selection.
Step 2: Analyze given labels
Both app: myapp and tier: frontend must be present on pods to match.
Final Answer:
Pods with labels app=myapp and tier=frontend -> Option D
Quick Check:
matchLabels means all labels must match [OK]
Hint: matchLabels requires all labels to match exactly [OK]
Common Mistakes:
Assuming matchLabels matches any one label
Confusing matchLabels with matchExpressions
Ignoring one of the labels in selector
4. You wrote this Deployment YAML but your pods are not created. What is the error?
The selector matchLabels: app: webapp does not match template labels: 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 C
Quick Check:
Selector must match template labels [OK]
Hint: Selector labels must exactly match template labels [OK]
Common Mistakes:
Assuming container ports are required for pod creation
Thinking image tag is mandatory
Confusing apiVersion for Deployment
5. You want to update your Deployment to run 5 replicas and change the container image to 'nginx:1.21'. Which YAML snippet correctly applies both changes?