Skip to content

Instantly share code, notes, and snippets.

@tamalsaha
Last active September 21, 2017 05:56
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamalsaha/4e6a5c1144e2daf370398520e20fdef7 to your computer and use it in GitHub Desktop.
Save tamalsaha/4e6a5c1144e2daf370398520e20fdef7 to your computer and use it in GitHub Desktop.
Kubectl exec with working terminal
#!/bin/sh
if [ "$1" = "" ]; then
echo "Usage: ksh <pod> [flags_to_kubectl]"
exit 1
fi
POD=$1
shift
COLUMNS=`tput cols`
LINES=`tput lines`
TERM=xterm
KUBE_SHELL=${KUBE_SHELL:-bash}
kubectl exec -i -t $POD "$@" -- env COLUMNS=$COLUMNS LINES=$LINES TERM=$TERM "$KUBE_SHELL"
@tamalsaha
Copy link
Author

tamalsaha commented Feb 24, 2017

I originally found this on Kubernetes slack channel. I adapted it so that it can be used with pods running in any namespace. Example:

$ ./ksh pod_name

$ ./ksh pod_name --namespace=kube-system

$ KUBE_SHELL=sh ./ksh pod_name

@coresolve
Copy link

coresolve commented Feb 24, 2017

And one for the bashrc or .functions if your a dotfiles man.

function ksh() {
  if [ $1 = "" ]; then
    echo "Usage: ksh <pod> [flags_to_kubectl]"
    return 1
  fi

  local POD=$1
  shift

  local COLUMNS=$(tput cols)
  local LINES=$(tput lines)
  local TERM=xterm
  local KUBE_SHELL=${KUBE_SHELL:-/bin/sh -il}
  local cmd="kubectl exec -i -t $POD $@ -- env COLUMNS=$COLUMNS LINES=$LINES TERM=$TERM $KUBE_SHELL"
  echo $cmd
  eval $cmd
}

@tamalsaha
Copy link
Author

Thanks @coresolve !

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