Skip to content

Instantly share code, notes, and snippets.

@AndrewDryga
Last active July 11, 2023 01:20
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AndrewDryga/6f273c7a9117d10ec347b47420367e49 to your computer and use it in GitHub Desktop.
Save AndrewDryga/6f273c7a9117d10ec347b47420367e49 to your computer and use it in GitHub Desktop.
Script for connecting Erlang Observer to remote node hosted in Kubernetes
#!/bin/bash
# This script provides easy way to debug remote Erlang nodes that is running in a kubernetes cluster.
# Usage: ./erl-observe.sh -l app=my_all -n default -c erlang_cookie
#
# Don't forget to include `:runtime_tools` in your mix.exs application dependencies.
set -e
# Trap exit so we can try to kill proxies that has stuck in background
function cleanup {
echo " - Stopping kubectl proxy."
kill $! &> /dev/null
}
trap cleanup EXIT
# Read configuration from CLI
while getopts "n:l:c:" opt; do
case "$opt" in
n) K8S_NAMESPACE="--namespace=${OPTARG}"
;;
l) K8S_SELECTOR=${OPTARG}
;;
c) ERL_COOKIE=${OPTARG}
;;
esac
done
# Required part of config
if [ ! $K8S_SELECTOR ]; then
echo "[E] You need to specify Kubernetes selector with '-l' option."
exit 1
fi
echo " - Selecting pod with '-l ${K8S_SELECTOR} ${K8S_NAMESPACE:-default}' selector."
POD_NAME=$(kubectl get pods -l ${K8S_SELECTOR} ${K8S_NAMESPACE} -o jsonpath='{.items[0].metadata.name}')
echo " - Resolving Erlang node port on a pod '${POD_NAME}'."
EPMD_OUTPUT=$(echo ${POD_NAME} | xargs -o -I my_pod kubectl exec my_pod ${K8S_NAMESPACE} -i -t -- epmd -names | tail -n 1)
eval 'EPMD_OUTPUT=($EPMD_OUTPUT)'
# By default, cookie is the same as node name
if [ ! $ERL_COOKIE ]; then
ERL_COOKIE=${EPMD_OUTPUT[1]}
fi
# Strip newlines from last element of output
OTP_PORT=${EPMD_OUTPUT[4]//[$'\t\r\n ']}
echo " - Connecting on port ${OTP_PORT} with cookie '${ERL_COOKIE}'."
# Kill epmd on local node to free 4369 port
killall epmd
# Replace it with remote nodes epmd and proxy remove erlang app port
kubectl port-forward $POD_NAME $K8S_NAMESPACE 4369 $OTP_PORT &> /dev/null &
sleep 1
# Run observer in hidden mode to don't heart clusters health
erl -name debug@127.0.0.1 -setcookie $ERL_COOKIE -hidden -run observer
@AndrewDryga
Copy link
Author

Now there is a public repo where we you can find other scripts like this one: https://github.com/Nebo15/k8s-utils

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