ElasticSearch實戰:Linux日誌對接Kibana

本文由雲+社區發表html

ElasticSearch是一個基於Lucene的搜索服務器。它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTFul web接口。ElasticSearch是用Java開發的,並做爲Apache許可條款下的開放源碼發佈,是當前流行的企業級搜索引擎。ElasticSearch經常使用於全文檢索,結構化檢索,數據分析等。web


下面,咱們以ElasticSearch接管Linux日誌(/var/log/xxx.log)爲例,詳細介紹如何進行配置與部署。express

img整體架構圖vim

一,準備工做

1,CVM及ElasticSearch

在騰訊雲賬號下,申請一臺CVM(Linux操做系統)、一個ElasticSearch集羣(後面簡稱ES),使用最簡配置便可;申請的CVM和ES,必須在同一個VPC的同一個子網下。centos

imgCVM詳情信息bash

imgElasticSearch詳情信息服務器

2,Filebeat工具

爲了將Linux日誌提取到ES中,咱們須要使用Filebeat工具。Filebeat是一個日誌文件託運工具,在你的服務器上安裝客戶端後,Filebeat會監控日誌目錄或者指定的日誌文件,追蹤讀取這些文件(追蹤文件的變化,不停的讀),而且轉發這些信息到ElasticSearch或者logstarsh中存放。當你開啓Filebeat程序的時候,它會啓動一個或多個探測器(prospectors)去檢測你指定的日誌目錄或文件,對於探測器找出的每個日誌文件,Filebeat啓動收割進程(harvester),每個收割進程讀取一個日誌文件的新內容,併發送這些新的日誌數據處處理程序(spooler),處理程序會集合這些事件,最後Filebeat會發送集合的數據到你指定的地點。架構

官網簡介:https://www.elastic.co/products/beats/filebeat併發

二,操做步驟

1,Filebeat下載與安裝

首先,登陸待接管日誌的CVM,在CVM上下載Filebeat工具:app

[root@VM_3_7_centos ~]# cd /opt/
[root@VM_3_7_centos opt]# ll
total 4
drwxr-xr-x. 2 root root 4096 Sep  7  2017 rh
[root@VM_3_7_centos opt]# wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.2-x86_64.rpm
--2018-12-10 20:24:26--  https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.2-x86_64.rpm
Resolving artifacts.elastic.co (artifacts.elastic.co)... 107.21.202.15, 107.21.127.184, 54.225.214.74, ...
Connecting to artifacts.elastic.co (artifacts.elastic.co)|107.21.202.15|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12697788 (12M) [binary/octet-stream]
Saving to: ‘filebeat-6.2.2-x86_64.rpm’

100%[=================================================================================================>] 12,697,788   160KB/s   in 1m 41s 

2018-12-10 20:26:08 (123 KB/s) - ‘filebeat-6.2.2-x86_64.rpm’ saved [12697788/12697788]

而後,進行安裝filebeat:

[root@VM_3_7_centos opt]# rpm -vi filebeat-6.2.2-x86_64.rpm
warning: filebeat-6.2.2-x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID d88e42b4: NOKEY
Preparing packages...
filebeat-6.2.2-1.x86_64
[root@VM_3_7_centos opt]#

至此,Filebeat安裝完成。

2,Filebeat配置

進入Filebeat配置文件目錄:/etc/filebeat/

[root@VM_3_7_centos opt]# cd /etc/filebeat/
[root@VM_3_7_centos filebeat]# ll
total 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.yml
drwxr-xr-x 2 root root  4096 Dec 10 20:35 modules.d
[root@VM_3_7_centos filebeat]#

其中,filebeat.yml就是咱們須要修改的配置文件。建議修改配置前,先備份此文件。

而後,確認須要對接ElasticSearch的Linux的日誌目錄,咱們如下圖(/var/log/secure)爲例。

img/var/log/secure日誌文件

使用vim打開/etc/filebeat/filebeat.yml文件,修改其中的:

