Robert Ranjan
Robert Ranjan

Reputation: 1886

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

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.

Upvotes: 9

Views: 12771

Answers (2)

Abu Hanifa
Abu Hanifa

Reputation: 3057

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

Upvotes: 13

Ajay
Ajay

Reputation: 272

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

Upvotes: -2

Related Questions