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

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

ELK原理介紹

什麼是ELK

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

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

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

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

ELK架構:
在CentOS7中部署ELK日誌分析系統
能夠看到首先由logstash負責蒐集各個節點服務器相關服務的日誌,如Nginx、系統日誌以及Redis的運行日誌等,而後經過logstash過濾(能夠基於正則表達式),將最終的結果輸出到elasticsearch中,elasticsearch將日誌信息創建相關的index,最終經過kibana將結果更加條理化地展示出來,這就是ELK的基本流程。web

實驗環境

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安裝json

[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配置文件vim

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

在CentOS7中部署ELK日誌分析系統
在CentOS7中部署ELK日誌分析系統
建立數據目錄,及修改目錄權限centos

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

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

[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/下面的日誌會發現
在CentOS7中部署ELK日誌分析系統
這個時候須要修改/etc/security/limits.conf文件
在CentOS7中部署ELK日誌分析系統
測試訪問http://192.168.58.147:9200
在CentOS7中部署ELK日誌分析系統
咱們使用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/
在CentOS7中部署ELK日誌分析系統
在CentOS7中部署ELK日誌分析系統
在CentOS7中部署ELK日誌分析系統
在CentOS7中部署ELK日誌分析系統

下面咱們建立另一個elasticsearch節點,從而構建es羣集。在另一臺虛擬機上安裝elasticsearch及java環境,最後修改配置文件。最後啓動節點2的es服務。
在CentOS7中部署ELK日誌分析系統
在CentOS7中部署ELK日誌分析系統

[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/,咱們會發現會有兩個節點。
在CentOS7中部署ELK日誌分析系統
這裏咱們再介紹一個插件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
在CentOS7中部署ELK日誌分析系統

安裝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/
在CentOS7中部署ELK日誌分析系統
使用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/。
在CentOS7中部署ELK日誌分析系統

下面咱們嘗試多個服務日誌,修改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/。
在CentOS7中部署ELK日誌分析系統

安裝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

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

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

訪問http://192.168.58.147:5601地址。
在CentOS7中部署ELK日誌分析系統
在CentOS7中部署ELK日誌分析系統
在CentOS7中部署ELK日誌分析系統
在CentOS7中部署ELK日誌分析系統

相關文章
相關標籤/搜索