微服務架構日誌集中化 安裝 EFK (Fluentd ElasticSearch Kibana) 採集nginx日誌
首先在nginx伺服器上執行以下操作.
安裝ruby
http://blog.csdn.net/chenhaifeng2016/article/details/78678680
安裝Fluentd 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 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 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的專案,所以只是為了瞭解一下。