Skip to content

Instantly share code, notes, and snippets.

@lavie
Created July 29, 2019 07:47
Show Gist options
  • Save lavie/80f53eeb09bac9fc1223a850f6977046 to your computer and use it in GitHub Desktop.
Save lavie/80f53eeb09bac9fc1223a850f6977046 to your computer and use it in GitHub Desktop.
how to tcpdump traffic from a specific kubernetes pod
#!/bin/bash -eu
POD_NAME="$1"
IFACE="$(kubectl exec -t "$POD_NAME" -- bash -c "cat /sys/class/net/eth0/iflink")"
echo "Internal interface index: $IFACE. Searching for it on host..."
for i in /sys/class/net/veth*/ifindex; do
if grep "$IFACE" <$i >/dev/null; then
echo -n "Host interface: "
echo "$i" | grep -o -E "veth[^/]*"
fi
done

Example usage

$ ./get_pod_interface.sh my-pod
Internal interface index: 281. Searching for it on host...
Host interface: veth0f6a8d6

$ sudo tcpdump -i veth0f6a8d6 port 80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment