If you find yourself needing to jump into a Kubernetes pod to either diagnose an issue, or run a particular command, you can use the bash connectivity functionality available from kubectl itself.

1. Get the pod name to connect to

First, you will need the pod name you want to connect to, you can get this by running kubectl get pods

This will return a list similar to:

1
2
3
$ kubectl get pods
NAME                                  READY   STATUS    RESTARTS   AGE
kubernetes-bootcamp-fb5c67579-8tghg   1/1     Running   0          7m39s

Take node of the name cell for later use.

2. Connect to the Pod using the Bash command

We can now connect to the pod by referring to it by name.

The syntax is kubectl exec -ti <pod-name> -- bash, so we can connect as follows:

kubectl exec -ti kubernetes-bootcamp-fb5c67579-8tghg -- bash

This will give us a response that indicates we are now on the pod via a bash shell:

1
2
$ kubectl exec -ti kubernetes-bootcamp-fb5c67579-8tghg -- bash
root@kubernetes-bootcamp-fb5c67579-8tghg:/#

We can now run any bash commands directly on the pod we connected to.

3. How to get out of the bash shell

Now that you’re in bash on the desired pod, you can return back to the host, either by typing exit or by pressing ctrl+d.