Platform Engineering · Lesson 159

Kubernetes Lab: Your First Pod, Deployment, and Service

Lesson 48 taught you Pods, Deployments, and Services. This lab is where you run them on your own cluster. You start minikube or k3s, you apply a Pod, you scale a Deployment to three replicas, you expose it with a Service, and you curl it through a port-forward. By the end you have three pods answering, and you know the difference between a Pod and a Deployment, because you made one and scaled the other.

Difficulty: Beginner · Know this first: Lesson 48 — Kubernetes Core; minikube or k3s installed, kubectl installed

Run it as:

When it breaks, I check

One thing to remember

Where the time goes

Every hop charges rent in milliseconds. The bar below is a worked example for this lesson's scenario — not a measurement from a live system — but the shape is honest: on a cache hit the trip is almost free, on a miss the kitchen eats the budget. Step through the lesson or switch the scenario and watch the bar change.

Extra diagram · from scratch to master

The first pod, mapped: cluster, pod, deployment, service

Start with one node. Add one box at a time until three pods are Ready and the Service answers.

7 boxes on the trip3 replicas1 wrong image
Cluster
0 min
Pod
1 min
Deploy
3 min
Service
1 min
Forward
1 min
Scale
2 min
Bad image
trip ends
Request / flowResponse · the way backFailure / dead end

Try it: change the inputs and watch the trip bend

The budget bar above shows three fixed stories. This simulator lets you write your own. Move a slider and watch the scheduling, the start, and the total change in the budget bar, in the hop timings, and in the total. The numbers are a teaching example, not live measurements. The shape is the lesson: more replicas means more scheduling, and a wrong image costs a fix per pod.

3

How many replicas the Deployment promises. Each replica is one more pod to schedule, pull, and start.

10

How long one image pull takes, in seconds. A big image pulls slow, and every pod pays for it.

0

How many pods cannot pull because the image name is wrong. Each one sits in ImagePullBackOff until the name is fixed.

Try this: (1) Set replicas to 3, pull to 10 s, bad to 0. The schedule is about 3 min, the start about 2 min, the check about 1 min, and the total is about 11 min. (2) Set all three to the worst end: 10 replicas, 60 s pull, 5 bad. The schedule is about 10 min, the start about 30 min, the check about 11 min, and the total is about 56 min. Same files. Only the inputs changed.

Check yourself

Three questions. No score is saved anywhere — this is just you against the material.

Try it: edit the pipeline and check it

Here is the Pod from this lab, ready to edit. Change it, then run the check. The check looks for the five things a working Pod needs: apiVersion, kind Pod, a name, a container with an image, and a port.

Try breaking it on purpose: change nginx:1.27 to nginx:9.99, or delete the ports line. Run the check and watch it fail. Then press Reset and try a change that keeps it passing, like adding a second label.

Press "Run the check" to see whether this pipeline does what the lesson asks.

Expected output when it passes: The file has apiVersion v1, kind Pod, the name hello-pod, the image nginx:1.27, and port 80. kubectl apply would create the pod, and it would become Ready.

kubectl apply -f, flag by flag. apply reads a file and moves the cluster toward it. The file is the desired state. The flag says which file.

  • -f deploy.yaml names the file to apply. Without it the command has nothing to read. The file is the desired state, and apply is what moves the cluster toward it.

Flags you will meet next: --dry-run=client prints what would happen without doing it, and -n picks the namespace.

Try it: apply the same file twice. The second run prints unchanged, not created. That is the proof apply is declarative: it compares, and it only changes what differs.

Basic to master

I take the same app four times here. First I run one pod. Then I promise three. Then I address them. Last I break one and watch it come back.

Basic — One Pod

Definition: A Pod is the smallest thing Kubernetes runs: one container, or a few that share a fate. kubectl apply creates it. kubectl get pods shows it. The READY column is the proof: 1/1 means the pod is Ready, and 0/1 means it is not.

Types:

  • A Pod, which is the unit Kubernetes schedules.
  • kubectl apply, which creates it from a file.
  • kubectl get pods, which shows the status.
  • The READY column, which is the proof.

Uses: I use a Pod when I need one thing running, once, for a test. Try it now: apply pod.yaml and watch it become 1/1.

Dimensions: At low volume everything is instant. What breaks first is the image name: a misspelled name puts the pod in ImagePullBackOff.

Try it: Basic

I run kubectl get pods and the READY column says 0/1. What does that mean?

Now do it: On your own cluster, apply pod.yaml and watch it become 1/1.

Intermediate — A Deployment promises

Definition: A Deployment promises a replica count, always. It owns a ReplicaSet, which owns the pods. kubectl scale changes the promise, and the controller makes the pods. If one dies, the Deployment makes another, without being asked.

Types:

  • A Deployment, which promises a replica count.
  • A ReplicaSet, which owns the pods.
  • kubectl scale, which changes the promise.
  • kubectl get deployments, which reads current/desired.

Uses: I use a Deployment whenever an app must keep running, which is nearly always. Try it now: scale hello to 5 and back to 3, and watch the pods.

Dimensions: As the replicas grow, the scheduling grows, and the time grows with the count. What breaks is a Deployment with no Service: the pods run, and nothing can reach them.

Try it: Intermediate

I scale hello to 5 and one pod dies. What happens next?

Now do it: On your own cluster, scale hello to 5 and back to 3, and watch the pods.

Advanced — A Service addresses

Definition: A Service is a stable name and IP for a set of pods, found by a label selector. ClusterIP is the default, reachable inside the cluster. Endpoints are the addresses of the Ready pods behind it. A port-forward is a tunnel from your laptop, for debugging, not for production.

Types:

  • A Service, which is the stable address.
  • A selector, which finds the pods by label.
  • Endpoints, which are the Ready pods behind it.
  • A port-forward, which is the debug tunnel.

Uses: I use a Service whenever anything needs to reach the pods, which is always. Try it now: get the endpoints and count them against the replicas.

Dimensions: At ten times the pods, the endpoints multiply, and the Service still works. What breaks is a selector that matches nothing: the Service exists, and it has no endpoints.

Try it: Advanced

My Service exists but it has no endpoints. What is the most likely cause?

Now do it: On your own cluster, get the endpoints and count them against the replicas.

Master — Break it and watch it come back

Definition: At this level the test is not that the app runs. It is that the app comes back. Delete a pod and watch the Deployment replace it. Scale up and down and watch the promise hold. The AGE column is the proof: the replacement is younger than the rest.

Types:

  • A delete, which is the test.
  • A replacement, which is the promise working.
  • The AGE column, which proves the replacement is new.
  • A clean delete at the end, which leaves nothing behind.

Uses: I use this before I trust any workload: break one pod on purpose, and watch it come back. Try it now: delete one pod and check the count is three again.

Dimensions: At a hundred pods, the promise is the same shape, and the controller is the same. What breaks is a Pod with no Deployment: it stays dead, and nothing replaces it.

Try it: Master

I delete one pod from a Deployment of three. How do I prove the promise held?

Now do it: On your own cluster, delete one pod and check the count is three again.

Go deeper

The Pod, the whole file. Do this: save it as pod.yaml and run kubectl apply -f pod.yaml. Expected output: pod/hello-pod created. This file was checked in this lab: it parses clean with yaml.safe_load, it is kind Pod, and its limits are set.

apiVersion: v1
kind: Pod
metadata:
  name: hello-pod
  labels: {app: hello}
spec:
  containers:
    - name: hello
      image: nginx:1.27
      ports: [{containerPort: 80}]
      resources:
        requests: {memory: "64Mi", cpu: "100m"}
        limits: {memory: "128Mi", cpu: "250m"}
      livenessProbe:
        httpGet: {path: /, port: 80}
        initialDelaySeconds: 5
        periodSeconds: 10
      readinessProbe:
        httpGet: {path: /, port: 80}
        initialDelaySeconds: 3
        periodSeconds: 5

The Deployment and the Service, the whole file. Do this: save it as deploy.yaml and run kubectl apply -f deploy.yaml. Expected output: deployment.apps/hello created and service/hello created. The Deployment promises 3 replicas, and the Service selects them by the label app: hello.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
spec:
  replicas: 3
  selector:
    matchLabels: {app: hello}
  template:
    metadata:
      labels: {app: hello}
    spec:
      containers:
        - name: hello
          image: nginx:1.27
          ports: [{containerPort: 80}]
