擴(kuò)展結(jié)構(gòu):
部件介紹
ElasticSearch
????????Elasticsearch 是一個(gè)實(shí)時(shí)的分布式搜索和分析引擎,它可以用于全文搜索鲤嫡,結(jié)構(gòu)化搜索以及分析钙畔。它是一個(gè)建立在全文搜索引擎 Apache Lucene 基礎(chǔ)上的搜索引擎蕴忆,使用 Java 語言編寫茧跋。
????????備注:ELK慰丛,kibana中過濾器使用語法和Lucene相同,可以使用對(duì)應(yīng)格式來查詢
????????https://segmentfault.com/a/1190000002972420
Logstash
????????Logstash 是一個(gè)具有實(shí)時(shí)渠道能力的數(shù)據(jù)收集引擎瘾杭,主要用于日志的收集與解析诅病、切割,并將其存入 ElasticSearch中。
Kibana
????????Kibana 是一款基于 Apache 開源協(xié)議贤笆,使用 JavaScript 語言編寫蝇棉,為 Elasticsearch 提供分析和可視化的 Web 平臺(tái)。它可以在 Elasticsearch 的索引中查找苏潜,交互數(shù)據(jù)银萍,并生成各種維度的表圖。
Filebeat
????????引入Filebeat作為日志搜集器恤左,主要是為了解決Logstash開銷大的問題。相比Logstash搀绣,F(xiàn)ilebeat 所占系統(tǒng)的 CPU 和內(nèi)存幾乎可以忽略不計(jì)飞袋。(備注:當(dāng)filebeat直接將數(shù)據(jù)發(fā)送logstash時(shí),采集發(fā)送速度與logstash處理能力有關(guān)链患,并成正比)
Redis:
????????引入緩存的原因:
????????????????第一巧鸭、起緩存作用,避免收集層某時(shí)刻的大量寫入沖垮數(shù)據(jù)分割層麻捻。
????????????????第二纲仍、起短暫存儲(chǔ)作用,即Logstash暫停贸毕、重啟過程不會(huì)造成收集數(shù)據(jù)的丟失郑叠。
部署:
前置條件:Centos7.2? jdk1.8
cd? /opt
yum? install? redis
wgethttps://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.1.tar.gz
wget https://artifacts.elastic.co/downloads/logstash/logstash-5.6.1.tar.gz
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.6.1-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.6.1-linux-x86_64.tar.gz
備注:需要特定版本可以直接改版本再下載。
tar? zxf? elasticsearch-5.6.1.tar.gz
tar? zxf? logstash-5.6.1.tar.gz
tar? zxf? kibana-5.6.1-linux-x86_64.tar.gz
tar? zxf? filebeat-5.6.1-linux-x86_64.tar.gz
mv? filebeat-5.6.1? filebeat
mv? kibana-5.6.1? kibana
mv? elasticsearch-5.6.1? elasticsearch
mv? logstash-5.6.1? logstash
============================================================================
啟動(dòng)Elasticsearch
Elasticsearch不能使用root用戶運(yùn)行
useradd elk
sudo su - elk -c? "/opt/elasticsearch/bin/elasticsearch"
驗(yàn)證:curl ?? http://localhost:9200
常用請(qǐng)求:
刪除某一索引:curl -XDELETE 'localhost:9200/logstash-nginx-2018.03.25?pretty'
查看集群各節(jié)點(diǎn)狀態(tài):curl 'localhost:9200/_cat/health?v'
列出所有索引:curl 'localhost:9200/_cat/indices?v'
============================================================================
啟動(dòng)redis
systemctl? start redis
#備注:其他配置請(qǐng)根據(jù)具體進(jìn)行配置
============================================================================
啟動(dòng)Logstash
根據(jù)業(yè)務(wù)定義好配置文件
vi? /opt/logstash/bin/logstash.conf
參考:
input {
? if? [type]? == "apache_access_log" {
? ? redis { host => "172.17.91.157"
? ? ? ? port => 6379
? ? ? ? key => "logstash:redis"
? ? ? ? data_type => "list"
????????threads => 50 } } }
output {
? if? [type]? == "apache_access_log" {
? ? ? ? elasticsearch { hosts => ["http://172.17.91.157:9200"]
? ? ? ? ? ? ? ? index => "logstash-nginx-%{+YYYY.MM.dd}"
? ? ? ? ? ? ? ? document_type => "nginx"
? ? ? ? ? ? ? ? workers => 1
? ? ? ? ? ? ? ? idle_flush_time => 10 }
stdout { codec => rubydebug} }
? }
#nohup /opt/logstash/bin/logstash -f /opt/logstash/bin/logstash.conf &
#nohup /opt/logstash/bin/logstash -f /opt/logstash/bin/logstash.conf >> /data/logs/logstash/nohup.out 2>&1 &
============================================================================
啟動(dòng)Filebeat
根據(jù)業(yè)務(wù)定義好配置文件
vi? /opt/filebeat/filebeat-nginx.yml
參考:
filebeat.prospectors:
-
? ? ? ? input_type: log
? ? ? ? paths:
? ? ? ? - /var/log/nginx/access.log
? ? ? ? tags: ["apache_access_log"]
? ? ? ? document_type: apache_access_log
output.redis:
? ? ? ? hosts: ["172.17.91.157"]
? ? ? ? port: 6379
? ? ? ? key: "logstash:redis"
? ? ? ? db: 0
? ? ? ? timeout: 5
#nohup /opt/filebeat/filebeat -e -c filebeat-nginx.yml &
#nohup /opt/filebeat/filebeat -e -c filebeat-nginx.yml? >> /data/logs/filebeat/nohup.out 2>&1? &
============================================================================
啟動(dòng)Kibana
#nohup /opt/kibana/bin/kibana &
#nohup /opt/kibana/bin/kibana >> /data/logs/kibana/nohup.out 2>&1 &
驗(yàn)證:curlhttp://localhost:5601
至此安裝完畢明棍,具體配置需要在特定的場(chǎng)景進(jìn)行調(diào)整
============================================================================
疑問:
一乡革、kibana解析IP做展示
1、bin/elasticsearch-plugin install ingest-geoip
安裝ingest-geoip 插件摊腋,并重啟elasticsearch
2沸版、kibana.yml添加以下內(nèi)容,并重啟
----------------------------------------------------------------------------------------------------------------
tilemap.url: "http://webrd02.is.autonavi.com/appmaptile?
lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}"
#tilemap.url: "https://tiles.elastic.co/v2/default/{z}/{x}/{y}.png?
elastic_tile_service_tos=agree&my_app_name=kibana"
tilemap.options.minZoom: "1"
tilemap.options.maxZoom: "10"
tilemap.options.attribution: "? [Elastic Maps Service](https://www.elastic.co/elastic-maps-service)"
----------------------------------------------------------------------------------------------------------------
3兴蒸、logstash.conf? 在filter部分中增加以下內(nèi)容
----------------------------------------------------------------------------------------------------------------
geoip {
? ? ? ? source => "clientip"
? ? ? ? target => "geoip"
? ? ? ? database => "/opt/logstash/bin/GeoLite2-City/GeoLite2-City.mmdb"
#http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
? ? ? ? add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]
? ? ? ? add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}"? ]
? ? ? ? }
? ? ? mutate {
? ? ? ? convert => [ "[geoip][coordinates]", "float"]
? ? ? ? }
----------------------------------------------------------------------------------------------------------------
????????參考資料:https://www.cnblogs.com/ahaii/p/7410421.html
二视粮、應(yīng)用場(chǎng)景:
????????1、收集Nginx訪問日志橙凳,分析用戶的地理分布情況蕾殴、分析URL等。
? ? ? ? 2痕惋、收集服務(wù)器登錄日志区宇,分析用戶登錄服務(wù)器的情況≈荡粒可定位是否有外地IP议谷。
? ? ? ? 3、收集程序的錯(cuò)誤日志堕虹。開發(fā)人員可直接在ELK平臺(tái)上查看日志信息卧晓,減少登錄服務(wù)器芬首。
? ? ? ? 4、收集MySQL逼裆、Redis等組件的慢日志郁稍。作用同第三點(diǎn)。
? ? ? ? 5胜宇、收集crontab定時(shí)任務(wù)的執(zhí)行日志耀怜。
? ? ? ? 以下為筆者使用的一些配置文件,僅供參考桐愉。
????????????https://github.com/hqh546020152/ELK.git