为什么当Docker/Containerd写*.json.log时,流始终是stderr?

huangapple go评论75阅读模式
英文:

Why the stream is always stderr, when docker/containerd writes *.json.log

问题

当我使用Docker作为容器运行时(CRI)时,日志文件的流(stream)始终为stderr。我期望信息消息将写入stdout,错误消息将写入stderr。

当我使用containerd作为CRI时,日志文件的示例如下:

2022-07-19T11:13:32.228206987+08:00 stderr F I0719 11:13:32.228176       1 conntrack.go:100] Set sysctl 'net/netfilter/nf_conntrack_max' to 524288
2022-07-19T11:13:32.228229469+08:00 stderr F I0719 11:13:32.228203       1 conntrack.go:52] Setting nf_conntrack_max to 524288
2022-07-19T11:13:32.228460095+08:00 stderr F I0719 11:13:32.228431       1 conntrack.go:83] Setting conntrack hashsize to 131072
2022-07-19T11:13:32.237601129+08:00 stderr F I0719 11:13:32.237568       1 conntrack.go:100] Set sysctl 'net/netfilter/nf_conntrack_tcp_timeout_established' to 86400
2022-07-19T11:13:32.237638594+08:00 stderr F I0719 11:13:32.237613       1 conntrack.go:100] Set sysctl 'net/netfilter/nf_conntrack_tcp_timeout_close_wait' to 300
2022-07-19T11:13:32.237787689+08:00 stderr F I0719 11:13:32.237756       1 config.go:315] Starting service config controller

请注意,以上示例中的日志记录是stderr,而不是stdout。要解决此问题,您可能需要检查容器运行时的配置,以确保日志消息被正确发送到stdout 或 stderr。

英文:

when i use docker as CRI:

{"log":"I0421 14:23:18.944348       1 node.go:172] Successfully retrieved node IP: 192.168.49.2\n","stream":"stderr","time":"2023-04-21T14:23:18.944635198Z"}
{"log":"I0421 14:23:18.944724       1 server_others.go:142] kube-proxy node IP is an IPv4 address (192.168.49.2), assume IPv4 operation\n","stream":"stderr","time":"2023-04-21T14:23:18.944838628Z"}
{"log":"W0421 14:23:19.008388       1 server_others.go:578] Unknown proxy mode \"\", assuming iptables proxy\n","stream":"stderr","time":"2023-04-21T14:23:19.008544314Z"}
{"log":"I0421 14:23:19.008581       1 server_others.go:185] Using iptables Proxier.\n","stream":"stderr","time":"2023-04-21T14:23:19.008653777Z"}
{"log":"I0421 14:23:19.008904       1 server.go:650] Version: v1.20.0\n","stream":"stderr","time":"2023-04-21T14:23:19.008963124Z"}
{"log":"I0421 14:23:19.009762       1 config.go:315] Starting service config controller\n","stream":"stderr","time":"2023-04-21T14:23:19.009986673Z"}
{"log":"I0421 14:23:19.009867       1 shared_informer.go:240] Waiting for caches to sync for service config\n","stream":"stderr","time":"2023-04-21T14:23:19.009999075Z"}
{"log":"I0421 14:23:19.009973       1 config.go:224] Starting endpoint slice config controller\n","stream":"stderr","time":"2023-04-21T14:23:19.010041688Z"}
{"log":"I0421 14:23:19.009979       1 shared_informer.go:240] Waiting for caches to sync for endpoint slice config\n","stream":"stderr","time":"2023-04-21T14:23:19.01004961Z"}
{"log":"I0421 14:23:19.110110       1 shared_informer.go:247] Caches are synced for endpoint slice config \n","stream":"stderr","time":"2023-04-21T14:23:19.110273306Z"}
{"log":"I0421 14:23:19.110120       1 shared_informer.go:247] Caches are synced for service config \n","stream":"stderr","time":"2023-04-21T14:23:19.110525274Z"}

And

when i use containerd as CRI:

2022-07-19T11:13:32.228206987+08:00 stderr F I0719 11:13:32.228176       1 conntrack.go:100] Set sysctl 'net/netfilter/nf_conntrack_max' to 524288
2022-07-19T11:13:32.228229469+08:00 stderr F I0719 11:13:32.228203       1 conntrack.go:52] Setting nf_conntrack_max to 524288
2022-07-19T11:13:32.228460095+08:00 stderr F I0719 11:13:32.228431       1 conntrack.go:83] Setting conntrack hashsize to 131072
2022-07-19T11:13:32.237601129+08:00 stderr F I0719 11:13:32.237568       1 conntrack.go:100] Set sysctl 'net/netfilter/nf_conntrack_tcp_timeout_established' to 86400
2022-07-19T11:13:32.237638594+08:00 stderr F I0719 11:13:32.237613       1 conntrack.go:100] Set sysctl 'net/netfilter/nf_conntrack_tcp_timeout_close_wait' to 300
2022-07-19T11:13:32.237787689+08:00 stderr F I0719 11:13:32.237756       1 config.go:315] Starting service config controller

Any body know why the stream of log file is always stderr?

i expect that info messages is writing to stdout and err messages is writing to stderr.

答案1

得分: 1

STDOUT通常是命令的正常输出,而STDERR通常用于输出错误消息。默认情况下,Docker/容器日志显示命令的STDOUT和STDERR。记录的信息和日志格式几乎完全取决于容器的端点命令。当您将Docker/容器用作CRI来写入Json格式的日志时,它将默认显示为STDERR,需要在移动到日志代理之前解析这些日志。

有关更多信息,请参阅此GIT链接,作者是StevenAcoffman,他们在这里使用fluentbit作为日志解析器。

您还可以使用sematext作为支持CRI-O日志格式的日志代理,并安装其插件以解析日志。

默认情况下,Docker捕获所有容器的标准输出(和标准错误),并以JSON格式将它们写入文件。JSON格式会用其来源(stdout或stderr)和时间戳注释每一行。每个日志文件包含有关仅一个容器的信息。{"log":"日志行在这里\n","stream":"stdout","time":"2019-01-01T11:11:11.111111111Z"}

除此之外,您需要安装JSON日志文件驱动程序

英文:

STDOUT is usually a command’s normal output, and STDERR is typically used to output error messages. By default, docker/container logs show the command’s STDOUT and STDERR. This information that is logged and the format of the log depends almost entirely on the container’s endpoint command. When you use docker/ container as CRI to write the logs in Json it shows the default as STDERR and this needs to be parsed before moving to the logging agent.

Refer to this GIT LINK by stevenAcoffman for more information and here they used fluentbit as log parser.

You can also use sematext as a Log agent which supports CRI-O log format as well and install its Plugin to parse the logs.

> By default, Docker captures the standard output (and standard error)
> of all your containers, and writes them in files using the JSON
> format. The JSON format annotates each line with its origin (stdout or
> stderr) and its timestamp. Each log file contains information about
> only one container. {"log":"Log line is here\n","stream":"stdout","time":"2019-01-01T11:11:11.111111111Z"}

Along with the above, you need to install JSON Logging file driver

huangapple
  • 本文由 发表于 2023年5月17日 15:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定