---
apiVersion: v1
kind: Service
metadata:
  name: hello
spec:
  type: ClusterIP
  selector: {app: hello}
  ports: [{port: 80, targetPort: 80}]

What the probes mean: the readiness probe says when the pod may receive traffic, and the liveness probe says when the pod must be restarted. Do this: run kubectl describe pod hello-pod and read the Probes lines. Expected output: both probes list httpGet on port 80, with the delays from the file. A pod with no readiness probe receives traffic before it is ready, and the first requests fail.

kubectl describe, flag by flag. describe is the command I run when something is wrong. The events at the bottom are where the reason lives.

  • describe pod hello-pod prints the events and status of one object. The events are where the reason for a failure lives: the image that would not pull, the port that was already taken.
  • describe pod -l app=hello describes every pod that matches the label. Without the label I have to name each pod, and I may miss the one that failed.

Flags you will meet next: -n picks the namespace, and kubectl get events --sort-by=.lastTimestamp lists the newest events first.

Try it: describe a healthy pod, then describe a failing one, and read the Events block in both. The healthy one has clean pulls and starts. The failing one names the image that would not pull or the port that was taken.

Honest limit, stated plainly: this lab was verified by parsing the manifests and by reading the kubectl docs, not by running kubectl apply. There is no cluster in the machine this lab was written on, so no pod was actually started here. The commands, the outputs, and the checks are the ones the reader runs on their own machine, where minikube or k3s is running.

At 10x the pods, the shape holds and the controller keeps the promise. At 100x, the design changes: the pods spread across nodes, and the scheduling needs room. The promise never changes, though. Three replicas means three, always.

This lab practices Lesson 48 — Kubernetes Core: Pods, Deployments, and Services, the objects this lab runs on your own cluster.

One time it broke

What broke

A team ran their app as bare Pods, with no Deployment, because the app was small and the Pods were easy to make. One night a node rebooted, and the Pods on it never came back. The app was down until morning, when someone noticed and recreated the Pods by hand. The Pods had done nothing wrong. They had just died, and nothing was watching them.

Timeline

  • 02:00,a node rebooted for a patch. The Pods on it died with it.
  • 02:01,the Service had no endpoints, because no Pods were Ready.
  • 02:05,the app stopped answering. Nobody was paged, because the Pods were not in any controller.
  • 09:00,someone noticed the app was down and recreated the Pods by hand.
  • 10:00,the Pods were moved into a Deployment, so the next node reboot would be a replacement, not an outage.

What changed after

Two things. First, no Pod runs without a Deployment in production, ever. Second, the team tests the promise: they delete a Pod on purpose after every deploy, and they watch it come back. Try it now: delete one pod from the lab Deployment and watch the replacement appear.

If you remember nothing else

Build it yourself

Build a Deployment for an app you choose with 3 replicas, a Service, and probes, then break one pod and watch it come back. It passes when the Deployment reads 3/3, the Service has three endpoints, and a deleted pod is replaced on its own.

Build it, step by step

  • Write a Pod file for your app and apply it, then check it reads 1/1.
  • Write a Deployment with 3 replicas and a Service, apply both, and check the Deployment reads 3/3.
  • Port-forward the Service and curl it, then check the endpoints count is three.
  • Scale to 5 and back to 3, and check the Deployment keeps the promise at both counts.
  • Delete one pod by name and check the count is three again, with one younger pod.

What success looks like

Success looks like this: Deployment 3/3, three endpoints, curl answers, and a deleted pod is replaced with a younger one. If the endpoints list is empty, no pod is Ready. If the count stays at 2 after a delete, the pods are bare Pods, not a Deployment.

If it fails, check this

If kubectl cannot reach the cluster, check the current context. If the pod is ImagePullBackOff, read the image name in describe. If the Service has no endpoints, check the selector against the pod labels. If the port-forward fails, check the Service port: host port first, service port second.

Next in this series
Lesson 160 — Kubernetes Lab: Config, Secrets, and Storage