DEV Community

Aleksandr Korolev
Aleksandr Korolev

Posted on

Tools: quickly connect to pods in your Kubernetes cluster

It seems that almost all companies use or try to use Kubernetes. And you, as a developer, need from time to time connect to your app, but when it is in a Kubernetes cluster, it is not so simple.
How it can look like in your case:

# switch to appropriate context 
kubectl ctx your-context
# get pods
kubectl -n namespace get pods
# connect to the pod
kubectl -n nameapce exec -it pod/#{pod-name-from-previous-step} -- bash 
Enter fullscreen mode Exit fullscreen mode

Do it every time - tedious, isn't it?
In my team, we are using next script:


# how make script executable

# chmod +x

# this is necessary to split the command output on new lines



show_help () {

  echo "Usage: [environment] [SUBENV] - ssh to a pod from the environment-subenv"

  echo "Example: staging web - connect to the web pod in the staging environment"

  echo "It uses current directory in order to build the pod name"


if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then


  exit 1





project=$(echo "$working_dir" | tr '[:upper:]' '[:lower:]' )


kubectl ctx |  grep "$ENVIRONMENT" | tr -d '\n'  | xargs -0 kubectl ctx

output=$(kubectl -n "$namespace" get pods | grep "$SUBENV" | Head -n 1)

podname=${output%% *}; remainder="${remainder#* }"

echo "ssh-ing you into $podname . . ."

kubectl --namespace=${namespace} exec -it pod/${podname} -- bash
Enter fullscreen mode Exit fullscreen mode

It allows easily connect to pods from a specific namespace (a namespace depends on current directory - current project).
Subenv is necessary because we have different types of pods (web, activejobs, migration).

Top comments (0)