Created
April 19, 2023 15:58
-
-
Save metajiji/e68a9b9ebe0e97360de72f50633d2c6b to your computer and use it in GitHub Desktop.
k8s nsenter script
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
#!/bin/sh | |
node=${1} | |
kube_context=${2:-""} | |
nodeName=$(kubectl ${kube_context:+--context $kube_context} get node "$node" -o template --template='{{index .metadata.labels "kubernetes.io/hostname"}}') | |
nodeSelector='"nodeSelector": { "kubernetes.io/hostname": "'${nodeName:?}'" },' | |
podName=$USER-nsenter-$node | |
kubectl ${kube_context:+--context $kube_context} run "${podName:?}" --restart=Never -it --rm --image overriden --overrides ' | |
{ | |
"spec": { | |
"hostPID": true, | |
"hostNetwork": true, | |
'"${nodeSelector?}"' | |
"tolerations": [{ | |
"operator": "Exists" | |
}], | |
"containers": [ | |
{ | |
"name": "nsenter", | |
"image": "alexeiled/nsenter:2.34", | |
"command": [ | |
"/nsenter", "--all", "--target=1", "--", "su", "-" | |
], | |
"stdin": true, | |
"tty": true, | |
"securityContext": { | |
"privileged": true | |
} | |
} | |
] | |
} | |
}' --attach "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example