Everyone Can Learn Program
中文 Start learning
Specialty · DevOpsLesson 5 of 5Free to read

Kubernetes basics · DevOps: Containers, CI and Kubernetes

How do you keep many copies of an app running, and send traffic to them?

Course contents

Why you need it

Production apps run as several copies on several machines, so one crash or one deploy doesn't take the site down. Kubernetes is the most widely used system for this: you describe what you want (three copies of this image, reachable on port 80), and it keeps reality matching that description, restarting and moving containers as needed.

The intuition

Kubernetes objects are YAML. Two you'll meet everywhere:

  • A Deployment keeps N copies (pods) of a container running. Its pod template has labels, like app: shop.
  • A Service gives those pods one stable address and spreads traffic across them. It finds its pods with a selector, which must match the pods' labels exactly.
client → Service (port 80) --selector app: shop--> pods labelled app: shop (containerPort 8000)

Three things must line up, and they're the most common cause of "the site doesn't respond":

  1. Deployment selector.matchLabels = pod template labels
  2. Service selector = pod labels
  3. Service targetPort = the container's containerPort

Two more settings make it production-ready: a readinessProbe (send traffic only once the app answers its health check) and resources (how much CPU and memory each pod asks for).

kubectl apply -f app.yaml sends the file to the cluster; Kubernetes does the rest.

Definition

Pod

The smallest thing Kubernetes runs: one or more containers sharing a network address.

Deployment

Keeps a set number of identical pods running and replaces them gradually on updates.

Service

A stable address that load-balances across the pods its selector matches.

Label selector

key: value pairs an object uses to find other objects with the same labels.

Examples

A Deployment and its Service

Run it in the lesson

Three pods of the shop, reachable on port 80. Every link lines up.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: shop
spec:
  replicas: 3
  selector:
    matchLabels:
      app: shop
  template:
    metadata:
      labels:
        app: shop
    spec:
      containers:
        - name: shop
          image: shop:1.4
          ports:
            - containerPort: 8000
          readinessProbe:
            httpGet:
              path: /health
              port: 8000
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
---
apiVersion: v1
kind: Service
metadata:
  name: shop
spec:
  selector:
    app: shop
  ports:
    - port: 80
      targetPort: 8000
Step by step
  1. L6Keep three pods running; if one dies, a new one replaces it.
  2. L8The Deployment manages pods with the label app: shop…
  3. L12…and its template gives every new pod that label.
  4. L20No traffic until GET /health succeeds.
  5. L26Ask for a tenth of a CPU core and 128 MiB of memory.
  6. L35The Service sends traffic to pods with this label.
  7. L38…on the port the container listens on.

Common mistakes

Selector and labels don't match

A Service whose selector matches no pods accepts connections and sends them nowhere. Copy the labels exactly, including spelling.

targetPort vs port

port is where the Service listens; targetPort must be the container's port. Mixing them up gives "connection refused".

No readiness probe

Without it, pods get traffic the moment they start, before the app is ready, so every deploy causes errors.

Quick quiz

1. How does a Service find its pods?

  1. AIts selector matches the pods' labels
  2. BBy the Deployment's name
  3. CBy the image name
Show the answer

Answer: A

Labels and selectors are how Kubernetes objects find each other.

2. The container listens on 3000. The Service has `port: 80`. What must `targetPort` be?

  1. A3000
  2. B80
  3. CAnything; it's ignored
Show the answer

Answer: A

targetPort is where the Service forwards to: the container's port.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictDoes traffic arrive?

    Will requests to the shop Service reach the pods?

  2. ModifyFix the broken Service

    The web Service refuses connections, and there's only one pod, so every deploy causes downtime. Make the Service forward to the port the container really listens on, and run 3 replicas.

  3. WriteDeploy an API

    Write a Deployment and a Service, both named api:

    • 3 replicas of image shop-api:1.4, pods labelled app: api, container listening on 8080
    • a readiness probe: HTTP GET /health on port 8080
    • resource requests: cpu: 100m, memory: 128Mi
    • the Service selects app: api, listens on port 80 and forwards to 8080

    Separate the two objects with a line containing ---.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
KubernetesKubernetesA system that runs and heals containers across many machines.
PodPodOne or more containers that Kubernetes runs together.
Service (Kubernetes)Service(Kubernetes)A stable address for a set of pods, found by labels.
Readiness probe就绪探针A health check that decides when a pod gets traffic.