Nginx配置fluentd
[TOC]
1、fluentd介紹
Fluentd是一個完全開源免費的log資訊收集軟體,支援超過125個系統的log資訊收集。
本質上,Fluentd可以分為用戶端和服務端兩種子產品。用戶端為安裝在被采集系統中的程式,用于讀取log檔案等資訊,并發送到Fluentd的服務端。服務端則是一個收集器。在Fluentd服務端,我們可以進行相應的配置,使其可以對收集到的資料進行過濾和處理,并最終路由到下一跳。下一跳可以是用于存儲的資料庫,如MongoDB, elasticsearch, 也可以是其他的資料處理平台,比如Hadoop。
2、fluentd安裝和部署
2.1 安裝前配置
ntp時間同步(重要)
yum install ntp
vim /etc/ntp.conf //添加要同步的時間伺服器
/etc/init.d/ntpd start
檔案打開數
檢視目前設定
ulimit -n
增加最大檔案打開數,修改
/etc/security/limits.conf
root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 65536
修改 /etc/sysctl.conf
,添加如下内容
/etc/sysctl.conf
net.core.somaxconn = 1024
net.core.netdev_max_backlog = 5000
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_wmem = 4096 12582912 16777216
net.ipv4.tcp_rmem = 4096 12582912 16777216
net.ipv4.tcp_max_syn_backlog = 8096
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240 65535
配置生效
sysctl -p
2.2 安裝fluentd
rpm形式安裝
下載下傳最新的yum源并安裝
/etc/yum.repos.d/td.repo
//官方腳本
curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sh
也可以直接設定repo
# cat /etc/yum.repos.d/td.repo
[treasuredata]
name=TreasureData
baseurl=http://packages.treasuredata.com/2/redhat/$releasever/$basearch
gpgcheck=1
gpgkey=https://packages.treasuredata.com/GPG-KEY-td-agent
# yum install -y td-agent
啟動fluend
$ /etc/init.d/td-agent start
Starting td-agent: [ OK ]
$ /etc/init.d/td-agent status
td-agent (pid 21678) is running...
檢視狀态
$ /etc/init.d/td-agent status
yum安裝之後的配置檔案
/etc/td-agent/td-agent.conf
| 項目 | 路徑 |
| :-------- | --------:|
| 主配置檔案 | /etc/td-agent/td-agent.conf |
| 主程式 | /usr/sbin/td-agent |
| 程式日志 | /var/log/td-agent/td-agent.log |
| ruby程式位置 | /opt/td-agent/embedded/bin/ruby |
| pid位置 | /var/run/td-atent/td-agent.pid |
檢視預設主配置檔案的配置
# egrep -v "^#|^$" /etc/td-agent/td-agent.conf
<match td.*.*>
@type tdlog
apikey YOUR_API_KEY
auto_create_table
buffer_type file
buffer_path /var/log/td-agent/buffer/td
<secondary>
@type file
path /var/log/td-agent/failed_records
</secondary>
</match>
<match debug.**>
@type stdout
</match>
<source>
@type forward
</source>
<source>
@type http
port 8888
</source>
<source>
@type debug_agent
bind 127.0.0.1
port 24230
</source>
預設配置下的監聽狀态
# netstat -utpln |grep ruby
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 818/ruby
tcp 0 0 0.0.0.0:24224 0.0.0.0:* LISTEN 823/ruby
tcp 0 0 127.0.0.1:24230 0.0.0.0:* LISTEN 823/ruby
udp 0 0 0.0.0.0:24224 0.0.0.0:* 823/ruby
測試
通過8888端口送出一條日志
curl -X POST -d 'json={"json":"message"}' http://localhost:8888/debug.test
檢視輸出的日志
# tail -n 1 /var/log/td-agent/td-agent.log
2018-02-09 00:09:06.719633187 +0800 debug.test: {"json":"message"}
以上就是官方給出的最簡化的demo,作用不大,但可以初步了解fluentd的工作方式
3、CS結構

