The pod

A pod is containers that share one address

The smallest thingKubernetes runsPod web-7d4b9-x2k4qwebnginx, port 80one address: 10.244.1.7log-agenta helper containerlocalhosta shared volume: logsreplaced, never repaired:a new pod gets a newname and a new addressA pod is one or more containers that share an address and can share files.Kubernetes does not run containers directly. It runs pods. Most pods hold one container; a fewadd a helper that must live beside it. Containers in a pod share one network namespace, so theyreach each other on localhost. Pods are disposable: never rely on one pod's name or address.

One address, shared localhost, shared files. And always replaceable.

Init containers and sidecars · Going deeper

Init containers run first, in order; a sidecar is one told to stay

apiVersion: v1kind: Podmetadata: name: webspec: initContainers: - name: wait-for-db image: busybox:1.36 command: ['sh', '-c', 'until nslookup db; do sleep 2; done'] - name: log-agent image: acme/log-agent:1.2 restartPolicy: Always containers: - name: web image: nginx:1.27run first, one at a time, in this ordermust exit with 0 before the next one startsthis line makes it a sidecar: it stays runningstarted once the init containers are doneInit containers run to completion, in order, before the main ones. A sidecar is one that is told to stay.While they run, kubectl get pods shows Init:0/2, and a failing one shows Init:CrashLoopBackOff. Read it with kubectl logs web -cwait-for-db. A sidecar (restartPolicy: Always, stable since v1.33) starts before the main containers, runs beside them and isstopped after them, so a log or proxy helper is there for the whole life of the pod. acme/log-agent is an example image.

initContainers finish before the main ones start. restartPolicy: Always makes one a sidecar.

Ownership

A Deployment makes a ReplicaSet, which makes the pods

Deployment web3 replicas of nginx:1.27you write this oneReplicaSetweb-7d4b9made by the Deployment:keeps the count at 3Podweb-7d4b9-x2k4qPodweb-7d4b9-m8n2pPodweb-7d4b9-c5t7wselector: app=webapp=webapp=webapp=webYou write a Deployment. It makes a ReplicaSet, which makes the pods.Each level does one job: the ReplicaSet keeps the right number of identical pods, and theDeployment manages changes of version. They are tied together only by labels: the ReplicaSetowns whichever pods carry the labels its selector asks for.

Deployment to ReplicaSet to pods, tied together by labels.

Rolling update

A new version replaces the old a few pods at a time

Stepnginx:1.27, old ReplicaSetnginx:1.28, new ReplicaSetPodsstart3three pods of the old version14one new pod is started first24it is ready, so one old pod goes34and againdone3the old ReplicaSet is kept, emptyA rolling update swaps pods a few at a time, so the service never stops.Change the image and the Deployment creates a second ReplicaSet. It grows the new one andshrinks the old one in turn, waiting for each new pod to report ready. If the new version neverbecomes ready, the roll-out halts with the old pods still serving. The count never drops below 3.

The new ReplicaSet grows as the old one shrinks. Never fewer than three.

Rolling update · Going deeper · 1 of 2

Lab: roll out, then roll back

TypeLook forkubectl set image deployment/web web=nginx:1.28kubectl rollout status deployment/webkubectl get replicasetskubectl rollout history deployment/webkubectl rollout undo deployment/webkubectl get pods -l app=web --show-labelsdeployment.apps/web image updatedpods replaced in turn, then "successfully rolled out"two: the new one full, the old one at 0revisions 1 and 2deployment.apps/web rolled backthe pods of the old ReplicaSet, running againLab: roll out a new version, then roll it back.Use the web Deployment from the previous lesson, made from web.yaml (the container is named web).The undo is fast because the old ReplicaSet was kept. It is simply scaled back up.

Change the image, watch the roll-out, then undo it.

maxSurge and maxUnavailable · Going deeper · 2 of 2

Surge rounds up, unavailable rounds down: the sums behind 25%

The defaults, written outspec: replicas: 3 minReadySeconds: 0 progressDeadlineSeconds: 600 revisionHistoryLimit: 10 strategy: type: RollingUpdate rollingUpdate: maxSurge: 25% maxUnavailable: 25%Replicas25% of itmaxSurge: round upmaxUnavailable: downPods30.751 extra0 missing3 to 4411 extra1 missing3 to 5102.53 extra2 missing8 to 132055 extra5 missing15 to 25minReadySeconds 0: a new pod counts as available the moment it is readyprogressDeadlineSeconds 600: ten minutes without progress marks the roll-out failedrevisionHistoryLimit 10: old ReplicaSets kept for kubectl rollout undoWith 3 replicas, 25% gives 1 extra pod and 0 missing, which is why the count never fell below 3.Surge is rounded up and unavailable is rounded down, so a small Deployment rolls one pod at a time and stays at full strength.A roll-out that passes its deadline is only reported, as ProgressDeadlineExceeded: nothing is rolled back for you. The otherstrategy, type: Recreate, stops every old pod before starting a new one, for programs that cannot run two versions at once.

With 3 replicas: 1 extra, 0 missing. The deadline is 600 seconds.

Other workloads

Choose the workload by how its pods must behave

KindIts pods on three nodesWhat it gives youDeploymentidentical copies, placed anywhereStatefulSetdb-0db-1db-2numbered pods, each with a disk of its ownDaemonSetexactly one pod on every nodeJobdoneruns to the end once, then stopsCronJob02:00starts a Job on a schedulePick the workload by how its pods must behave, then let its controller run them.A web server is a Deployment: any copy will do. A database is a StatefulSet: db-0 must come backas db-0 with its own data. A log collector is a DaemonSet: one on each node. A backup or a reportis a Job, or a CronJob if it repeats.

Deployment, StatefulSet, DaemonSet, Job, CronJob.

Jobs and CronJobs · Going deeper

A CronJob as written, with the defaults that catch people out

apiVersion: batch/v1kind: CronJobmetadata: name: reportspec: schedule: "0 2 * * *" timeZone: Europe/London concurrencyPolicy: Forbid jobTemplate: spec: backoffLimit: 6 activeDeadlineSeconds: 600 ttlSecondsAfterFinished: 3600 template: spec: restartPolicy: OnFailure containers: - name: report image: acme/report:1.0minute, hour, day, month, weekday: every day at 02:00without it: the time zone of the controller managerAllow (the default), Forbid: skip while one still runs, or Replaceretries before the Job is marked failed: 6 is the defaultstop the Job after 10 minutes, whatever happensdelete the finished Job and its pods an hour laterOnFailure or Never: a Job with Always is refusedA CronJob makes a Job at each scheduled time. A Job runs pods until enough of them succeed.A Job wants 1 completion from 1 pod at a time unless you set completions and parallelism. A CronJob keeps its last 3 successfulJobs and 1 failed one, so their logs are still there next morning. Run one now: kubectl create job test --from=cronjob/report.

Five-field schedule, backoffLimit 6, and a restartPolicy that is never Always.

Labels

Labels tag things; selectors find them

PodIts labelsSelectorsweb-1app=web env=prodweb-2app=web env=prodweb-3app=web env=devapi-1app=api env=proddb-0app=db env=prodapp=web✓✓✓3 podsenv=prod✓✓✓✓4 podsapp=web,env=prod✓✓2 podsLabels are free-form tags. A selector is a question asked of them.Nothing in Kubernetes is wired to anything else by name. A ReplicaSet, a Service and kubectl allfind pods by asking for labels, and the answer is worked out again every time. Relabel a pod andit leaves one group and joins another without being restarted.

A selector is a question. The labels give the answer.

Lesson complete

You finished “Pods and Deployments”

Nicely done.

Open in a new tab