2

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.

zweack
  • 141
  • 1
  • 10
  • 1
    Inside the docker container, what happens if you configure the app to write its logs on `/dev/stdout`? Should (in theory) redirect the output on `stdout`, at least on a linux os. I never tried inside a docker container so I don't really know if it works or not inside, one but I'd make a try – AndD Nov 12 '21 at 07:57
  • Thanks for the comment, it works inside centos docker container and it is similar to the first solution suggested by @moonkotte – zweack Nov 15 '21 at 13:58

1 Answers1

2

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):

moonkotte
  • 3,661
  • 2
  • 10
  • 25
  • @WytrzymałyWiktor apologies, it was long weekend for me. – zweack Nov 15 '21 at 13:48
  • 2
    @moonkotte thanks for the answer. I have a design restriction - one container per pod, so can't use sidecar container but I will go with the first approach. – zweack Nov 15 '21 at 13:49