使用fluentd作爲docker日誌驅動收集日誌

前言

docker默認的日誌驅動是json-file,每一個容器都會在本地生成一個/var/lib/docker/containers/containerID/containerID-json.log,而日誌驅動是支持擴展的,本章主要講解的是Fluentd驅動收集docker日誌.

Fluentd是用於統一日誌記錄層的開源數據收集器,是繼Kubernetes、Prometheus、Envoy 、CoreDNS 和containerd後的第6個CNCF畢業項目,常用來對比的是elastic的logstash,相對而言fluentd更加輕量靈活,現在發展非常迅速社區很活躍,在編寫這篇blog的時候github的star是8.8k,fork是1k就可見一斑.

前提

  1. docker

  2. 瞭解fluentd配置

  3. docker-compose

準備配置文件

docker-compose.yml

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_1services: 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.conf

<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的目錄以及給予權限.
創建目錄

mkdir /var/log/fluentd

給予權限,這裏用於實驗演示,直接授權777

chmod -R 777 /var/log/fluentd

在docker-compose.yml和fluentd.conf的目錄中執行命令:
docker-compose up -d

[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

查看一下日誌目錄下,應該就有對應的容器日誌文件了:

[root@master log]# ls /var/log/fluentd/kafeidou/
fluentd-worker.20200215.log ${tag[1]}

這是我最後的一個實驗結果,會創建一個${tag[1]}目錄,挺奇怪的,而且在這個目錄下還會有兩個文件

[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容器日誌的效果.


本文分享自微信公衆號 - 四顆咖啡豆(gh_71aedc0a994c)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章