Reputation: 223
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?
Upvotes: 8
Views: 6298
Reputation: 1244
A container engine handles and redirects any output generated to a containerized application's
stdout
andstderr
streams.
On Linux, the set of file descriptors open in a process can be accessed under the path
/proc/PID/fd/
, wherePID
is the process identifier. File descriptor/proc/PID/fd/0
isstdin
,/proc/PID/fd/1
isstdout
, and/proc/PID/fd/2
isstderr
.
So, assuming the main command in a container has PID '1' run the following command inside a k8s Pod:
$ sh restorebackup.sh >> /proc/1/fd/1
Then you will see its stdout
in the Pod's logs.
$ kubectl logs yourPodName
...
some stdout from your script...
...
Keep in mind, that this way you only write the stdout
of your bash script.
To write stdout and stderr from your script, run it like this:
$ sh restorebackup.sh 1>> /proc/1/fd/1 2>> /proc/1/fd/2
Or modify the code of the script to write info messages to /proc/1/fd/1
and error messages to /proc/1/fd/2
.
Upvotes: 1
Reputation: 3254
Example :
[root@c3~]# echo foo | tee boo
foo
[root@c3~]# cat boo
foo
restorebackup.sh
should be modified in such a way that all the output generating commands should use tee
Upvotes: 2
Reputation: 531
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.
Upvotes: 12