ElasticSearch實(shí)戰(zhàn):Linux日志對(duì)接Kibana

ElasticSearch是一個(gè)基于Lucene的搜索服務(wù)器。它提供了一個(gè)分布式多用戶(hù)能力的全文搜索引擎,基于RESTFul web接口。ElasticSearch是用Java開(kāi)發(fā)的,并作為Apache許可條款下的開(kāi)放源碼發(fā)布,是當(dāng)前流行的企業(yè)級(jí)搜索引擎厚脉。ElasticSearch常用于全文檢索,結(jié)構(gòu)化檢索胶惰,數(shù)據(jù)分析等傻工。

如果想學(xué)習(xí)Java工程化、高性能及分布式孵滞、深入淺出中捆。微服務(wù)、Spring坊饶,MyBatis泄伪,Netty源碼分析的朋友可以加我的Java高級(jí)交流:854630135,群里有阿里大牛直播講解技術(shù)匿级,以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家蟋滴。

下面,我們以ElasticSearch接管Linux日志(/var/log/xxx.log)為例痘绎,詳細(xì)介紹如何進(jìn)行配置與部署津函。

總體架構(gòu)圖

一,準(zhǔn)備工作

1孤页,CVM及ElasticSearch

在騰訊云帳號(hào)下尔苦,申請(qǐng)一臺(tái)CVM(Linux操作系統(tǒng))、一個(gè)ElasticSearch集群(后面簡(jiǎn)稱(chēng)ES)行施,使用最簡(jiǎn)配置即可允坚;申請(qǐng)的CVM和ES,必須在同一個(gè)VPC的同一個(gè)子網(wǎng)下悲龟。

CVM詳情信息

ElasticSearch詳情信息

2屋讶,F(xiàn)ilebeat工具

為了將Linux日志提取到ES中冰寻,我們需要使用Filebeat工具须教。Filebeat是一個(gè)日志文件托運(yùn)工具,在你的服務(wù)器上安裝客戶(hù)端后,F(xiàn)ilebeat會(huì)監(jiān)控日志目錄或者指定的日志文件轻腺,追蹤讀取這些文件(追蹤文件的變化乐疆,不停的讀),并且轉(zhuǎn)發(fā)這些信息到ElasticSearch或者logstarsh中存放贬养。當(dāng)你開(kāi)啟Filebeat程序的時(shí)候挤土,它會(huì)啟動(dòng)一個(gè)或多個(gè)探測(cè)器(prospectors)去檢測(cè)你指定的日志目錄或文件,對(duì)于探測(cè)器找出的每一個(gè)日志文件误算,F(xiàn)ilebeat啟動(dòng)收割進(jìn)程(harvester)仰美,每一個(gè)收割進(jìn)程讀取一個(gè)日志文件的新內(nèi)容,并發(fā)送這些新的日志數(shù)據(jù)到處理程序(spooler)儿礼,處理程序會(huì)集合這些事件咖杂,最后Filebeat會(huì)發(fā)送集合的數(shù)據(jù)到你指定的地點(diǎn)。

官網(wǎng)簡(jiǎn)介:https://www.elastic.co/products/beats/filebeat

二蚊夫,操作步驟

1诉字,F(xiàn)ilebeat下載與安裝

首先,登錄待接管日志的CVM知纷,在CVM上下載Filebeat工具:

[root@VM_3_7_centos ~]#cd/opt/[root@VM_3_7_centos opt]#lltotal4drwxr-xr-x.2rootroot4096Sep72017rh[root@VM_3_7_centos opt]#wgethttps://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.2-x86_64.rpm--2018-12-1020:24:26--https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.2-x86_64.rpmResolvingartifacts.elastic.co(artifacts.elastic.co)...107.21.202.15,107.21.127.184,54.225.214.74, ...Connectingtoartifacts.elastic.co(artifacts.elastic.co)|107.21.202.15|:443...connected.HTTPrequestsent,awaitingresponse...200OKLength:12697788(12M)[binary/octet-stream]Savingto: ‘filebeat-6.2.2-x86_64.rpm’100%[=================================================================================================>]12,697,788160KB/sin1m41s2018-12-1020:26:08(123KB/s)-‘filebeat-6.2.2-x86_64.rpm’saved[12697788/12697788]

