avatarSaeed Zarinfam

Summary

The website content provides a step-by-step guide on how to obtain a heap or thread dump from a Java Virtual Machine (JVM) running inside a pod in a Kubernetes cluster.

Abstract

The article "How to get a heap or thread dump from JVM inside a pod in Kubernetes?" outlines the process of retrieving diagnostic information from a Java application running in a Kubernetes environment. Unlike traditional server setups, Kubernetes requires additional steps to access the JVM within a pod. The process involves connecting to the target pod, using JDK tools such as jmap and jstack to generate the dumps, and finally copying the resulting files to a local machine for analysis. The article emphasizes the use of kubectl commands to execute these tasks and provides examples for each step. It also suggests alternative methods and external resources for capturing heap and thread dumps and recommends tools like VisualVM for further analysis.

Opinions

  • The author implies that obtaining heap or thread dumps in a Kubernetes environment is more complex than on a desktop or server due to the lack of direct access to the container.
  • The article suggests that using kubectl to execute JDK tools directly is a convenient approach.
  • It is inferred that the use of kubectl exec and kubectl cp commands is essential for interacting with and copying files from a pod.
  • The author provides external links for further reading, indicating a preference for comprehensive understanding and exploration of additional methods.
  • The invitation to follow the author or engage on Twitter hints at a desire to build a community or audience around the shared interest in technical content.

How to get a heap or thread dump from JVM inside a pod in Kubernetes?

Using JDK tools inside the Kubernetes

JVM inside Kubernetes

1- Connect to the POD 2- Get the heap and thread dump 3- Copy dump files to your machine

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.

Kubernetes
JVM
Java
Heapdump
Thread Dump
Recommended from ReadMedium