Operating, scaling and debugging · 6 MIN READ
A production checklist for Deployments, applied to gopherforge/web:v2 in the lab
Review a Deployment against a production checklist and write one that pins its image, sizes its resources, probes its health, survives disruptions, runs with least privilege and shuts down cleanly.
Most Kubernetes outages are not exotic: an image tag that moved, a pod without requests evicted first under memory pressure, a rollout that sent traffic to pods that were not ready, a token with more rights than the app needed. A checklist turns those incidents into review items that every Deployment passes before it ships.
kubernetes · A MAP OF THE IDEA
- Pin image, set requests and probes
- Spread replicas, add a PDB
- Non-root, read-only, no token
- Roll out, load test, watch
Useful when
- Each item prevents a failure seen in real outages.
- The checklist can be reviewed line by line in a pull request.
Watch out
- Hard spread rules can leave pods Pending on small clusters.
- readOnlyRootFilesystem breaks apps that write outside mounted volumes.
Implementation & operation cost: A longer manifest, spare capacity for surge and spread, and a review step for every new service.
Image, resources and probes
Pin the image to a version tag and, where your registry and tooling allow, to a digest: gopherforge/web:v2@sha256:... cannot change under you, and latest makes rollbacks meaningless. Give every container CPU and memory requests: the scheduler places pods by them, the HPA divides by them, and pods without them are evicted first. Set a memory limit so that a leak ends in a visible OOMKilled instead of a starving node; many teams leave CPU limits off because throttling hurts latency. In Go, set GOMEMLIMIT to about 90% of the memory limit; since Go 1.25, GOMAXPROCS follows a CPU limit automatically. Probe with intent: a startupProbe for slow starts, a readinessProbe that checks the process can serve, and a livenessProbe only for deadlocks. Never let liveness check a database: an outage there would restart every pod.
Availability: replicas, spread, budgets and rollouts
Run at least two replicas, three if one may be down during maintenance, and spread them with topologySpreadConstraints across nodes and, on real clusters, zones. Add a PodDisruptionBudget with maxUnavailable: 1 so that drains and upgrades take one pod at a time. Roll out with maxSurge: 1 and maxUnavailable: 0: capacity never drops, and a version that never becomes ready stalls while the old pods keep serving. progressDeadlineSeconds turns a stall into a ProgressDeadlineExceeded condition that CI can fail on, and revisionHistoryLimit keeps old ReplicaSets to roll back to. Shut down gracefully with a preStop sleep of a few seconds while endpoints update, SIGTERM handling in the code, and a terminationGracePeriodSeconds longer than both. Add an HPA when load varies, and then drop replicas from what CI applies.
Least privilege: securityContext, ServiceAccount, NetworkPolicy
runAsNonRoot makes the kubelet refuse to start a container as UID 0: an image whose user is root, with no runAsUser set, fails with CreateContainerConfigError instead of running as root; a numeric runAsUser picks the UID explicitly. Set readOnlyRootFilesystem and mount an emptyDir where the app writes, disable allowPrivilegeEscalation, drop ALL capabilities and use the RuntimeDefault seccomp profile: this meets the Pod Security Standards restricted profile, which a namespace enforces with the label pod-security.kubernetes.io/enforce: restricted. Give the app its own ServiceAccount with automountServiceAccountToken: false unless it calls the API, and then a Role with only the verbs it needs. Add a default-deny NetworkPolicy per namespace and allow what each app needs; policies take effect only with a network plugin that enforces them, such as Calico or Cilium.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-ingress
spec:
podSelector: {}
policyTypes:
- Ingress
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: web-allow-http
spec:
podSelector:
matchLabels:
app.kubernetes.io/name: web
policyTypes:
- Ingress
ingress:
- ports:
- protocol: TCP
port: 8080The first policy selects every pod in the namespace and allows no ingress. The second allows TCP 8080 to the web pods from any source, as an Ingress controller in another namespace needs; add a from list with namespaceSelector or podSelector when only known clients may call. Egress stays open until a policy lists Egress in policyTypes, and from then on DNS to kube-dns must be allowed explicitly.
Namespace guardrails, observability and backups
A ResourceQuota per namespace caps total requests, limits and object counts, so one team's runaway autoscaler cannot take the whole cluster; a LimitRange supplies default requests for containers that forget them. The lab terminal applies LimitRange defaults and enforces ResourceQuota for pods, CPU and memory. Observe every service the same way: structured JSON logs on stdout, for example with log/slog; rate, error and duration metrics exposed for Prometheus; traces through OpenTelemetry with trace IDs in the logs; and alerts on symptoms users feel rather than on CPU. Back up what Git cannot recreate: etcd snapshots with etcdctl snapshot save on self-managed control planes, and volumes plus objects created at runtime with a tool such as Velero. A backup that has never been restored is a hope, so rehearse the restore.
The complete Deployment
The manifest applies every item of the checklist to gopherforge/web:v2. In production you would also pin the digest your registry reports for v2, so that the tag cannot move, and add an HPA if the load varies. The lab file below adds the ServiceAccount the pods run as, the Service in front of them and the PodDisruptionBudget that paces drains.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
labels:
app.kubernetes.io/name: web
spec:
replicas: 3
revisionHistoryLimit: 5
progressDeadlineSeconds: 300
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app.kubernetes.io/name: web
template:
metadata:
labels:
app.kubernetes.io/name: web
spec:
serviceAccountName: web
automountServiceAccountToken: false
terminationGracePeriodSeconds: 30
securityContext:
runAsNonRoot: true
runAsUser: 10001
runAsGroup: 10001
seccompProfile:
type: RuntimeDefault
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
nodeTaintsPolicy: Honor
matchLabelKeys:
- pod-template-hash
labelSelector:
matchLabels:
app.kubernetes.io/name: web
containers:
- name: web
image: gopherforge/web:v2
ports:
- name: http
containerPort: 8080
env:
- name: GOMEMLIMIT
value: 115MiB
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
memory: 128Mi
startupProbe:
httpGet:
path: /healthz
port: http
periodSeconds: 2
failureThreshold: 30
readinessProbe:
httpGet:
path: /readyz
port: http
periodSeconds: 5
livenessProbe:
httpGet:
path: /healthz
port: http
periodSeconds: 10
lifecycle:
preStop:
sleep:
seconds: 5
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
volumeMounts:
- name: tmp
mountPath: /tmp
volumes:
- name: tmp
emptyDir:
sizeLimit: 64MiThree replicas at most one apart per node, with tainted nodes left out of the count; a surge-only rollout with a five-minute progress deadline; requests of 100m and 64Mi, a 128Mi limit and GOMEMLIMIT at 115MiB; a startup probe that allows 60 seconds, readiness on /readyz and liveness on /healthz; a 5-second preStop sleep inside a 30-second grace period; and a non-root, read-only container without capabilities or a ServiceAccount token, writing only to a size-limited /tmp.
Try it in the lab terminal
Start three workers so that the spread rule has room, create a namespace and make it the context's default, apply the full file with the ServiceAccount, Deployment, Service and PodDisruptionBudget, and put the service under load. Then ship a broken release, watch the readiness probe and the rollout strategy keep the service up, roll back, and check the ServiceAccount's rights.
cluster create --workers 3
kubectl create namespace shop
kubectl config set-context --current --namespace=shop
cat > web.yaml <<'EOF'
apiVersion: v1
kind: ServiceAccount
metadata:
name: web
automountServiceAccountToken: false
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
labels:
app.kubernetes.io/name: web
spec:
replicas: 3
revisionHistoryLimit: 5
progressDeadlineSeconds: 300
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app.kubernetes.io/name: web
template:
metadata:
labels:
app.kubernetes.io/name: web
spec:
serviceAccountName: web
automountServiceAccountToken: false
terminationGracePeriodSeconds: 30
securityContext:
runAsNonRoot: true
runAsUser: 10001
runAsGroup: 10001
seccompProfile:
type: RuntimeDefault
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
nodeTaintsPolicy: Honor
matchLabelKeys:
- pod-template-hash
labelSelector:
matchLabels:
app.kubernetes.io/name: web
containers:
- name: web
image: gopherforge/web:v2
ports:
- name: http
containerPort: 8080
env:
- name: GOMEMLIMIT
value: 115MiB
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
memory: 128Mi
startupProbe:
httpGet:
path: /healthz
port: http
periodSeconds: 2
failureThreshold: 30
readinessProbe:
httpGet:
path: /readyz
port: http
periodSeconds: 5
livenessProbe:
httpGet:
path: /healthz
port: http
periodSeconds: 10
lifecycle:
preStop:
sleep:
seconds: 5
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
volumeMounts:
- name: tmp
mountPath: /tmp
volumes:
- name: tmp
emptyDir:
sizeLimit: 64Mi
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app.kubernetes.io/name: web
ports:
- name: http
port: 80
targetPort: http
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web
spec:
maxUnavailable: 1
selector:
matchLabels:
app.kubernetes.io/name: web
EOF
kubectl apply -f web.yaml
kubectl rollout status deploy/web
kubectl get pods -o wide
traffic run svc/web --rps 200 --duration 30s
kubectl set image deploy/web web=gopherforge/web:v2-broken
traffic run svc/web --rps 200 --duration 30s
kubectl rollout status deploy/web --timeout=10s
kubectl rollout undo deploy/web
kubectl rollout status deploy/web
kubectl auth can-i list secrets --as=system:serviceaccount:shop:webThe apply creates four objects, and get pods -o wide shows one pod each on kind-worker, kind-worker2 and kind-worker3. The first traffic run ends with Total 6000 requests, 100.0% ok, about a third per pod. With v2-broken the surge pod never passes /readyz, so the timeline shows READY PODS 3/4 and still 100.0% ok: maxUnavailable: 0 kept every old pod. rollout status --timeout=10s fails with "1 out of 3 new replicas have been updated", rollout undo restores v2, and auth can-i answers no.
TAKE THIS WITH YOU
A production Deployment pins its image, declares requests and a memory limit, probes with intent, spreads two or more replicas under a PodDisruptionBudget, rolls out without losing capacity, runs as non-root without a token, and shuts down within its grace period.
Check your understanding
The new image of your Deployment never passes its readiness probe. With which settings does the rollout stall while every old pod keeps serving traffic?
Go to the source
Progress stays in this browser. No account needed.