Reference

Docker & Kubernetes

Read image builds, container commands, Compose files and Kubernetes workload configuration.

Scope and conventions

Docker CLI/Dockerfile, Docker Compose and Kubernetes native YAML/kubectl. These are complementary tools, not interchangeable editions. Commands assume your own disposable local environment; image names, cluster objects and paths are illustrative. Nothing runs from this guide.

Health & rollout

Readiness, liveness and startup answer different questions

Docker & Kubernetes

Readiness controls whether a Pod should receive Service traffic. Liveness failures can restart a container. A startup probe delays liveness/readiness probing until startup succeeds.

Fragment

Docker & Kubernetes · Readiness, liveness and startup answer different questions
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
readinessProbe:
  httpGet:
    path: /ready
    port: 8080
  periodSeconds: 5
livenessProbe:
  httpGet:
    path: /live
    port: 8080
  periodSeconds: 10

These are container-spec fragments. Avoid restarting an otherwise healthy app solely because a shared dependency is unavailable.

Inspect a rollout before declaring completion

Docker & Kubernetes

A Deployment updates Pods toward its template. Rollout status observes progress; available replicas and application behavior remain separate evidence.

Fragment

Docker & Kubernetes · Inspect a rollout before declaring completion
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
kubectl rollout status deployment/demo -n demo
kubectl rollout history deployment/demo -n demo

A rollback changes workload configuration; it does not automatically reverse database migrations or external effects.

Leave time to drain and stop

Docker & Kubernetes

Pod termination has a grace period. Containers should stop accepting new work, finish or cancel in-flight work and exit within it.

Fragment

Docker & Kubernetes · Leave time to drain and stop
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
# Pod-spec fragment:
terminationGracePeriodSeconds: 30

preStop hooks consume the same grace-period budget. Endpoint changes and process termination are not one instantaneous event.

Roll back, restart, pause and resume a Deployment

Docker & Kubernetes

Each change to a Deployment’s Pod template creates a new ReplicaSet, which is a numbered revision. Scaling does not. rollout history lists the revisions that are still kept (revisionHistoryLimit, default 10), and --revision=N shows that revision’s template. rollout undo returns to the previous revision, or to a chosen one with --to-revision. It does this by rolling out the old template again as a new revision number. rollout restart replaces every Pod with the same template, following the Deployment’s strategy: it stamps a kubectl.kubernetes.io/restartedAt annotation on the Pod template, which creates a new revision. Use it to pick up a changed ConfigMap or Secret that Pods read only at startup (environment variables or files read once). rollout pause stops the controller from acting on template changes, so several edits (image, env, resources) can be applied and then released as one rollout with rollout resume. In practice: record why each revision exists with the kubernetes.io/change-cause annotation, check rollout status after an undo, and then fix the manifest in source control, or the next apply will reintroduce the bad version.

Fragment

Docker & Kubernetes · Roll back, restart, pause and resume a Deployment
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
kubectl rollout history deployment/demo -n demo
kubectl rollout history deployment/demo -n demo --revision=3

# Back to the previous revision, or to a specific one
kubectl rollout undo deployment/demo -n demo
kubectl rollout undo deployment/demo -n demo --to-revision=2
kubectl rollout status deployment/demo -n demo --timeout=5m

# Replace every Pod with the same template (rolling, follows the strategy)
kubectl rollout restart deployment/demo -n demo

# Batch several template changes into one rollout
kubectl rollout pause deployment/demo -n demo
kubectl set image deployment/demo app=registry.example.test/demo:1.4.3 -n demo
kubectl set resources deployment/demo -c app --limits=memory=512Mi -n demo
kubectl rollout resume deployment/demo -n demo

# Fill the CHANGE-CAUSE column of rollout history
kubectl annotate deployment/demo -n demo \
  kubernetes.io/change-cause="demo 1.4.3: fix cache headers" --overwrite

A paused Deployment cannot be rolled back until it is resumed.

Undo restores the Pod template only. It does not reverse database migrations, ConfigMap or Secret contents referenced by name, or other external effects.

The --record flag is deprecated. Set the change-cause annotation instead.

Choose a rollout strategy and its limits

Docker & Kubernetes

RollingUpdate, the default, replaces Pods gradually. maxSurge is how many Pods may exist above replicas during the update, and maxUnavailable is how many may be missing below it. Both default to 25% and accept a count or a percentage, and they cannot both be 0. maxSurge: 1 with maxUnavailable: 0 keeps full capacity but needs room for one extra Pod. maxUnavailable above 0 is faster and frees capacity, at the cost of fewer serving Pods. Recreate stops every old Pod before starting new ones. That means downtime, but during an upgrade old and new versions do not overlap, which matters for schema-incompatible versions. It is not an at-most-one guarantee: a Pod deleted by hand is replaced immediately, even while the old one is still terminating. Use a StatefulSet or leader election when two copies must never run. minReadySeconds (default 0) is how long a new Pod must stay Ready before it counts as available. progressDeadlineSeconds (default 600) is how long the rollout may go without progress before its Progressing condition reports ProgressDeadlineExceeded. In practice: pair RollingUpdate with a real readinessProbe, set minReadySeconds to catch Pods that crash shortly after starting, and treat a deadline failure as a signal to roll back.

Fragment

Docker & Kubernetes · Choose a rollout strategy and its limits
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo
  namespace: demo
spec:
  replicas: 4
  minReadySeconds: 10
  progressDeadlineSeconds: 300
  revisionHistoryLimit: 5
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: demo
  template:
    metadata:
      labels:
        app: demo
    spec:
      containers:
        - name: app
          image: registry.example.test/demo:1.4.2
          readinessProbe:
            httpGet:
              path: /ready
              port: 8080
            periodSeconds: 5
---
# Versions must not overlap during an upgrade: stop old, then start new.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-worker
  namespace: demo
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: demo-worker
  template:
    metadata:
      labels:
        app: demo-worker
    spec:
      containers:
        - name: worker
          image: registry.example.test/demo-worker:1.4.2

Kubernetes does not roll back automatically when the progress deadline passes. It only sets the condition, and kubectl rollout status exits non-zero, so CI or an operator must react.

Percentages are rounded: maxSurge rounds up and maxUnavailable rounds down. With 4 replicas, 25% means 1 Pod each way.

Recreate is not a substitute for a migration plan: it removes overlap but still causes a gap in service.

Limit voluntary disruptions with a PodDisruptionBudget

Docker & Kubernetes

A PodDisruptionBudget (policy/v1) limits how many of the Pods matching its selector may be down at once because of voluntary disruptions. A PDB governs only the voluntary disruptions that go through the Eviction API, such as kubectl drain, node upgrades and cluster autoscalers; deleting a Pod or a Deployment directly, or changing the Pod template, is also voluntary but bypasses it. Set exactly one of minAvailable or maxUnavailable, as a count or a percentage. An eviction that would break the budget is refused (HTTP 429), and kubectl drain keeps retrying it until the budget allows it or --timeout expires. A PDB does not protect against involuntary disruptions such as a node crash or an out-of-memory kill, and it is not consulted when someone deletes Pods or a Deployment directly. Rolling updates are governed by the Deployment strategy, not the PDB. In practice: give each replicated workload a PDB that still allows at least one eviction, so node maintenance can proceed. Run enough replicas, spread across nodes, for the budget to mean something.

Fragment

Docker & Kubernetes · Limit voluntary disruptions with a PodDisruptionBudget
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: demo
  namespace: demo
spec:
  minAvailable: 3          # or maxUnavailable: 1 (set only one)
  selector:
    matchLabels:
      app: demo
  unhealthyPodEvictionPolicy: AlwaysAllow

# Check the budget, then drain a node for maintenance:
#   kubectl get pdb -n demo
#   (ALLOWED DISRUPTIONS must be above 0 for a drain to make progress)
#   kubectl cordon NODE_NAME
#   kubectl drain NODE_NAME --ignore-daemonsets --delete-emptydir-data --timeout=10m
#   kubectl uncordon NODE_NAME

With minAvailable equal to replicas (or maxUnavailable: 0), ALLOWED DISRUPTIONS is 0 and every drain blocks. That is a valid choice for a singleton only if someone deletes the PDB deliberately during maintenance.

Percentages round up: minAvailable "50%" of 7 Pods keeps 4. A maxUnavailable percentage can allow more disruption than intended for small replica counts.

unhealthyPodEvictionPolicy: AlwaysAllow (stable since Kubernetes 1.31) lets drains evict Pods that are running but not Ready, so a broken rollout cannot block node maintenance. The default, IfHealthyBudget, only evicts them while the budget is met.

--delete-emptydir-data discards emptyDir contents on the drained Pods. Drain only nodes in your own disposable cluster when practising.