日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

企业级日志收集系统——ELKstack

發(fā)布時間:2025/3/20 windows 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 企业级日志收集系统——ELKstack 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

ELKstack簡介:

? ?ELKstack是Elasticsearch、Logstash、Kibana三個開源軟件的組合而成,形成一款強大的實時日志收集展示系統(tǒng)。

? ?各組件作用如下:

? ?Logstash:日志收集工具,可以從本地磁盤,網絡服務(自己監(jiān)聽端口,接受用戶日志),消息隊列中收集各種各樣的日志,然后進行過濾分析,并將日志輸出到Elasticsearch中。

? ?Elasticsearch:日志分布式存儲/搜索工具,原生支持集群功能,可以將指定時間的日志生成一個索引,加快日志查詢和訪問。

? ?Kibana:可視化日志Web展示工具,對Elasticsearch中存儲的日志進行展示,還可以生成炫麗的儀表盤。

使用ELKstack對運維工作的好處:

? 1、應用程序的日志大部分都是輸出在服務器的日志文件中,這些日志大多數(shù)都是開發(fā)人員來看,然后開發(fā)卻沒有登陸服務器的權限,如果開發(fā)人員需要查看日志就需要到服務器來拿日志,然后交給開發(fā);試想下,一個公司有10個開發(fā),一個開發(fā)每天找運維拿一次日志,對運維人員來說就是一個不小的工作量,這樣大大影響了運維的工作效率,部署ELKstack之后,開發(fā)任意就可以直接登陸到Kibana中進行日志的查看,就不需要通過運維查看日志,這樣就減輕了運維的工作。

? 2、日志種類多,且分散在不同的位置難以查找:如LAMP/LNMP網站出現(xiàn)訪問故障,這個時候可能就需要通過查詢日志來進行分析故障原因,如果需要查看apache的錯誤日志,就需要登陸到Apache服務器查看,如果查看數(shù)據(jù)庫錯誤日志就需要登陸到數(shù)據(jù)庫查詢,試想一下,如果是一個集群環(huán)境幾十臺主機呢?這時如果部署了ELKstack就可以登陸到Kibana頁面進行查看日志,查看不同類型的日志只需要電動鼠標切換一下索引即可。

ELKstack實驗架構圖:

redis消息隊列作用說明:

????1、防止Logstash和ES無法正常通信,從而丟失日志。

????2、防止日志量過大導致ES無法承受大量寫操作從而丟失日志。

????3、應用程序(php,java)在輸出日志時,可以直接輸出到消息隊列,從而完成日志收集。

補充:如果redis使用的消息隊列出現(xiàn)擴展瓶頸,可以使用更加強大的kafka,flume來代替。


實驗環(huán)境說明:[root@es1?~]#?cat?/etc/redhat-release? CentOS?Linux?release?7.2.1511?(Core)? [root@es1?~]#?uname?-rm 3.10.0-327.el7.x86_64?x86_64使用軟件說明:

1、jdk-8u92 ?官方rpm包

2、Elasticsearch 2.3.3 官方rpm包

3、Logstash 2.3.2 官方rpm包

4、Kibana 4.5.1 官方rpm包

5、Redis 3.2.1 remi rpm 包

6、nginx 1.10.0-1 官方rpm包

部署順序說明:

1、Elasticsearch集群配置

2、Logstash客戶端配置(直接寫入數(shù)據(jù)到ES集群,寫入系統(tǒng)messages日志)

3、Redis消息隊列配置(Logstash寫入數(shù)據(jù)到消息隊列)

4、Kibana部署

5、nginx負載均衡Kibana請求

6、手機nginx日志

7、Kibana報表功能說明

配置注意事項:

1、時間必須同步

2、關閉防火墻,selinux

3、出了問題,檢查日志


Elasticsearch集群安裝配置

1、配置Java環(huán)境

[root@es1?~]#?yum?-y?install?jdk1.8.0_92 [root@es1?~]#?java?-version java?version?"1.8.0_92" Java(TM)?SE?Runtime?Environment?(build?1.8.0_92-b14) Java?HotSpot(TM)?64-Bit?Server?VM?(build?25.92-b14,?mixed?mode)

2、安裝Elasticsearch,因為我這里yum源已經創(chuàng)建好,所以可以直接安裝

官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

官方下載地址:https://www.elastic.co/downloads/elasticsearch??

[root@es1?~]#?yum?-y?install?elasticstarch [root@es1?~]#?rpm?-ql?elasticsearch /etc/elasticsearch /etc/elasticsearch/elasticsearch.yml???#主配置文件 /etc/elasticsearch/logging.yml /etc/elasticsearch/scripts /etc/init.d/elasticsearch /etc/sysconfig/elasticsearch /usr/lib/sysctl.d /usr/lib/sysctl.d/elasticsearch.conf /usr/lib/systemd/system/elasticsearch.service????#啟動腳本 /usr/lib/tmpfiles.d /usr/lib/tmpfiles.d/elasticsearch.conf

3、修改配置文件,這里的一些路徑看個人習慣

[root@es1?~]#?vim?/etc/elasticsearch/elasticsearch.yml? 17?cluster.name:?"linux-ES" 23?node.name:?es1.bjwf.com 33?path.data:?/elk/data 37?path.logs:?/elk/logs 43?bootstrap.mlockall:?true 54?network.host:?0.0.0.0 58?http.port:?9200 68?discovery.zen.ping.unicast.hosts:?["192.168.130.221",?"192.168.130.222"]

4、創(chuàng)建相關目錄并賦予權限

[root@es1?~]#?mkdir?-pv?/elk/{data,logs} [root@es1?~]#?chown?-R?elasticsearch.elasticsearch?/elk [root@es1?~]#?ll?/elk drwxr-xr-x.?2?elasticsearch?elasticsearch?6?Jun?28?03:51?data drwxr-xr-x.?2?elasticsearch?elasticsearch?6?Jun?28?03:51?logs

5、啟動ES,并檢查是否監(jiān)聽9200和9300端口

[root@es1?~]#?systemctl?start?elasticsearch.service [root@es1?~]#?netstat?-tnlp|egrep?"9200|9300" tcp6???0??????0?:::9200???:::*???????LISTEN??????17535/java?????????? tcp6???0??????0?:::9300???:::*???????LISTEN??????17535/java

6、安裝另一臺機器,步驟與第一臺一樣 ? ?

[root@es2?~]#?vim?/etc/elasticsearch/elasticsearch.yml? 23?node.name:?es2.bjwf.com???#主要修改主機名

7、查看兩個節(jié)點的狀態(tài)

配置集群管理插件(head、kopf等)

官方提供了一個ES集群管理插件,可以非常直觀的查看ES的集群狀態(tài)和索引數(shù)據(jù)信息

[root@es1?~]#?/usr/share/elasticsearch/bin/plugin?install?mobz/elasticsearch-head [root@es1?~]#?/usr/share/elasticsearch/bin/plugin?install?lmenezes/elasticsearch-kopf

訪問插件:

http://192.168.130.222:9200/_plugin/head/

http://192.168.130.222:9200/_plugin/kopf/

? ?上面已經把ES集群配置完成了,下面就可以配置Logstash向ES集群中寫入數(shù)據(jù)了


Logstash部署

1、配置Java環(huán)境,安裝logstash

[root@logstash1?~]#?yum?-y?install?jdk1.8.0_92 [root@logstash1?~]#?yum?-y?install?logstash

2、通過配置文件驗證Logstash的輸入和輸出

[root@logstash1?~]#?vim?/etc/logstash/conf.d/stdout.conf input?{stdin?{} }output?{stdout?{codec?=>?"rubydebug"} }

3、定義輸出到Elasticsearch

[root@logstash1?~]#?vim?/etc/logstash/conf.d/logstash.conf input?{stdin?{} } output?{ input?{stdin?{} } output?{elasticsearch?{hosts?=>?["192.168.130.221:9200","192.168.130.222:9200"]index?=>?"test"} } [root@logstash1?~]#?/opt/logstash/bin/logstash?-f?/etc/logstash/conf.d/logstash.conf? Settings:?Default?pipeline?workers:?4 Pipeline?main?started hello! 你好

這個時候說明,Logstash接好Elasticsearch是可以正常工作的,下面介紹如何收集系統(tǒng)日志

4、Logstash收集系統(tǒng)日志

修改Logstash配置文件如下所示內容,并啟動Logstash服務就可以在head中正常看到messages的日志已經寫入到了ES中,并且創(chuàng)建了索引

