Created
August 7, 2019 00:02
-
-
Save cdemers/d1c0ba8586492a15d7ce1c7631e09367 to your computer and use it in GitHub Desktop.
Shell script to be used to simplify opening a shell in a container within a pod in a Kubernetes cluster.
This file contains 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/sh | |
# Using this script you simply have to specify the first letters of a pod | |
# to open a shell, the default shell is bash, but you can specify an | |
# alternative as second parameter. | |
# | |
# For example, if you have the two following pods: | |
# - caching-service-blablabla-123123-abcdef | |
# - creditcard-frontend-blabla-3213213-fghijk | |
# | |
# And you want to get a bash shell into the second, you would do: | |
# `kshell.sh cr` | |
# ...and that should do the trick. | |
# | |
# And if you happend to have a pod with a container without bash, you | |
# could do: | |
# `kshell.sh cr sh` | |
# and that will open sh instead of the default bash. | |
# | |
# If multiple pods match, the first one is selected. For exaple: | |
# `kshell.sh c` | |
# will probably give you a bash in the `caching-service...` | |
# | |
# This script doesn't support pods with multiple containers yet, if you | |
# try to use it on these, you will get the familliar kubectl error about | |
# you having to specify a container. | |
if [ "$1" = "" ]; then | |
echo "Usage: kshell [Pod] {shell (defaults to bash)}" | |
exit 1 | |
fi | |
NEEDLE=$1 | |
SHELL=$2 | |
SHELL="${SHELL:-bash}" | |
COLUMNS=`tput cols` | |
LINES=`tput lines` | |
TERM=xterm | |
for name in `kubectl get pods -o json | jq '.items[].metadata.name' -r`; do | |
if [[ $name == $NEEDLE* ]]; then | |
kubectl exec -i -t $name env COLUMNS=$COLUMNS LINES=$LINES TERM=$TERM $SHELL | |
exit 0 | |
fi | |
done | |
echo "No Pod begining by $NEEDLE was found" | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment