Low Orbit Flux Logo 2 F

Kubectl Exec

This command allows you to connect to containers inside a cluster.

Use this to connect to the first container in a pod.


kubectl exec -it my-pod1 -- /bin/sh

Specify the container:


kubectl exec -it my-pod1 -c my-container3 -- /bin/sh

Connect by just specifying a deployment:


kubectl exec -it deployment/my-deployment1 -- /bin/sh

Table of options:

my-pod1 the pod
-t use it as a TTY
-i terminal is routed to the containers stdin
anything after this is passed to the container
/bin/sh the shell to run on the container
-q disable kubectl output and only show container output
-c specify a container
my-container3 the container name to connect to

This is a great tool that can be used for debugging and troubleshooting. Don’t use this to make permanent changes. That would kind of defeat the purpose of using Kubernetes and containers in the first place. Containers are meant to be ephemeral. Kubernetes creates, destroys, starts, and stops them dynamically.