Video in progress
How do you take a thread dump and a heap dump from a Java pod running on a distroless or minimal image?
If the image has no shell and no jcmd, I attach a temporary container with a JDK to the pod using kubectl debug: it shares the process namespace and takes the dump through jcmd. The other way is to take the dump from the application itself, for example through Actuator.
📖 Answer
The app has frozen, and kubectl exec ... sh says "executable file not found".
kubectl debug -it shop-api-7d9f-abcde \
--image=eclipse-temurin:21-jdk \
--target=app -- bash
# inside the debug container
jps -l # or ps
jcmd <pid> Thread.print > /tmp/threads.txt
jcmd <pid> GC.heap_dump /tmp/heap.hprof
A few subtle points:
--targetshares the process namespace, but the file system is separate. Thejcmdattach mechanism works through/tmp, so sometimes you have to reach the target process's root through/proc/<pid>/root/tmp;- the user UID must match: if the app runs as
10001, the debug container should use that UID too, otherwise the attach is refused; - while the heap dump is being taken the app stops (STW). For a 4 GB heap that is a few seconds, and the liveness probe may kill it in the meantime. Write the dump file to a PVC or an
emptyDirand fetch it withkubectl cp.
Preparing in advance is better: -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dumps and a volume mounted at /dumps. For a thread dump, /actuator/threaddump, protected on the internal network, also works, and no attach is needed.
Sign up to mark studied questions and save your progress.