Is it possible to exec in to a K8s pod the same way we exec in to a docker containers or containers running inside of a pod?

7/13/2020

Is it possible to exec in to a K8s pod the same way we exec into docker containers or containers running inside of a pod?p

Edit -
This question not about execing into a container in a pod. This is about the pod itself. Might be it's not possible but that is what the question is about. So stop marking it duplicate with - Can we exec into container in a POD in K8S?

-- samshers
docker
kubernetes
kubernetes-pod
namespaces
nsenter

3 Answers

7/13/2020

Pod is a group of containers and is a logical concept. So you can not really exec into a pod. All you can do is exec into one of the containers in the pod.

kubectl exec command might make you think that you are execingy into a pod but you actually exec into the container.This command works only if its a single container pod.If there are multiple container in the pod i.e it's a multi container pod then you need to choose a container explicitly using -c option.

Here is the output of kubectl exec -h which mentions about containers too.

Execute a command in a container.

Examples:
  # Get output from running 'date' command from pod mypod, using the first container by default
  kubectl exec mypod -- date
  
  # Get output from running 'date' command in ruby-container from pod mypod
  kubectl exec mypod -c ruby-container -- date
  
  # Switch to raw terminal mode, sends stdin to 'bash' in ruby-container from pod mypod
  # and sends stdout/stderr from 'bash' back to the client
  kubectl exec mypod -c ruby-container -i -t -- bash -il

A pause container gets created before any other actual container of the pod gets created.Responsibility of the pause container is to create the linux namespaces which will be shared among the other containers of the pod.

There is no way to exec into that pause container using kubectl exec but you can exec into it using docker exec.

-- Arghya Sadhu
Source: StackOverflow

7/13/2020

Yes, using kubectl exec command we can shell into a running container/pod

controlplane $ kubectl run --image=nginx web --restart=Never
pod/web created
controlplane $ kubectl get po
NAME   READY   STATUS              RESTARTS   AGE
web    0/1     ContainerCreating   0          4s
controlplane $ kubectl exec -it web -- /bin/bash
root@web:/# ls
bin   dev                  docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc                   lib   media  opt  root  sbin  sys  usr
-- P Ekambaram
Source: StackOverflow

7/13/2020

A pod is an abstract entity that wraps your container. When you exec to a pod via kubectl exec -it <yourPod> you are actually executing the wrapper command for your container. Also there is nothing for you to exec in to Pod.

-- Amit
Source: StackOverflow