資料流的處理都是雙向的,既有Input方向也有Output方向。
角色:
- 日志轉發 //client承擔此角色
- 日志聚合 //server扮演此角色
通常安裝在每個節點上以接收本地日志,一旦接收到資料,通過網絡将其轉發到server端。
日志轉發
接收轉發而來的資料,緩沖,過濾并定期通過插件将資料上傳到其他存儲程式、本地檔案等媒介中。
日志聚合
==client、server使用同一樣部署方式,差別在于配置檔案的不同。==
4、配置講解
4.1 td-agent.conf
配置的組成部分
td-agent.conf
-
directives determine the input sources.source
-
directives determine the output destinations.match
-
directives determine the event processing pipelines.filter
-
directives set system wide configuration.system
-
directives group the output and filter for internal routinglabel
-
directives include other files.@include
source: 定義輸入,資料的來源,input方向
match:定義輸出,下一步的去向,如寫入檔案,或者發送到指定軟體存儲。output方向
filter:定義過濾,也即事件處理流水線,一般在輸入和輸出之間運作,可減少字段,也可豐富資訊。
system:系統級别的設定,如日志級别、程序名稱。
label:定義一組操作,進而實作複用和内部路由。
@include:引入其他檔案,和Java、python的import類似。
//使用最多的就是source、match、filter
例如一個source部分:
# 從 24224/tcp 接收資料
# This is used by log forwarding and the fluent-cat command
<source>
@type forward
port 24224
</source>
# 從9880/http協定接收資料
# http://this.host:9880/myapp.access?json={"event":"data"}
<source>
@type http
port 9880
</source>
//@type 是必須有的參數,指定類型就是指定使用的插件
//http:使 fluentd 轉變為一個 httpd 端點,以接受進入的 http 封包。
//forward:使 fluentd 轉變為一個 TCP 端點,以接受 TCP 封包。
5、插件介紹及安裝
Fluentd有6種類型的插件(或者叫方法),分别是:
Input:完成輸入資料的讀取,由source部配置設定置
//常用類型:tail、http、forward、tcp、udp、exec
Parser:解析插件,常與輸入、輸處配合使用,多見于`format`字段後面
//常用類型:ltsv、json、自定義等
Output:完成輸出資料的操作,由match部配置設定置
//常用配置:file、forward、copy、stdout、exec
filter:過濾插件
//常用配置:grep、ignore、record_transformer
Buffer:緩存插件,用于緩存資料
//常用配置:file、mem
Formatter:消息格式化的插件,用于輸出,允許使用者擴充和重新使用自定義輸出格式
//常用類型:ltsv、json等
注意:out_forward 轉發輸出插件将事件轉發到其他fluentd節點。 此插件支援負載平衡和自動故障切換,由 <server></server>
标記。 對于複制,請使用 out_copy 插件,
copy 輸出插件将事件複制到多個輸出。由
<store></store>
标記
安裝一些需要的插件的指令
/opt/td-agent/embedded/bin/gem install woothee fluent-plugin-woothee fluent-plugin-elasticsearch
安裝指定版本插件
td-agent-gem install fluent-plugin-woothee --version=0.2.1
td-agent-gem install woothee --version=1.4.0
//'fluent-plugin-woothee' is a Fluentd filter plugin to parse UserAgent strings and to filter/drop specified categories of user terminals (like 'pc', 'smartphone' and so on).
yum安裝的fluentd程式,等價于
td-agent-gem
/opt/td-agent/embedded/bin/gem
6、nginx日志配置樣例
nginx日志格式 ltsv
ltsv
log_format ltsv "time:$time_local"
"\trealip:$remote_addr"
"\txffip:$http_x_forwarded_for"
"\treq:$request"
"\tstatus:$status"
"\tsize:$body_bytes_sent"
"\treferer:$http_referer"
"\tua:$http_user_agent"
"\treqtime:$request_time"
"\tvhost:$host" ;
client端配置
##pc端日志收集
<source>
type tail
format ltsv
path /log/nginx/www.itouzi.access.log
pos_file /log/buffer/posfile/mergeua.www.access.log.pos
tag mergeua.www.access.nginx
time_key time
time_format %d/%b/%Y:%H:%M:%S %z
</source>
##app端日志收集
<source>
type tail
format ltsv
path /log/nginx/api.itzcdn.access.log
pos_file /log/buffer/posfile/mergeua.api.access.log.pos
tag mergeua.api.access.nginx
time_key time
time_format %d/%b/%Y:%H:%M:%S %z
</source>
##m站日志收集
<source>
type tail
format ltsv
path /log/nginx/m.itouzi.access.log
pos_file /log/buffer/posfile/mergeua.m.access.log.pos
tag mergeua.m.access.nginx
time_key time
time_format %d/%b/%Y:%H:%M:%S %z
</source>
##前端slb日志搜集
<source>
type tail
format ltsv
path /log/nginx/itouzi.frontEnd.log
pos_file /log/buffer/posfile/mergeua.frontend.log.pos
tag mergeua.frontend.access.nginx
time_key time
time_format %d/%b/%Y:%H:%M:%S %z
</source>
##對日志中的ua進行合并和分類
# Merged ua
<match mergeua.**>
type woothee
key_name ua
add_prefix merged
merge_agent_info yes
</match>
##收集的資料由tcp協定轉發到多個server的49875端口
## Multiple output
<match merged.mergeua.**>
type forward
<server>
name es01
host es01
port 49875
weight 60
</server>
<server>
name es02
host es02
port 49875
weight 60
</server>
</match>
server端配置
##定義收集來源和監聽端口
<source>
@type forward
bind 10.10.10.10
port 49875
</source>
##對聚合的資料将ip轉為地圖位址,添加新的tag頭部
<match merged.mergeua.www.access.nginx>
@type geoip
geoip_lookup_key realip
geoip_database /etc/td-agent/GeoLiteCity.dat
<record>
location_array '[${longitude["realip"]},${latitude["realip"]}]'
country_code3 ${country_code3["realip"]}
country ${country_code["realip"]}
city ${city["realip"]}
</record>
skip_adding_null_record true
add_tag_prefix geoip.
</match>
##聚合pc端資料複制到es和本地檔案
<match geoip.merged.mergeua.www.access.nginx>
@type copy
<store>
@type elasticsearch
localtime
index_name wwwaccess
hosts es-master-01,es-master-02,es-master-03
#port 9200
logstash_format true
logstash_prefix wwwaccess
flush_interval 3s
</store>
<store>
@type file
path /data/fluentd/www/wwwaccess.log
compress gzip
flush_interval 86400s
</store>
</match>
##聚合app端資料複制到es和本地檔案
<match merged.mergeua.api.access.nginx>
@type copy
<store>
@type elasticsearch
localtime
index_name apiaccess
hosts es-master-01,es-master-02,es-master-03
#port 9200
logstash_format true
logstash_prefix apiaccess
flush_interval 3s
</store>
<store>
@type file
path /data/fluentd/api/apiaccess.log
compress gzip
flush_interval 86400s
</store>
</match>
##聚合slb資料複制到es
<match merged.mergeua.frontend.access.nginx>
@type copy
<store>
@type elasticsearch
localtime
index_name frontendaccess
hosts es-master-01,es-master-02,es-master-03
#port 9200
logstash_format true
logstash_prefix frontendaccess
flush_interval 3s
</store>
</match>
##聚合m站資料複制到es
<match merged.mergeua.m.access.nginx>
@type copy
<store>
@type elasticsearch
localtime
index_name maccess
hosts es-master-01,es-master-02,es-master-03
#port 9200
logstash_format true
logstash_prefix maccess
logstash_dateformat %Y.%m
flush_interval 3s
</store>
</match>