1. 程式人生 > 實用技巧 >運維必備之日誌分析工具ELK

運維必備之日誌分析工具ELK

摘要

日誌分析是運維工程師解決系統故障,發現問題的主要手段。日誌主要包括系統日誌、應用程式日誌和安全日誌。系統運維和開發人員可以通過日誌瞭解伺服器軟硬體資訊、檢查配置過程中的錯誤及錯誤發生的原因,經常分析日誌可以瞭解伺服器的負荷,效能安全性,從而及時採取措施糾正錯誤。
日誌是一個非常龐大的資料,並且常常被分散在不同的裝置上,這樣排查問題的時候找日誌就非常繁瑣困難。
這時,一個專門處理日誌的系統就非常必要,這裡介紹其中的一種,ELK日誌分析系統(ELasticsearch+Logstash+Kibana)

日誌伺服器
提高安全性、集中存放日誌、缺陷:對日誌分析困難
ELK日誌分析系統的日誌處理步驟

  • 首先在叢集的每臺伺服器上都需要安裝日記收集工具logstash用於日誌的收集工作
  • 每個伺服器節點會將本地的日誌檔案輸入到LogStash中去。
  • LogStash會將日誌檔案以json的形式輸出到ES叢集中去。
  • 最後可以在視覺化介面kibana中對日誌進行展示查詢操作。

一、Elasticsearch簡介

Elasticsearch的概述

提供了一個分散式多使用者能力的全文搜尋引擎

Elasticsearch核心概念

  • 接近實時
  • 叢集
  • 節點
  • 索引
  • 索引(庫)> 型別(表)> 文件(記錄)
  • 分片和副本

二、Logstash簡介

Logstash介紹

    • 一款強大的資料處理工具
    • 可實現資料傳輸、格式處理、格式化輸出
    • 資料輸入、資料加工(如過濾,改寫等)以及資料輸出
      LogStash主要元件·
    • Shipper: 日誌收集者。負責監控本地日誌檔案的變化,及時收集最新的日誌檔案內容。
    • Indexer:日誌儲存者。負責接收日誌並寫入到本地檔案。
    • Broker: 日誌Hub。 負責連線多個Shipper 和多個Indexer.
    • Search and Storage: 允許對事件進行搜尋和儲存。
    • Web Interface: 基於Web 的展示介面。
      在Logstash 中,包括了三個階段,分別是輸入(Input )、處理(Filter, 非必需)和輸出(Output) 。

三、Kibana簡介

一個針對ELasticsearch的開源分析及視覺化平臺

搜尋、檢視儲存在Elasticsearch索引中的資料

通過各種圖示進行高階資料分析和展示

功能

  • Elasticsearch無縫與之整合
  • 整合資料,進行復雜資料的分析
  • 讓團隊內的人員更加受益
  • 介面靈活,易於分享
  • 配置簡單,視覺化展示多資料來源
  • 簡單資料匯出

四、ELK實戰分析

4.1、環境部署

部署兩臺Elasticsearch做叢集,node1做ELK日誌分析系統

apache

20.0.0.30

httpd

Logstash

node1

20.0.0.10

Elasticsearch

Kinaba

node2

20.0.0.20

Elasticsearch

4.2、在node1和node2上進行Elasticsearch配置

只在node1上進行演示

[root@node1 ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
[root@node1 ~]# cd /etc/yum.repos.d/
[root@node1 yum.repos.d]# vi elasticsearch.repo
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enable=1

[root@node1 ~]# yum list       #檢視yum清單
[root@node1 ~]# yum install elasticsearch -y    #安裝elasticsearch
[root@node1 ~]# yum install java -y      #安裝Java
[root@node1 ~]# java -version           #檢視Java版本
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)

4.3、修改配置檔案

[root@node1 ~]# vi /etc/elasticsearch/elasticsearch.yml 
cluster.name: elastic  #17行  叢集名稱

node.name: node1     #第23行  節點名稱

path.data: /data/es-data   #第33行  工作目錄
path.logs: /var/log/elasticsearch

bootstrap.memory_lock: true  #第43行  防止交換swap分割槽刪除#無需修改

network.host: 0.0.0.0 #第54行 監聽網路

http.port: 9200  # 第58 行 埠

4.4、建立目錄並開啟服務

[root@node1 ~]# mkdir -p /data/es-data
[root@node1 ~]# chown -R elasticsearch:elasticsearch /data/es-data/
[root@node1 ~]# systemctl start elasticsearch.service 
[root@node1 ~]# netstat -anpt | grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      56017/java 

4.4、測試

第二種方法測試

[root@node1 ~]# curl -i -XGET 'http://20.0.0.10:9200/_count?pretty' -d '{
> "query" {
>    "match_all"; {}
> }
> }'
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 95

{
  "count" : 0,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  }
}

4.5。安裝elasticsearch外掛

[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
Downloading ...................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE
Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed head into /usr/share/elasticsearch/plugins/head  

測試

4.6 、複合查詢

  • 提交請求

刪除後查詢

4.7 elasticsearch 叢集部署

[root@node1 ~]# vi /etc/elasticsearch/elasticsearch.yml 
discovery.zen.ping.unicast.hosts: ["20.0.0.10", "20.0.0.20"] #69 行 單播列表自動發現機制



重啟服務

[root@node1 ~]# systemctl restart elasticsearch.service 

4.8 測試

 

4.9、node1和node2日誌提示不讓鎖記憶體

[root@node1 ~]# less /var/log/elasticsearch/elastic.log
[2020-11-30 18:33:39,167][INFO ][cluster.metadata         ] [node1] [index-demo] creating index, cause [auto(index api)], templates [], shards [5]/[1], mappings []
[2020-11-30 18:33:39,422][INFO ][cluster.routing.allocation] [node1] Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[index-demo][2], [index-demo][3], [index-demo][3], [index-demo][2], [index-demo][0], [index-demo][0], [index-demo][4]] ...]).
[2020-11-30 18:33:39,485][INFO ][cluster.metadata         ] [node1] [index-demo] create_mapping [test]
[2020-11-30 18:47:09,875][INFO ][node                     ] [node1] stopping ...
[2020-11-30 18:47:09,907][INFO ][node                     ] [node1] stopped
[2020-11-30 18:47:09,907][INFO ][node                     ] [node1] closing ...
[2020-11-30 18:47:09,911][INFO ][node                     ] [node1] clo  

修改配置檔案

[root@node1 ~]# vi /etc/security/limits.conf 
# allow user 'elasticsearch' mlockall
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited   #在行尾插入

4.10 安裝監控元件

[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf  

 測試

4.11 Logstash部署 (apache上部署)

1、安裝httpd 並開啟服務

 

yum -y install httpd
[root@apache ~]# systemctl start httpd
[root@apache ~]# netstat -anpt | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      61700/httpd 
[root@apache ~]# echo '<h1>this is my web!!!!!</h1>' > /var/www/html/index.html
[root@apache ~]# curl http://localhost
<h1>this is my web!!!!!</h1>

2、安裝祕鑰及logstash源

  

[root@apache ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
[root@apache ~]# vi /etc/yum.repos.d/logstash.repo 

[logstash-2.1]
name=Logstash repository for 2.1.x packages
baseurl=http://packages.elastic.co/logstash/2.1/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enable=1

[root@apache ~]# yum -y install logstash

 3、安裝java環境。 

[root@apache ~]# yum -y install java
[root@apache ~]# java -version
openjdk version "1.8.0_272"
OpenJDK Runtime Environment (build 1.8.0_272-b10)
OpenJDK 64-Bit Server VM (build 25.272-b10, mixed mode)
ln -s /opt/logstash/bin/logstash /usr/local/bin/

4、logstash(apache)與elasticsearch(node)功能是否正常,做對接測試

 

[root@apache ~]# logstash -e 'input { stdin{} } output { stdout{} }'
Settings: Default filter workers: 1
Logstash startup completed
www.aliyun.com
2020-11-30T14:27:35.958Z apache www.aliyun.com
www.baidu.com
2020-11-30T14:27:41.062Z apache www.baidu.com

 

1 logstash命令選項解釋:
2 -f:指定logstash的配置檔案,根據配置檔案配置logstash
3 -e:後面跟著字串,該字串可以被當做logstash的配置(如果是“ ”,則預設使用stdin做輸入,stdout為輸出)
4 -t:測試配置檔案是否正確,然後退出
5 #輸入採用標準輸入,輸出採用標準輸出

5、使用rubydebug顯示詳細輸出,codec為一種解碼器

 

[root@apache ~]# logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug } }'
Settings: Default filter workers: 1
Logstash startup completed
www.baidu.com
{
       "message" => "www.baidu.com",
      "@version" => "1",
    "@timestamp" => "2020-11-30T14:29:58.952Z",
          "host" => "apache"
}

6、使用logstash講資訊輸出給elasticsearch

[root@apache ~]
# logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["20.0.0.10:9200"] } }' Settings: Default filter workers: 1 Logstash startup completed aaa bbb ccc

.7、開啟瀏覽器輸入http://20.0.0.10:9200/_plugin/head/,檢視索引資訊,顯示新的資料資訊說明輸出到elasticsearch成功  

8、在apache主機中做對接配置,logstash收集系統日誌

[root@apache ~]# chmod o+r /var/log/messages
[root@apache ~]# ll /var/log/messages 
-rw----r--. 1 root root 1292470 11月 30 22:30 /var/log/messages


[root@apache ~]# vi /etc/logstash/conf.d/system.conf
input {                    #logstash輸入:從/var/log/messages輸入,型別為system,起始位                     置為從頭開始
        file {
          path => "/var/log/messages"
          type => "system"
          start_position => "beginning"
        }
      }

output {                #logstash輸出:輸出給elasticsearch(以IP地址指定位置)
        elasticsearch {
        hosts => ["20.0.0.10:9200"]
        index => "system-%{+YYY.MM.dd}"
        }
      }

[root@apache ~]# systemctl restart logstash.service

9、開啟瀏覽器輸入http://20.0.0.10:9200/_plugin/head/,檢視索引資訊,顯示新的索引資訊代表logstash蒐集到的日誌資訊成功輸出給elasticsearch

 

5、在node1主機上安裝kibana

[root@node1 ~]# tar zxf kibana-4.3.1-linux-x64.tar.gz 
[root@node1 ~]# mv kibana-4.3.1-linux-x64 kibana
[root@node1 ~]# mv kibana /usr/local/
[root@node1 ~]# cd /usr/l
lib/     lib64/   libexec/ local/   
[root@node1 ~]# cd /usr/local/kibana/
vi config/kibana.yml 
//2行 kibana開啟的埠
server.port: 5601

//5行 kibana偵聽的地址
server.host: "0.0.0.0"


//12行 和elasticsearch建立聯絡
elasticsearch.url: "http://20.0.0.10:9200"

//20行 在elasticsearch中新增.kibana索引
kibana.index: ".kibana"

[root@node1 kibana]# yum -y install screen
[root@node1 kibana]# yum -y install screen
啟動kibana
[root@node1 kibana]# /usr/local/kibana/bin/kibana 
  log   [22:41:10.966] [info][status][plugin:kibana] Status changed from uninitialized to green - Ready
  log   [22:41:10.998] [info][status][plugin:elasticsearch] Status changed from uninitialized to yellow - Waiting for Elasticsearch
  log   [22:41:11.018] [info][status][plugin:kbn_vislib_vis_types] Status changed from uninitialized to green - Ready
  log   [22:41:11.041] [info][status][plugin:markdown_vis] Status changed from uninitialized to green - Ready
  log   [22:41:11.050] [info][status][plugin:metric_vis] Status changed from uninitialized to green - Ready
  log   [22:41:11.065] [info][status][plugin:spyModes] Status changed from uninitialized to green - Ready
  log   [22:41:11.069] [info][status][plugin:statusPage] Status changed from uninitialized to green - Ready
  log   [22:41:11.072] [info][status][plugin:table_vis] Status changed from uninitialized to green - Ready
  log   [22:41:11.095] [info][listening] Server running at http://0.0.0.0:5601
  log   [22:41:16.085] [info][status][plugin:elasticsearch] Status changed from yellow to yellow - No existing Kibana index found
  log   [22:41:18.763] [info][status][plugin:elasticsearch] Status changed from yellow to green - Kibana index ready

5.2 瀏覽器中登入20.0.0.10:5601,首次登入提示建立一個索引名字:
填入system-*,即對接系統日誌檔名

 

5.3同時也能看到剛才在kibana.yml中新增的在elasticsearch中新增.kibana索引配置生效了(用於驗證kibana和elasticsearch是否成功連線)

5.4/ 新增logstash配置,收集apache服務的訪問日誌和錯誤日誌

中的資料

[root@apache ~]# cd /etc/logstash/conf.d/
[root@apache conf.d]# vi apache_log.conf
input {
        file {
          path => "/etc/httpd/logs/access_log"
          type => "access"
          start_position => "beginning"
        }

        file {
          path => "/etc/httpd/logs/error_log"
          type => "error"
          start_position => "beginning"
        }
   }
output {
        if [type] == "access" {
          elasticsearch {
            hosts => ["20.0.0.10:9200"]
            index => "apache_access-%{+YYY.MM.dd}"
          }
        }
        if [type] == "error" {
          elasticsearch {
            hosts => ["20.0.0.10:9200"]
            index => "apache_error-%{+YYY.MM.dd}"
          }
        }
    }

[root@apache conf.d]# logstash -f apache_log.conf    #指定logstash的配置檔案,根據apache_log.conf配置logstash(-f可以不重啟載入)

5.5/ 瀏覽器訪問http://20.0.0.10:9200/_plugin/head/,檢視apache日誌檔案的索引是否建立成功