Skip to content

Instantly share code, notes, and snippets.

@leodido
Last active March 26, 2020 19:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leodido/eb71ce6a140c48d422131b6bade6cb50 to your computer and use it in GitHub Desktop.
Save leodido/eb71ce6a140c48d422131b6bade6cb50 to your computer and use it in GitHub Desktop.
Exec into a node with kubectl
#!/usr/bin/env bash
NODE=kind-control-plane
IMG="docker.io/library/alpine"
POD="nsenter-$(tr -dc a-z0-9 < /dev/urandom | head -c 6)"
OVERRIDE="{\"spec\":{\"nodeName\":\"$NODE\",\"hostPID\":true,\"containers\":[{\"securityContext\":{\"privileged\":true},\"image\":\"${IMG}\",\"name\":\"nsenter\",\"stdin\":true,\"stdinOnce\":true,\"tty\":true,\"command\":[\"nsenter\",\"--target\",\"1\",\"--mount\",\"--uts\",\"--ipc\",\"--net\",\"--pid\",\"--\",\"bash\",\"-l\"]}]}}"
kubectl run --rm \
--image "${IMG}" \
--overrides="${OVERRIDE}" \
--generator=run-pod/v1 \
-ti "${POD}" \
-n default
@leodido
Copy link
Author

leodido commented Mar 23, 2020

Copy it and execute.

You can download it as follows:

curl -fsSL https://git.io/kubectl-exec-node

@leodido
Copy link
Author

leodido commented Mar 23, 2020

TODO LIST

  • flags and minimal validation
    • node name
    • namespace
  • check node exists
  • check kubectl exists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment