How to get a heap or thread dump from JVM inside a pod in Kubernetes?
Using JDK tools inside the Kubernetes
--
When you run Java programs on a desktop or a server, you can quickly get heap or thread dump using JDK tools like jmap
, jstack
Or other tools and commands.
But in the Kubernetes environment, you don't have access to the container in which your java application is included directly. So you need to do some extra steps to get the heap or thread dump from the JVM inside the pod.
1- Connect to the POD
First, you need to connect to the pod which is running your Java application. For this purpose, you can get the shell to that pod using this command:
kubectl exec -it POD_NAME -- /bin/bash
and then you can get the heap or thread dump by using the jmap
or jstack
.
Or
You can directly run the jmap
or jstack
command using the kubectl
command:
kubectl exec POD_NAME -c CONTAINER_NAME -- bash -c "COMMAND TO GET HEAP OR THREAD DUMP"
2- Get the heap and thread dump
There are several commands to get the heap or thread dump. To get the heap dump, you can use this command:
jmap -dump:file=/tmp/HEAP_DUMP_FILENAME.jmap JAVA_PROCESS_ID
To get the thread dump, you can use this command:
jstack JAVA_PROCESS_ID > /tmp/THREAD_DUMP_FILENAME.tdump
You have other options to get heap and thread dump. You can read more about these options in this and this article.
3- Copy dump files to your machine
In the last step, you need to copy the dump files to your machine to analyze them using other tools like VisualVM. To do that, you can use the kubectl
copy command:
kubectl cp POD_NAME:/tmp/DUMP_FILENAME.tdump /PATH_IN_YOUR_MACHINE/DUMP_FILENAME.tdump -c CONTAINER_NAME
You can follow me for upcoming stories:
Read my short technical posts on Twitter.