'How to get output of any command run inside k8 POD to stout?

My requirement is that in my POD multiple processes are running and I want to collect the metrices for all the processes i.e (CPU AND MEMORY and other details OF all Process). Now , I want to write the output of any command I run inside my pod to stout .



Solution 1:[1]

A container engine handles and redirects any output generated to a containerized application's stdout and stderr streams. For example, the Docker container engine redirects those two streams to a logging driver, which is configured in Kubernetes to write to a file in JSON format.

Usually, it is PID1 process's stdout and stderr.
So, try the following command inside a k8s Pod:

$ cat /proc/meminfo >> /proc/1/fd/1

Then you will see the standard output in the pod's logs:

$ kubectl logs yourPodName
...
MemTotal:       12807408 kB
MemFree:        10283624 kB
MemAvailable:   11461168 kB
Buffers:           50996 kB
Cached:          1345376 kB
...

To write stdout and stderr from the command, run it like this:

$ cat /proc/meminfo 1>> /proc/1/fd/1 2>> /proc/1/fd/2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1