Kubernetes basics · DevOps: Containers, CI and Kubernetes
How do you keep many copies of an app running, and send traffic to them?
Course contentsWhy 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":
- Deployment
selector.matchLabels= pod templatelabels - Service
selector= podlabels - Service
targetPort= the container'scontainerPort
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 lessonThree 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- L6Keep three pods running; if one dies, a new one replaces it.
- L8The Deployment manages pods with the label app: shop…
- L12…and its template gives every new pod that label.
- L20No traffic until GET /health succeeds.
- L26Ask for a tenth of a CPU core and 128 MiB of memory.
- L35The Service sends traffic to pods with this label.
- 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?
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?
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.
- PredictDoes traffic arrive?
Will requests to the
shopService reach the pods? - ModifyFix the broken Service
The
webService 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. - WriteDeploy an API
Write a Deployment and a Service, both named
api:- 3 replicas of image
shop-api:1.4, pods labelledapp: api, container listening on8080 - a readiness probe: HTTP GET
/healthon port8080 - resource requests:
cpu: 100m,memory: 128Mi - the Service selects
app: api, listens on port80and forwards to8080
Separate the two objects with a line containing
---. - 3 replicas of image
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
| Term | Chinese | Definition |
|---|---|---|
| Kubernetes | Kubernetes | A system that runs and heals containers across many machines. |
| Pod | Pod | One 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. |