Log from kubernetes to file

10/1/2019

I want to continuously log from kubernetes pod where my application is running to a custom path/file. Is this possible and how to do it without some 3rd party logging processors?

-- deckard cain
kubernetes
logging

2 Answers

10/1/2019

Yes that's possible. For example, if your application is a Java application you can use logback or log4j2 libraries and configure it to write logs to a file location of your preference. I am pretty sure other runtimes also have similar logging framework. But, keep this in mind, kubernetes pods are ephemeral, it can die anytime, it is supposed to be stateless. So persist your state (in this case your logs) elsewhere asap. That's where the centralized logging solutions like Splunk or ELK becomes relevant.

-- so-random-dude
Source: StackOverflow

10/3/2019

Kubernetes by itself provides only basic logging like in this tutorial.

However in my attempt I was unsuccessful in writing any logs from default nginx container by using custom echo commands from cli using this technique. Only pre-configured nginx logs were working.

According to Kubernetes documentation this can't be done without using logging driver.

While Kubernetes does not provide a native solution for cluster-level logging, there are several common approaches you can consider. Here are some options:

  • Use a node-level logging agent that runs on every node.
  • Include a dedicated sidecar container for logging in an application pod.
  • Push logs directly to a backend from within an application.

Which is basically using 3rd party logging processors.

Kubernetes doesn’t specify a logging agent, but two optional logging agents are packaged with the Kubernetes release: Stackdriver Logging for use with Google Cloud Platform, and Elasticsearch. You can find more information and instructions in the dedicated documents. Both use fluentd with custom configuration as an agent on the node.

Intercepting stdout and stderr without logging driver also had negative results. Simplest solution is to use logging agent.

-- Piotr Malec
Source: StackOverflow