简体   繁体   中英

Kubernetes - How to get pod logs within container

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?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM