Skip to content
Tutorials Center
My learning

Topics

Lesson 74 of 78 ยท Practice

Exercises: Pods, Deployments and Services

Practice writing Kubernetes manifests for a Pod, a Deployment and a Service, fix broken YAML and check your work with runnable validators and full solutions.

Reading manifests teaches you to recognize them; writing them teaches you to remember them. These exercises cover the three objects you will write most often: a Pod, a Deployment and a Service. Each exercise has a goal, a starter manifest with mistakes inside a runnable editor, and a validator that prints PASS or FAIL for every requirement. Edit the manifest until everything passes, then compare with the read-only solution.

There is no cluster on this site, so the validators are stand-ins: small Python programs that parse your YAML and check the rules we care about. They are not the real API server, which checks much more (and would catch different mistakes). They are good enough to build the habit of checking your own work before you run kubectl apply. Try each exercise before opening its solution, and use kubectl explain pod.spec.containers from kubectl Essentials whenever you forget a field name.

How to practice

Edit Fix the starter manifest.
Run Read every PASS and FAIL.
Repeat Until all checks pass.
Compare Open the solution last.

Reading the FAIL hints is part of the exercise.

1 / 2

Exercise 1: a Pod

Goal. Write a Pod named hello with the label app: hello. It runs the image nginx with a pinned version tag (1.27 is fine), listens on container port 80 and receives an environment variable GREETING with any non-empty value. Relevant lessons: Pods and Labels, Selectors and Annotations.

The starter below has several mistakes. Run it and read the hints.

Python Exercise 1: fix the Pod

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution.

YAML
apiVersion: v1
kind: Pod
metadata:
  name: hello
  labels:
    app: hello
spec:
  containers:
    - name: hello
      image: nginx:1.27
      ports:
        - containerPort: 80
      env:
        - name: GREETING
          value: "hi from the pod"

Why these details matter: an image without a tag means latest, which can change under you; labels are how Services and Deployments find pods; and containerPort is mostly documentation (it does not open or block anything), but a wrong number misleads everyone who reads it, and a Service that targets it by name will break.

Exercise 2: a Deployment

Goal. Write a Deployment named web with 3 replicas of nginx:1.27. Pods are labelled app: web, and the selector must match them. Each container requests 100m CPU and 64Mi memory, has a memory limit of 128Mi, and a readiness probe that sends an HTTP GET to / on port 80. See Deployments, Resource Requests and Limits and Liveness, Readiness and Startup Probes.

This starter has the classic selector mistake, a string where a number belongs, and missing fields.

Python Exercise 2: fix the Deployment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution.

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: nginx:1.27
          ports:
            - containerPort: 80
          resources:
            requests:
              cpu: 100m
              memory: 64Mi
            limits:
              memory: 128Mi
          readinessProbe:
            httpGet:
              path: /
              port: 80

The selector rule is strict: the API server rejects a Deployment whose selector does not match its own template, and the selector cannot be changed afterwards. Note also that quantities such as 100m are strings, while replicas is an integer; mixing them up is a common YAML slip.

Exercise 3: a Service

Goal. Expose the web Deployment inside the cluster. Its pods carry the labels app: web and tier: frontend, and the container listens on 8080. Create a Service named web of type ClusterIP that accepts traffic on port 80 and forwards it to the container's port. Related: Services.

The validator below does something a real cluster does too: it matches the Service selector against the pod labels and counts the endpoints.

Python Exercise 3: fix the Service

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution.

YAML
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  type: ClusterIP
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080

A Service with a typo in its selector is created without any error; it simply has zero endpoints and connections fail. When a Service "does not work", check kubectl get endpoints web first. You could also give the container port the name http in the Deployment and set targetPort: http, which survives port changes.

Exercise 4: find the bugs

Goal. Below are three manifests in one file, each with something wrong. The linter reports what it finds; for each finding, decide how to fix it. The mistakes are realistic: an API version that was removed from Kubernetes, a selector that differs from the pod labels in capitalization, a number written as a string, and a value that YAML silently turns into a boolean. That last trap is covered in YAML and the Shape of a Manifest.

Python Exercise 4: lint three broken manifests

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution. Fix them one by one:

  • legacy: the old extensions/v1beta1 group for Deployments was removed long ago; use apps/v1 (see The Kubernetes API, Resources and Versions).
  • api: write replicas: 2 and use identical labels, app: api, in the selector and in the pod template. Label values are case sensitive.
  • worker: pin the image (for example busybox:1.37) and write value: "yes". Environment values must be strings, and unquoted yes, no, on, off, true or 1 are parsed as booleans or numbers, which the API rejects.

Exercise 5: use the imperative generators

Writing every field from memory is slow. Real work (and certification exams) rely on generators that print a manifest you then adjust. These commands are read-only here, since no cluster exists; run them against kind or minikube to check the result (Running a Local Cluster).

Terminal
kubectl create deployment web --image=nginx:1.27 --replicas=3 --dry-run=client -o yaml > web.yaml
kubectl expose deployment web --port=80 --target-port=80 --dry-run=client -o yaml > web-svc.yaml
kubectl run hello --image=nginx:1.27 --port=80 --labels=app=hello --env=GREETING=hi --dry-run=client -o yaml

Tasks. Generate the three manifests, then (1) add the resource requests and the readiness probe from Exercise 2 to web.yaml using an editor, (2) apply both files with kubectl apply -f ., (3) confirm with kubectl get endpoints web that there are three addresses, (4) delete everything with kubectl delete -f .. --dry-run=client prints the object without sending it, and -o yaml shows it in full. The generated Deployment already carries correct labels and a matching selector, which is why generating first and editing second prevents the mistakes you fixed above.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A Service was created without errors, but connections fail. What is the first thing to check?

    A selector typo or a failing readiness probe leaves the Service with no endpoints. kubectl get endpoints shows this immediately.
  2. 2. Which is correct for a Deployment manifest?

    Deployments use apps/v1 and replicas is an integer. The extensions group was removed and a quoted number is a string.
  3. 3. Why is env value: yes a problem in YAML?

    YAML 1.1 parsers turn yes into true. Quote it as "yes" so it stays a string.
  4. 4. What does kubectl create deployment web --image=nginx:1.27 --dry-run=client -o yaml do?

    --dry-run=client builds the object locally and -o yaml prints it, giving you a starting manifest to edit and apply.

Summary

  • Pods, Deployments and Services share the same skeleton: apiVersion, kind, metadata and spec.
  • A Deployment's selector must match its template labels, and a Service's selector must match pod labels, or it has no endpoints.
  • Pin image tags, set requests and a readiness probe, and keep numbers as numbers and strings as strings.
  • Generators with --dry-run=client -o yaml give you correct skeletons quickly.
  • The validators here are Python stand-ins; the real API server and kubectl apply remain the final check.

Next, harder scenarios: Exercises: Scheduling, Security and Troubleshooting.