ELK日誌服務器搭建

ELK

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

Elasticsearch是個開源分佈式搜索引擎,提供蒐集、分析、存儲數據三大功能。它的特色有:分佈式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多數據源,自動搜索負載等。node

Logstash 主要是用來日誌的蒐集、分析、過濾日誌的工具,支持大量的數據獲取方式。通常工做方式爲c/s架構,client端安裝在須要收集日誌的主機上,server端負責將收到的各節點日誌進行過濾、修改等操做在一併發往elasticsearch上去。linux

Kibana 也是一個開源和免費的工具,Kibana能夠爲 Logstash 和 ElasticSearch 提供的日誌分析友好的 Web 界面,能夠幫助彙總、分析和搜索重要數據日誌。git

Filebeat隸屬於Beats。目前Beats包含四種工具:github

    1. Packetbeat(蒐集網絡流量數據)
    2. Topbeat(蒐集系統、進程和文件系統級別的 CPU 和內存使用狀況等數據)
    3. Filebeat(蒐集文件數據)
    4. Winlogbeat(蒐集 Windows 事件日誌數據)

在這裏插入圖片描述

<!--more-->redis

部署安裝ELK服務器

環境準備 (須要java環境)

ELK-1 192.168.1.10
ELk-2 192.168.1.20
apache 192.168.1.30

修改主機名稱

[root@localhost ~]# hostnamectl  set-hostname  elk-1
[root@localhost ~]# bash
[root@elk-1 ~]#
[root@localhost ~]# hostnamectl  set-hostname  elk-2
[root@localhost ~]# bash
[root@elk-2 ~]#

在 ELK-1 2 中 /etc/hosts 文件中添加域名解析

