Nur Rony
Polyglot Programmer, DevOps and Forever Learner

SSH into Kubernetes to a running container

SSH into Kubernetes to a running container

The Kubernetes command line tool, kubectl, allows you to run different commands against a Kubernetes cluster. You can manipulate Kubernetes API objects, manage worker nodes, inspect cluster, execute commands inside running container, and get an interactive shell to a running container.

Suppose you have a pod, named hello-ssh. To get a shell to the running container on this pod, just run:

kubectl exec -it hello-ssh -- /bin/bash 
# see shell prompt ... 
root@hello-ssh:/#

How Does exec Work?

kubectl exec invokes Kubernetes API Server and it "asks" a Kubelet "node agent" to run an exec command against CRI (Container Runtime Interface), most frequently it is a Docker runtime.

The docker exec API/command creates a new process, sets its namespaces to a target container's namespaces and then executes the requested command, handling also input and output streams for created process.