Home > Software engineering >  how to generate a log in kubectl pod by command line
how to generate a log in kubectl pod by command line

Time:03-19

I have a mysqldb pod and need to add a log when we run a script restorebackup.sh. The script is inside the pod and i need this log in the general logs of the pod (to access it with kubectl get logs services/mysqldb).

There is any way to do this?

CodePudding user response:

Generally the kubectl logs shows the first process's stdout (pid=1). So you could try put logs to /proc/1/fd/1 in you pod.

An example command in pod:

echo hello >> /proc/1/fd/1

Then you will able to see this hello by kubectl logs.

For you script restorebackup.sh, maybe you could try sh restorebackup.sh >> /proc/1/fd/1 to redirect all outputs.

CodePudding user response:

  • kubectl logs is nothing but the stdout&stderr stream of the container (pid 1 process) in it
  • So to have restorebackup.sh output saved to a file and also to stream it to stdout , we can use utilities like tee ( it saves output to a file and also to stdout)

Example :

[root@c3~]# echo foo | tee boo
foo
[root@c3~]# cat boo
foo
  • in your case restorebackup.sh should be modified in such a way that all the output generating commands should use tee
  • Related