[root@elk-1 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.10  elk-1
192.168.1.20  elk-2

上傳elasticsearch文件解壓安裝(ELK—1 2)

[root@elk-1 ~]# ls
anaconda-ks.cfg                     initial-setup-ks.cfg  模板  圖片  下載  桌面
elasticsearch-7.9.2-x86_64(新).rpm  公共                  視頻  文檔  音樂
[root@elk-1 ~]# rpm -ivh elasticsearch-7.9.2-x86_64\(新\).rpm 
警告:elasticsearch-7.9.2-x86_64(新).rpm: 頭V4 RSA/SHA512 Signature, 密鑰 ID d88e42b4: NOKEY
準備中...                          ################################# [100%]
Creating elasticsearch group... OK
Creating elasticsearch user... OK
正在升級/安裝...
   1:elasticsearch-0:7.9.2-1          ################################# [100%]
### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
 sudo systemctl daemon-reload
 sudo systemctl enable elasticsearch.service
### You can start elasticsearch service by executing
 sudo systemctl start elasticsearch.service
Created elasticsearch keystore in /etc/elasticsearch/elasticsearch.keystore

[root@elk-1 ~]# systemctl  daemon-reload 
[root@elk-1 ~]# sudo systemctl enable elasticsearch.service

Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.

更改配置文件(ELK-1 2)

[root@elk-1 ~]# vim /etc/elasticsearch/elasticsearch.yml
17:cluster.name: my-elk-cluster 集羣名字
23:node.name: elk-1  節點名稱
33:path.data: /data/elk_data 數據存放路徑
37:path.logs: /var/log/elasticsearch/ 日誌存放路徑 
43:bootstrap.memory_lock: false 不在啓動的時候鎖定內存 
55:network.host: 0.0.0.0 提供服務綁定的 IP 地址,0.0.0.0 表明全部地址 
59:http.port: 9200 偵聽端口爲 9200
68:discovery.seed_hosts: ["elk-1", "elk-2"]  集羣中的主機
72:cluster.initial_master_nodes: ["elk-1"]  主節點

[root@elk-2 ~]# vim /etc/elasticsearch/elasticsearch.yml
17:cluster.name: my-elk-cluster 集羣名字
23:node.name: elk-2  節點名稱
33:path.data: /data/elk_data 數據存放路徑
37:path.logs: /var/log/elasticsearch/ 日誌存放路徑 
43:bootstrap.memory_lock: false 不在啓動的時候鎖定內存 
55:network.host: 0.0.0.0 提供服務綁定的 IP 地址,0.0.0.0 表明全部地址 
59:http.port: 9200 偵聽端口爲 9200
68:discovery.seed_hosts: ["elk-1", "elk-2"]  集羣中的主機
72:cluster.initial_master_nodes: ["elk-1"]  主節點

建立文件所須要的目錄(ELK-1 2)

[root@elk-2 ~]# mkdir -p /data/elk_data
[root@elk-2 ~]# mkdir -p /var/log/elasticsearch/
[root@elk-2 ~]# chown elasticsearch:elasticsearch  /data/elk_data/
[root@elk-2 ~]# chown elasticsearch:elasticsearch  /var/log/elasticsearch/
[root@elk-2 ~]#

開啓elk(ELK-1 2)

[root@elk-1 ~]# systemctl start elasticsearch.service
[root@elk-1 ~]# netstat -anpt | grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      43577/java

查看健康狀態(查看集羣的健康狀況,能夠看到 status 爲 green 綠色)

http://192.168.1.10:9200/_cluster/health?prettyapache

http://192.168.1.20:9200/_cluster/health?prettynpm

http://192.168.1.10:9200/_cluster/state?prettyjson

http://192.168.1.20:9200/_cluster/state?prettybootstrap

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

上傳node安裝包安裝(ELK-1)

[root@elk-1 ~]# ls
anaconda-ks.cfg                     initial-setup-ks.cfg     node-v12.13.0-linux-x64.tar  模板  圖片  下載  桌面
elasticsearch-7.9.2-x86_64(新).rpm  node-v12.13.0-linux-x64  公共                         視頻  文檔  音樂
[root@elk-1 ~]# tar -vxf node-v12.13.0-linux-x64.tar 
[root@elk-1 ~]# mv node-v12.13.0-linux-x64 /usr/local/nodesjs
[root@elk-1 ~]# vim /etc/profile
VERSION=v10.15.0
DISTRO=linux-x64
export PATH=/usr/local/nodesjs/bin:$PATH
[root@elk-1 ~]# source  /etc/profile

安裝es-head,上傳安裝包:es-head.tar.gz解壓安裝(ELK-1)

# 安裝phantomjs
[root@elk-1 ~]# tar  jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2 
[root@elk-1 ~]# cd phantomjs-2.1.1-linux-x86_64/bin/
[root@elk-1 bin]# cp phantomjs  /usr/local/bin/

[root@elk-1 ~]# git clone git://github.com/mobz/elasticsearch-head.git
[root@elk-1 ~]# cd elasticsearch-head/
[root@elk-1 elasticsearch-head]# npm install
[root@elk-1 elasticsearch-head]# npm  run start &
[root@elk-1 elasticsearch-head]# netstat -anpt  | grep 9100

修改 elasticsearch 主配置文件,添加以下內容,而後重啓 elasticsearch 服務(ELK-1)

[root@elk-1 ~]#  vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true 開啓跨域訪問支持,默認爲 false
http.cors.allow-origin: "*" 跨域訪問容許的域名地址
[root@elk-1 ~]# systemctl restart elasticsearch

經過瀏覽器進行訪問:http://192.168.1.10:9100/,並鏈接集羣

在這裏插入圖片描述

能夠看到集羣很健康,健康值爲 green 綠色。點擊數據瀏覽,能夠看到索引、類型爲空

在這裏插入圖片描述

經過命令插入一個測試索引,索引爲 index-demo,類型爲 test(ELK-1)

[root@elk-1 ~]# curl -XPUT '192.168.1.10:9200/index-demo/test/1?pretty&pretty' -H 'Content-Type:  application/json' -d '{ "user": "zhangsan","mesg":"hello world" }'  # 回車
{
  "_index" : "index-demo",
  "_type" : "test",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
[root@elk-1 ~]#

刷新瀏覽器,能夠看到建立成功的索引

在這裏插入圖片描述

安裝 logstash 並作一些日誌蒐集輸出到 elasticsearch 中(ELK-1)

[root@elk-1 ~]# rpm -ivh  logstash-7.9.2.rpm 
警告:logstash-7.9.2.rpm: 頭V4 RSA/SHA512 Signature, 密鑰 ID d88e42b4: NOKEY
準備中...                          ################################# [100%]
正在升級/安裝...
   1:logstash-1:7.9.2-1               ################################# [100%]
Using provided startup.options file: /etc/logstash/startup.options
/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/pleaserun-0.0.31/lib/pleaserun/platform/base.rb:112: warning: constant ::Fixnum is deprecated
Successfully created system startup script for Logstash

[root@elk-1 ~]# systemctl start logstash.service
[root@elk-1 ~]# ln -s /usr/share/logstash/bin/logstash  /usr/local/bin/

logstash 基本使用

Logstash 使用管道方式進行日誌的蒐集處理和輸出。有點相似 linux 系統的管道命令 xxx | ccc | ddd,xxx 執行完了會執行 ccc,而後執行 ddd。 在 logstash 中,包括了三個階段:
輸入 input --> 處理 filter(不是必須的) --> 輸出 output
每一個階段都由不少的插件配合工做,好比 file、elasticsearch、redis 等等。
每一個階段也能夠指定多種方式,好比輸出既能夠輸出到 elasticsearch 中,也能夠指定到 stdout在控制檯打印。

因爲這種插件式的組織方式,使得 logstash 變得易於擴展和定製。

logstash 命令行中經常使用的命令: -f:經過這個命令能夠指定 Logstash 的配置文件,根據配置文件配置 logstash

-e:後面跟着字符串,該字符串能夠被當作 logstash 的配置(若是是「」 則默認使用 stdin
做爲輸入,stdout 做爲輸出)

-t:測試配置文件是否正確,而後退出

啓動一個 logstash,-e:在命令行執行;input 輸入,stdin 標準輸入,是一個插件;output 輸出,stdout:標準輸出

[root@elk-1 ~]#  logstash -e 'input { stdin{} } output { stdout{} }'
[INFO ] 2020-10-22 06:38:09.069 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com   #輸入
{
    "@timestamp" => 2020-10-22T13:38:19.724Z,
          "host" => "elk-1",
      "@version" => "1",
       "message" => "www.baidu.comwww.baidu.com"
}
www.sina.com.cn  #輸入
{
    "@timestamp" => 2020-10-22T13:38:30.823Z,
          "host" => "elk-1",
      "@version" => "1",
       "message" => "www.sina.com.cn"
}

使用 rubydebug 顯示詳細輸出,codec 爲一種編解碼器

[root@elk-1 ~]# logstash -e 'input { stdin{} } output { stdout{ codec =>rubydebug} }'
22:54:23.075 [Api Webserver] INFO logstash.agent - Successfully started Logstash API 
endpoint {:port=>9600}
www.baidu.com 輸入
{
 "@timestamp" => 2017-08-09T14:54:31.236Z,
 "@version" => "1",
 "host" => "elk-node1",
 "message" => "www.baidu.com"
}
www.sina.com.cn 輸入
{
 "@timestamp" => 2017-08-09T14:54:39.412Z,
 "@version" => "1",
 "host" => "elk-node1",
 "message" => "www.sina.com.cn"
}

使用 logstash 將信息寫入到 elasticsearch 中

[root@elk-1 ~]#  logstash -e 'input { stdin{} } output { elasticsearch { hosts=> ["192.168.1.10:9200"]} }'
[INFO ] 2020-10-22 06:40:44.995 [Ruby-0-Thread-5: :1] elasticsearch - Installing ILM policy {"policy"=>{"phases"=>{"hot"=>{"actions"=>{"rollover"=>{"max_size"=>"50gb", "max_age"=>"30d"}}}}}} to _ilm/policy/logstash-policy

www.baidu.com  #輸入
www.sina.com.cn
www.google.com

在 elasticsearch 中查看 logstash 新加的索引

在這裏插入圖片描述

logstash 配置文件使用

logstash 配置文件基本上由三部分組成,input、output 以及用戶須要才添加的 filter,
所以標準的配置文件格式以下:
input {...} 
filter {...} 
output {...}
在每一個部分中,也能夠指定多個訪問方式,例如我想要指定兩個日誌來源文件,則能夠這樣寫:
input { 
file { path =>"/var/log/messages" type =>"syslog"} 
file { path =>"/var/log/apache/access.log" type =>"apache"} 
}
 下面是一個收集系統日誌的配置文件例子,將其放到/etc/logstash/conf.d/目錄中,
logstash 啓動的時候便會加載。注意要給 logstash 讀取日誌文件的權限
[root@elk-1 ~]# chmod o+r  /var/log/messages 
[root@elk-1 ~]# ll /var/log/messages 
-rw----r--. 1 root root 746161 10月 22 06:55 /var/log/messages
[root@elk-1 ~]# cd /etc/logstash/conf.d/
[root@elk-1 conf.d]# vim system.conf

input {
 file {   從文件中讀取
 path => "/var/log/messages"    文件路徑
 type => "system"
 start_position => "beginning"  是否從頭開始讀取
 } 
}

output {      
 elasticsearch {       輸出到 elasticsearch 中
 hosts => ["192.168.1.10:9200"]   elasticsearch 主機地址和端口
 index => "system-%{+YYYY.MM.dd}"  索引名稱
 } 
}

重啓 logstash 服務

[root@elk-1 conf.d]#  systemctl restart logstash

在瀏覽器中便可看到新加索引及其內容

在這裏插入圖片描述

在這裏插入圖片描述

安裝Kibana(注意須要和elasticsearch,logstash同一版本)(ELK-1)

[root@elk-1 ~]# rpm -ivh kibana-5.5.3-x86_64.rpm 
警告:kibana-5.5.3-x86_64.rpm: 頭V4 RSA/SHA512 Signature, 密鑰 ID d88e42b4: NOKEY
準備中...                          ################################# [100%]
正在升級/安裝...
   1:kibana-5.5.3-1 ################################# [100%]
[root@elk-1 ~]# systemctl enable kibana.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/kibana.service to /etc/systemd/system/kibana.service.

設置 kibana 的主配置文件/etc/kibana/kibana.yml

[root@elk-1 ~]# rpm -ivh kibana-7.9.2-x86_64.rpm
server.port: 5601 kibana 打開的端口
server.host: "0.0.0.0" kibana 偵聽的地址
elasticsearch.url: "http://192.168.1.10:9200" 和 elasticsearch 創建聯繫
kibana.index: ".kibana" 在 elasticsearch 中添加.kibana 索引
[root@elk-1 ~]# systemctl start kibana.service

使用瀏覽器訪問 192.168.1.10:5601,第一次登陸須要添加一個 elasticsearch 索引,咱們添加前面的索引 system-*

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

點擊 discover 進行查看

在這裏插入圖片描述

在這裏插入圖片描述

能夠進行 fields 區域篩選,點擊「Available Fields」中的「host」,而後點擊添加「add」

在這裏插入圖片描述

能夠看到按照」host」篩選後的結果

在這裏插入圖片描述

將 apache 服務器的日誌添加到 elasticsearch 並經過 kibana 顯示

[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# curl 127.0.0.1
[root@localhost ~]# rpm -ivh logstash-7.9.2.rpm
[root@localhost ~]# systemctl enable logstash.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/logstash.service to /etc/systemd/system/logstash.service.

編寫 logstash 配置文件 apache_log.conf 蒐集 apache 日誌並執行

[root@localhost ~]# cd /etc/logstash/conf.d/
[root@localhost conf.d]# vim http_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 => ["192.168.1.10:9200"]
                index => "apache_access-%{+YYYY.MM.dd}"
        }
 }

        if [type] == "error" {
                elasticsearch {
                hosts => ["192.168.1.10:9200"]
                index => "apache_error-%{+YYYY.MM.dd}"
                }
        }
}
[root@localhost conf.d]#  /usr/share/logstash/bin/logstash -f http_log.conf
相關文章
相關標籤/搜索