Skip to content

Instantly share code, notes, and snippets.

@ranjanashish
Forked from smola/k8s-jprofiler-attach.sh
Created March 18, 2020 02:29
Show Gist options
  • Save ranjanashish/1d0d8024047a03b3ea35f6472e99bc5e to your computer and use it in GitHub Desktop.
Save ranjanashish/1d0d8024047a03b3ea35f6472e99bc5e 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment