Created
July 7, 2025 10:55
-
-
Save babs/6406e0ff14f596b32d60c2bf68b24de3 to your computer and use it in GitHub Desktop.
see active connection per kube pods / netns
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 | |
| roottcpcount=$(ss -tan state established | tail -n +2 | wc -l) | |
| rootudpcount=$(ss -uan state established | tail -n +2 | wc -l) | |
| for pod in $(crictl pods -s Ready -q 2>/dev/null); do | |
| pod_info=$(crictl inspectp $pod 2>/dev/null) | |
| pod_name=$(echo $pod_info | jq -r .status.metadata.name) | |
| pod_ns=$(echo $pod_info | jq -r .status.metadata.namespace) | |
| pid=$(echo $pod_info | jq -r .info.pid) | |
| [ $pid -eq 0 ] && continue | |
| if [ ! -e /proc/$pid/ns/net ]; then | |
| tcpcount=$roottcpcount | |
| udpcount=$rootudpcount | |
| else | |
| netns=$(ls -l /proc/$pid/ns/net | awk '{print $NF}' | cut -d'[' -f2 | cut -d']' -f1) | |
| nsfs=$(ip netns identify $pid) | |
| if [ -z "$nsfs" ]; then | |
| nsfs=none | |
| tcpcount=$roottcpcount | |
| udpcount=$rootudpcount | |
| else | |
| tcpcount=$(ip netns exec $nsfs ss -tan state established | tail -n +2 | wc -l) | |
| udpcount=$(ip netns exec $nsfs ss -uan state established | tail -n +2 | wc -l) | |
| fi | |
| fi | |
| printf "%-10s %-40s %6d %6d %s %s %d\n" "$netns" "$nsfs" "$tcpcount" "$udpcount" "$pod_ns" "$pod_name" "$pid" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment