微服务架构日志集中化 安装 EFK (Fluentd ElasticSearch Kibana) 采集nginx日志
本文描述如何通過FEK組合集中化nginx的訪問日志。本人更喜歡按順序來命名,所以使用FEK而不是EFK.
首先在nginx服務器上執行以下操作.
安裝ruby
http://blog.csdn.net/chenhaifeng2016/article/details/78678680
curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sh
systemctl start td-agent
安裝插件
td-agent-gem install fluent-plugin-elasticsearch
td-agent-gem install fluent-plugin-typecast
安裝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 tailpath /usr/local/nginx/logs/access.logpos_file /usr/local/nginx/logs/access.log.postag nginx.accessformat /^(?<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 elasticsearchhost localhostport 9200 # index_name fluentdflush_interval 10slogstash_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"';重啟nginxnginx -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的項目,所以只是為了了解一下。
總結
以上是生活随笔為你收集整理的微服务架构日志集中化 安装 EFK (Fluentd ElasticSearch Kibana) 采集nginx日志的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos安装ruby
- 下一篇: calico 跨主机容器网络通信