Module 7 · Project 07 · Run the App on Kubernetes · Lesson 24 of 48

Pods, Deployments and the control loop

What Kubernetes does, why it keeps fixing things on its own, and the three objects every app starts with: Pods, Deployments and Services.

In this lesson · about 30 minutes

  • Explain desired state and the controller loop
  • Describe Pods, ReplicaSets, Deployments and Services
  • Read a Deployment and a Service manifest

From one server to a fleet

Docker Compose (Project 03) runs containers on one machine. When that machine dies, so does your app. Kubernetes (often written K8s) runs containers across a cluster of machines called nodes, and keeps them running when containers crash, nodes fail or traffic grows.

The key idea is desired state. You don't tell Kubernetes "start three containers". You write "there should be three copies of this app", store it in the cluster, and controllers watch the real state and keep nudging it towards what you asked for. A Pod dies, so the count is two, which is wrong, so a controller starts another. This never-ending check-and-fix is the control loop (or reconcile loop).

The building blocks

  • Pod: the smallest unit, one or more containers that share a network address. Pods are disposable: when one dies, it's replaced by a new one with a new IP.
  • ReplicaSet: keeps N identical Pods running.
  • Deployment: manages ReplicaSets, so you can roll out a new version gradually and roll back. This is what you write for a normal app.
  • Service: one stable name and IP in front of a changing set of Pods, picked by labels. Other apps talk to the Service, never to a Pod directly.
  • Node: a machine (VM) that runs Pods. The control plane (API server, scheduler, controllers, etcd database) decides what runs where.
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: notes-api
  namespace: notes
spec:
  replicas: 3
  selector:
    matchLabels: { app: notes-api }
  template:                       # the Pod to stamp out
    metadata:
      labels: { app: notes-api }
    spec:
      containers:
        - name: api
          image: ghcr.io/you/notes-api:1.4.0
          ports:
            - containerPort: 3000
---
# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: notes-api
  namespace: notes
spec:
  selector: { app: notes-api }    # every Pod with this label
  ports:
    - port: 80
      targetPort: 3000

Apply both with kubectl apply -f .. Inside the cluster, other Pods now reach the API at http://notes-api, whichever Pods happen to be behind it.

Try it · watch it run

A Kubernetes Deployment that heals itself

This is a live cluster in miniature. Change the replica count, delete a Pod, crash a whole node and send traffic. Watch the controller put things back, and see what the readiness probe changes.

Service notes:80→ load-balances to 0 ready pods (of 3 wanted)

node-1

node-2

node-3

kubectl get events -w
$ kubectl apply -f deployment.yaml -f service.yaml
deployment.apps/notes created
service/notes created
You never start pods yourself. You declare replicas: 3, and the Deployment's controller keeps comparing what you want with what exists, fixing any difference: a deleted pod or a crashed node is replaced within seconds. With the readiness probe on, the Service only sends traffic to pods that have said they are ready.

Whiteboard · watch and try

Draw the control loop

Press Play. The tutor shows why Kubernetes heals itself.

4 steps
Press Play and the tutor will talk you through it while writing on the board. Pause any time to write on the board yourself.

Tip: press Pause, then use the pen to work along on the board. Space plays and pauses; Ctrl+Z undoes your last mark.

Check your understanding0 of 3 correct

Question 1

1.A Pod in a 3-replica Deployment is deleted. What happens?

Question 2

2.Why should other apps call a Service instead of a Pod's IP?

Question 3

3.How does a Service know which Pods to send traffic to?

Key terms

Say the meaning to yourself, then tap a card to check.

Quiz score: 0 of 3

Tip: answer every quiz question correctly first.

Written by Noviq Academy in its own words. The labs are simulations: no real servers are touched. When you build the projects yourself, use a practice virtual machine or a free-tier cloud account you control, watch your cloud spending, and never run cleanup or delete commands on a machine you care about. Tool names belong to their owners; versions and output are shown for learning and may differ slightly from yours. Read-aloud and the whiteboard voice use your device's built-in voices.