2

I am using nomad-autoscaler image to run a pod that emits the logs on standard out, I want to send these logs into a file (within the pod) to enable a liveness check based on logfile content.

Used below dockerfile -

FROM hashicorp/nomad-autoscaler:0.3.5

USER 0
RUN touch /var/log/nomad-autoscaler.log \
&& ln -sf /proc/$$/fd/1 /var/log/nomad-autoscaler.log

but there is nothing in the log file when tailing the file.

I've followed the solutions -

  1. Added an entrypoint.sh file which redirects the output with tee
# entrypoint.sh
set -e 
"$@" | tee -a "/log/nomad-autoscaler-logs.log"

This creates the file but not adding the logs to that, it is always 0 size, I can see the below processes in the container

/ $ ps
PID   USER     TIME  COMMAND
    1 nomad-au  0:00 sh /log/script/entrypoint.sh nomad-autoscaler agent -config /etc/nomad-autoscaler/conf/shared -config /etc/nomad-autoscaler/conf/secret
    7 nomad-au  2:37 nomad-autoscaler agent -config /etc/nomad-autoscaler/conf/shared -config /etc/nomad-autoscaler/conf/secret
    8 nomad-au  0:00 tee -a /log/nomad-autoscaler-logs.log
  1. But when I redirect the output, I can see the logs in file but not it is not populated on standard output.
# entrypoint.sh
set -e 
"$@" &> "/log/nomad-autoscaler-logs.log"

Additional info about log redirection configured within the container -

/ $ ls -l /dev/stdout /dev/stderr  /proc/self/fd/2 /proc/self/fd/1
lrwxrwxrwx    1 root     root            15 Jan  3 18:44 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx    1 root     root            15 Jan  3 18:44 /dev/stdout -> /proc/self/fd/1
lrwx------    1 nomad-au nomad-au        64 Jan  4 19:37 /proc/self/fd/1 -> /dev/pts/2
lrwx------    1 nomad-au nomad-au        64 Jan  4 19:37 /proc/self/fd/2 -> /dev/pts/2

Any suggestions will be a great help.

Atul Singh
  • 59
  • 1
  • 9
  • `tee` only redirects `stdout`. If the output is going out of `stderr`, it will be ignored by `tee`. You can redirect `stderr` to `stdout` and then `tee` the result using `{command} 2>&1 | tee ...` see if that helps . – Blender Fox Jan 05 '23 at 07:55
  • Hi @Atul Singh will this [link](https://www.reddit.com/r/kubernetes/comments/zw656u/send_standard_output_log_to_a_file_in_k8s/) helps? – Dharani Dhar Golladasari Jan 05 '23 at 15:29
  • @DharaniDharGolladasari I have tried all those options but nothing seems like working. – Atul Singh Jan 05 '23 at 16:09
  • @BlenderFox I am getting below error when used the tee like that `line 3: syntax error: unexpected end of file (expecting "}")` my entrypoint.sh is below - `set -e; { "$@" } 2>&1 | tee -a /log/nomad-autoscaler-logs.log` – Atul Singh Jan 05 '23 at 16:17
  • 1
    @AtulSingh try `set -e; { "$@" 2>&1 | tee -a /log/nomad-autoscaler-logs.log } ` and also, where did the `{..}` come from, they were not in your question – Blender Fox Jan 05 '23 at 17:42
  • @BlenderFox Error is same with this also, and yes, My entrypoint.sh script does not have "}". If I am using only `"$@" | tee -a /log/nomad-autoscaler-logs.log`, I can see the file has been created but nothing is written. I am trying to understand why It is 0 byte. – Atul Singh Jan 05 '23 at 18:03
  • @BlenderFox Thanks after doing some research, things worked out for me. `"$@" | tee -a /log/nomad-autoscaler-logs.log` – Atul Singh Mar 28 '23 at 02:01

0 Answers0