ElK + Filebeat安裝教程

EKL Stack簡介

ELK 不是一款軟件,而是Elasticsearch、Logstash和Kibana首字母的縮寫。這三者是開源軟件,通常配合一起使用,而且先后歸于Elasic.co公司的名下,所以簡稱ELK Stack。根據Google Trend的信息顯示,ELK已經成為目前最流行的的集中式日志解決方案。

  • Elasticsearch:分布式搜索和分析引擎,具有高可伸縮、高可靠和易管理等特點。基于Apache Lucene構建,能對大容量的數據進行接近實時的存儲、搜索和分析操作。

  • Logstash:數據收集引擎。它支持從各種數據源收集數據,并對數據進行過濾、分析、豐富、統一格式等操作,然后存儲到用戶指定的位置。

  • Kibana:數據分析和可視化平臺。通常與Elasticsearch配合使用,對其中的數據進行搜索、分析和以統計圖表的方式顯示。

  • Filebeat:ELK協議棧的新成員,一個輕量級開源日志文件數據搜集器,基于Logstash-Forwarder源碼開發,是對它的替換。在需要采集日志數據的服務器上安裝Filebeat,并指定日志目錄或日志文件后,Filebeat就能讀取數據,迅速發送到Logstash進行解析,亦或直接發送到Elasticsearch進行集中式存儲和分析。

ELK+Filebeat體系結構圖

  • 以FileBeat作為日志收集器
以FileBeat作為日志收集器

這種結構適用日志規模比較小的場景,用Filebeat替換Logstash作日志收集器,解決Logstash在各應用服務器占用資源高的問題,相對Logstash,Filebeat所占用的CPU和內存幾乎可以忽略不計。

  • 引入消息隊列作為消息緩存
引入消息隊列作為消息緩存

這種結構適用于日志規模比較大的場景。但由于Logstash日志解析節點和Elasticsearch節點的負荷比較重,可將他們配置為集群模式,以分擔負荷。引入消息隊列,減低網絡阻塞,緩存數據,避免數據丟失,但Logstash占用資源過多的問題依然存在。

本教程以第1種體系結構為例,提供安裝教程。

安裝環境信息

操作系統

Centos7- Minimal

JDK版本

jdk-8u40-linux-x64

軟件版本

elasticsearch-5.1.1
logstash-5.1.1
kibana-5.1.1
filebeat-5.1.2
elasticsearch-head-master

Elasticsearch安裝

創建elk用戶組和用戶

root用戶無法啟動Elasticsearch,需要創建非root用戶。
運行groupadd elasticsearch #創建用戶組
運行useradd -g elasticsearch el01 #添加用戶到指定用戶組
運行passwd !@#123 #創建密碼
切換到el01用戶

下載地址

https://www.elastic.co/downloads/elasticsearch
下載完成后ftp上傳到服務器

解壓到指定目錄

運行tar -zxvf elasticsearch-5.1.1.tar.gz -C /opt
授權用戶 chown -R el01:elasticsearch elasticsearch-5.1.1

配置./conf/elasticsearch.yml

配置文件說明參考:http://blog.csdn.net/zxf_668899/article/details/54582849
單機部署可不配
注意配置文件的格式要求:參數冒號后加空格,或者是數組中間加空格
還有注釋掉的參數不能在#后邊加空格不然報錯

啟動

運行./bin/elasticsearch -d #后臺進程運行模式

啟動碰到的問題

1)max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
2)max number of threads [1024] for user [lishang] likely too low, increase to at least [2048]

解決1),vi /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

解決2),vi /etc/security/limits.d/90-nproc.conf
注釋如下內容:
* soft nproc 1024
修改為
#* soft nproc 1024

vi /etc/sysctl.conf 添加下面配置: vm.max_map_count=655360
運行 source sysctl.conf
如果source不行,就重啟系統

查看啟動狀態

運行curl 'http://localhost:9200'
如果看到以下信息,說明安裝成功。

安裝狀態

運行tail -f logs/elasticsearch.log可 查詢Elasticsearch啟動及運行日志。

Elasticsearch-Head插件安裝

切換用戶su - root

軟件下載地址

下載elasticsearch-head插件
下載地址:https://github.com/mobz/elasticsearch-head.git
下載node.js
下載地址: https://nodejs.org/en/download/
下載完成后ftp上傳到服務器

解壓到指定目錄

運行tar -zxvf elasticsearch-head-master.zip -C /opt
運行tar -zxvf node-v6.9.4-linux-x64.tar.xz -C /opt

配置node.js的環境參數

運行vi /etc/profile

配置完成后運行 source /etc/profile 參數設置生效。
運行node -v 和npm -v 命令,檢查node和npm是否安裝成功。

npm安裝依賴

進入elasticsearch-head-master目錄下,
npm install
運行 npm install -g cnpm --registry=https://registry.npm.taobao.org 安裝npm的依賴。

配置參數
  • 進入elasticsearch-head-master目錄,修改Gruntfile.js文件
    運行vi Gruntfile.js,添加hostname參數
  • 修改elasticsearch的elasticsearch.yml文件,添加參數,允許跨域訪問。
    cd elasticsearch-5.1.1/config
    vi elasticsearch.yml
    在文件的最后添加:
    http.cors.enabled: true
    http.cors.allow-origin: "*"

配置完成后kill掉原來的進程,運行elasticsearch./bin/elasticsearch -d,重啟Elasticsearch。(需要切換成el01用戶來操作)

啟動

切換成root 用戶
進入elasticsearche-head-master目錄
運行./node_modules/grunt/bin/grunt server
運行nohup ./node_modules/grunt/bin/grunt server & #后臺進程啟動方式
運行tail -f nohup.out 查看啟動及運行日志。

查看啟動狀態

瀏覽器訪問:http://localhost:9100/

Logstash安裝

軟件下載地址

https://www.elastic.co/downloads/logstash
下載完成后ftp上傳到服務器

解壓到指定目錄

tar -zxvf logstash-5.1.1.tar.gz -C /opt

配置配置文件

進入./logstash-5.1.1/config目錄,創建配置文件filebeat.conf

input {
  beats {
       port => 5044
       codec => json{
            charset => "UTF-8"
        } 
       #sincedb_path => "/app/logstash-5.1.1/.sincedb"
  }
}
output {
    elasticsearch {
           hosts => "10.0.2.15:9200"
           index => "test-%{+YYYY.MM.dd}"
           document_type => "%{[type]}"
     }
    stdout {
        codec => rubydebug
     }
}

配置說明參考:
http://udn.yyuap.com/doc/logstash-best-practice-cn/get_start/install.html

啟動

運行nohup ./bin/logstash -f config/filebeat.conf & #后臺進程運行模式

查看啟動狀態

運行tail -f nohup.out 查看啟動及運行日志

Filebeat安裝

軟件下載地址

https://www.elastic.co/downloads/beats/filebeat
下載完成后ftp上傳到服務器

解壓到指定目錄

tar -zxvf filebeat-5.1.2-linux-x86_64.tar -C /opt

配置參數

進入./ filebeat-5.1.2-linux-x86_64目錄,修改配置文件vi filebeat.yml
input_type:log #配置輸入類型,log或stdin
paths:/home/el01/logs/test_json.log #配置文件目錄
output.logstash #Filebeat將日志傳送給Logstash
hosts:["localhost:5044"] #Logstash的監聽端口

配置說明參考:http://michaelkang.blog.51cto.com/1553154/1864225

啟動

運行nohup ./filebeat & #后臺進程運行模式

查看啟動狀態

運行tail -f nohup.out 查看運行日志

Kibana安裝

軟件下載地址

https://www.elastic.co/downloads/kibana
下載完成后ftp上傳到服務器

解壓到指定目錄

tar -zxvf kibana-5.1.1-linux-x86_64.tar.gz -C /opt

配置參數

進入. /kibana-5.1.1-linux-x86_64/config目錄,修改配置文件vi kibana.yml
server.port: 5601 #Kibana默認端口
server.host: "0.0.0.0" #Kibana地址,配置0.0.0.0可外部訪問
elasticsearch.url: "http://xxxx:9200" #elasticsearch的地址

配置說明參考:http://blog.csdn.net/molaifeng/article/details/53889547#

啟動

運行nohup ./bin/kibana & #后臺進程運行模式

查看啟動狀態

運行tail -f nohup.out 查看啟動及運行日志

瀏覽器訪問http://192.168.1.220:5601/

AlastAlert安裝

軟件下載地址

https://codeload.github.com/Yelp/elastalert/zip/master
下載完成后ftp上傳到服務器

解壓到指定目錄

unzip elastalert-master.zip -d /opt

配置參數

進入/opt/elastalert-master目錄
查看是否安裝gcc,如果沒有yum -y install gcc
測試python是否安裝 python,運行python 命令
運行python setup.py install #如果python沒安裝,請安裝python2.6或2.7版本
安裝過程如果提示需要安裝setuptools,

則運行,下載安裝包安裝
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz
安裝setuptools:
tar -xvf setuptools-1.4.2.tar.gz
cd setuptools-1.4.2
python setup.py install
更新版本:
pip install -U pip
pip install -U setuptools

重新進入/opt/elastalert-master目錄,運行python setup.py install,
如果出現以下錯誤:

則運行:yum install python-devel -y
重新運行python setup.py install
最后運行:pip install -r requirements.txt #安裝依賴庫
如果報錯:

更新版本:
pip install -U pip
pip install -U setuptools
再執行:pip install -r requirements.txt

配置

進入/opt/elastalert-master目錄
創建配置目錄:mkdir rules #保存配置文件
cp example_rules/example_frequency.yaml rules/frequency.yaml #復制配置文件
修改 vi rules/frequency.yaml 參考如下:

參考資料:http://blog.csdn.net/gamer_gyt/article/details/52917116

然后cd /app/elastalert-master
創建文件 vi smtp_auth_file.yaml #文件名是固定的

填寫發件人郵箱賬號和密碼(郵箱客戶端的密碼)。

創建config文件:
cp config.yaml.example config.yaml
vi config.yaml

啟動

運行:
python -m elastalert.elastalert --config ./config.yaml --rule ./examele_rules/one_rule.yaml

后臺進程運行模式

查看啟動狀態

運行tail -f nohup.out 查看啟動及運行日志

Nginx安裝自帶身份驗證(實現Kibana賬號密碼登錄)

生成賬號和登錄密碼

通過Nginx的ngx_http_auth_basic_module生成賬號和密碼
查看nginx的模塊是否有包含它; ./nginx -V
開始生成賬號密碼;
cd /app/nginx/conf
運行生成命令:
# printf "username:$(openssl passwd -crypt 20@17xxx)\n" > htpasswd
帳號:username
密碼:20@17xxx
日后添加用戶則使用追加命令就可以
添加用戶:
printf "xxxx2:$(openssl passwd -crypt 20@17xxx)\n" >> htpasswd

配置參數

vi kibana.conf

server {
      listen 80;
      server_name kibana.gzkkonline.com;
      
     #配置身份驗證的注釋和用戶驗證文件
      auth_basic "Kibana Auth";
      auth_basic_user_file  htpasswd;
      
      access_log  logs/kibana_access.log main;
      error_log  logs/kibana_error.log;
      
     location / {
          proxy_pass http://10.0.2.15:5601;
      }
}
啟動

運行:
systemctl start nginx.service
如何錯誤日志出現以下問題:
*1 connect() to 127.0.0.1:5601 failed (13: Permission denied)
運行:selinux 的狀態
setsebool -P httpd_can_network_connect 1

查看啟動狀態

查看啟動及運行日志
tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/access.log

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容