Kubernetes - How to get pod logs within container

2/27/2019

I have two containers within my pod, one container is an app, that write logs (stdout/stderr), and second container is to read the logs of the app container, and to write it logs.txt file.

My question is, how can I tell the second container to read the logs of the app container?

When I'm on the host (with k8s), I can run:

 $kubectl logs -f my_pod >> logs.txt

and get the logs of pod.

But how can I tell one container to read the logs of another container inside same pod?

I did something similar with dockers: I mounted the docker.sock of the docker of the host, and did $docker logs -f app_container >> logs.txt But I can't (as far as I know) mount kubectl to container in pod, to run kubectl commands.

So how do you think I can do it?

-- Yagel
docker
kubectl
kubernetes

2 Answers

2/27/2019

You can use http request to obtain other pod logs from inside. Here are the docs

Basically you can use curl to do the following request:

GET /api/v1/namespaces/{namespace}/pods/{name}/log

-- Karol Samborski
Source: StackOverflow

2/27/2019

you could mount a volume on both containers, write a logfile on that volume on your "app" container, then read from that logfile in the other container.

the preferred solution is probably to use some kind of logshipper or sidecar to scrape the logs from stdout

(side note: you should probably never mount docker.sock into a container or try to run kubectl from one inside a container to the cluster, because you essentially give that container control over your cluster, which is a security no-no imho. if you still have to do that for whatever reason, make sure the cluster security is REALLY tight)

-- cy4n
Source: StackOverflow