Init:CrashLoopBackOff: an init container keeps failing

Reproduced on k3s v1.31.5 (k3d) with kubectl v1.36.1.

What you see

$ kubectl get pod init-demo -n lab
NAME        READY   STATUS                  RESTARTS      AGE
init-demo   0/1     Init:CrashLoopBackOff   4 (54s ago)   2m35s

$ kubectl logs init-demo -n lab -c wait-for-db
waiting for db:5432
nc: bad address 'db'
db not reachable

$ kubectl describe pod init-demo -n lab
Init Containers:
  wait-for-db:
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
    Restart Count:  4
Containers:
  app:
    State:          Waiting
      Reason:       PodInitializing

What it means

Init containers run one at a time, in order. Each must exit 0 before the next one starts. The app containers start only after all of them succeed.

When an init container fails and the pod's restartPolicy is Always or OnFailure, the kubelet restarts it with the same back-off as any crashing container. The status shows Init:Error just after a failure and Init:CrashLoopBackOff while waiting. With restartPolicy Never, the whole pod fails instead.

Plain kubectl logs <pod> shows the app container, which has not started. You must name the init container with -c to see the real error.

Common causes

Wait-for-dependency script gives up

The init container checks that a database or Service is reachable and exits non-zero if not. The Service may not exist, have no endpoints or not resolve in DNS.

How to tell: Logs show bad address, connection refused or timeout for the dependency's host and port.

Database migration fails

An init container runs schema migrations and one of them fails, for example on a lock or a bad script.

How to tell: Logs show a migration or SQL error with the migration's name.

Permission setup fails

An init container runs chown or chmod on a volume but runs as non-root or on a read-only mount.

How to tell: Logs show operation not permitted or read-only file system.

Missing config or credentials

The init container needs a token, URL or file to fetch secrets or config. It is not there.

How to tell: Logs show a missing variable, 401 or 403 from the remote service, or file not found.

How to diagnose it

  1. kubectl get pod <pod> -n <namespace>

    Init:N/M shows how many init containers finished. Init:CrashLoopBackOff or Init:Error shows one is failing.

  2. kubectl get pod <pod> -n <namespace> -o jsonpath='{range .status.initContainerStatuses[*]}{.name}{"\t"}{.lastState.terminated.exitCode}{"\n"}{end}'

    Shows each init container and its last exit code, so you know which one to read.

  3. kubectl logs <pod> -n <namespace> -c <init-container>

    The error from the failing init container. Add --previous if it has already restarted and the current run has no output yet.

  4. kubectl describe pod <pod> -n <namespace>

    Read the Init Containers section for the command, mounts and Last State of each one.

  5. kubectl run dns-test -n <namespace> --rm -it --image=busybox:1.36 --restart=Never -- nslookup <service>

    For wait-for scripts, check the dependency's name resolves from the same namespace. Then check it has endpoints with kubectl get endpointslices -n <namespace>.

How to fix it

Fix or create the dependency

Create the missing Service, fix its selector so it has endpoints or correct the host name in the init script.

Trade-off: If the dependency is outside your control, the pod keeps failing until it is back. Consider whether the pod should depend on it at startup at all.

Wait with a timeout instead of failing fast

Loop and retry in the init container for a set time before exiting non-zero.

initContainers:
- name: wait-for-db
  image: busybox:1.36
  command: ["sh", "-c", "for i in $(seq 1 30); do nc -z -w 2 db 5432 && exit 0; echo waiting for db; sleep 2; done; exit 1"]

Trade-off: The pod sits in Init:0/1 longer, which can hide a real outage. In our lab it showed Init:0/1 with no restarts while it waited. Keep the timeout short and log each attempt.

Move migrations to a Job

Run schema migrations once as a Job before the rollout instead of in every pod's init container.

Trade-off: You now need to order the Job and the Deployment in your pipeline. Many replicas no longer race to migrate.

Use fsGroup for volume permissions

Set securityContext.fsGroup so Kubernetes sets group ownership on the volume, instead of a chown init container.

Trade-off: On large volumes the ownership change can slow pod start. fsGroupChangePolicy: OnRootMismatch reduces that.

How to stop it coming back

  • Give every init container a clear log line before it exits non-zero, naming what it was waiting for.
  • Keep init containers small and focused. Heavy one-time work belongs in a Job.
  • Include init containers in your log collection. They are easy to miss.

How KubeGraf investigates it

Specialist agents pull the logs, events, metrics, workloads and recent deploys for the failing workload into one view. How KubeGraf handles Kubernetes incidents.

Frequently asked questions

Why is my init container failing?

Common reasons are a wait script that gives up on a dependency, a failed database migration, a permission step that is not allowed or missing config or credentials. The init container's own logs show which.

How do I see the logs of an init container?

Run kubectl logs <pod> -c <init-container> -n <namespace>. Plain kubectl logs shows the app container, which has not started yet. Add --previous to see the run before the last restart.

What is the difference between Init:Error and Init:CrashLoopBackOff?

Init:Error shows just after an init container fails. Init:CrashLoopBackOff shows while the kubelet waits before restarting it. With restartPolicy Never the whole pod fails instead.

Last updated . Try KubeGraf Freemium.

Related

Kubernetes documentation