我正在使用的当前设置是具有多个容器的 Docker 组合堆栈。这些容器将它们的日志信息发送到运行 Fluentd 守护进程的日志容器(在 compose 堆栈内)。 Fluentd 的配置包含一个 in_forward
收集日志并将它们写入单独文件的源,具体取决于容器。我的 Fluentd 配置文件类似于:
<source>
@type forward
port 24224
bind 0.0.0.0
</source>
<match container1>
@type copy
<store>
@type file
path /fluentd/log/container1.*.log
format single_value
message_key "log"
</store>
</match>
...
我的 docker-compose.yml 文件如下所示:
version: '3'
services:
container1:
build: ./container1
container_name: "container1"
depends_on:
- "logger"
logging:
driver: "fluentd"
options:
tag: container1
networks:
static-net:
ipv4_address: 172.28.0.4
...
logger:
build: ./logger
container_name: "logger"
ports:
- "24224:24224"
- "24224:24224/udp"
volumes:
- ./logger/logs:/fluentd/log
networks:
static-net:
ipv4_address: 172.28.0.5
networks:
static-net:
ipam:
driver: default
config:
- subnet: 172.28.0.0/16
一切都按预期工作,但我希望将 Fluentd 设置为保留一定数量的日志文件。我可以通过配置
chunk_limit_size
来更改日志文件的大小。缓冲区中的参数。然而,即使我想要这个选项,我仍然不希望 Fluentd 编写无限数量的文件。 buffer_queue_limit
和 overflow_action
在缓冲区配置中似乎没有任何影响。此应用程序在部署后将持续运行,因此日志轮换是必要的。我有几个问题:最佳答案
当您为 docker 使用 fluentd 日志驱动程序时,没有容器日志文件,只有 fluentd 日志,要轮换它们,您可以使用 this link.
如果您希望 docker 保留日志并轮换它们,那么您必须从以下位置更改堆栈文件:
logging:
driver: "fluentd"
options:
tag: container1
到
logging:
driver: "json-file"
options:
max-size: "5m" #max size of log file
max-file: "2" #how many files should docker keep
在 fluentd 中,您必须使用 in_tail 插件而不是 forward(fluentd 应该可以访问
/var/lib/docker/containers/*/*-json.log
中的日志文件)<source>
type tail
read_from_head true
pos_file fluentd-docker.pos
path /var/lib/docker/containers/*/*-json.log
tag docker.*
format json
</source>
关于ruby - Fluentd 是否支持文件输出的日志轮换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49632142/