Executing command inside pod from outside

2/13/2018

I have a deployment that creates pods on demand with an image and a volume mounted to it.

I have to exec a command in the pod to start up the service, but only once the node is running (hence why I am not doing it in the docker image).

How can I run a command inside the pod from outside (ie like a .sh file I can run from outside the cluster that will exec into the pods and run the command?)

Update: I found that using the command: header in the yaml will allow you to overwrite the ENTRYPOINT in the Dockerfile. See here

My new issue is that i don't know which is first; the volume being mounted or the command being run (I assume the latter). I need to have some contents from the volume before i can run the command. Any advice will be appreciated.

-- Ramboo19
kubernetes

2 Answers

2/13/2018
$ kubectl exec --help
Execute a command in a container.

Options:
  -c, --container='': Container name. If omitted, the first container in the pod will be chosen
  -p, --pod='': Pod name
  -i, --stdin=false: Pass stdin to the container
  -t, --tty=false: Stdin is a TTY

Usage:
  kubectl exec POD [-c CONTAINER] -- COMMAND [args...] [options]

Addition to Alex's answer

If you have multiple Containers in your Pod, you need to specify Container name, otherwise, you will exec into first Container.

kubectl exec -it -n <namespace-name> <pod-name> -c <container-name> -- COMMAND [args...]

Read details about kubectl exec

-- Mir Shahriar Sabuj
Source: StackOverflow

2/13/2018

For the initial question, something like the following would work:

kubectl exec -it pod --namespace=namespace cat /etc/hosts

Obviously change the pod name, namespace and command.

With regards to your update, the volume is created first, I know from mounting persistent storage for mongo containers then on startup the mongo entrypoint writes data to this location.

-- Alex Johnston
Source: StackOverflow