DevOps/SRE

Fluentd+Fluent-bit日志集中方案

2019-03-14  本文已影响0人  大鹏之动

背景

最近在调研容器的日志集中方案,有考虑到使用ELK方案,但是把所有的数据都写进ES确实很浪费的资源的。既然考虑到成本,就直接把K8S的pod的日志通过中间层写入大存储,然后在大存储之上做日志的常规分析和提取。那么就选一个中间转换的组建,logstash、filebeat之前用过,不太理想,那今天尝试下CNCF(云原生计算基金会)孵化的一个项目Fluentd吧!

简介

Fluentd是一个开源的统一数据收集器,为了给你最好用,容易理数据,以统一的数据收集和消费数据。它关键的特性有,尽可能的把数据结构化为json,可插拔的架构,最小化的使用资源,支持缓存避免数据丢失,支持failover,支持高可用,强大的社区。

架构

下面是官方的,从我第一眼打开官网就喜欢这个组件了,因为架构清晰明了。


Fluentd架构 工作流

Fluentd 和 Fluent-bit对比差异

看到下面的差异,fluent-bit可以说是fluentd的精华版,支持的都是常用的模块。


Fluentd和Fluent-bit对比

环境说明

两台服务器(注:Fluent-bit只支持7以上版本,Fluentd可以支持6版本),本次实验模拟应用服务器写日志到本地,然后通过Fluent-bit支持的forward到Fluentd,Fluentd将日志集中写入本地存储归档。
存储服务器,IP:10.10.80.205,centos6
应用服务器,IP:10.10.69.235,centos7

安装软件

在应用服务器(10.10.69.235)上安装Fluent-bit,可以直接使用下面命令,如果版本不同,注意替换

yum install cmake gcc gcc-g++ -y
cd /opt/
wget https://fluentbit.io/releases/1.0/fluent-bit-1.0.4.tar.gz
tar -zxf fluent-bit-1.0.4.tar.gz
cd fluent-bit-1.0.4/build
cmake ../
make 
make install

在存储服务器(10.10.80.205)上安装Fluentd,可以直接使用下面命令。

curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent3.sh | sh
echo 'export PATH=$PATH:/opt/td-agent/embedded/bin/' >> /etc/profile
source /etc/profile

注:如果上面的文件不能下载,可以直接运行下面内容

# clear any previous sudo permission
sudo -k

# run inside sudo
sudo sh <<SCRIPT

  # add GPG key
  rpm --import https://packages.treasuredata.com/GPG-KEY-td-agent

  # add treasure data repository to yum
  cat >/etc/yum.repos.d/td.repo <<'EOF';
[treasuredata]
name=TreasureData
baseurl=http://packages.treasuredata.com/3/redhat/\$releasever/\$basearch
gpgcheck=1
gpgkey=https://packages.treasuredata.com/GPG-KEY-td-agent
EOF

  # update your sources
  yum check-update

  # install the toolbelt
  yes | yum install -y td-agent

SCRIPT

在存储服务器上配置Fluentd

内容如下保存为fluentd_forward.conf

<source>
  @type forward                       #开启forward,目前只有Fluentd支持
  bind 0.0.0.0
  port 24224
</source>

<match mem>                        #匹配有tag为mem的类型
  @type stdout                        #匹配成功直接标准输出
</match>

<match nginx-log>                 #匹配tag为nginx-log的类型
  @type file                             #匹配成功的通过file模块输出
  path /data/fluend-nginx         #输出文件路径
  append true                          #输出文件的模式,默认为false
  #compress gzip                    #生产环境可以使用压缩,释放空间
  <format>                               #对输出做格式化,这部分可以参照官网学习下,https://docs.fluentd.org/v1.0/articles/formatter_single_value
     @type single_value           #
     message_key log               #将json中,key为log的直接输出
</format>
</match>

上面保存成功后,用命令行运行服务

fluentd -c fluentd_forward.conf

在应用服务器上配置Fluent-bit

两种方式:
第一种,直接命令行

fluent-bit -i tail -p path=/var/log/nginx/access.log -p db=/tmp/nginx-log.db  -t nginx-log -o forward://10.10.80.205:24224

第二种,使用配置文件fluent-bit.conf,内容如下

[SERVICE]
    Flush        1
    Daemon       Off           
    Log_Level    info

[INPUT]
    Name        tail
    Path        /var/log/nginx/access.log
    Db          /tmp/nginx-log.db
    Tag         nginx-log 

[OUTPUT]
    Name        forward
    Match       *
    Host        10.10.80.205
    Port        24224

开始验证

启动存储服务器上的Fluentd,输出如下

[root@vincent tmp]# fluentd -c fluentd_forward.conf 
2019-03-15 05:21:39 +0800 [info]: parsing config file is succeeded path="fluentd_forward.conf"
2019-03-15 05:21:39 +0800 [info]: using configuration file: <ROOT>
  <source>
    @type forward
    bind "0.0.0.0"
    port 24224
  </source>
  <match mem>
    @type stdout
  </match>
  <match nginx-log>
    @type file
    path "/data/fluend-nginx"
    append true
    <format>
      @type "single_value"
      message_key "log"
    </format>
    <buffer time>
      path "/data/fluend-nginx"
    </buffer>
  </match>
</ROOT>
2019-03-15 05:21:39 +0800 [info]: starting fluentd-1.3.3 pid=13834 ruby="2.4.5"
2019-03-15 05:21:39 +0800 [info]: spawn command to main:  cmdline=["/opt/td-agent/embedded/bin/ruby", "-Eascii-8bit:ascii-8bit", "/opt/td-agent/embedded/bin/fluentd", "-c", "fluentd_forward.conf", "--under-supervisor"]
2019-03-15 05:21:39 +0800 [info]: gem 'fluent-plugin-elasticsearch' version '3.0.1'
2019-03-15 05:21:39 +0800 [info]: gem 'fluent-plugin-kafka' version '0.8.3'
2019-03-15 05:21:39 +0800 [info]: gem 'fluent-plugin-record-modifier' version '1.1.0'
2019-03-15 05:21:39 +0800 [info]: gem 'fluent-plugin-rewrite-tag-filter' version '2.1.1'
2019-03-15 05:21:39 +0800 [info]: gem 'fluent-plugin-s3' version '1.1.7'
2019-03-15 05:21:39 +0800 [info]: gem 'fluent-plugin-td' version '1.0.0'
2019-03-15 05:21:39 +0800 [info]: gem 'fluent-plugin-td-monitoring' version '0.2.4'
2019-03-15 05:21:39 +0800 [info]: gem 'fluent-plugin-webhdfs' version '1.2.3'
2019-03-15 05:21:39 +0800 [info]: gem 'fluentd' version '1.3.3'
2019-03-15 05:21:39 +0800 [info]: adding match pattern="mem" type="stdout"
2019-03-15 05:21:39 +0800 [info]: adding match pattern="nginx-log" type="file"
2019-03-15 05:21:39 +0800 [info]: adding source type="forward"
2019-03-15 05:21:39 +0800 [info]: #0 starting fluentd worker pid=13838 ppid=13834 worker=0
2019-03-15 05:21:39 +0800 [info]: #0 listening port port=24224 bind="0.0.0.0"
2019-03-15 05:21:39 +0800 [info]: #0 fluentd worker is now running worker=0

启动应用服务器上的fluent-bit,命令行启动和输出如下:

[root@node2 tmp]# fluent-bit -i tail -p path=/var/log/nginx/access.log -p db=/tmp/nginx-log.db  -t nginx-log -o forward://10.10.80.205:24224
Fluent Bit v1.0.4
Copyright (C) Treasure Data

[2019/03/14 21:31:35] [ info] [storage] initializing...
[2019/03/14 21:31:35] [ info] [storage] in-memory
[2019/03/14 21:31:35] [ info] [storage] normal synchronization mode, checksum disabled
[2019/03/14 21:31:35] [ info] [engine] started (pid=893)

查看日志归档

[root@vincent fluend-nginx]# pwd
/data/fluend-nginx
[root@vincent fluend-nginx]# ll
total 8
-rw-r--r--. 1 root root 612 Mar 15 05:36 buffer.b58414b649b0c6fcb33cc4dd9c5811a90.log
-rw-r--r--. 1 root root  68 Mar 15 05:36 buffer.b58414b649b0c6fcb33cc4dd9c5811a90.log.meta
[root@vincent fluend-nginx]# tail -n 3 buffer.b58414b649b0c6fcb33cc4dd9c5811a90.log
10.244.2.33 - - [14/Mar/2019:07:47:33 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "10.244.1.0"
10.244.2.33 - - [14/Mar/2019:07:47:36 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "10.244.1.0"
10.244.2.33 - - [14/Mar/2019:07:47:56 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "10.244.1.0"

参考文档

https://docs.fluentbit.io/manual/input/tail
https://docs.fluentbit.io/manual/output/forward
https://docs.fluentd.org/v1.0/articles/out_file
https://docs.fluentd.org/v1.0/articles/formatter_single_value
https://toolbelt.treasuredata.com/sh/install-redhat-td-agent3.sh

上一篇下一篇

猜你喜欢

热点阅读