EFK (Fluentd ElasticSearch Kibana) 採集nginx日誌

本文描述如何經過FEK組合集中化nginx的訪問日誌。本人更喜歡按順序來命名,因此使用FEK而不是EFK.linux

首先在nginx服務器上執行如下操做.nginx

安裝ruby正則表達式

http://blog.csdn.net/chenhaifeng2016/article/details/78678680瀏覽器

安裝Fluentd curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | shruby

systemctl start td-agent服務器

安裝插件併發

td-agent-gem install fluent-plugin-elasticsearch td-agent-gem install fluent-plugin-typecastapp

安裝ElasticSearchcurl

下載軟件包並解壓elasticsearch

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.tar.gz

建立組和用戶

groupadd elsearch

useradd elsearch -g elsearch -p elsearch

chown -R elsearch:elsearch /usr/local/src/elsearch

編輯/etc/sysctl.conf

vm.max_map_count=655360

查看一下是否生效

sysctl -p

編輯/etc/security/limits.conf,在文件尾添加如下內容

elsearch soft nofile 65536

elsearch hard nofile 65536

編輯config/elasticsearch.yml,修改如下配置項

network.host: 0.0.0.0

切換到用戶elsearch

su elsearch

運行elasticsearch

bin/elasticsearch

若是要以守護進程方式運行加上參數-d

安裝Kibana

下載並解壓縮文件。

wget https://artifacts.elastic.co/downloads/kibana/kibana-6.0.0-linux-x86_64.tar.gz

修改配置文件config\kibana.yaml

server.port: 5601

server.host: "0.0.0.0"

elasticsearch.url: "http://localhost:9200"

運行kibana

bin/kibana

安裝Logstash (這一步非必須)

下載並解壓縮文件

wget https://artifacts.elastic.co/downloads/logstash/logstash-6.0.0.tar.gz

建立配置文件logstash.conf

運行./logstash -f logstash.conf

輸入hello the world

在kibana建立索引模式

在kibana的Discover界面就能夠看到信息了

接下來配置fluentd

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

<source> [@type](https://my.oschina.net/fanren1919) tail path /usr/local/nginx/logs/access.log pos_file /usr/local/nginx/logs/access.log.pos

tag nginx.access format /^(?<host>[^ ]) [^ ] (?<user>[^ ]) [(?<time>[^]])] "(?<method>\S+)(?: +(?<path>[^ ]) +\S)?" (?<code>[^ ]) (?<size>[^ ])(?: "(?<referer>[^"])" "(?<agent>[^"])")?$/ time_format %d/%b/%Y:%H:%M:%S %z

</source> <match nginx.access> [@type](https://my.oschina.net/fanren1919) elasticsearch host localhost port 9200 # index_name fluentd flush_interval 10s logstash_format true # typename fluentd </match>

在/usr/local/nginx/logs/下面建立access.log.pos文件

touch access.log.pos

chmod a+rw access.log.pos

重啓fluentd

systemctl restart fluentd

修改nginx.conf, 更改日誌格式

log_format main '$remote_addr $http_host [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'; 重啓nginx nginx -s reload

經過瀏覽器訪問nginx

查看fluentd的日誌

tail -f /var/log/td-agent/td-agent.log

說明fluent讀取nginx的access.log成功併發給了elasticsearch.

在kibana查看日誌

參考資料

正則表達式:http://fluentular.herokuapp.com/

http://blog.csdn.net/qq_27252133/article/details/53520416

補充說明:

採集端能夠使用logstash, filebeat, 也能夠使用fluentbit。由於fluent是CNCF的項目,因此只是爲了瞭解一下。

相關文章
相關標籤/搜索