Skip to content

Instantly share code, notes, and snippets.

@PieterScheffers
Created July 28, 2020 12:36
Show Gist options
  • Save PieterScheffers/43d7dc409b1bff990a2b396dc8f86a42 to your computer and use it in GitHub Desktop.
Save PieterScheffers/43d7dc409b1bff990a2b396dc8f86a42 to your computer and use it in GitHub Desktop.
Get a shell to a Kubernetes node with root capabilities
#!/bin/sh
# https://alexei-led.github.io/post/k8s_node_shell/
if [ -z "${1}" ]; then
echo "ERROR No kubernetes node supplied.
Script needs a node name as first argument!
Find node names with:
$ kubectl get node
"
exit 1
fi
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