1)Filebeat prospectors類目中,enable默認爲false,咱們要改成true

2)paths,默認爲/var/log/*.log,咱們要改成待接管的日誌路徑:/var/log/secure

3)Outputs類目中,有ElasticSearchoutput配置,其中hosts默認爲"localhost:9200",須要咱們手工修改成上面申請的ES子網地址和端口,即**"10.0.3.8:9200"**。

修改好上述內容後,保存退出。

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

[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 be set at the prospector level, so
# you can use different prospectors for various configurations.
# Below are the prospector specific configurations.

- type: log

  # Change to true to enable this 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 crawled log files for filtering
  #fields:
  #  level: debug
  #  review: 1

  ### Multiline options

  # Mutiline can be used for log messages spanning multiple lines. This is common
  # for Java Stack Traces or C-Line Continuation

  # The regexp Pattern that has to be matched. The example pattern matches all lines starting with [
  #multiline.pattern: ^\[

  # Defines if the pattern set under pattern should be negated or not. Default is false.
  #multiline.negate: false

  # Match can be set to "after" or "before". It is used to define if lines 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 next in Logstash
  #multiline.match: after


#============================= Filebeat modules ===============================

filebeat.config.modules:
  # Glob pattern for configuration loading
  path: ${path.config}/modules.d/*.yml

  # Set to true to enable config reloading
  reload.enabled: false

  # Period on which files under path should be checked for changes
  #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 shipper in the web interface.
#name:

# The tags of the shipper are included in their 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 from where to download the dashboards archive. By default this URL
# has a value which is 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 be set to the default (http and 5601)
  # In case you 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` in the 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 certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem"

  # Client Certificate Key
  #ssl.key: "/etc/pki/client/cert.key"

#================================ Logging =====================================

# Sets log level. The default log level is info.
# Available log levels are: error, warning, info, debug
#logging.level: debug

# At debug level, you can selectively enable logging only for some components.
# To enable all selectors use ["*"]. Examples of other selectors are "beat",
# "publish", "service".
#logging.selectors: ["*"]

#============================== Xpack Monitoring ===============================
# filebeat can export internal metrics to a central Elasticsearch monitoring
# cluster.  This requires xpack monitoring to be enabled in Elasticsearch.  The
# reporting is disabled by default.

# Set to true to enable the 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 not set is
# automatically inherited from the Elasticsearch output configuration, so if you
# have the Elasticsearch output configured, you can simply uncomment the
# following line.
#xpack.monitoring.elasticsearch:
[root@VM_3_7_centos /]#

執行下列命令啓動filebeat

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

3,Kibana配置

進入ElasticSearch對應的Kibana管理頁,以下圖。

img首次訪問Kibana默認會顯示管理頁

首次登錄,會默認進入Management頁面,咱們須要將Index pattern內容修改成:filebeat-*,而後頁面會自動填充**Time Filter field name,**不需手動設置,直接點擊Create便可。點擊Create後,頁面須要必定時間來加載配置和數據,請稍等。以下圖:

img將Index pattern內容修改成:filebeat-*,而後點擊Create

至此,CVM上,/var/log/secure日誌文件,已對接到ElasticSearch中,歷史日誌能夠經過Kibana進行查詢,最新產生的日誌也會實時同步到Kibana中。

三,實戰效果

日誌接管已完成配置,如何使用呢?

以下圖:

img在Index Patterns中能夠看到咱們配置過的filebeat-*

點擊Discover,便可看到secure中的全部日誌,頁面上方的搜索框中輸入關鍵字,便可完成日誌的檢索。以下圖(點擊圖片,可查看高清大圖):

img使用Kibana進行日誌檢索

實際上,檢索只是Kibana提供的諸多功能之一,還有其餘功能如可視化、分詞檢索等,還有待後續研究。

此文已由做者受權騰訊雲+社區發佈

搜索關注公衆號「雲加社區」,第一時間獲取技術乾貨,關注後回覆1024 送你一份技術課程大禮包!

相關文章
相關標籤/搜索