Skip to content

Instantly share code, notes, and snippets.

@Stono
Created July 21, 2016 20:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Stono/dcf91b94fd68a50ce1eaa76ed70957e1 to your computer and use it in GitHub Desktop.
Save Stono/dcf91b94fd68a50ce1eaa76ed70957e1 to your computer and use it in GitHub Desktop.
A shell script which enables easy execing into kube pods
#!/bin/sh
if [ "$1" = "" ]; then
echo "Usage: kshell <pod>"
exit 1
fi
echo Getting pods...
PODS=$(kubectl get pods --no-headers --output=name | grep $1)
PODS=(${PODS})
NUM_PODS=${#PODS[@]}
if [ $NUM_PODS -gt 1 ]; then
echo Your search matched multiple pods, please be more specific:
for pod in "${PODS[@]}" ; do # <- quotes required
echo " - $pod"
done
exit 1
fi
POD=${PODS[0]}
POD=${POD/pod\//}
echo Connecting to $POD...
COLUMNS=`tput cols`
LINES=`tput lines`
TERM=xterm
kubectl exec -i -t $POD env COLUMNS=$COLUMNS LINES=$LINES TERM=$TERM bash
@Stono
Copy link
Author

Stono commented Jul 21, 2016

Stick this in /usr/local/bin/kshell and chmod +x

It'll also ensure the terminal size is set correctly, in VIM for example (something fixed in kubernetes 1.3)

To use, you just need to specify part of the pod name that you want to go to, but it has to be specific enough to match a single pod

$ kshell gocd
Getting pods...
Your search matched multiple pods, please be more specific:
 - pod/gocd-agent-2493599176-bycc5
 - pod/gocd-agent-2493599176-scvc7
 - pod/gocd-master-1388090259-9tkmm
 - pod/gocd-nginx-2641456012-3ts30

So, be more specific...

$ kshell nginx
Getting pods...
Connecting to gocd-nginx-2641456012-3ts30...
[root@gocd-nginx-2641456012-3ts30 storage]# exit

@jamesholcomb
Copy link

jamesholcomb commented Mar 13, 2018

Very helpful...Forked to let you choose a pod from the list.

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