Skip to content

Instantly share code, notes, and snippets.

Last active July 11, 2023 01:20
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Script for connecting Erlang Observer to remote node hosted in Kubernetes
# This script provides easy way to debug remote Erlang nodes that is running in a kubernetes cluster.
# Usage: ./ -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}"
# Required part of config
if [ ! $K8S_SELECTOR ]; then
echo "[E] You need to specify Kubernetes selector with '-l' option."
exit 1
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]}')
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)
# By default, cookie is the same as node name
if [ ! $ERL_COOKIE ]; then
# 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@ -setcookie $ERL_COOKIE -hidden -run observer
Copy link

Now there is a public repo where we you can find other scripts like this one:

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