Kubernetes cookbook · Beginner
Wait for dependencies at startup without init container hacks
Pods crash-loop while a database or another service is still starting.
Approach
Make the app retry its connections with backoff, report not-ready until dependencies are reachable, and use a
startup probe so slow starts aren't killed. Init containers that loop on nc only help at first start.
Solution
apiVersion: apps/v1
kind: Deployment
metadata: {name: api}
spec:
selector: {matchLabels: {app: api}}
template:
metadata: {labels: {app: api}}
spec:
containers:
- name: api
image: ghcr.io/acme/api:1.4.2
startupProbe:
httpGet: {path: /ready, port: 8080}
periodSeconds: 2
failureThreshold: 60
readinessProbe:
httpGet: {path: /ready, port: 8080}
periodSeconds: 5
Pitfalls
- Liveness probes that fail while dependencies are down, restarting healthy pods.
- Crashing on the first failed connection instead of retrying.
Variations
- Native sidecar containers (
initContainerswithrestartPolicy: Always) for proxies that must start first. - Service meshes' hold-application-until-proxy-starts options.
Further reading: Configure probes
More Kubernetes recipes
- Autoscale a Deployment on CPU and memory
- Debug a distroless pod with an ephemeral container
- Default-deny network policies that keep DNS working
- Enforce the restricted Pod Security Standard
- Expose a service over HTTPS with automatic certificates
- Give a CI pipeline least-privilege deploy access
- Handle SIGTERM in your application for Kubernetes
- Manage dev, staging and prod with Kustomize overlays