简体   繁体   English

使用 kubectl log 命令从 Kubernetes 集群中容器内的特定文件获取日志

[英]Get logs from a specific file within container in Kubernetes cluster using kubectl log command

I have an application which stores logs in a file at a configurable location.我有一个应用程序,它将日志存储在一个可配置位置的文件中。 Let's say /abc/pqr/application.log假设/abc/pqr/application.log

Application is being migrated to Kubernetes where it will run in a single pod.应用程序正在迁移到 Kubernetes,它将在单个 Pod 中运行。 If I run kubectl log <pod-name> , I get anything that gets printed on stdout which I can redirect to a file.如果我运行kubectl log <pod-name> ,我会得到打印在stdout上的任何内容,我可以将其重定向到一个文件。 I want to do the other way around, I have a file containing logs at above location and I want kubectl logs <pod-name> to print logs from that file.我想kubectl logs <pod-name>做,我在上述位置有一个包含日志的文件,我希望kubectl logs <pod-name>从该文件打印日志。

For example, if run kubectl logs kafka for a kafka pod deployed using bitnami/kafka , I get logs from /opt/bitnami/kafka/logs/server.log .例如,如果为使用bitnami/kafka部署的 kafka pod 运行kubectl logs kafka kafka ,我会从/opt/bitnami/kafka/logs/server.log获取日志。 I want to mimic this behavior.我想模仿这种行为。

kubectl logs command takes everything from stdout and stderr , so you need to supply logs there. kubectl logs命令从stdoutstderr获取所有内容,因此您需要在那里提供日志。

It's a common practice when containerised applications write their logs to stdout and stderr .当容器化应用程序将其日志写入stdoutstderr时,这是一种常见做法。

This way there are two main options:这种方式有两个主要选择:

  1. Adjust the application so it writes logs to stdout and file as well.调整应用程序,使其也将日志写入stdout和文件。 Eg using shell it can be done with tee command.例如,使用 shell 可以通过tee命令来完成。

    Please find a good answers with description of the command.请找到带有命令描述的好答案

  2. Use a sidecar container which will be getting logs from file and translating them into its own stdout .使用 sidecar 容器,它将从文件中获取日志并将它们转换为自己的stdout Please find Using a sidecar container with the logging agent请找到使用带有日志代理的 sidecar 容器

Useful link about kubernetes logging (including containers):有关 kubernetes 日志记录(包括容器)的有用链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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