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)分享給大家缘回。