Kubernetes on Pods and workloads
A Pod is the smallest unit you can run
- A Pod is one or more containers that share an IP address and storage volumes.
- Most Pods hold one container. Containers in the same Pod reach each other on localhost.
- Pods are disposable. They get replaced, not repaired.
A workload object keeps your Pods alive
- You rarely create Pods yourself. A workload object holds a Pod template and manages them.
- Pick the workload by how the app runs: forever, once, on every node, or with identity.
| Workload | What it runs | Use it for |
|---|---|---|
| Deployment | N identical Pods | Web apps and APIs |
| StatefulSet | Pods named db-0, db-1, each with a disk | Databases and queues |
| DaemonSet | One Pod on every node | Log and metric agents |
| Job | Pods that run until they succeed | Migrations, batch work |
| CronJob | A Job on a schedule | Nightly reports, backups |
A Deployment rolls out changes gradually
- Change the image, and it starts new Pods while it stops old ones, a few at a time.
- By default at most 25% of Pods are unavailable and at most 25% extra exist in a rollout.
- “kubectl rollout undo deployment/web” goes back to the previous version.
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: example/web:1.4}]Probes tell Kubernetes how the app is doing
- Readiness: can it take traffic? If not, the Pod is taken out of its Services.
- Liveness: is it stuck? If the probe keeps failing, the kubelet restarts the container.
- Startup: has it finished starting? The other two probes wait until it passes.
- Keep liveness checks cheap. One that fails under load restarts Pods when you need them.
Init and sidecar containers help the main one
- Init containers run to completion, in order, before the app starts.
- A sidecar runs beside the app for the Pod's whole life, such as a log shipper.
- Declare a sidecar as an init container with restartPolicy: Always. Stable since v1.33.
- Sidecars start before the app, stop after it, and do not keep a Job from finishing.
A stopping Pod gets time to finish
- Kubernetes sends SIGTERM first, then waits 30 seconds by default before it sends SIGKILL.
- On SIGTERM, the app should finish its open requests and then exit.