Skip to content

Instantly share code, notes, and snippets.

@mmastoras
Last active July 1, 2019 19:46
Show Gist options
  • Save mmastoras/94af8c2a09c55e8315bfc08fea63960f to your computer and use it in GitHub Desktop.
Save mmastoras/94af8c2a09c55e8315bfc08fea63960f to your computer and use it in GitHub Desktop.
shell into k8s pods
Here’s a bash function to make it easier to shell into kubernetes pods. Running `kubash` by itself will give you a list of pod names;
$ kubash <pod name # will open a bash shell on a pod with that name. It works via prefix matching so any of the following would work:
$ kubash railsapp-68b45f5c7b-98qqz #attach to this specific pod
$ kubash railsapp #attach to any railsapp-* pod
$ kubash ra #attach to some pod that starts with "ra"
/bin/kubash
kubash() {
if [ "$1" = "" ]; then
echo `kubectl config view --minify --output 'jsonpath={"context:"} {.current-context}{", namespace:"} {..namespace}'`
kubectl get pods | awk '{print $1}' | grep -v 'NAME' | sed -E 's/(-[a-z0-9]+){2}$//' | sort | uniq
else
kubectl exec -it $(kubectl get pods | grep -E "^$1.*Running" | awk '{print $1}') /bin/bash
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment