How to execute a 'command with arguments' on a container of 'multi-container pod'?

12/4/2018

I have an app/pod: app1 with 2 containers service1 and service2. These services writes log at /var/log/app1Service1.log and /var/log/aapp1Service2.log. I'd like to tail log from mac's cli. Tried as below but did not work.

~ $ kubectl exec app1-6f6749ccdd-4ktwf -c app1Service1 "tail -f -n +1 /var/log/app1Service1.log"
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused "exec: \"tail -f -n +1 /var/log/app1Service1.log\": stat tail -f -n +1 /var/log/app1Service1.log: no such file or directory"

command terminated with exit code 126
~ $

below command works:

kubectl exec app1-6f6749ccdd-4ktwf -c app1Service1 ls
kubectl exec app1-6f6749ccdd-4ktwf -c app1Service1 "ls"

Seeing failures when I pass arguments to command.

-- Robert Ranjan
kubectl
kubernetes

2 Answers

12/4/2018

Add bash -c or if your container has sh then add sh -c

kubectl exec app1-6f6749ccdd-4ktwf -c app1Service1 -- bash -c "tail -f -n +1 /var/log/app1Service1.log"

Hope this'll help

-- Abu Hanifa
Source: StackOverflow

12/4/2018

Try this, login into the container by running the below cmd kubectl exec -it app1-6f6749ccdd-4ktwf -c app1Service1 bash

Now you are inside the container, check if the file exists and execute tail -f /var/log/app1Service1.log

-- Ajay G
Source: StackOverflow