然后壤圃,進(jìn)行安裝filebeat:

[root@VM_3_7_centosopt]# rpm -vi filebeat-6.2.2-x86_64.rpmwarning: filebeat-6.2.2-x86_64.rpm: Header V4RSA/SHA512Signature, key ID d88e42b4: NOKEYPreparing packages...filebeat-6.2.2-1.x86_64[root@VM_3_7_centosopt]#

至此,F(xiàn)ilebeat安裝完成琅轧。

2伍绳,F(xiàn)ilebeat配置

如果想學(xué)習(xí)Java工程化、高性能及分布式乍桂、深入淺出墨叛。微服務(wù)、Spring模蜡,MyBatis漠趁,Netty源碼分析的朋友可以加我的Java高級(jí)交流:854630135,群里有阿里大牛直播講解技術(shù)忍疾,以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家闯传。

進(jìn)入Filebeat配置文件目錄:/etc/filebeat/

[root@VM_3_7_centos opt]# cd /etc/filebeat/[root@VM_3_7_centos filebeat]# lltotal 108-rw-r--r-- 1 root root 44384 Feb 17 2018 fields.yml-rw-r----- 1 root root 52193 Feb 17 2018 filebeat.reference.yml-rw------- 1 root root 7264 Feb 17 2018 filebeat.ymldrwxr-xr-x 2 root root 4096 Dec 10 20:35 modules.d[root@VM_3_7_centos filebeat]#

其中,filebeat.yml就是我們需要修改的配置文件卤妒。建議修改配置前甥绿,先備份此文件。

然后则披,確認(rèn)需要對(duì)接ElasticSearch的Linux的日志目錄共缕,我們以下圖(/var/log/secure)為例。

/var/log/secure日志文件

使用vim打開(kāi)/etc/filebeat/filebeat.yml文件士复,修改其中的:

1)Filebeat prospectors類(lèi)目中图谷,enable默認(rèn)為false翩活,我們要改為true

2)paths,默認(rèn)為/var/log/*.log便贵,我們要改為待接管的日志路徑:/var/log/secure

3)Outputs類(lèi)目中菠镇,有ElasticSearchoutput配置,其中hosts默認(rèn)為"localhost:9200"承璃,需要我們手工修改為上面申請(qǐng)的ES子網(wǎng)地址和端口利耍,即"10.0.3.8:9200"

修改好上述內(nèi)容后盔粹,保存退出隘梨。

修改好的配置文件全文如下:

[root@VM_3_7_centos /]# vim /etc/filebeat/filebeat.yml[root@VM_3_7_centos /]# cat /etc/filebeat/filebeat.yml###################### Filebeat Configuration Example ########################## This file is an example configuration file highlighting only the most common# options. The filebeat.reference.yml file from the same directory contains all the# supported options with more comments. You can use it as a reference.## You can find the full configuration reference here:# https://www.elastic.co/guide/en/beats/filebeat/index.html# For more available modules and options, please see the filebeat.reference.yml sample# configuration file.#=========================== Filebeat prospectors =============================filebeat.prospectors:#Each - is a prospector. Most options can besetat the prospector level, so#you can use different prospectorsforvarious configurations.# Below are the prospector specific configurations.- type: log #Change totruetoenablethis prospector configuration.enabled: true # Paths that should be crawled and fetched. Glob based paths.paths: - /var/log/secure #- c:\programdata\elasticsearch\logs\* # Exclude lines. A list of regular expressions to match. It drops the lines that are # matching any regular expression from the list. #exclude_lines: ['^DBG'] # Include lines. A list of regular expressions to match. It exports the lines that are # matching any regular expression from the list. #include_lines: ['^ERR','^WARN'] # Exclude files. A list of regular expressions to match. Filebeat drops the files that # are matching any regular expression from the list. By default, no files are dropped. #exclude_files: ['.gz$'] # Optional additional fields. These fields can be freely picked #to add additional information to the crawledlogfilesforfiltering #fields: # level: debug # review: 1 ### Multiline options #Mutiline can be usedforlogmessages spanning multiple lines. This is common #forJava Stack Traces or C-Line Continuation # The regexp Pattern that has to be matched. The example pattern matches all lines starting with [ #multiline.pattern: ^\[ #Definesifthe patternsetunder pattern should be negated or not. Default isfalse. #multiline.negate:false #Match can besetto"after"or"before". It is used to defineiflines should be append to a pattern # that was (not) matched before or after or as long as a pattern is not matched based on negate. #Note: After is the equivalent to previous and before is the equivalent to to nextinLogstash #multiline.match: after#============================= Filebeat modules ===============================filebeat.config.modules: #Glob patternforconfiguration loadingpath: ${path.config}/modules.d/*.yml #Set totruetoenableconfig reloadingreload.enabled: false #Period onwhichfiles under path should be checkedforchanges #reload.period: 10s#==================== Elasticsearch template setting ==========================setup.template.settings: index.number_of_shards: 3 #index.codec: best_compression #_source.enabled:false#================================ General =====================================# The name of the shipper that publishes the network data. It can be used to group#all the transactions sent by a single shipperinthe web interface.#name:#The tags of the shipper are includedintheir own field with each# transaction published.#tags: ["service-X","web-tier"]# Optional fields that you can specify to add additional information to the# output.#fields:# env: staging#============================== Dashboards =====================================# These settings control loading the sample dashboards to the Kibana index. Loading# the dashboards is disabled by default and can be enabled either by setting the#options here, or by using the `-setup` CLI flag or the `setup`command.#setup.dashboards.enabled:false#The URL fromwhereto download the dashboards archive. By default this URL#has a valuewhichis computed based on the Beat name and version. For released# versions, this URL points to the dashboard archive on the artifacts.elastic.co# website.#setup.dashboards.url:#============================== Kibana =====================================# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.# This requires a Kibana endpoint configuration.setup.kibana: # Kibana Host #Scheme and port can be left out and will besetto the default (http and 5601) #Incaseyou specify and additional path, the scheme is required: http://localhost:5601/path # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601 #host:"localhost:5601"#============================= Elastic Cloud ==================================# These settings simplify using filebeat with the Elastic Cloud (https://cloud.elastic.co/).# The cloud.id setting overwrites the `output.elasticsearch.hosts` and# `setup.kibana.host` options.#You can find the `cloud.id`inthe Elastic Cloud web UI.#cloud.id:# The cloud.auth setting overwrites the `output.elasticsearch.username` and# `output.elasticsearch.password` settings. The format is `<user>:<pass>`.#cloud.auth:#================================ Outputs =====================================# Configure what output to use when sending the data collected by the beat.#-------------------------- Elasticsearch output ------------------------------output.elasticsearch: # Array of hosts to connect to.hosts: ["10.0.3.8:9200"] # Optional protocol and basic auth credentials. #protocol:"https" #username:"elastic" #password:"changeme"#----------------------------- Logstash output --------------------------------#output.logstash: # The Logstash hosts #hosts: ["localhost:5044"] # Optional SSL. By default is off. #List of root certificatesforHTTPS server verifications #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"] #CertificateforSSL client authentication #ssl.certificate:"/etc/pki/client/cert.pem" # Client Certificate Key #ssl.key:"/etc/pki/client/cert.key"#================================ Logging =====================================#Setsloglevel. The defaultloglevel is info.#Availableloglevels are: error, warning, info, debug#logging.level: debug#At debug level, you can selectivelyenablelogging onlyforsome components.#Toenableall selectors use ["*"]. Examples of other selectors are"beat",#"publish","service".#logging.selectors: ["*"]#============================== Xpack Monitoring ===============================#filebeat canexportinternal metrics to a central Elasticsearch monitoring#cluster. This requires xpack monitoring to be enabledinElasticsearch. The# reporting is disabled by default.#Set totruetoenablethe monitoring reporter.#xpack.monitoring.enabled:false# Uncomment to send the metrics to Elasticsearch. Most settings from the#Elasticsearch output are accepted here as well. Any setting that is notsetis#automatically inherited from the Elasticsearch output configuration, soifyou# have the Elasticsearch output configured, you can simply uncomment the# following line.#xpack.monitoring.elasticsearch:[root@VM_3_7_centos /]#

執(zhí)行下列命令啟動(dòng)filebeat

[root@VM_3_7_centos/]# sudo /etc/init.d/filebeat startStarting filebeat (via systemctl): [ OK ][root@VM_3_7_centos/]#

3,Kibana配置

如果想學(xué)習(xí)Java工程化舷嗡、高性能及分布式出嘹、深入淺出。微服務(wù)咬崔、Spring税稼,MyBatis,Netty源碼分析的朋友可以加我的Java高級(jí)交流:854630135垮斯,群里有阿里大牛直播講解技術(shù)郎仆,以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家。

進(jìn)入ElasticSearch對(duì)應(yīng)的Kibana管理頁(yè)兜蠕,如下圖扰肌。

首次訪(fǎng)問(wèn)Kibana默認(rèn)會(huì)顯示管理頁(yè)

首次登陸,會(huì)默認(rèn)進(jìn)入Management頁(yè)面熊杨,我們需要將Index pattern內(nèi)容修改為:filebeat-*曙旭,然后頁(yè)面會(huì)自動(dòng)填充Time Filter field name,不需手動(dòng)設(shè)置晶府,直接點(diǎn)擊Create即可桂躏。點(diǎn)擊Create后,頁(yè)面需要一定時(shí)間來(lái)加載配置和數(shù)據(jù)川陆,請(qǐng)稍等剂习。如下圖:

將Index pattern內(nèi)容修改為:filebeat-*,然后點(diǎn)擊Create

至此较沪,CVM上鳞绕,/var/log/secure日志文件,已對(duì)接到ElasticSearch中尸曼,歷史日志可以通過(guò)Kibana進(jìn)行查詢(xún)们何,最新產(chǎn)生的日志也會(huì)實(shí)時(shí)同步到Kibana中。

三控轿,實(shí)戰(zhàn)效果

日志接管已完成配置冤竹,如何使用呢拂封?

如下圖:

在Index Patterns中可以看到我們配置過(guò)的filebeat-*

點(diǎn)擊Discover,即可看到secure中的所有日志贴见,頁(yè)面上方的搜索框中輸入關(guān)鍵字烘苹,即可完成日志的檢索躲株。如下圖(點(diǎn)擊圖片片部,可查看高清大圖):

使用Kibana進(jìn)行日志檢索

如果想學(xué)習(xí)Java工程化、高性能及分布式霜定、深入淺出档悠。微服務(wù)、Spring望浩,MyBatis辖所,Netty源碼分析的朋友可以加我的Java高級(jí)交流:854630135,群里有阿里大牛直播講解技術(shù)磨德,以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家缘回。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市典挑,隨后出現(xiàn)的幾起案子酥宴,更是在濱河造成了極大的恐慌,老刑警劉巖您觉,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拙寡,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡琳水,警方通過(guò)查閱死者的電腦和手機(jī)肆糕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)在孝,“玉大人诚啃,你說(shuō)我怎么就攤上這事∷骄冢” “怎么了绍申?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)顾彰。 經(jīng)常有香客問(wèn)我极阅,道長(zhǎng),這世上最難降的妖魔是什么涨享? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任筋搏,我火速辦了婚禮,結(jié)果婚禮上厕隧,老公的妹妹穿的比我還像新娘奔脐。我一直安慰自己俄周,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布髓迎。 她就那樣靜靜地躺著峦朗,像睡著了一般。 火紅的嫁衣襯著肌膚如雪排龄。 梳的紋絲不亂的頭發(fā)上波势,一...
    開(kāi)封第一講書(shū)人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音橄维,去河邊找鬼尺铣。 笑死,一個(gè)胖子當(dāng)著我的面吹牛争舞,可吹牛的內(nèi)容都是我干的凛忿。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼竞川,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼店溢!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起委乌,我...
    開(kāi)封第一講書(shū)人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤床牧,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后福澡,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體叠赦,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年革砸,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了除秀。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡算利,死狀恐怖册踩,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情效拭,我是刑警寧澤暂吉,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站缎患,受9級(jí)特大地震影響慕的,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜挤渔,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一肮街、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧判导,春花似錦嫉父、人聲如沸沛硅。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)摇肌。三九已至,卻和暖如春仪际,著一層夾襖步出監(jiān)牢的瞬間围小,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工弟头, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留吩抓,地道東北人涉茧。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓赴恨,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親伴栓。 傳聞我的和親對(duì)象是個(gè)殘疾皇子伦连,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容