Skip to content

Instantly share code, notes, and snippets.

@brianonn
Created January 18, 2023 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianonn/c9db6a56f333e6b1d56885c58c7f8582 to your computer and use it in GitHub Desktop.
Save brianonn/c9db6a56f333e6b1d56885c58c7f8582 to your computer and use it in GitHub Desktop.
get a shell on a kubernetes node using just the nodename
#!/bin/sh
set -x
node=${1}
nodeName=$(kubectl get node ${node} -o template --template='{{index .metadata.labels "kubernetes.io/hostname"}}')
nodeSelector='"nodeSelector": { "kubernetes.io/hostname": "'${nodeName:?}'" },'
podName=${USER}-nsenter-${node}
kubectl 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