[root@logstash1?~]#?vim?/etc/logstash/conf.d/logstash.conf input?{file?{type?=>?"messagelog"path?=>?"/var/log/messages"start_position?=>?"beginning"} } output?{file?{path?=>?"/tmp/123.txt"}elasticsearch?{hosts?=>?["192.168.130.221:9200","192.168.130.222:9200"]index?=>?"system-messages-%{+yyyy.MM.dd}"} }#檢查配置文件語法: /etc/init.d/logstash?configtest /opt/logstash/bin/logstash?-f?/etc/logstash/conf.d/logstash.conf?--configtest #更改啟動Logstash用戶: #?vim?/etc/init.d/logstash LS_USER=root LS_GROUP=root #通過配置文件啟動 [root@logstash1?~]#?/opt/logstash/bin/logstash?-f?/etc/logstash/conf.d/logstash.conf?&

收集成功如圖所示,自動生成了system-messages的索引

Kibana部署

? 說明:我這里是在兩個ES節(jié)點部署kibana并且使用nginx實現(xiàn)負載均衡,如果沒有特殊需要,可以只部署單臺節(jié)點

1、安裝Kibana,每個ES節(jié)點部署一個 [root@es1?~]#?yum?-y?install?kibana 2、配置Kibana,只需要指定ES地址其他配置保持默認即可 [root@es1?~]#?vim?/opt/kibana/config/kibana.yml? 15?elasticsearch.url:?"http://192.168.130.221:9200" [root@es1?~]#?systemctl?start?kibana.service [root@es1?~]#?netstat?-tnlp|grep?5601????#Kibana監(jiān)聽端口 tcp????????0??????0?0.0.0.0:5601??0.0.0.0:*?????LISTEN??????17880/node

查看效果,這個圖是盜版的。。我做的這,忘記截圖了

創(chuàng)建完成后

filebeat部署收集日志

1、安裝nginx并將日志轉換為json [root@logstash1?~]#?yum?-y?install?nginx [root@logstash1?~]#?vim?/etc/nginx/nginx.conflog_format?access1?'{"@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",''"domain":"$host",''"xff":"$http_x_forwarded_for",''"referer":"$http_referer",''"status":"$status"}';access_log??/var/log/nginx/access.log??access1; #保存配置文件,啟動服務 [root@logstash1?~]#?systemctl?start?nginx #驗證nginx日志轉json [root@logstash1?~]#?tail?/var/log/nginx/log/host.access.log? {"@timestamp":"2016-06-27T05:28:47-04:00",'"host":"192.168.130.223",'' "clientip":"192.168.120.222",''"size":15,''"responsetime":0.000,''"upstreamtime":"-",' '"upstreamhost":"-",''"http_host":"192.168.130.223",''"url":"/index.html",''"domain": "192.168.130.223",''"xff":"-",''"referer":"-",''"status":"200"}' 2、安裝tomcat并將日志轉換為json [root@logstash1?~]#?tar?xf?apache-tomcat-8.0.36.tar.gz?-C?/usr/local [root@logstash1?~]#?cd?/usr/local [root@logstash1?local]#?ln?-sv?apache-tomcat-8.0.36/?tomcat [root@logstash1?~]#?vim?/usr/local/tomcat/conf/server.xml <Contest?path=""?docBase="/web"/><Valve?className="org.apache.catalina.valves.AccessLogValve"?directory="logs"prefix="localhost_access_log"?suffix=".txt"pattern="{&quot;clientip&quot;:&quot;%h&quot;,&quot;ClientUser&quot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;method&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?string&quot;:&quot;%q&quot;,&quot;partner&quot;:&quot;%{Referer}i&quot;,&quot;AgentVersion&quot;:&quot;%{User-Agent}i&quot;}"/> #啟動服務驗證日志 [root@logstash1?~]#?/usr/local/tomcat/bin/startup.sh [root@logstash1?~]#?tail?/usr/local/tomcat/logs/localhost_access_log.2016-06-28.txt {"clientip":"192.168.120.8","ClientUser":"-","authenticated":"-","AccessTime": "[28/Jun/2016:23:31:31?-0400]","method":"GET?/bg-button.png?HTTP/1.1","status" :"200","SendBytes":"713","Query?string":"","partner":"http://192.168.130.223:8080/tomcat.css",? "AgentVersion":"Mozilla/5.0?(Windows?NT?6.1;?WOW64;?rv:47.0)Gecko/20100101?Firefox/47.0"} 3、web安裝filebeat并配置filebeat收集nginx和tomcat日志發(fā)送給logstash #官方文檔??https://www.elastic.co/guide/en/beats/filebeat/current/index.html #下載地址??https://www.elastic.co/downloads/beats/filebeat #安裝??????[root@logstash1?~]#?yum?-y?install?filebeat #作用:在web端實時收集日志并傳遞給Logstash #為什么不用logstash在web端收集?依賴java環(huán)境,一旦java出問題,可能會影響到web服務系統(tǒng)資源占用率高配置比較復雜,支持匹配過濾Filebeat挺好的,專注日志手機,語法簡單 ##配置filebeat從兩個文件收集日志傳給Logstash filebeat:prospectors:-paths:-?/var/log/messages???#收集系統(tǒng)日志input_type:?logdocument_type:?nginx1-system-message-paths:-?/var/log/nginx/log/host.access.log???#nginx訪問日志input_type:?logdocument_type:?nginx1-nginx-log-paths:????????-?/usr/local/tomcat/logs/localhost_access_log.*.txt??#tomcat訪問日志????????????????????????????input_type:?logdocument_type:?nginx1-tomcat-log #??registry_file:?/var/lib/filebeat/registry??#這一條不知道怎么回事,出錯了output:logstash:?#將收集到的文件輸出到Logstashhosts:?["192.168.130.223:5044"]path:?"/tmp"filename:?filebeat.txt shipper:logging:to_files:?truefiles:path:?/tmp/mybeat #配置logstash從filebeat接受nginx日志 [root@logstash1?~]#?vim?/etc/logstash/conf.d/nginx-to-redis.confinput?{beats?{port?=>?5044codec?=>?"json"?#編碼格式為json} }output?{if?[type]?==?"nginx1-system-message"?{redis?{data_type?=>?"list"key?=>?"nginx1-system-message"?#寫入到redis的key名稱host?=>?"192.168.130.225"???#redis服務器地址port?=>?"6379"db?=>?"0"}}if?[type]?==?"nginx1-nginx-log"?{redis?{data_type?=>?"list"key?=>?"nginx1-nginx-log"host?=>?"192.168.130.225"port?=>?"6379"db?=>?"0"}}if?[type]?==?"nginx1-tomcat-log"?{redis?{data_type?=>?"list"key?=>?"nginx1-tomcat-log"host?=>?"192.168.130.225"port?=>?"6379"db?=>?"0"}}file?{path?=>?"/tmp/nginx-%{+yyyy-MM-dd}messages.gz"?#測試日志輸出} }??????? #這塊必須注意符號的問題,符號如果不對,有可能發(fā)生錯誤 #啟動Logstash和filebeat [root@logstash1?~]#?/etc/init.d/logstash?start [root@logstash1?~]#?netstat?-tnlp|grep?5044???#查看是否正常運行 tcp6???????0??????0?:::5044??????:::*???????LISTEN??????18255/java?? [root@logstash1?~]#?/etc/init.d/filebeat?start #查看本地輸出日志 [root@logstash1?~]#?tail?/tmp/nginx-2016-06-29messages.gz? {"message":"Jun?29?01:40:04?logstash1?systemd:?Unit?filebeat.service?entered?failed?state.", "tags":["_jsonparsefailure","beats_input_codec_json_applied"],"@version":"1","@timestamp": "2016-06-29T05:50:54.697Z","offset":323938,"type":"nginx1-system-message","input_type":"log", "source":"/var/log/messages","count":1,"fields":null,"beat":{"hostname":"logstash1.bjwf.com", "name":"logstash1.bjwf.com"},"host":"logstash1.bjwf.com"} 4、安裝配置redis [root@redis?~]#?yum?-y?install?redis [root@redis?~]#?vim?/etc/redis.conf bind?0.0.0.0?????#監(jiān)聽本機所有地址 daemonize?yes????#在后臺運行 appendonly?yes???#開啟aof [root@redis?~]#?systemctl?start?redis.service #這里需要訪問nginx和tomcat生成一些日志 [root@redis?~]#?netstat?-tnlp|grep?6379 tcp??0?0?0.0.0.0:6379???0.0.0.0:*??LISTEN?17630/redis-server?? #連接redis查看生成的日志是否存在 [root@redis?~]#?redis-cli?-h?192.168.130.225 192.168.130.225:6379>?KEYS?* 1)?"nginx1-tomcat-log" 2)?"nginx1-system-message" 3)?"nginx1-nginx-log" 5、在另外一臺logstash上收集nginx的日志 [root@logstash2?~]#?yum?-y?install?logstash [root@logstash2?~]#?vim?/etc/logstash/conf.d/redis-to-elast.conf input?{redis?{host?=>?"192.168.130.225"port?=>?"6379"db?=>?"0"key?=>?"nginx1-system-message"data_type?=>?"list"codec?=>?"json"}redis?{host?=>?"192.168.130.225"port?=>?"6379"db?=>?"0"key?=>?"nginx1-nginx-log"data_type?=>?"list"codec?=>?"json"}redis?{host?=>?"192.168.130.225"port?=>?"6379"db?=>?"0"key?=>?"nginx1-tomcat-log"data_type?=>?"list"codec?=>?"json"} }filter?{if?[type]?==?"nginx1-nginx-log"?or?[type]?==?"nginx1-tomcat-log"?{geoip?{source?=>?"clientip"target?=>?"geoip" #???????????????database?=>?"/etc/logstash/GeoLiteCity.dat"add_field?=>?[?"[geoip][coordinaters]","%{[geoip][longitude]}"?]add_field?=>?[?"[geoip][coordinaters]","%{[geoip][latitude]}"?]}mutate?{convert?=>?[?"geoip][coordinates]","float"]}} }output?{if?[type]?==?"nginx1-system-message"?{elasticsearch?{hosts?=>?["192.168.130.221:9200","192.168.130.222:9200"]index?=>?"nginx1-system-message-%{+yyyy.MM.dd}"manage_template?=>?truefulsh_size?=>?2000idle_flush_time?=>?10?}}if?[type]?==?"nginx1-nginx-log"?{elasticsearch?{hosts?=>?["192.168.130.221:9200","192.168.130.222:9200"]index?=>?"logstash1-nginx1-nginx-log-%{+yyyy.MM.dd}"manage_template?=>?truefulsh_size?=>?2000idle_flush_time?=>?10?}}if?[type]?==?"nginx1-tomcat-log"?{elasticsearch?{hosts?=>?["192.168.130.221:9200","192.168.130.222:9200"]index?=>?"logstash-nginx1-tomcat-log-%{+yyyy.MM.dd}"manage_template?=>?truefulsh_size?=>?2000idle_flush_time?=>?10?}}file?{path?=>?"/tmp/log-%{+yyyy-MM-dd}messages.gz"gzip?=>?"true"}?????? }[root@logstash2?~]#?/etc/init.d/logstash?configtest Configuration?OK [root@logstash2?~]#?/etc/init.d/logstash?start #驗證數(shù)據(jù)寫入 #Elasticsearch的數(shù)據(jù)目錄,可以確定已經寫入 [root@es1?0]#?du?-sh?/elk/data/linux-ES/nodes/0/indices/*? 148K /elk/data/linux-ES/nodes/0/indices/logstash1-nginx1-nginx-log-2016.06.29 180K /elk/data/linux-ES/nodes/0/indices/logstash-nginx1-tomcat-log-2016.06.29 208K /elk/data/linux-ES/nodes/0/indices/nginx1-system-message-2016.06.29 576K /elk/data/linux-ES/nodes/0/indices/system-messages-2016.06.28 580K /elk/data/linux-ES/nodes/0/indices/system-messages-2016.06.29 108K /elk/data/linux-ES/nodes/0/indices/test

配置nginx進行反向代理

[root@nginx?~]#?vim?/etc/nginx/nginx.conf?upstream?kibana?{??????#定義后端主機組server?192.168.130.221:5601?weight=1?max_fails=2?fail_timeout=2;server?192.168.130.221:5602?weight=1?max_fails=2?fail_timeout=2;}??server?{listen???????80;server_name??192.168.130.226;location?/?{???????????#定義反向代理,將訪問自己的請求,都轉發(fā)到kibana服務器proxy_pass?http://kibana/;index??index.html?index.htm;}} [root@nginx?~]#?systemctl?start?nginx.service????#啟動服務

#查看Elasticsearch和Kibana輸出結果

??



#到這里基本上結束了。以后在補充


轉載于:https://blog.51cto.com/79076431/1793682

總結

以上是生活随笔為你收集整理的企业级日志收集系统——ELKstack的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。