Skip to content

Instantly share code, notes, and snippets.

@brettbuddin
Created April 13, 2020 15:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettbuddin/05fdf3e741d887594659e8d39a15c8c9 to your computer and use it in GitHub Desktop.
Save brettbuddin/05fdf3e741d887594659e8d39a15c8c9 to your computer and use it in GitHub Desktop.
Script to dump goroutine stack traces from Pods in Kubernetes.
#!/usr/bin/env zsh
if [ "$#" -ne 2 ]; then
echo "dump-pod-goroutines <labels> <port>"
exit 1
fi
labels="$1"
port="$2"
mkdir -p pods
# collect pod names with specific "app" label
all_pods=$(kubectl get pods -l "$labels" --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo "$all_pods" | while read -r pod; do
# port-forward to the pod
kubectl port-forward $pod $port:$port &
pid=$!
# wait for port to come alive
while ! nc -z localhost $port; do
sleep 0.1
done
local_url="http://localhost:$port/debug/pprof/goroutine?debug=2"
curl "$local_url" > "pods/$pod.txt"
# stop port-forwarding
kill $pid
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment