1. 程式人生 > >在CentOS7中部署ELK日誌分析系統

在CentOS7中部署ELK日誌分析系統

-- worker lin config startup thead version 導入 使用

在CentOS7中部署ELK日誌分析系統

ELK原理介紹

什麽是ELK

ELK是三個開源軟件的縮寫,分別表示:Elasticsearch , Logstash, Kibana , 它們都是開源軟件。新增了一個FileBeat,它是一個輕量級的日誌收集處理工具(Agent),Filebeat占用資源少,適合於在各個服務器上搜集日誌後傳輸給Logstash,官方也推薦此工具。

Elasticsearch是實時全文搜索和分析引擎,提供搜集、分析、存儲數據三大功能;是一套開放REST和JAVA API等結構提供高效搜索功能,可擴展的分布式系統。它構建於Apache Lucene搜索引擎庫之上。

Logstash是一個用來搜集、分析、過濾日誌的工具。它支持幾乎任何類型的日誌,包括系統日誌、錯誤日誌和自定義應用程序日誌。它可以從許多來源接收日誌,這些來源包括 syslog、消息傳遞(例如 RabbitMQ)和JMX,它能夠以多種方式輸出數據,包括電子郵件、websockets和Elasticsearch。

Kibana是一個基於Web的圖形界面,用於搜索、分析和可視化存儲在 Elasticsearch指標中的日誌數據。它利用Elasticsearch的REST接口來檢索數據,不僅允許用戶創建他們自己的數據的定制儀表板視圖,還允許他們以特殊的方式查詢和過濾數據。

ELK架構:
技術分享圖片
可以看到首先由logstash負責搜集各個節點服務器相關服務的日誌,如Nginx、系統日誌以及Redis的運行日誌等,然後通過logstash過濾(可以基於正則表達式),將最終的結果輸出到elasticsearch中,elasticsearch將日誌信息建立相關的index,最終通過kibana將結果更加條理化地展現出來,這就是ELK的基本流程。

實驗環境

IP 相關軟件
192.168.58.147 elasticsearch、logstash、kibana、httpd
192.168.58.147 elasticsearch
192.168.58.157 logstash

實驗實施

安裝elasticsearch

我們這次做的是搭建兩個elasticsearch節點,做分布式搜索及存儲,首先修改yum源,使用yum安裝elasticsearch,註意elasticsearch服務器內存需要大於2G

[root@promote ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
#導入GPG校驗密鑰
[root@promote ~]# vim /etc/yum.repos.d/elasticsearch.repo
#創建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@promote ~]# yum install elasticsearch -y
#使用yum安裝elasticsearch軟件包

安裝java環境,直接使用yum安裝

[root@promote ~]# yum install java -y
#使用java -version測試java環境是否搭建好
[root@promote ~]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
#可以看到java已經更新到最新版本

修改elasticsearch配置文件

[root@promote ~]# cd /etc/elasticsearch/
[root@promote elasticsearch]# vim elasticsearch.yml 

技術分享圖片
技術分享圖片
創建數據目錄,及修改目錄權限

[root@promote elasticsearch]# mkdir -p /data/es-data
[root@promote elasticsearch]# chown -R elasticsearch:elasticsearch /data/es-data/

啟動服務,並查看9200端口是否開啟

[root@promote elasticsearch]# systemctl start elasticsearch.service 
[root@promote elasticsearch]# netstat -ntap | grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      90165/java
#可以看到9200端口已經開啟

有時會碰到es服務無法啟動的情況,查看/var/log/elasticsearch/下面的日誌會發現
技術分享圖片
這個時候需要修改/etc/security/limits.conf文件
技術分享圖片
測試訪問http://192.168.58.147:9200
技術分享圖片
我們使用json格式進行交互測試

[root@promote elasticsearch]# curl -i -XGET ‘http://192.168.58.147: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
  }
}
#測試成功

可以看到上面兩種交互方式並不友好,我們可以通過安裝head插件,進行更加友好的訪問。

[root@promote elasticsearch]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
....省略
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

安裝好head插件後,我們繼續進行訪問測試http://192.168.58.147:9200/_plugin/head/
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片

下面我們創建另外一個elasticsearch節點,從而構建es群集。在另外一臺虛擬機上安裝elasticsearch及java環境,最後修改配置文件。最後啟動節點2的es服務。
技術分享圖片
技術分享圖片

[root@promote elasticsearch]# systemctl start elasticsearch.service 
[root@promote elasticsearch]# netstat -ntap | grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      2194/java           

這個時候再訪問http://192.168.58.147:9200/_plugin/head/,我們會發現會有兩個節點。
技術分享圖片
這裏我們再介紹一個插件kopf

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

安裝完後我們訪問http://192.168.58.147:9200/_plugin/kopf
技術分享圖片

安裝logstash

配置yum源文件

[root@www1 ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
#導入軟件包校驗密鑰
[root@www1 ~]# vim /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@www1 yum.repos.d]# yum install logstash -y
#安裝logstash服務

可以測試logstash

[root@www1 yum.repos.d]# /opt/logstash/bin/logstash -e ‘input { stdin{} } output { stdout{} }‘
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Settings: Default filter workers: 1
Logstash startup completed
abc123
2018-08-21T14:07:37.666Z www1.yx.com abc123
test
2018-08-21T14:07:46.156Z www1.yx.com test
#可以看到我們輸入什麽,後面就會直接輸出什麽內容

按住Ctrl+c退出後,換一種格式輸入輸出

[root@www1 yum.repos.d]# /opt/logstash/bin/logstash -e ‘input { stdin{} } output { stdout{ codec => rubydebug } }‘
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Settings: Default filter workers: 1
Logstash startup completed
abc123
{
       "message" => "abc123",
      "@version" => "1",
    "@timestamp" => "2018-08-21T14:09:18.094Z",
          "host" => "www1.yx.com"
}
#這是詳細格式輸出,可以看到更加詳細的內容

同樣,我們可以將輸入內容輸出到elasticsearch中。

[root@www1 yum.repos.d]# /opt/logstash/bin/logstash -e ‘input { stdin{} } output { elasticsearch { hosts => ["192.168.58.147:9200"] } }‘
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Settings: Default filter workers: 1
Logstash startup completed
abc123
test123
123456

然後我們到http://192.168.58.147:9200/_plugin/head/
技術分享圖片
使用logstash收集系統日誌

[root@promote ~]# ln -s /opt/logstash/bin/logstash /usr/bin/
[root@promote ~]# vim file.conf

input {
      file {
          path => "/var/log/messages"
          type => "system"
          start_position => "beginning"
      }
}

output {
     elasticsearch {
          hosts => ["192.168.58.147:9200"]
          index => "system-%{+YYYY.MM.dd}"
     }
}

啟動logstash後,我們再來訪問http://192.168.58.147:9200/_plugin/head/。
技術分享圖片

下面我們嘗試多個服務日誌,修改file.conf.

input {
    file {
       path => "/var/log/messages"
       type => "system"
       start_position => "beginning"
    }
    file {
       path => "/var/log/httpd/access_log"
       type => "httpd"
       start_position => "beginning"
    }
}

output {
   if [type] == "system" {
     elasticsearch {
         hosts => ["192.168.58.147:9200"]
         index => "system-%{+YYYY.MMdd}"
     }
   }
   if [type] == "httpd" {
     elasticsearch {
         hosts => ["192.168.58.147:9200"]
         index => "httpd-%{+YYYY.MMdd}"
     }
   }
}

我們再來訪問http://192.168.58.147:9200/_plugin/head/。
技術分享圖片

安裝kibana

下載kibana

[root@localhost ~]# wget https://download.elastic.co/kibana/kibana/kibana-4.3.1-linux-x64.tar.gz
--2018-08-21 23:02:18--  https://download.elastic.co/kibana/kibana/kibana-4.3.1-linux-x64.tar.gz
正在解析主機 download.elastic.co (download.elastic.co)... 54.235.171.120, 54.225.214.74, 54.225.221.128, ...
正在連接 download.elastic.co (download.elastic.co)|54.235.171.120|:443... 已連接。
已發出 HTTP 請求,正在等待回應... 200 OK
長度:30408272 (29M) [binary/octet-stream]
正在保存至: “kibana-4.3.1-linux-x64.tar.gz”

100%[==================================================>] 30,408,272   512KB/s 用時 82s    

2018-08-21 23:03:43 (361 KB/s) - 已保存 “kibana-4.3.1-linux-x64.tar.gz” [30408272/30408272]

解壓kibana到指定目錄

[root@localhost ~]# tar zxvf kibana-4.3.1-linux-x64.tar.gz -C /opt/

將解壓的目錄重命名為kibana

[root@localhost ~]# mv /opt/kibana-4.3.1-linux-x64/ /opt/kibana/

修改kibana配置文件

[root@localhost config]# vim /opt/kibana/config/kibana.yml 

技術分享圖片
啟動kibana

[root@localhost config]# /opt/kibana/bin/kibana 

訪問http://192.168.58.147:5601地址。
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片

在CentOS7中部署ELK日誌分析系統