Skip to content

Instantly share code, notes, and snippets.

@uablrek
Last active December 6, 2023 07:26
Show Gist options
  • Save uablrek/27f80896a2eca5ace781ce5b6b067c2e to your computer and use it in GitHub Desktop.
Save uablrek/27f80896a2eca5ace781ce5b6b067c2e to your computer and use it in GitHub Desktop.
Ping K8s pods in parallel
#! /bin/sh
##
## pod-ping.sh <src-pod> <dst-selector>
## Env:
## SRC_NS - Source pod namespace
## DST_NS - Destination pods namespace
## Example:
## pod-ping.sh tserver-569b74646b-tnwzj app=tserver
##
if test -z "$2"; then
grep '^##' $0 | cut -c3-
exit 0
fi
tmp=/tmp/pod-ping-$$
mkdir -p $tmp/pod
die() {
echo "ERROR: $*" >&2
rm -rf $tmp
exit 1
}
# podping <dst-pod>
# SRC_POD - Source pod name
podping() {
local status=DEAD
local ip=$(kubectl get -n $DST_NS $1 -o json | jq -r .status.podIP)
#echo $ip
kubectl exec -n $SRC_NS $SRC_POD -- ping -c1 -W1 $ip > /dev/null 2>&1 && status=ALIVE
echo "$ip $status" > $tmp/$1
}
test -n "$SRC_NS" || SRC_NS=default
test -n "$DST_NS" || DST_NS=default
SRC_POD=$1
kubectl exec -n $SRC_NS $SRC_POD -- ping -c1 -W1 127.0.0.1 > /dev/null || \
die "Can't exec ping in [$SRC_NS/$SRC_POD]"
for dst in $(kubectl get pods -n $DST_NS -l $2 -o name); do
podping $dst &
done
wait
grep -r DEAD $tmp/pod | sed -e "s,$tmp/pod/,,"
echo "Succesful pings $(grep -r ALIVE $tmp/pod | wc -l)"
rm -rf $tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment