When you run the command:
kubectl exec -it <pod> -n <namespace> -- /bin/sh
kubectl
determines which container to target based on the number of containers in the pod.
1. Single-container pod
If the pod contains only one container, kubectl
automatically executes the command in that container.
2. Multi-container pod
If the pod has multiple containers and you do not specify a container, kubectl
defaults to the first container defined in the pod's specification.
2.1. Specifying a container
To explicitly choose a container, use the -c
or --container
option:
kubectl exec -it <pod> -n <namespace> -c <container> -- /bin/sh
3. Understanding the connection:
kubectl exec
does not establish an SSH connection.
It uses the Kubernetes API to execute commands inside the container.
Therefore, an SSH server is not required inside the container for kubectl exec
to work.