我正在运行多个包含Apache的容器。我希望所有这些特定的容器集将其日志输出记录到单个位置-文件还是-或可能记录日志?
通过某种方式,我可以将他们的日志汇总在一起-可以一起查看
我不是在寻找像流利的/ ELK堆栈这样的沉重解决方案。
如何实现以上目标?当前,所有容器都注销到/dev/stdout
,因此收集在“ docker日志”中。但是这些似乎不可能聚合在一起。
根据Save docker-compose logs to a file,看来我可以设置“日志路径”了-但是怎么办?记录驱动程序?可以在多个容器之间共享此日志文件吗?
systemd日志记录驱动程序是否合适?
所以journald
日志记录驱动程序让我很幸运。我在这样的容器上设置了一些labels
:
version: "3"
services:
nginx-lb:
labels:
- "node_service=nginx"
logging:
driver: "journald"
options:
labels: "node_service=nginx"
restart: always
network_mode: host
build: .
ports:
- "80:80"
- "443:443"
但是现在,当用journalctl
查看这些标签时,如何过滤这些标签?
以下是生成的日记条目示例:
{ "__CURSOR" : "s=b300aa41db4946f1bcc528e2522627ce;i=1087c;b=e6decf90a91f40c2ad7507e342fda85a;m=8744b1cdfa;t=5934bdb103a24;x=9ba66ecb768eb67", "__REALTIME_TIMESTAMP" : "1569328890657316", "__MONOTONIC_TIMESTAMP" : "580973088250", "_BOOT_ID" : "e6decf90a91f40c2ad7507e342fda85a", "_MACHINE_ID" : "c1339882251041f48f4612e758675ff3", "_HOSTNAME" : "staging", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_CAP_EFFECTIVE" : "3fffffffff", "_SELINUX_CONTEXT" : "unconfined\n", "_SYSTEMD_SLICE" : "system.slice", "_TRANSPORT" : "journal", "_PID" : "3969", "_COMM" : "dockerd", "_EXE" : "/usr/bin/dockerd", "_CMDLINE" : "/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock", "_SYSTEMD_CGROUP" : "/system.slice/docker.service", "_SYSTEMD_UNIT" : "docker.service", "_SYSTEMD_INVOCATION_ID" : "9f1488b462ae478a84bec6e64d72886b", "CONTAINER_NAME" : "3b9b51b4cda1a1e3b21a01f6fe80c7748fb3d231_apache_1", "CONTAINER_TAG" : "497b2f965b76", "SYSLOG_IDENTIFIER" : "497b2f965b76", "CONTAINER_ID" : "497b2f965b76", "CONTAINER_ID_FULL" : "497b2f965b767f897786f3bb8c4789dd91db1a91fe34e5ede368172f44fb3aac", "MESSAGE" : "192.168.240.1 - - [24/Sep/2019:12:41:30 +0000] \"GET / HTTP/1.0\" 200 2697 \"-\" \"curl/7.58.0\"", "_SOURCE_REALTIME_TIMESTAMP" : "1569328890657297" }
答案 0 :(得分:1)
我改用tag
日志记录选项。
version: "3"
services:
nginx-lb:
labels:
- "node_service=nginx"
logging:
driver: "journald"
options:
labels: "node_service=nginx"
tag: "nginx"
restart: always
network_mode: host
build: .
ports:
- "80:80"
- "443:443"
然后查看/过滤:
journalctl CONTAINER_TAG=nginx --since "1 hour ago"