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?
Asked Answered
B

3

-5

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?

Bobettebobina answered 13/7, 2020 at 11:22 Comment(3)
A pod is nothing but a group of one or more containers.Can you elaborate what you mean by exec into a pod?Dessiedessma
so, the answer is no.. that's what I am looking at. A POD is a collection of one or more containers, it does not have an image or os associated with it... so it's not possible to exec using kubectl...Bobettebobina
but if pod has it's own process, which i think it does, then it will be possible to do nsenter to the process (if it has it's own namespace). I am not sure if a pod has it's own logical NS.Bobettebobina
D
4

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.

Dessiedessma answered 13/7, 2020 at 11:51 Comment(2)
does the pod have it's own NS different from the Host and the containers. ???Bobettebobina
By default pod has separate namespace than the host..but you can manipulate it to share certain things from the host..for example 'hostNetwork:true' will make the container inside the pod share same network namespace as the hostDessiedessma
T
1

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
Toothbrush answered 13/7, 2020 at 11:26 Comment(0)
O
1

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

Orian answered 13/7, 2020 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.