Search code examples
kubernetesloggingcontainersbitnami

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

Application is being migrated to Kubernetes where it will run in a single pod. If I run kubectl log <pod-name>, I get anything that gets printed on stdout which I can redirect to a file. 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.

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. I want to mimic this behavior.


Solution

  • kubectl logs command takes everything from stdout and stderr, so you need to supply logs there.

    It's a common practice when containerised applications write their logs to stdout and stderr.

    This way there are two main options:

    1. Adjust the application so it writes logs to stdout and file as well. E.g. using shell it can be done with tee command.

      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. Please find Using a sidecar container with the logging agent

    Useful link about kubernetes logging (including containers):