Medium java spring

A Kubernetes pod keeps failing. How do you troubleshoot it?

The 30 second answer

First I read the pod status, because it tells me which stage broke. Pending means it was never scheduled, so I check resources and node constraints. ImagePullBackOff means the image or registry credentials are wrong. CrashLoopBackOff means the app starts and dies, so I read the previous container's logs and the exit code; 137 with OOMKilled means the memory limit. Then I check probes, config and recent changes.

Could you say this in an interview?

Start from the status, not from the logs. kubectl get pod and kubectl describe pod <name> show the phase, each container's state, the restart count, the last termination reason and exit code, and the events at the bottom. The events usually name the cause directly: a scheduling failure, a pull error or a failed probe. kubectl get events --sort-by=.lastTimestamp gives the same events across the namespace.

Pending: the pod was never scheduled. Per the Kubernetes debugging guide, a pod stuck in Pending cannot be placed on a node, usually because there are not enough resources of one type or another. I check the requested CPU and memory against free node capacity, and then node selectors, affinity rules, taints without matching tolerations, and PersistentVolumeClaims that are not bound.

ImagePullBackOff: the image never arrived. The cause is a wrong image name or tag, a private registry without the right imagePullSecret, or a registry that cannot be reached. Kubernetes keeps retrying with a growing delay, capped at 300 seconds (5 minutes), so the pod looks stuck rather than failed.

CrashLoopBackOff: the container starts and then exits. The kubelet restarts it with an exponential backoff (10s, 20s, 40s and so on), capped at 300 seconds, and resets the timer once the container has run for 10 minutes without problems. The current container may have no useful output yet, so I read the crashed one with kubectl logs <pod> --previous, and -c <container> when there are several. Then I read the exit code and reason in describe:

  • **Exit code 137 with reason OOMKilled:** the container went over its memory limit and the kernel killed it. For a JVM this is often heap plus metaspace, thread stacks and direct buffers adding up past the limit. I set the heap as a share of the container limit (for example with -XX:MaxRAMPercentage), raise the limit if the working set is genuine, and look for a leak if memory keeps growing.
  • Exit code 1 or a stack trace on startup: an application error. Typical causes are a missing environment variable or secret, a wrong Spring profile, a database that cannot be reached, or a bean that fails to start.
  • Liveness probe failures in the events: the app is alive but is killed because the probe checks too early or checks the wrong thing. A slow-starting Spring Boot service needs a startup probe. Kubernetes runs no liveness or readiness checks until the startup probe succeeds; with failureThreshold: 30 and periodSeconds: 10 the app gets up to 300 seconds to start.

Running but not Ready. The readiness probe is failing, so the Service sends it no traffic. Spring Boot exposes /actuator/health/liveness and /actuator/health/readiness as separate health groups for these probes. Liveness should not depend on a downstream such as the database; otherwise one database blip makes Kubernetes restart every pod at once.

Then ask what changed. kubectl rollout history shows recent rollouts. A new image, a changed ConfigMap or Secret, or a lowered memory limit explains most sudden failures, and kubectl rollout undo is the fastest mitigation while I find the root cause.

Alternate interview scenarios

"One pod behaves differently from the others."

All pods run the same Deployment, so the difference is in state or placement, not code. I compare that pod's image digest, environment and mounted config with a healthy pod (Spring Boot's /actuator/env and /actuator/info help); check which node it is on, since a noisy neighbour or clock skew affects only that node; check whether it holds local cache state or is the one instance running @Scheduled jobs; and look at its restart count and memory trend. If nothing stands out, I take it out of rotation, capture a thread dump and heap data, and replace it.

Sources (7)
  1. Kubernetes: Debug Podskubernetes.io
  2. Kubernetes: Pod lifecycle (CrashLoopBackOff and restart backoff)kubernetes.io
  3. Kubernetes: Images (ImagePullBackOff)kubernetes.io
  4. Kubernetes: Assign memory resources (OOMKilled, exit code 137)kubernetes.io
  5. Kubernetes: Configure liveness, readiness and startup probeskubernetes.io
  6. kubectl logs reference (--previous)kubernetes.io
  7. Spring Boot: Kubernetes probes in Actuatordocs.spring.io