天天看點

ELK 架構之 Logstash 和 Filebeat 安裝配置

閱讀目錄:

  • 1. 環境準備
  • 2. 安裝 Logstash
  • 3. 配置 Logstash
  • 4. Logstash 采集的日志資料,在 Kibana 中顯示
  • 5. 安裝配置 Filebeat
  • 6. Filebeat 采集的日志資料,在 Kibana 中顯示
  • 7. Filebeat 采集日志資料,Logstash 過濾
  • 8. Filebeat 采集的日志資料,Logstash 過濾後,在 Kibana 中顯示

上一篇主要說的是 Elasticsearch 和 Kibana 安裝配置,以及服務追蹤資料的處理和展示,日志資料采集使用的 Spring Cloud Sleuth Zipkin + Stream/RabbitMQ 中間件(Service 端配置),然後 Zipkin Server 從隊列中擷取日志資料,再使用 HTTP 的請求的方式,傳輸并存儲到 Elasticsearch 中,最後 Kibana 進行日志資料展示。

在 ELK Stack 中,日志資料采集有單獨的工具,就是 Logstash 和 Beats。

  • Logstash 主要是用來日志的搜集、分析、過濾日志的工具,支援大量的資料擷取方式。一般工作方式為 c/s 架構,client 端安裝在需要收集日志的主機上,server 端負責将收到的各節點日志進行過濾、修改等操作在一并發往 Elasticsearch 上去。
  • Beats 在這裡是一個輕量級日志采集器,其實 Beats 家族有 6 個成員,早期的 ELK 架構中使用 Logstash 收集、解析日志,但是 Logstash 對記憶體、cpu、io 等資源消耗比較高。相比 Logstash,Beats 所占系統的 CPU 和記憶體幾乎可以忽略不計。

目前 Beats 包含六種工具:

  • Packetbeat: 網絡資料(收集網絡流量資料)
  • Metricbeat: 名額(收集系統、程序和檔案系統級别的 CPU 和記憶體使用情況等資料)
  • Filebeat: 日志檔案(收集檔案資料)
  • Winlogbeat: windows 事件日志(收集 Windows 事件日志資料)
  • Auditbeat:審計資料(收集審計日志)
  • Heartbeat:運作時間監控(收集系統運作時的資料)

本篇文章目的:

  • 1. Logstash 日志資料采集,Elasticsearch 存儲,Kibana 展示
  • 2. Filebeat 日志資料采集,Elasticsearch 存儲,Kibana 展示
  • 3. Filebeat 日志資料采集,Logstash 過濾,Elasticsearch 存儲,Kibana 展示

第三種方案的實作架構圖:

ELK 架構之 Logstash 和 Filebeat 安裝配置

伺服器環境:Centos 7.0(單機版)

Elasticsearch 和 Logstash 需要 Java,Elasticsearch 推薦的版本為 Java 8,安裝教程:​​确定穩定的 Spring Cloud 相關環境版本​​

另外,我們需要修改下伺服器主機資訊:

[root@node1 ~]# vi /etc/hostname
node1

[root@node1 ~]# vi /etc/hosts
192.168.0.11 node1
127.0.0.1   node1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         node1 localhost localhost.localdomain localhost6 localhost6.localdomain6      

運作以下指令将 Elasticsearch 公共 GPG 密鑰導入 rpm:

[root@node1 ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch      

在​

​/etc/yum.repos.d/​

​​目錄中,建立一個名為​

​logstash.repo​

​的檔案,添加下面配置:

[logstash-5.x]
name=Logstash repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md      

然後安裝 Logstash:

[root@node1 ~]# yum makecache && yum install logstash -y
已加載插件:fastestmirror
base                                                                          | 3.6 kB  00:00:00
elasticsearch-5.x                                                             | 1.3 kB  00:00:00
extras                                                                        | 3.4 kB  00:00:00
kibana-5.x                                                                    | 1.3 kB  00:00:00
logstash-5.x                                                                  | 1.3 kB  00:00:00
rabbitmq-erlang                                                               | 1.3 kB  00:00:00
updates                                                                       | 3.4 kB  00:00:00
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
中繼資料緩存已建立
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
正在解決依賴關系
--> 正在檢查事務
---> 軟體包 logstash.noarch.1.5.6.9-1 将被 安裝
--> 解決依賴關系完成

依賴關系解決

=====================================================================================================
 Package              架構               版本                    源                             大小
=====================================================================================================
正在安裝:
 logstash             noarch             1:5.6.9-1               elasticsearch-5.x              98 M

事務概要
=====================================================================================================
安裝  1 軟體包

總下載下傳量:98 M
安裝大小:188 M
Downloading packages:
logstash-5.6.9.rpm                                                            |  98 MB  00:06:38
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安裝    : 1:logstash-5.6.9-1.noarch                                                        1/1
Using provided startup.options file: /etc/logstash/startup.options
/usr/share/logstash/vendor/jruby/bin/jruby:行388: /usr/bin/java: 沒有那個檔案或目錄
Unable to install system startup script for Logstash.
  驗證中      : 1:logstash-5.6.9-1.noarch                                                        1/1

已安裝:
  logstash.noarch 1:5.6.9-1

完畢!      

安裝報錯,具體問題:​

​/usr/share/logstash/vendor/jruby/bin/jruby:行388: /usr/bin/java: 沒有那個檔案或目錄​

​。

解決方案:​​安裝 Logstash 提示 /usr/bin/java: No such file or directory​​

對 Java 配置軟連結,連結到​

​/usr/bin/java​

​:

[root@node1 ~]# ln -s /usr/local/java/bin/java /usr/bin/java      

然後解除安裝 Logstash:

[root@node1 ~]# yum -y remove logstash      

重新安裝:

[root@node1 ~]# yum makecache && yum install logstash -y      

設定開機啟動,以及啟動系統服務:

[root@node1 ~]# sudo /bin/systemctl daemon-reload
[root@node1 ~]# sudo /bin/systemctl enable logstash.service      

先不急啟動 Logstash,需要先配置下 Logstash。

采集日志資料,需要有個資料源,這裡我們使用 rsyslog 進行測試。

Linux 日志機制的核心是 rsyslog 守護程序,該服務負責監聽 Linux下 的日志資訊,并把日志資訊追加到對應的日志檔案中,一般在 /var/log 目錄下。 它還可以把日志資訊通過網絡協定發送到另一台 Linux 伺服器上,或者将日志存儲在 MySQL 或 Oracle 等資料庫中。

修改 rsyslog 配置:

[root@node1 ~]# vi /etc/rsyslog.conf
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
*.* @@192.168.1.40:10514      

重新開機 rsyslog:

[root@node1 ~]# systemctl restart rsyslog      

增加配置檔案:

[root@node1 ~]# vi /etc/logstash/conf.d/syslog.conf
input {
  syslog {
    type => "system-syslog"
    port => 10514
  }
}
output {
  elasticsearch {
    hosts => ["node1:9200"]  # 定義es伺服器的ip
    index => "system-syslog-%{+YYYY.MM}" # 定義索引
  }
}      

驗證配置檔案,是否有效:

[root@node1 ~]# cd /usr/share/logstash/bin
[root@node1 bin]# ./logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/syslog.conf --config.test_and_exit
Sending Logstash's logs to /var/log/logstash which is now configured via log4j2.properties
Configuration OK      

啟動 Logstash:

[root@node1 ~]# systemctl start logstash      

檢視啟動日志:

[root@node1 ~]# cat /var/log/logstash/logstash-plain.log
[2018-05-15T08:19:05,578][FATAL][logstash.runner          ] An unexpected error occurred! {:error=>#<ArgumentError: Path "/var/lib/logstash/queue" must be a writable directory. It is not writable.>, :backtrace=>["/usr/share/logstash/logstash-core/lib/logstash/settings.rb:439:in `validate'", "/usr/share/logstash/logstash-core/lib/logstash/settings.rb:222:in `validate_value'", "/usr/share/logstash/logstash-core/lib/logstash/settings.rb:138:in `validate_all'", "org/jruby/RubyHash.java:1342:in `each'", "/usr/share/logstash/logstash-core/lib/logstash/settings.rb:137:in `validate_all'", "/usr/share/logstash/logstash-core/lib/logstash/runner.rb:243:in `execute'", "/usr/share/logstash/vendor/bundle/jruby/1.9/gems/clamp-0.6.5/lib/clamp/command.rb:67:in `run'", "/usr/share/logstash/logstash-core/lib/logstash/runner.rb:204:in `run'", "/usr/share/logstash/vendor/bundle/jruby/1.9/gems/clamp-0.6.5/lib/clamp/command.rb:132:in `run'", "/usr/share/logstash/lib/bootstrap/environment.rb:71:in `(root)'"]}      

具體錯誤:​

​Path "/var/lib/logstash/queue" must be a writable directory. It is not writable​

執行指令:

[root@node1 ~]# chown -R logstash /var/log/logstash /var/lib/logstash      

重新啟動 Logstash:

[root@node1 ~]# systemctl restart logstash      

檢視 Logstash 啟動狀态:

[root@node1 ~]# systemctl status logstash
logstash.service - logstash
   Loaded: loaded (/etc/systemd/system/logstash.service; enabled)
   Active: active (running) since 二 2018-05-15 08:23:37 CEST; 1min 4s ago
 Main PID: 10855 (java)
   CGroup: /system.slice/logstash.service
           └─10855 /usr/bin/java -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupan...

5月 15 08:23:37 node1 systemd[1]: Starting logstash...
5月 15 08:23:37 node1 systemd[1]: Started logstash.
5月 15 08:23:46 node1 logstash[10855]: Sending Logstash's logs to /var/log/logstash which is ...ties
Hint: Some lines were ellipsized, use -l to show in full.      

檢視端口監聽(是否正常):

[root@node1 ~]# netstat -lntp |grep 9600
tcp6       0      0 127.0.0.1:9600          :::*                    LISTEN      10855/java
[root@node1 ~]# netstat -lntp |grep 10514
tcp6       0      0 :::10514                :::*                    LISTEN      10855/java      

Logstash 的監聽 IP 是​

​127.0.0.1​

​這個本地 IP,本地 IP 無法遠端通信,是以需要修改一下配置檔案,配置一下監聽的 IP(需要設定 IP 位址,不能設定 Hostname):

[root@node1 ~]# vi /etc/logstash/logstash.yml
http.host: "192.168.1.40"      
[root@node1 ~]# systemctl restart logstash      
[root@node1 ~]# netstat -lntp |grep 9600
tcp6       0      0 192.168.1.40:9600       :::*                    LISTEN      11214/java      

檢視 Elasticsearch 索引清單,可以看到生成了​

​system-syslog-*​

​類型的日志資料:

[root@node1 ~]# curl http://node1:9200/_cat/indices?v
health status index                  uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   twitter                k1KnzWyYRDeckjt7GASh8w   5   1          1            0      5.2kb          5.2kb
yellow open   .kibana                8zJGQkq8TwC4s3JJLMX44g   1   1          5            0     27.1kb         27.1kb
yellow open   system-syslog-2018.05  0aKfJeOmTZ6yen9eSlCOig   5   1         32            0    201.9kb        201.9kb
yellow open   zipkin:span-2018-05-15 8vnm9SJvSlK-cAtakK8VeQ   3   1          9            0     47.3kb         47.3kb
yellow open   zipkin:span-2018-05-14 TZ-MmJpoSs-NwjHjxEvhFQ   3   1        219            0    127.1kb        127.1kb
yellow open   student                iZPqPcwrQbifGOfE9DQYvg   5   1          0            0       955b           955b      

然後在 Kibana 上面建立​

​system-syslog-*​

​索引:

ELK 架構之 Logstash 和 Filebeat 安裝配置

建立好之後,就可以看到整個日志資料了:

ELK 架構之 Logstash 和 Filebeat 安裝配置

其實這裡顯示的日志資料就是​

​/var/log/messages​

​​檔案裡的資料,因為 Logstash 裡配置的就是收集​

​messages​

​檔案裡的資料。

以上這就是如何使用 Logstash 收集系統日志,輸出到 Elasticsearch 伺服器上,并在 Kibana 的頁面上進行檢視。

[root@node1 ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch      

​/etc/yum.repos.d/​

​elastic.repo​

[elastic-5.x]
name=Elastic repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md      

安裝 Filebeat:

[root@node1 ~]# yum makecache && yum install filebeat -y      
[root@node1 ~]# sudo /bin/systemctl daemon-reload
[root@node1 ~]# sudo /bin/systemctl enable filebeat.service      

編輯配置檔案:

[root@node1 ~]# vi /etc/filebeat/filebeat.yml
filebeat.prospectors:
- type: log
   paths:
      - /var/log/messages  # 指定需要收集的日志檔案的路徑

output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["node:9200"]  # 配置 Elasticsearch 伺服器的 IP 位址      

啟動 Filebeat 服務:

[root@node1 ~]# systemctl start filebeat      

檢視 Filebeat 服務狀态:

[root@node1 ~]# systemctl status filebeat
filebeat.service - filebeat
   Loaded: loaded (/usr/lib/systemd/system/filebeat.service; enabled)
   Active: active (running) since 二 2018-05-15 09:29:44 CEST; 1min 12s ago
     Docs: https://www.elastic.co/guide/en/beats/filebeat/current/index.html
 Main PID: 12332 (filebeat)
   CGroup: /system.slice/filebeat.service
           └─12332 /usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml -path.home /usr/...

5月 15 09:29:44 node1 systemd[1]: Started filebeat.      

​filebeat-*​

[root@node1 ~]# curl http://node1:9200/_cat/indices?v
health status index                  uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   twitter                k1KnzWyYRDeckjt7GASh8w   5   1          1            0      5.2kb          5.2kb
yellow open   .kibana                8zJGQkq8TwC4s3JJLMX44g   1   1          6            0     33.3kb         33.3kb
yellow open   system-syslog-2018.05  0aKfJeOmTZ6yen9eSlCOig   5   1        689            0    652.3kb        652.3kb
yellow open   filebeat-2018.05.15    K852lKlgSaKG6-hE_GNB4w   5   1      27417            0      6.7mb          6.7mb
yellow open   zipkin:span-2018-05-15 8vnm9SJvSlK-cAtakK8VeQ   3   1          9            0     47.3kb         47.3kb
yellow open   zipkin:span-2018-05-14 TZ-MmJpoSs-NwjHjxEvhFQ   3   1        219            0    127.1kb        127.1kb
yellow open   student                iZPqPcwrQbifGOfE9DQYvg   5   1          0            0       955b           955b      

​filebeat-*​

ELK 架構之 Logstash 和 Filebeat 安裝配置
ELK 架構之 Logstash 和 Filebeat 安裝配置

和上面 Logstash 收集 rsyslog 日志資料一樣,Filebeat 配置的,也是收集的​

​/var/log/messages​

​日志資料。

可以看到,​

​system-syslog​

​​和​

​filebeat-​

​索引對應的日志資料,是同步更新的(如果沒效果,重新開機下 Logstash 和 Filebeat)。

先停止 Logstash 和 Filebeat:

[root@node1 ~]# systemctl stop logstash && 
systemctl stop filebeat      

然後删除上面産生的索引日志資料:

[root@node1 ~]# curl -XDELETE http://node1:9200/system-syslog-2018.05 && 
curl -XDELETE http://node1:9200/filebeat-2018.05.15      

建立新的 Logstash 配置檔案:

[root@node1 ~]# vi /etc/logstash/conf.d/logstash-filebeat-syslog.conf
input {
 beats {
   port => 10515
  }
}
filter {
  if [type] == "syslog" {
    grok {
      match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
      add_field => [ "received_at", "%{@timestamp}" ]
      add_field => [ "received_from", "%{host}" ]
    }
    syslog_pri { }
    date {
      match => [ "syslog_timestamp", "MMM  d HH:mm:ss", "MMM dd HH:mm:ss" ]
    }
  }
}
output {
 elasticsearch {
  hosts => [ "node1:9200" ]
    index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}"
  }
}      

驗證 Logstash 配置檔案,是否有效:

[root@node1 ~]# cd /usr/share/logstash/bin
[root@node1 bin]# ./logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/logstash-filebeat-syslog.conf --config.test_and_exit
Sending Logstash's logs to /var/log/logstash which is now configured via log4j2.properties
Configuration OK      

編輯 Filebeat 配置檔案(注釋掉​

​output.elasticsearch​

​):

[root@node1 ~]# vi /etc/filebeat/filebeat.yml
filebeat.prospectors:
- type: log
   paths:
      - /var/log/messages  # 指定需要收集的日志檔案的路徑
  fields:
    log_type: syslog

output.logstash:
  # The Logstash hosts
  hosts: ["node1:10515"]      

重新啟動 Logstash 和 Filebeat:

[root@node1 ~]# systemctl restart logstash && 
systemctl restart filebeat      

檢視 Logstash 是否正常監聽:

[root@node1 conf.d]# netstat -lnp|grep 10515
tcp6       0      0 :::10515                :::*                    LISTEN      22384/java      

可以看到生成了​

​filebeat-*​

​​類型的日志資料(​

​system-syslog-2018.05​

​​索引日志資料,是由 Logstash 的​

​syslog.conf​

​配置檔案采集的):

[root@node1 ~]# curl http://node1:9200/_cat/indices?v
health status index                           uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   twitter                         k1KnzWyYRDeckjt7GASh8w   5   1          1            0      5.2kb          5.2kb
yellow open   .kibana                         8zJGQkq8TwC4s3JJLMX44g   1   1          7            1     57.4kb         57.4kb
yellow open   system-syslog-2018.05           dzRXf3eFQ_-hKqlGILTSJg   5   1        223            0    511.9kb        511.9kb
yellow open   zipkin:span-2018-05-15          8vnm9SJvSlK-cAtakK8VeQ   3   1         49            0    142.6kb        142.6kb
yellow open   filebeat-2018.05.15             eEuumW8pTdeXa7Ny1gLIPQ   5   1        177            0    300.2kb        300.2kb
yellow open   zipkin:span-2018-05-14          TZ-MmJpoSs-NwjHjxEvhFQ   3   1        219            0    127.1kb        127.1kb
yellow open   student                         iZPqPcwrQbifGOfE9DQYvg   5   1          0            0       955b           955b      

因為之前建立了​

​filebeat-*​

​system-syslog-*​

​索引,這邊我們可以直接檢視日志資料了: