Last active
January 16, 2024 14:53
-
-
Save draganHR/203a951c4ee7d9ace5c266ddf57414dc to your computer and use it in GitHub Desktop.
Run kubectl exec in multiple pods in a single tmux session
This file contains hidden or 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
#!/bin/bash | |
# | |
# Example: | |
# $ bash tmux-exec.sh <app> | |
# $ bash tmux-exec.sh <app> <namespace> | |
if [ -n "$1" ]; then | |
NAME="$1" | |
else | |
echo "Pleae specify app.kubernetes.io/name" | |
exit 1 | |
fi | |
if [ -n "$2" ]; then | |
NAMESPACE="$2" | |
else | |
NAMESPACE="$(kubectl config view --minify -o jsonpath='{..namespace}')" | |
fi | |
tmux_args='new-session -n '"${NAMESPACE}/${NAME}"' ;' | |
for podname in `kubectl get pods -l app.kubernetes.io/name="${NAME}" -n "${NAMESPACE}" --no-headers -o custom-columns=":metadata.name"`; do | |
tmux_args="${tmux_args} split-window -v kubectl exec -it ${podname} -n "${NAMESPACE}" -- bash; select-layout even-horizontal ; " | |
done | |
tmux ${tmux_args} \ | |
kill-pane -t 1 \; \ | |
select-layout even-horizontal \; \ | |
select-layout even-vertical \; \ | |
set-option -w synchronize-panes \; \ | |
attach |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment