Skip to content

Instantly share code, notes, and snippets.

@johscheuer
Last active April 24, 2024 17:38
Show Gist options
  • Save johscheuer/dc20988895d6fddfd057e221d47587d3 to your computer and use it in GitHub Desktop.
Save johscheuer/dc20988895d6fddfd057e221d47587d3 to your computer and use it in GitHub Desktop.
This script get's the netns from a Kubernetes pod
#### WAY with crictl
#### TODO validate crictl with docker runtime !!
# Get all pods of a specific node
#kubectl get po --field-selector=spec.nodeName="fluffy-master" --all-namespaces
kubectl get --all-namespaces po --field-selector=spec.nodeName=="$(hostname)" -o json | jq -r '.items[] | select(.status.hostIP!=.status.podIP) | "\(.metadata.name) \(.metadata.namespace)"'
# Get the Pod ID of the Pod with crictl
POD_ID=$(sudo crictl pods --name=nginx-6f858d4d45-vnszm --namespace=default -q --no-trunc)
# Get the network namespace of this pod
NETNS=$(sudo crictl inspectp ${POD_ID} | jq -r '.info.runtimeSpec.linux.namespaces[] |select(.type=="network") | .path')
# Jump into the network namespace and execute something
sudo nsenter --net=${NETNS} ip -o a s
@jallirs
Copy link

jallirs commented Apr 24, 2024

in case you're running somewhere with no jq
crictl inspectp ${POD_ID} | grep "/var/run/netns" | grep path | awk '{print $2}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment