Created
March 4, 2022 13:33
-
-
Save nlm/253f66b717eabd16b264ec253b3cb913 to your computer and use it in GitHub Desktop.
Kubectl extension to spawn a shell on a k8s node
This file contains hidden or 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/bash -eu | |
# This script starts a new pod with a privileged container to run admin commands | |
if [ "$#" -ne 1 ]; then | |
echo "usage: $0 nodeName" >&2 | |
exit 1 | |
fi | |
node=${1:?} | |
nodeName=$(kubectl get node ${node} -o template --template='{{index .metadata.labels "kubernetes.io/hostname"}}') | |
podName=${USER:-unknown}-shell-${nodeName} | |
exec kubectl run ${podName:?} --restart=Never -it --rm --image overriden --overrides ' | |
{ | |
"spec": { | |
"priorityClassName": "system-node-critical", | |
"hostPID": true, | |
"hostNetwork": true, | |
"nodeSelector": { "kubernetes.io/hostname": "'${nodeName:?}'" }, | |
"tolerations": [{ | |
"operator": "Exists" | |
}], | |
"containers": [ | |
{ | |
"name": "nsenter", | |
"image": "ubuntu:latest", | |
"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