Skip to content

Instantly share code, notes, and snippets.

@CsBigDataHub
Forked from adiii717/k8s-jprofiler-attach.sh
Created August 27, 2021 14:24
Show Gist options
  • Save CsBigDataHub/e43e83e3cadc37bc5c158e63d83826e5 to your computer and use it in GitHub Desktop.
Save CsBigDataHub/e43e83e3cadc37bc5c158e63d83826e5 to your computer and use it in GitHub Desktop.
Attach JProfiler agent to a JVM running in a Kubernetes pod
#!/bin/bash
set -ex
usage()
{
echo "usage: k8s-jprofiler-attach.sh pod_name"
}
k8s_jvm_pod="${1}"
if [[ -z $k8s_jvm_pod ]]; then
echo "Pod name not defined"
usage
exit 1
fi
EXEC="kubectl exec $k8s_jvm_pod"
CP="kubectl cp $k8s_jvm_pod"
PORT_FORWARD="kubectl port-forward $k8s_jvm_pod"
JPROFILER_PACKAGE="jprofiler_linux_12_0_3.tar.gz"
if [[ ! -f ${JPROFILER_PACKAGE} ]]; then
wget https://download-gcdn.ej-technologies.com/jprofiler/jprofiler_linux_12_0_3.tar.gz -O jprofiler_linux_12_0_3.tar.gz
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
K8S_JVM_PID="$(${EXEC} -- /bin/ps -ef | grep "java" | grep -v "grep" | awk '{print $2}')"
if [[ -z ${K8S_JVM_PID} ]]; then
echo "K8S_JVM_PID not defined, pick one:"
${EXEC} jps 2>/dev/null
exit 1
fi
${EXEC} -- tar -C /root -xf "/root/${JPROFILER_PACKAGE}"
${EXEC} -- /root/jprofiler12.0.3/bin/jpenable --pid=${K8S_JVM_PID} --port=${JPROFILER_PORT} --noinput --gui
${PORT_FORWARD} ${JPROFILER_PORT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment