目录
  1. 1. Fluentd安装
    1. 1.0.1. 配置NTP
    2. 1.0.2. 设置最大文件描述符
    3. 1.0.3. 优化内核TCP参数
    4. 1.0.4. 安装td-agent
    5. 1.0.5. 安装插件(可选)
    6. 1.0.6. 启动 & 测试
  • 2. 配置Fluentd
    1. 2.1. source
      1. 2.1.1. http
      2. 2.1.2. forward
      3. 2.1.3. secure_forward
      4. 2.1.4. tail
    2. 2.2. match
      1. 2.2.1. file
      2. 2.2.2. forward
      3. 2.2.3. secure_forward
      4. 2.2.4. mongo
    3. 2.3. include
  • 3. 配置实例
    1. 3.0.1. B(server)
    2. 3.0.2. A(client)
  • Fluentd安装

     多数内容都是按照官方文档来,详细参考官方文档

    配置NTP

     官方文档说明每个节点需要配好NTP,只有确保节点的时间是一致就行,我使用ntpdate使节点时间一致,或直接网络同步时间rdate -s time.nist.gov

    设置最大文件描述符

    1
    2
    3
    4
    ulimit -n 65535
    #新增以下内容到/etc/security/limits.conf
    * soft nofile 65535
    * hard nofile 65535

    优化内核TCP参数

    /etc/sysctl.conf配置文件新增以下内容,保持退出后执行sysctl -p

    1
    2
    3
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.ip_local_port_range = 10240 65535

    安装td-agent

     Fluentd有两个版本,一个是稳定版td-agent,一个是开发版Fluentd。这里在CentOS6.4 64bit上使用稳定版td-agent,不同平台参考官方安装文档

    1
    curl -L https://td-toolbelt.herokuapp.com/sh/install-redhat-td-agent2.sh | sh

     安装完后情况如下:

    • 默认td-agent安装路径:/opt/td-agent
    • 启动/停止/重启:/etc/init.d/td-agent [start|stop|restart|status|configtest]
    • 配置文件:/etc/td-agent/td-agent.conf
    • 日志路径:/var/log/td-agent/td-agent.log
    • 查看默认已安装插件:/opt/td-agent/embedded/bin/gem list|grep plugin
      1
      2
      3
      4
      5
      6
      7
      fluent-plugin-mongo (0.7.10)
      fluent-plugin-rewrite-tag-filter (1.4.1)
      fluent-plugin-s3 (0.5.9)
      fluent-plugin-scribe (0.10.14)
      fluent-plugin-td (0.10.27)
      fluent-plugin-td-monitoring (0.2.1)
      fluent-plugin-webhdfs (0.4.1)

    安装插件(可选)

     Fluentd支持许多插件,详见fluentd插件

    • 安装方法:td-agent-gem install plugin_name

    • 安装secure-forwardelasticsearch插件
      secure-forward加密传输内容,elasticsearch可将Fluentd收集的日志存入Elasticsearch

      1
      $> td-agent-gem install fluent-plugin-secure-forward fluent-plugin-elasticsearch

     如果是使用fluent-gem来安装,最好改用淘宝源

    1
    2
    3
    $> /opt/td-agent/embedded/bin/fluent-gem sources --remove https://rubygems.org/
    $> /opt/td-agent/embedded/bin/fluent-gem sources -a https://ruby.taobao.org/
    $> /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-secure-forward fluent-plugin-elasticsearch

    启动 & 测试

     测试Fluentd,动态查看日志tailf /var/log/td-agent/td-agent.log

    1
    2
    3
    $> /etc/init.d/td-agent start

    curl -X POST -d 'json={"json":"message"}' http://localhost:8888/debug.test

    配置Fluentd

     通过官方脚本安装(rpm/deb)的td-agent,默认配置文件/etc/td-agent/td-agent.conf
     Fluentd配置中只有三种基本指令:

    • source:指定日志记录的来源
    • match:指定输出动作,指定经过Fluentd处理后的结果输出目的地,可以是输出到文件、数据库,也可以转发到另一个Fluentd中。
    • include:指定包含的其他配置文件

     详细配置分析

    source

    source配置输入来源以及选择什么输入插件,官方列出的所有输入插件详见这里。Fluentd内置标准的输入插件httpforward,除了httpforward外还有tail也是非常常用的,source必须包含type指明Fluentd使用哪个输入插件接收数据。

    • http:指定Fluentd接收来自HTTP的数据
    • forward:指定Fluentd接收来自TCP的数据。sourceforward用于汇聚端,接收来之客户端转发的日志。
    • secure_forward:通过SSL使Fluentd传输数据更安全。同forward插件,多用于汇聚端。
    • tail:指定Fluentd从文件中接收数据

    http

    1
    2
    3
    4
    5
    6
    7
    # http input
    # POST http://localhost:8888/<tag>?json=<json>
    # POST http://localhost:8888/td.myapp.login?json={"user"%3A"me"}
    <source>
    type http
    port 8888
    </source>

    forward

    1
    2
    3
    4
    5
    6
    7
    8

    # forward input
    # Receive events from 24224/tcp
    # This is used by log forwarding and the fluent-cat command
    <source>
    type forward
    port 24224
    </source>

    secure_forward

     汇聚端先使用secure-forward-ca-generate 路径 密码生成证书和公钥(需要将ca_cert.pem拷贝到客户端中)
     默认的端口是24284

    1
    $> /opt/td-agent/embedded/bin/secure-forward-ca-generate /path/to/certificate passphrase_for_private_CA_secret_key

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #secure_forward input
    <source>
    type secure_forward
    shared_key secret_string
    port 22222
    self_hostname server.fqdn.local
    secure true
    ca_cert_path /path/to/certificate/ca_cert.pem
    ca_private_key_path /path/to/certificate/ca_key.pem
    ca_private_key_passphrase passphrase_for_private_CA_secret_key
    authentication yes
    <user>
    username tagomoris
    password foobar012
    </user>
    </source>

    tail

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    # tail input
    <source>
    type          tail
    tag           fluentd.tail.test #事件tag
    path          /var/wwwlog/nginx/access.log #日志路径,多个日志用','分隔
    pos_file      /tmp/fluentd.pos #记录上次读到日志的位置
    #format       [apache2|nginx|自定义正则] #日志格式匹配
    format        /^(?<remote>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*) "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" (?<forward>[^ ]*) (?<request_length>[^ ]*) (?<request_time>[^ ]*) (?<upstream_addr>[^ ]*) (?<host>[^ ]*)/
    time_format %d/%b/%Y:%H:%M:%S %z
    </source>

    tail的日志参数format支持正则表达式(?<名称>模式),可以通过这个网站对自己日志格式的正则进行调试
    time_format是指定format中时间格式time,假若format中有time则需要time_format
     我用的是Nginx,所用日志格式如下所示:

    1
    2
    3
    4
    #Nginx log_format
    log_format main '$remote_addr [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" $http_x_forwarded_for $request_length $request_time $upstream_addr $host';

    match

    match指定Fluentd输出动作,官方列出的所有输出插件详见这里match会根据source中的tag进行匹配进而做不同的处理。Fluentd标准的输出插件是fileforward。和source一样,match中会有type

    • file:将输出内容写入文件
    • forward:将输出内容转发到其他fluentd节点。matchforward多用于客户端转发日志
      forward输出插件有重试机制以确保数据能被顺利的被转发,转发数据缓存在磁盘,初始默认重发间隔为1s,重试17次,每次间隔时间增加2倍(类似TCP的指数退避),超过17后日志会被丢弃(极端情况是36小时都无法转发则日志会被丢弃),可以设置无限重发(disable_retry_limit=true)
    • secure_forward:通过SSL安全的将输入内容转发到fluentd汇聚节点。如果汇聚节点的fluentd开启了secure true则需要将汇聚节点的证书拷贝过来。
    • mongo:将输出内容写入到MongoDB(td-agent默认已安装mongodb插件)

    file

     从sourceforward中接收日志并通过matchfile写入到文件
     默认的file输出格式:time[delimiter]tag[delimiter]record\n,可通过参数自定义,详细参考这里

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    #forward input
    <source>
    type forward
    port 22222
    </source>

    #file output
    #tag匹配'fluentd.tail.test'的输出将写入到文件中
    <match fluentd.tail.test>
    type file
    append true #日志部分割
    output_tag false #不输出tag,默认输出
    output_time false #不单独输出time
    include_time_key true #将time放入record中,默认将time单独输出
    flush_interval 5s
    time_format %Y/%m/%d %H:%M:%S #设置时间格式
    path /var/log/td-agent/test/test #文件保持路径,文件名类似:/var/log/td-agent/test/test.20150901.log
    </match>

    forward

    typeserver是必选项,其余可选。secondary——当所有server都不可用时则使用此项(备份选项)
    matchforward转发host机器的port需要iptables对相应的TCPUPD端口都要放行,若只开放TCP端口是无法forward成功的!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    #forward output
    <match pattern>
    type forward
    send_timeout 60s
    recover_wait 10s
    heartbeat_interval 1s
    phi_threshold 16
    hard_timeout 60s

    <server>
    name myserver1
    host 192.168.1.3
    port 24224
    weight 60
    </server>
    <server>
    name myserver2
    host 192.168.1.4
    port 24224
    weight 60
    </server>

    <secondary>
    type file
    path /var/log/fluent/forward-failed
    </secondary>
    </match>

    secure_forward

    ca_cert_path证书需要从汇聚端上拷贝。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <match secret.data.**>
    type secure_forward
    shared_key secret_string
    self_hostname client.fqdn.local
    secure true
    ca_cert_path /path/to/certificate/ca_cert.pem

    <server>
    host first.fqdn.local
    port 22222
    username tagomoris
    password foobar012
    </server>
    </match>

    mongo

    typehostportdatabase(库名)、collection都是必选项(collection没有时tag_mapped才是必须)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <match mongo.**>
    type mongo
    host fluentd
    port 27017
    database fluentd
    collection fluentd

    # for capped collection
    capped
    capped_size 1024m

    # authentication
    user fluentd
    password fluentd

    # key name of timestamp
    time_key time

    # flush
    flush_interval 5s
    </match>

    matchtag支持通配符匹配,详细内容查看这里tag的匹配按照配置文件从上到下进行,一旦匹配成功则执行相应操作并停止继续匹配
    MongoDB输出中使用了capped collection,对于纯粹日志类型数据效果很好,这种类型的collection是指定大小,当空间满后会用新数据覆盖旧数据,更详细信息查阅MongoDB官方文档

    include

    include为了方便不同配置文件的管理,避免将全部项目的配置都写到一个配置文件中导致臃肿。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # absolute path
    include /path/to/config.conf

    # if using a relative path, the directive will use
    # the dirname of this config file to expand the path
    include extra.conf

    # glob match pattern
    include config.d/*.conf

    # http
    include http://example.com/fluent.conf

    配置实例

     以下是具体的配置实例。

    1. A服务器为日志收集客户端,B服务器为日志收集的集中汇总端。
    2. A(client)通过Fluentd中sourcetail插件收集日志并通过matchforward转发给B(server)
    3. B(server)通过Fluentd中sourceforward接收A(client)转发过来的日志并通过matchfilemongo等插件汇入到不同的存储中。

    B(server)

     配置文件/etc/td-agent/td-agent.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    $> /sbin/iptables -A INPUT -p tcp --dport 22222 -j ACCEPT
    $> /sbin/iptables -A INPUT -p udp --dport 22222 -j ACCEPT


    <source>
    type forward
    port 22222
    </source>

    <match mogl_test.**>
    type file
    append true
    output_tag false
    output_time false
    include_time_key true
    flush_interval 5s
    time_format %Y/%m/%d %H:%M:%S
    path /var/wwwlog/fluentd/mogl_test/mogl_test
    </match>

    matchfile参数详解

    • append:文件是否使用追加模式。默认false,但会生成许多零碎文件。true则每天只有一个文件,所有的日志都追加到此文件中。
    • output_tag:tag字段取消。默认true。默认格式:2014-06-08T23:59:40[TAB]your.tag[TAB]{"field1":"value1","field2":"value2"}\n,更改后格式:2014-06-08T23:59:40[TAB]{"field1":"value1", "field2":"value2"}\n
    • output_time、include_time_key:output_time取消time字段、include_time_keytime字段添加到记录中。默认true。默认格式见上
    • flush_interval:写入磁盘时间间隔
    • time_format:time字段的格式
    • path:文件存储路径。文件名:/var/wwwlog/fluentd/mogl_test/mogl_test.20150901.log

    A(client)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    <source>
    type tail
    tag mogl_test
    path /var/wwwlog/mogl_test/access.log
    pos_file /tmp/mogl_test.fluentd.pos
    format        /^(?<remote>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*) "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" (?<forward>[^ ]*) (?<request_length>[^ ]*) (?<request_time>[^ ]*) (?<upstream_addr>[^ ]*) (?<host>[^ ]*)/
    time_format %d/%b/%Y:%H:%M:%S %z
    </source>


    <match mogl_test>
    type forward
    send_timeout 60s
    recover_wait 10s
    heartbeat_interval 15s
    phi_threshold 16
    hard_timeout 60s

    <server>
    name log.mogl.net
    host log.mogl.net
    port 22222
    </server>

    <secondary>
    type file
    path /tmp/mogl_test.fail
    </secondary>
    </match>

    Powered: Hexo, Theme: Nadya remastered from NadyMain