Bird
Raised Fist0
Kubernetesdevops~5 mins

Creating Deployments with YAML in Kubernetes - Step-by-Step CLI Walkthrough

Choose your learning style10 modes available
Introduction
When you want to run and manage multiple copies of your app in Kubernetes, you use a Deployment. It helps keep your app running smoothly by automatically restarting or updating it. Writing a Deployment in YAML lets you tell Kubernetes exactly how to do this.
When you want to run several copies of a web app to handle more users.
When you need to update your app without downtime by rolling out changes gradually.
When you want Kubernetes to restart your app automatically if it crashes.
When you want to manage app versions and roll back if something goes wrong.
When you want to define your app setup in a file to share or reuse later.
Config File - deployment.yaml
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-deployment
  labels:
    app: example-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: example-app
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: example-container
        image: nginx:1.23.3
        ports:
        - containerPort: 80

apiVersion: Specifies the Kubernetes API version to use for Deployment.

kind: Defines this resource as a Deployment.

metadata: Names and labels the Deployment for identification.

spec: Describes the desired state, including number of replicas and pod template.

replicas: Number of pod copies to run.

selector: Matches pods with the label app: example-app.

template: Defines the pod's metadata and spec, including container image and port.

Commands
This command creates the Deployment in Kubernetes using the YAML file. It tells Kubernetes to start 3 pods running the nginx container.
Terminal
kubectl apply -f deployment.yaml
Expected OutputExpected
deployment.apps/example-deployment created
This command lists all Deployments in the current namespace to verify the Deployment was created.
Terminal
kubectl get deployments
Expected OutputExpected
NAME READY UP-TO-DATE AVAILABLE AGE example-deployment 3/3 3 3 10s
This command lists all pods with the label app=example-app to check that the pods are running as expected.
Terminal
kubectl get pods -l app=example-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-deployment-6d4b75f7d7-abc12 1/1 Running 0 10s example-deployment-6d4b75f7d7-def34 1/1 Running 0 10s example-deployment-6d4b75f7d7-ghi56 1/1 Running 0 10s
→
-l - Filter pods by label
This command shows detailed information about the Deployment, including events and pod status.
Terminal
kubectl describe deployment example-deployment
Expected OutputExpected
Name: example-deployment Namespace: default CreationTimestamp: Thu, 01 Jun 2023 12:00:00 +0000 Labels: app=example-app Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 1m deployment-controller Scaled up replica set example-deployment-6d4b75f7d7 to 3
Key Concept

If you remember nothing else from this pattern, remember: a Deployment manages multiple copies of your app and keeps them running as you defined in the YAML file.

Common Mistakes
Not matching the selector labels with the pod template labels
Kubernetes won't know which pods belong to the Deployment, so it won't manage them properly.
Ensure the selector.matchLabels exactly matches the labels in template.metadata.labels.
Forgetting to specify the container image in the pod template
Pods won't start because Kubernetes doesn't know what container to run.
Always include the container image under spec.template.spec.containers.
Applying the YAML file without saving changes
Kubernetes applies the old configuration, so your updates don't take effect.
Save the YAML file after editing before running kubectl apply.
Summary
Write a Deployment YAML file to define how many app copies to run and what container to use.
Use 'kubectl apply -f deployment.yaml' to create or update the Deployment in Kubernetes.
Check the Deployment and pods with 'kubectl get deployments' and 'kubectl get pods -l app=example-app' to confirm they are running.

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

  1. Step 1: Understand Deployment role

    A Deployment manages the lifecycle of pods, ensuring the desired number of replicas run and updates happen smoothly.
  2. Step 2: Compare options

    Options B, C, and D describe other Kubernetes resources (PersistentVolume, Service, NetworkPolicy), not Deployment.
  3. Final Answer:

    To manage and update multiple copies of an application smoothly -> Option A
  4. 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

  1. Step 1: Identify correct replicas field

    The correct field to specify pod count in Deployment YAML is replicas.
  2. Step 2: Check other options

    Fields like replicaCount, count, or podReplicas are invalid and will cause errors.
  3. Final Answer:

    replicas: 3 -> Option A
  4. 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

  1. Step 1: Understand matchLabels selector

    The matchLabels requires all listed labels to match exactly for pod selection.
  2. Step 2: Analyze given labels

    Both app: myapp and tier: frontend must be present on pods to match.
  3. Final Answer:

    Pods with labels app=myapp and tier=frontend -> Option D
  4. 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?
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: nginx
medium
A. No container ports defined
B. Incorrect apiVersion for Deployment
C. Selector labels do not match template labels
D. Missing container image tag

Solution

  1. Step 1: Check selector and template labels

    The selector matchLabels: app: webapp does not match template labels: app: frontend. Labels must match exactly or Deployment creation fails.
  2. Step 2: Verify other fields

    Ports are optional for pod creation; apiVersion is correct; image tag defaults to latest.
  3. Final Answer:

    Selector labels do not match template labels -> Option C
  4. 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?
hard
A.
spec:
  replicas: 5
  template:
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
B.
spec:
  replicas: 5
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
C.
spec:
  replicas: 5
  containers:
  - name: nginx
    image: nginx:1.21
D.
spec:
  template:
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
  replicas: 5

Solution

  1. Step 1: Understand Deployment spec structure

    The replicas field is directly under spec. The pod template is under spec.template, which must include metadata.labels and spec.containers.
  2. Step 2: Check each option's correctness

    spec:
      replicas: 5
      template:
        spec:
          containers:
          - name: nginx
            image: nginx:1.21
    misses metadata.labels under template, which is required for selector matching.
    spec:
      replicas: 5
      containers:
      - name: nginx
        image: nginx:1.21
    places containers directly under spec, which is invalid.
    spec:
      template:
        spec:
          containers:
          - name: nginx
            image: nginx:1.21
      replicas: 5
    places 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.21
    correctly includes replicas, template metadata labels, and container spec with updated image.
  3. Final Answer:

    spec: replicas: 5, template with metadata.labels app: nginx, containers image: nginx:1.21 -> Option B
  4. Quick Check:

    replicas under spec, labels in template metadata, containers in template spec [OK]
Hint: Always include labels in template metadata for selector matching [OK]
Common Mistakes:
  • Omitting labels in pod template metadata
  • Placing containers outside template spec
  • Misplacing replicas field
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard