[英]Where exactly do the logs of kubernetes pods come from (at the container level)?
I'm looking to redirect some logs from a command run with kubectl exec
to that pod's logs, so that they can be read with kubectl logs <pod-name>
(or really, /var/log/containers/<pod-name>.log
).我希望将一些日志从使用kubectl exec
运行的命令重定向到该 pod 的日志,以便可以使用kubectl logs <pod-name>
(或者实际上是/var/log/containers/<pod-name>.log
)。 I can see the logs I need as output when running the command, and they're stored inside a separate log directory inside the running container.运行命令时,我可以看到我需要的日志为 output,它们存储在运行容器内的单独日志目录中。
Redirecting the output (ie >> logfile.log
) to the file which I thought was mirroring what is in kubectl logs <pod-name>
does not update that container's logs, and neither does redirecting to stdout.将 output (即>> logfile.log
)重定向到我认为镜像kubectl logs <pod-name>
中的内容的文件不会更新该容器的日志,也不会重定向到标准输出。
When calling kubectl logs <pod-name>
, my understanding is that kubelet gets them from it's internal /var/log/containers/
directory.当调用kubectl logs <pod-name>
时,我的理解是 kubelet 从它的内部/var/log/containers/
目录中获取它们。 But what determines which logs are stored there?但是是什么决定了哪些日志存储在那里? Is it the same process as the way logs get stored inside any other docker container?它与日志存储在任何其他 docker 容器中的方式相同吗?
Is there a way to examine/trace the logging process, or determine where these logs are coming from?有没有办法检查/跟踪日志记录过程,或确定这些日志的来源?
Logs from the STDOUT
and STDERR
of containers in the pod are captured and stored inside files in /var/log/containers.来自 pod 中容器的STDOUT
和STDERR
的日志被捕获并存储在 /var/log/containers 中的文件中。 This is what is presented when kubectl log
is run.这是运行kubectl log
时显示的内容。
In order to understand why output from commands run by kubectl exec is not shown when running kubectl log
, let's have a look how it all works with an example:为了理解为什么在运行kubectl log
时没有显示 kubectl exec 运行的命令中的 output ,让我们通过一个示例来看看它是如何工作的:
First launch a pod running ubuntu that are sleeping forever:首先启动一个运行 ubuntu 且永远处于休眠状态的 pod:
$> kubectl run test --image=ubuntu --restart=Never -- sleep infinity
Exec into it执行它
$> kubectl exec -it test bash
Seen from inside the container it is the STDOUT
and STDERR
of PID 1 that are being captured.从容器内部看,捕获的是 PID 1 的STDOUT
和STDERR
。 When you do a kubectl exec
into the container a new process is created living alongside PID 1:当您在容器中kubectl exec
时,会创建一个与 PID 1 并存的新进程:
root@test:/# ps -auxf
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 7 0.0 0.0 18504 3400 pts/0 Ss 20:04 0:00 bash
root 19 0.0 0.0 34396 2908 pts/0 R+ 20:07 0:00 \_ ps -auxf
root 1 0.0 0.0 4528 836 ? Ss 20:03 0:00 sleep infinity
Redirecting to STDOUT
is not working because /dev/stdout
is a symlink to the process accessing it ( /proc/self/fd/1
rather than /proc/1/fd/1
).重定向到STDOUT
不起作用,因为/dev/stdout
是访问它的进程的符号链接( /proc/self/fd/1
而不是/proc/1/fd/1
)。
root@test:/# ls -lrt /dev/stdout
lrwxrwxrwx 1 root root 15 Nov 5 20:03 /dev/stdout -> /proc/self/fd/1
In order to see the logs from commands run with kubectl exec
the logs need to be redirected to the streams that are captured by the kubelet ( STDOUT
and STDERR
of pid 1).为了查看使用kubectl exec
运行的命令的日志,需要将日志重定向到 kubelet 捕获的流(pid 1 的STDOUT
和STDERR
)。 This can be done by redirecting output to /proc/1/fd/1
.这可以通过将 output 重定向到/proc/1/fd/1
来完成。
root@test:/# echo "Hello" > /proc/1/fd/1
Exiting the interactive shell and checking the logs using kubectl logs
should now show the output退出交互式 shell 并使用kubectl logs
检查日志现在应该显示 output
$> kubectl logs test
Hello
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.