Debug a pod (CrashLoopBackOff, Error, not Ready)
Read-onlyPaste the pod and namespace. Shows every container's state and last crash reason, conditions, events, current and previous logs, errors, probes, resources and owner.
(
P="<pod>"; NS="<namespace>"
h() { printf '\n=== %s ===\n' "$*"; }
k() { kubectl -n "$NS" "$@"; }
h "Pod"
k get pod "$P" -o wide || { echo "No pod '$P' in namespace '$NS'. List them with: kubectl get po -n $NS"; exit 1; }
h "Container states (current and last termination)"
k get pod "$P" -o jsonpath='{range .status.initContainerStatuses[*]}init {.name}: ready={.ready} restarts={.restartCount}{"\n"} state={.state}{"\n"} last={.lastState}{"\n"}{end}{range .status.containerStatuses[*]}{.name}: ready={.ready} restarts={.restartCount}{"\n"} state={.state}{"\n"} last={.lastState}{"\n"}{end}'
h "Conditions"
k get pod "$P" -o jsonpath='{range .status.conditions[*]}{.type}={.status} {.reason} {.message}{"\n"}{end}'
h "Events for this pod"
k get events --field-selector involvedObject.name="$P" --sort-by=.lastTimestamp
h "Logs: last 50 lines (all containers)"
k logs "$P" --all-containers --prefix --tail=50 2>&1
h "Logs of the previous (crashed) containers"
k logs "$P" --all-containers --prefix --previous --tail=50 2>&1
h "Error lines in the last 2000 log lines"
ERR=$(k logs "$P" --all-containers --prefix --tail=2000 2>&1 | grep -iE 'error|exception|fatal|panic|refused|denied|timed? ?out|killed|no such|not found' | tail -25)
[ -n "$ERR" ] && echo "$ERR" || echo "no error-looking lines"
h "Images, resources and probes"
k get pod "$P" -o jsonpath='{range .spec.containers[*]}{.name}: image={.image}{"\n"} requests={.resources.requests} limits={.resources.limits}{"\n"} liveness={.livenessProbe}{"\n"} readiness={.readinessProbe}{"\n"}{end}'
h "Current CPU and memory"
k top pod "$P" --containers 2>/dev/null || echo "metrics-server not available"
h "Owner and node"
k get pod "$P" -o jsonpath='owner: {.metadata.ownerReferences[0].kind}/{.metadata.ownerReferences[0].name}{"\n"}node: {.spec.nodeName}{"\n"}serviceAccount: {.spec.serviceAccountName}{"\n"}'
) 2>&1 | tee "debug-pod-$(date +%Y%m%d-%H%M%S).txt"What it checks
Podstatus, ready count, restarts, IP and nodeContainer stateswaiting reason (CrashLoopBackOff, ImagePullBackOff, CreateContainerConfigError) and the last termination: reason (OOMKilled, Error), exit code, timeConditionsPodScheduled, Initialized, ContainersReady, Ready with the reason when falseEventsscheduling, image pulls, probe failures, kills, volume mount errors, in time orderLogsthe last 50 lines of every containerPrevious logsoutput of the container that crashed, before Kubernetes restarted itError lineserror, exception, refused, timeout... lines from recent logsImages, resources and probesimage tags, requests/limits (OOM if usage hits the limit) and probe settings (too-strict probes cause restarts)CPU and memorycurrent usage per container (needs metrics-server)Owner and nodewhich Deployment / StatefulSet / Job owns it, the node and the service account