Home > Blockchain >  in k8s how to redirect docker‘s stdout to volume file?
in k8s how to redirect docker‘s stdout to volume file?

Time:03-07

I am working with logs in my system.
I want to use a log sidecar to collect business container's log.

And my business container's log will write to its STDOUT.

So I want to redirect this STDOUT to pod's volume file, because in a pod all containers share the same volume, so my sidecar can collect log from volume.

How should I configuer this?
I mean maybe I should write some configuration in my k8s yaml so k8s will automaticlly redirect the container's STDOUT to pod's volume?

CodePudding user response:

You could use a Sidecar container with a streaming container

By having your sidecar containers write to their own stdout and stderr streams, you can take advantage of the kubelet and the logging agent that already run on each node.

The sidecar containers read logs from a file, a socket, or journald. Each sidecar container prints a log to its own stdout or stderr stream.

This approach allows you to separate several log streams from different parts of your application, some of which can lack support for writing to stdout or stderr.
The logic behind redirecting logs is minimal, so it's not a significant overhead.

Additionally, because stdout and stderr are handled by the kubelet, you can use built-in tools like kubectl logs.

In your case, it depends on how your application pod can be configured (for intance, with the journald service active, in order to record logs)

And the backend would be your common volume file.

CodePudding user response:

Adding this 2>&1 > /<your_path_to_volume_inside_pod>/file.log to your command would redirect STDOUT and STDERR to a file

  • Related