Quick revision
The short answer
We bring the tools to the pod: kubectl debug starts an ephemeral container with a full JDK and --target, so it shares the process namespace with the application container. Attaching needs the same user id as the target and access to its temporary directory, which we reach through /proc/<pid>/root; a small tool such as jattach handles both. For a thread dump alone, kill -3 (SIGQUIT) makes the JVM print one to its stdout. The heap dump goes to a volume that survives a restart, and we copy it out with kubectl cp from a container that has tar. For next time, we add jdk.jcmd to the jlinked image or document a debug image, enable heap dump on OOM, and expose secured diagnostics endpoints such as the Actuator threaddump and heapdump.
What to remember
In this answer, we saw that a jlinked image without jcmd or a shell blocks the usual kubectl exec approach. We attached from an ephemeral debug container with --target to share the process namespace, matched the target's user id and reached its temporary directory through /proc, or used jattach; we used kill -3 for a thread dump on stdout; and we wrote the heap dump to a surviving volume and copied it through the debug container. For next time we include jdk.jcmd or a documented debug image, enable heap dump on OOM, and expose secured Actuator diagnostics.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We bring the tools to the pod: kubectl debug starts an ephemeral container with a full JDK and --target, so it shares the process namespace with the application container. Attaching needs the same user id as the target and access to its temporary directory, which we reach through /proc/<pid>/root; a small tool such as jattach handles both. For a thread dump alone, kill -3 (SIGQUIT) makes the JVM print one to its stdout. The heap dump goes to a volume that survives a restart, and we copy it out with kubectl cp from a container that has tar. For next time, we add jdk.jcmd to the jlinked image or document a debug image, enable heap dump on OOM, and expose secured diagnostics endpoints such as the Actuator threaddump and heapdump.
Why this question is asked
Small images are good practice: a jlinked runtime with only the needed modules and no shell has less to patch and a smaller attack surface. The catch arrives during an incident, when the usual kubectl exec into the pod to run jcmd is impossible because there is neither a shell nor jcmd. Interviewers ask this to see whether we understand how the attach mechanism and Kubernetes process isolation work, so we can still get evidence, and whether we fix the gap afterwards. In this answer, we will see why the usual way fails, attach from an ephemeral debug container, use SIGQUIT as a fallback, get the heap dump out safely, and prepare the image and platform for next time.
1Why the Usual Approach Fails
Normally we would run kubectl exec into the container and call jcmd. Here that fails twice: there is no shell to run, and the jlink tool built a runtime with only the modules the application needs, which excludes jdk.jcmd, the module that contains jcmd, jstack and jmap. We could rebuild the image, but restarting the pod would destroy the state we want to look at, such as a stuck thread or a memory leak.
So we need tools from outside the container that can still reach the JVM process. Two Kubernetes and Linux features make that possible: ephemeral containers and shared process namespaces.
2Attaching From an Ephemeral Debug Container
kubectl debug adds an ephemeral container to a running pod without restarting it. With --target, the new container joins the process namespace of the named container, so it can see the application's processes. We choose an image that has a full JDK of the same major version as the application, because jcmd and the target JVM should match.
7 messages in order. Play them, or step through them one at a time.
The target JVM writes the heap dump, so the path is in the application container's file system.
Two details make the attach work. First, the attach mechanism requires the tool to run with the same effective user id as the target JVM, here 1000, so we either start the debug container as that user (for example with a custom debug profile or a security context) or switch to it. Second, jcmd and the target communicate through a socket file in the target's temporary directory, and the debug container has its own /tmp. Modern jcmd knows to look for the target's files through /proc/<pid>/root/tmp, but user id and namespace issues still trip people up. jattach, a small standalone tool, handles both: it switches to the target's user and namespaces and speaks the attach protocol directly.
3A Fallback for Thread Dumps: SIGQUIT
If attaching is not possible, a thread dump needs no tool at all. When a HotSpot JVM receives SIGQUIT, signal 3, it prints a full thread dump to its standard output and keeps running. From the debug container, which shares the process namespace, we can send the signal; then kubectl logs shows the dump because the container's stdout is captured.
This works unless the JVM was started with -Xrs, which reduces signal use. It gives thread dumps only; there is no signal for a heap dump.
4Writing and Copying the Heap Dump Safely
A heap dump is written by the target JVM, so the file path is a path in the application container, not in the debug container. If we write it to the container's own file system and the pod restarts, the dump is lost, and a large dump can fill the container's writable layer and get the pod evicted. So we write it to a mounted volume, ideally one that survives the pod, or at least an emptyDir sized for it.
The dump contains everything in memory, including customer data and possibly credentials, so it is copied only to an approved, access-controlled location for analysis, not to a laptop, and deleted afterwards. It also pauses the JVM while it is written, so if the pod is still serving traffic, we remove it from the load balancer first, for example by failing its readiness probe.
5What We Change for Next Time
The incident shows a gap, and we close it in several layers. In the image, add the jdk.jcmd module to the jlink command; it costs a few megabytes and makes kubectl exec usable for diagnostics even without a shell, by calling the jcmd binary directly. If the image must stay minimal, document an approved debug image with the matching JDK, user id and a ready kubectl debug command in the runbook.
In the JVM options, enable -XX:+HeapDumpOnOutOfMemoryError with -XX:HeapDumpPath pointing at the persistent volume, so the most common memory incident produces evidence automatically. In the application, Spring Boot Actuator's threaddump and heapdump endpoints provide the same data over HTTP; they must be exposed only on a management port that is not public and protected by authentication, because a heap dump endpoint is a data leak if exposed. A continuous JFR recording to the same volume adds the history leading up to an incident. Finally, rehearse the procedure once, so the next on-call engineer is not learning it during an outage.
6Follow-up questions
Why does attaching need the same user id?
The attach mechanism trusts processes of the same user only. The JVM checks the credentials of the process that connects to its attach socket and refuses others, so a debug container running as root may still fail against a JVM running as user 1000 unless it switches user.
Can we just run the JDK tools from the node?
Technically, with node access, yes, by entering the container's namespaces. But node access is usually restricted, and kubectl debug achieves the same without it, with an audit trail in the Kubernetes API.
What should go into the jlink command?
Add jdk.jcmd, which brings jcmd, jstack and jmap and requires jdk.attach, and jdk.jfr if the application should record flight recordings. Check the module list with java --list-modules on the built image.
7Conclusion
In this answer, we saw that a jlinked image without jcmd or a shell blocks the usual kubectl exec approach. We attached from an ephemeral debug container with --target to share the process namespace, matched the target's user id and reached its temporary directory through /proc, or used jattach; we used kill -3 for a thread dump on stdout; and we wrote the heap dump to a surviving volume and copied it through the debug container. For next time we include jdk.jcmd or a documented debug image, enable heap dump on OOM, and expose secured Actuator diagnostics.
Open with
Since I can't exec in and run jcmd, I bring the tools to the pod. kubectl debug adds an ephemeral container with a full JDK of the same version, and with --target set to the app container it shares the process namespace, so I can see the JVM, often as pid 1.
Then, if they want more
Attaching has two requirements: the same user id as the target JVM, so I run the debug container as that uid or switch to it, and access to the attach socket in the target's /tmp, which from my container is under /proc/pid/root/tmp. jattach is handy here because it switches user and namespace and speaks the attach protocol itself.
If attaching fails, kill -3 makes the JVM print a thread dump to stdout, and I read it with kubectl logs. For the heap dump, the target JVM writes the file, so the path must be a volume in the app container that survives a restart, not its writable layer.
Then I copy it out with kubectl cp from the debug container through /proc, since the app container has no tar. I'd take the pod out of traffic first because the dump pauses it, and treat the file as sensitive.
For next time: add jdk.jcmd to the jlink module list or document a debug image in the runbook, enable HeapDumpOnOutOfMemoryError to that volume, and expose Actuator threaddump and heapdump only on a secured management port.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You need a thread dump and a heap dump from a JVM in a Kubernetes pod. The image...": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/a6bfe0a9d0/