Kubernetes - How to get pod logs within container

大兔子大兔子 提交于 2021-01-29 07:36:13

问题


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?


回答1:


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)




回答2:


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



来源:https://stackoverflow.com/questions/54907546/kubernetes-how-to-get-pod-logs-within-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!