我在 docker 中有以下设置:
应用程序的logdriver的配置正在描述Fluentd容器。日志将保存在 ES 中并显示在 Kibana 中。
当日志驱动程序配置如下时,它可以工作:
web:
image: httpd
container_name: httpd
ports:
- "80:80"
links:
- fluentd
logging:
driver: "fluentd"
options:
fluentd-address: localhost:24224
tag: httpd.access
Fluentd 正在将其暴露的端口 24224 映射到主机的端口 24224 上。
fluentd:
build: ./fluentd
image: fluentd
container_name: fluentd
links:
- "elasticsearch"
ports:
- "24224:24224"
但我不想在主机网络上暴露我的 fluidd。我想将其保持在 docker 网络内的“私有”状态(我只想将应用程序和 kibana 映射到主机网络上),如下所示:
fluentd:
build: ./fluentd
image: fluentd
container_name: fluentd
links:
- "elasticsearch"
端口 24224 仍然暴露(在 dockerfile 中),但它没有映射到主机网络上。现在我想更改我的应用程序的日志驱动程序的配置: 记录: 司机:“流利” 选项: 流利的地址:流利的:24224 标签:httpd.access
所以 Fluentd 是 Fluentd 容器的名称,它们位于同一网络中,但应用程序无法与其建立连接。
failed to initialize logging driver: dial tcp: lookup fluentd
这可能是因为日志记录选项是在撰写文件中的“链接”选项之前执行的吗?
有办法让它发挥作用吗?
目前这是不可能的。处理日志驱动程序的 docker deamon 是在主机上运行的进程。它不是您网络中的服务,因此无法将服务名称解析为 IP。请参阅此github问题以获取更详细的解释。
您必须发布一个端口才能使其工作。
这可以使用 UNIX 域套接字而不是 TCP 套接字来完成,以便日志记录驱动程序与 fluidd 进行通信。它独立于 docker 网络工作,并且可以在同一主机上的任何容器之间使用。不需要暴露 TCP 端口。
在 docker-compose.yml 中:
fluentd:
volumes:
# Create the folder and make sure it is writable by container so it creates the socket on first run
- "/var/run/fluentd/:/var/run/fluentd/"
web:
logging:
driver: fluentd
options:
fluentd-address: "unix:///var/run/fluentd/fluentd.sock"
fluentd-async: "true"
fluentd-max-retries: 60
depends_on:
- fluentd
在 Fluent.conf 中:
<source>
@type unix
path /var/run/fluentd/fluentd.sock
</source>
请注意,卷指的是目录,而不是 UNIX 套接字文件,因为套接字只会在首次运行时创建。
我还尝试使用
depends_on
和 fluentd-async
来允许正常启动(日志记录容器将重试,直到 fluidd 创建套接字)。我的意图是 fluent-max-retries
将阻止容器在没有日志记录的情况下无限期运行,但尚未对此进行实际测试。