1、ELK介紹
ELK不是一款軟件,而是elasticsearch+Logstash+kibana三款開源軟件組合而成的日志收集處理套件,堪稱神器。其中Logstash負責日志收集,elasticsearch負責日志的搜索、統計,而kibana則是ES的展示神器,前端炫麗,點幾下鼠標簡單配置,就可以完成搜索、聚合功能,生成華麗的報表。
目前我們的日志方案:
- flume負責收集,服務寫日志到文件,flume收集日志文件
- flume匯總到數據通道kafka,供其他服務消費
- 日志搜索:從kafka讀取日志寫入到solr cloud提供搜索
- 日志統計:將kafka的日志寫到hdfs,使用spark、hive來做統計
- 日志展示:開發的java-web,讀取數據庫生成統計報表
當前日志方案問題分析:
- 需要預先編程才能使用,開發工作量大
- 不夠靈活,新需求需要改代碼
- 離線統計,實時性不高
- 未提供基於搜索結果的統計功能
- 系統方案較為復雜,需要了解多項技術,學習維護成本高
- ……
- 新增需求都是淚,開發出來后變動很少
通過調研ELK,發現真是解救目前問題的一個神器,大部分問題都可以迎刃而解。
2、ELK安裝
默認需要先安裝jdk1.8,自行安裝即可
2.1、安裝ES
2.1.1 下載ES
下載地址:https://www.elastic.co/downloads/elasticsearch
最新版本是2.28發布的5.2.2版本
windows選擇ZIP,linux選擇tar,ubuntu選擇DEB
測試服務器是ubuntu,直接下載deb包,然后安裝即可
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.2.deb
sudo dpkg -i elasticsearch-5.2.2.deb
2.1.2 配置
如果使用deb包安裝的,配置文件路徑如下:
類型 |
描述 |
默認路經 |
Setting |
home |
Elasticsearch home directory or $ES_HOME |
/usr/share/elasticsearch |
|
bin |
Binary scripts including elasticsearch to start a node and elasticsearch-plugin to install plugins |
/usr/share/elasticsearch/bin |
|
conf |
Configuration files including elasticsearch.yml |
/etc/elasticsearch |
path.conf |
conf |
Environment variables including heap size, file descriptors. |
/etc/default/elasticsearch |
|
data |
The location of the data files of each index / shard allocated on the node. Can hold multiple locations. |
/var/lib/elasticsearch |
path.data |
logs |
Log files location. |
/var/log/elasticsearch |
path.logs |
plugins |
Plugin files location. Each plugin will be contained in a subdirectory. |
/usr/share/elasticsearch/plugins |
|
repo |
Shared file system repository locations. Can hold multiple locations. A file system repository can be placed in to any subdirectory of any directory specified here. |
Not configured |
path.repo |
script |
Location of script files. |
/etc/elasticsearch/scripts |
path.scripts |
修改/etc/elasticsearch/elasticsearch.yml即可
測試使用,主要設置網絡:
network.host: 192.168.86.108 http.port: 9200
ES的集群配置比較方便,設置cluster.name就可以:
cluster.name: es-log
2.1.3 啟動ES
由於我們使用的deb安裝的,因此可以用服務方式啟動:
service elasticsearch start
如果使用的壓縮包,執行 bin/elasticsearch (or bin\elasticsearch.bat on Windows)即可
然后,打開 curl http://192.168.86.108:9200/ 測試(ip換成配置的ip)
2.2 安裝 Logstash
現在收集處理框架非常多,像 facebook 出品的scribe ,apache 基金會的親兒子flume,Logstash也是一個非常出名的日志框架,使用jruby開發,可以運行在jvm之上實現跨平台,具體的介紹可以到官網http://logstash.net查看。
Logstash安裝比較簡單,下載壓縮包、解壓、配置,啟動即可。
2.2.1 下載安裝
wget https://artifacts.elastic.co/downloads/logstash/logstash-5.2.2.tar.gz tar zxvf logstash-5.2.2.tar.gz ln -s logstash-5.2.2 logstash
2.2.2 配置
Logstash 和flume比較類似,有input和output的概念。不過logstash社區通常習慣用 shipper,broker 和 indexer 來描述數據流中不同進程各自的角色。
我們來看怎么配置logstash。
創建一個配置文件nginxlog2es.conf,讀取nginx日志,輸出到elasticsearch 。具體的配置格式參見官方文檔。
input { file { path => "/var/log/nginx/access.log_json" codec => "json" } } filter { mutate { split => [ "upstreamtime", "," ] } mutate { convert => [ "upstreamtime", "float" ] } } output { stdout { codec => rubydebug } elasticsearch { hosts => ["192.168.86.108:9200"] index => "logstash-%{type}-%{+YYYY.MM.dd}" document_type => "%{type}" flush_size => 20000 idle_flush_time => 10 sniffing => true template_overwrite => true } }
這里簡單說明下,input是file類型,/var/log/nginx/access.log_json每行是一個json數據,codec => "json"指定按json解析。
output 配置了兩個,stdout 是控制台輸出,elasticsearch 則是輸出到前面配置的es服務器,index 索引名稱為logstash-nginx-日期,這樣每天為一個索引。
Nginx 直接生成json日志可以通過指定logformat,拼接為json:
配置logformat:
log_format json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"url":"$uri",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"agent":"$http_user_agent",' '"status":"$status"}';
然后使用:
access_log /var/log/nginx/access.log_json json;
這樣nginx的日志就變為:
2.2.3 啟動logstash
使用-f指定配置文件
bin/logstash -f nginxlog2es.conf
啟動成功后就能看到收集到的日志:
2.3 kibana
2.3.1 安裝
kibana安裝也比較簡單,下載,解壓,配置即可
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.2.2-linux-x86_64.tar.gz
解壓后建立軟連接
ln -s kibana-5.2.2-linux-x86_64 kibana
cd kibana
修改配置文件
vim config/kibana.yml
修改
server.host: "192.168.86.108"
elasticsearch.url: "http://192.168.86.108:9200"
然后啟動:
bin/kibana
2.3.2 配置報表
啟動后打開http://192.168.86.108:5601,就能看到日志了,在輸入框里還可以輸入關鍵詞進行搜索,並且自帶流量統計。
下面來看如何生成報表,比如搞個響應時間折線圖,進入visualize,選擇折線圖
分別配置X和y軸,Y為平均響應時間,X為時間,最后保存即可。
再來配置一個表格報表,通產各個url的訪問量
Metrics 配置使用count作為聚合函數
buckets增加rows,字段選url
再增加一個狀態碼:
這樣就完成了一個漂亮的表格
2.3.3 將報表添加到dashboard
在dashboard點add,將兩個報表加入即可
3、總結
簡單的介紹就到這里為止了,更多的功能后面繼續挖掘。
總結下,借助ELK可以快速搭建日志收集、日志搜索和統計分析平台,可以不寫一行代碼完成漂亮的統計報表,簡直是開發和運維的福音,是不是心動了?如果是,快行動起來吧!