Kubernetes-如何在容器中获取pod日志 [英] Kubernetes - How to get pod logs within container

查看:651
本文介绍了Kubernetes-如何在容器中获取pod日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pod中有两个容器,一个容器是一个应用程序,用于写入日志(stdout/stderr),第二个容器是读取应用程序容器的日志,并将其写入logs.txt文件.

我的问题是,如何告诉第二个容器读取应用程序容器的日志?

当我在主机上(使用k8s)时,我可以运行:

 $kubectl logs -f my_pod >> logs.txt

并获取pod的日志.

但是如何告诉一个容器读取同一容器中另一个容器的日志?

我对码头工人做了类似的事情: 我挂载了主机的docker的docker.sock,并做了$docker logs -f app_container >> logs.txt 但是我(据我所知)无法将kubectl安装到pod的容器中,以运行kubectl命令.

那么您认为我能做到吗?

解决方案

您可以在两个容器上安装一个卷,在"app"容器上的该卷上写入一个日志文件,然后从另一个容器中的该日志文件中读取. /p>

首选解决方案可能是使用某种logshipper或sidecar来从stdout刮取日志

(旁注:您可能永远不应该将docker.sock安装到容器中或尝试从容器中的一个容器运行kubectl到集群,因为本质上是让该容器对集群进行控制,这是安全性恕我直言. 如果出于某种原因仍然需要这样做,请确保集群安全性非常严格)

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)

这篇关于Kubernetes-如何在容器中获取pod日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