前言
docker默认的日志驱动是json-file,每个容器都会在本地生成一个/var/lib/docker/containers/containerID/containerID-json.log
,而日志驱动是支持扩展的,本章主要讲解的是Fluentd驱动收集docker日志.node
Fluentd是用于统一日志记录层的开源数据收集器,是继Kubernetes、Prometheus、Envoy 、CoreDNS 和containerd后的第6个CNCF毕业项目,经常使用来对比的是elastic的logstash,相对而言fluentd更加轻量灵活,如今发展很是迅速社区很活跃,在编写这篇blog的时候github的star是8.8k,fork是1k就可见一斑.git
前提
准备配置文件
docker-compose.ymlgithub
version: '3.7' x-logging: &default-logging driver: fluentd options: fluentd-address: localhost:24224 fluentd-async-connect: 'true' mode: non-blocking max-buffer-size: 4m tag: "kafeidou.{{.Name}}" #配置容器的tag,以kafeidou.为前缀,容器名称为后缀,docker-compose会给容器添加副本后缀,如 fluentd_1 services: fluentd: image: fluent/fluentd:v1.3.2 ports: - 24224:24224 volumes: - ./:/fluentd/etc - /var/log/fluentd:/var/log/fluentd environment: - FLUENTD_CONF=fluentd.conf fluentd-worker: image: fluent/fluentd:v1.3.2 depends_on: - fluentd logging: *default-logging
fluentd.confdocker
<source> @type forward port 24224 bind 0.0.0.0 </source> <match kafeidou.*> @type file path /var/log/fluentd/kafeidou/${tag[1]} append true <format> @type single_value message_key log </format> <buffer tag,time> @type file timekey 1d timekey_wait 10m flush_mode interval flush_interval 5s </buffer> </match> <match **> @type file path /var/log/fluentd/${tag} append true <format> @type single_value message_key log </format> <buffer tag,time> @type file timekey 1d timekey_wait 10m flush_mode interval flush_interval 5s </buffer> </match>
因为fluentd须要在配置的目录中有写入的权限,因此须要先准备好存放log的目录以及给予权限.
建立目录json
mkdir /var/log/fluentd
给予权限,这里用于实验演示,直接受权777架构
chmod -R 777 /var/log/fluentd
在docker-compose.yml和fluentd.conf的目录中执行命令:
docker-compose up -d
app
[root@master log]# docker-compose up -d WARNING: The Docker Engine you're using is running in swarm mode. Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node. To deploy your application across the swarm, use `docker stack deploy`. Creating network "log_default" with the default driver Creating fluentd ... done Creating fluentd-worker ... done
查看一下日志目录下,应该就有对应的容器日志文件了:async
[root@master log]# ls /var/log/fluentd/kafeidou/ fluentd-worker.20200215.log ${tag[1]}
这是我最后的一个实验结果,会建立一个${tag[1]}
目录,挺奇怪的,并且在这个目录下还会有两个文件elasticsearch
[root@master log]# ls /var/log/fluentd/kafeidou/\$\{tag\[1\]\}/ buffer.b59ea0804f0c1f8b6206cf76aacf52fb0.log buffer.b59ea0804f0c1f8b6206cf76aacf52fb0.log.meta
若是有明白这块的也欢迎一块儿交流!微服务
架构总结
为何不用docker的原始日志呢?
咱们先看一下原始的docker日志是怎么样一个架构:
docker会在本机的/var/lib/docker/containers/containerID/containerID-json.log
路径为每个容器生成一个log文件,存储docker的日志.
上图中总共有7个容器,当成7个微服务的话,在须要查看日志的时候就已经很不方便了,最差状况须要分别在三台机器上查看每个容器的日志.
使用了fluentd后有什么不同?
使用fluentd收集docker日志后能够将容器汇总到一块儿.来看看配置了本文的fluentd配置文件后的架构:
因为fluentd配置的是存储在fluentd所在机器的本地目录,因此效果是将其余机器的容器日志收集到fluentd所在机器的本地目录中.
fluentd只能将容器日志收集到本地吗?
fluentd实际上能够将收集到的日志再次传输出去,例如传输到elasticsearch等存储软件中:
fluentd灵活性
fluentd能作的事情还有不少,fluentd自己能做为传输节点也能做为接受节点,还可以过滤特定日志,格式化特定内容的日志,将匹配的特定日志再次传输出去,这里只是作到一个简单的收集docker容器日志的效果.
始发于 四颗咖啡豆 ,转载请声明出处.
关注公众号->[四颗咖啡豆] 获取最新内容