Last active
November 28, 2024 17:34
-
-
Save johscheuer/dc20988895d6fddfd057e221d47587d3 to your computer and use it in GitHub Desktop.
This script get's the netns from a Kubernetes pod
This file contains 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
#### 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 |
in case you're running somewhere with no jq crictl inspectp ${POD_ID} | grep "/var/run/netns" | grep path | awk '{print $2}'
I'd suggest to use tr -d '"'
at the end as well. This took me about twenty minutes of troubleshooting
it works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in case you're running somewhere with no jq
crictl inspectp ${POD_ID} | grep "/var/run/netns" | grep path | awk '{print $2}'