Skip to content

Instantly share code, notes, and snippets.

@smola
Created March 23, 2018 14:49
Show Gist options
  • Save smola/2fe28e9bf64ad0144f53f4e9473bfb3c to your computer and use it in GitHub Desktop.
Save smola/2fe28e9bf64ad0144f53f4e9473bfb3c to your computer and use it in GitHub Desktop.
Attach JProfiler agent to a JVM running in a Kubernetes pod
#!/bin/bash
set -e
if [[ -z ${K8S_JVM_POD} ]]; then
echo "K8S_JVM_POD not defined"
exit 1
fi
EXEC="kubectl exec ${K8S_JVM_POD}"
CP="kubectl cp ${K8S_JVM_POD}"
if [[ -z ${K8S_JVM_PID} ]]; then
echo "K8S_JVM_PID not defined, pick one:"
${EXEC} jps 2>/dev/null
exit 1
fi
JPROFILER_PACKAGE=jprofiler_linux_10_1.tar.gz
JPROFILER_PACKAGE_URL=https://download-keycdn.ej-technologies.com/jprofiler/${JPROFILER_PACKAGE}
if [[ ! -f ${JPROFILER_PACKAGE} ]]; then
wget -O "${JPROFILER_PACKAGE}" "${JPROFILER_PACKAGE_URL}"
fi
if ! ${EXEC} -- find /root/${JPROFILER_PACKAGE} &>/dev/null; then
echo "${JPROFILER_PACKAGE} not found on the server, copying..."
kubectl cp "${JPROFILER_PACKAGE}" "${K8S_JVM_POD}:/root/${JPROFILER_PACKAGE}"
else
echo "${JPROFILER_PACKAGE} already found in the server"
fi
JPROFILER_PORT=31757
${EXEC} -- tar -C /root -xf "/root/${JPROFILER_PACKAGE}"
${EXEC} -- /root/jprofiler10.1/bin/jpenable --pid=${K8S_JVM_PID} --port=${JPROFILER_PORT} --noinput --gui
@UrkoLekuona
Copy link

UrkoLekuona commented Oct 6, 2020

This still works. I just needed the following:

  • Your container image must have a JDK installed (JREs are not valid), as the JDK includes the jps binary used in the script. You can use ps instead to identify your process.
  • You might need to change the JProfiler version to the one you are using locally.
  • You might need to change the installation directory (currently /root) due to permissions.
  • You need to run jpenable as a superuser. The easiest way to do this is to run the container as root (https://stackoverflow.com/a/48409635/14015237)

Once that's done, you need to attach your profiler to the container in the port specified (31757 by default). To accomplish this, I've found the easiest way to port forward the pod port to a local port:

kubectl port-forward <pod-name> 31757:31757

Leave the process running in the background and use your local JProfiler to attach a JVM in the address localhost:31757.

I hope this helps someone.

@zeroFruit
Copy link

Need to fix L14

${EXEC} -- jps 2>/dev/null

@adiii717
Copy link

The download link is broken. here is the new one
https://www.ej-technologies.com/download/jprofiler/files
or

https://download-gcdn.ej-technologies.com/jprofiler/jprofiler_linux_12_0_3.tar.gz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment