Created
April 13, 2020 15:28
-
-
Save brettbuddin/05fdf3e741d887594659e8d39a15c8c9 to your computer and use it in GitHub Desktop.
Script to dump goroutine stack traces from Pods in Kubernetes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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