Skip to content

Instantly share code, notes, and snippets.

@kprasad99
Last active December 5, 2019 15:36
Show Gist options
  • Save kprasad99/4bd4dd79f6100a79288aa45bb036cdc3 to your computer and use it in GitHub Desktop.
Save kprasad99/4bd4dd79f6100a79288aa45bb036cdc3 to your computer and use it in GitHub Desktop.
How to take Thread dump for Java Application in kubernetes.

How to take Thread Dump for Java Application in Kubernetes.

  • Get the container id of the pod.

    $ kubectl get po <pod-id>  -o=jsonpath='{.status.containerStatuses[0].containerID}{"\n"}' | awk '{print substr($0, 10, 12);}'
    Note
    In my case there is only one container, if there are mulitple container, you can use selectors.

If you don’t have jstack or jcmd. you can use following steps to take thread dump.

  • Now get PIDs of Java Application in host machine.

    $ docker top <container-id>
  • On host machine run below command to take thread dump.

    $ kill -3 <pid>
    Tip
    By default thread dump is redirected to standard out which in our case is docker standard out which can be access using docker logs -f <container-id>

If you have jstack or jcmd in docker container prebuilt.

  • Login to container

    $ docker exec -it <container-id> sh
  • Get the Java PID

    $ ps -eaf
  • Now execute jstack or jcmd to take thread dump

    $ jcmd <PID> /tmp/Thread.print or jstack <PID> > /tmp/Thread.print
  • Exit from the contrainer and then copy the thread dump using below command.

    $ docker cp <container-id>:/tmp/Thread.print .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment