溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ELKstack搭建微服務(wù)日志中心的教程

發(fā)布時間:2020-06-12 11:23:51 來源:億速云 閱讀:629 作者:Leah 欄目:系統(tǒng)運維

這篇文章給大家分享的是ELKstack搭建微服務(wù)日志中心的教程,相信大部分人都還不知道怎么部署,為了讓大家學(xué)會,故而給大家總結(jié)了以下內(nèi)容。

ELK簡介

什么是ELK?通俗來講,ELK是由Elasticsearch、Logstash、Kibana 三個開源軟件組成的一個組合體,這三個軟件當中,每個軟件用于完成不同的功能,ELK又稱ELKstack,官網(wǎng) https://www.elastic.co/ , ELK主要優(yōu)點有如下幾個:
1、處理方式靈活:elasticsearch是實時全文索引,具有強大的搜索功能
2、配置相對簡單:elasticsearch全部使用JSON接口,logstash使用模塊配置,kibana的配置文件部分更簡單
3、檢索性能高:基于優(yōu)秀的設(shè)計,雖然每次查詢都是實時,但是也可以達到百億級數(shù)據(jù)的查詢秒級響應(yīng)
4、集群線性擴展:elasticsearch和logstash都可以靈活線性擴展
5、前端操作絢麗:kibana的前端設(shè)計比較絢麗,而且操作簡單

Elasticsearch

elasticsearch是一個高度可擴展全文搜索和分析引擎,基于Apache Lucene 構(gòu)建,能對大容量的數(shù)據(jù)進行接近實時的存儲、搜索和分析操作,可以處理大規(guī)模日志數(shù)據(jù),比如Nginx、Tomcat、系統(tǒng)日志等功能。

Logstash

數(shù)據(jù)收集引擎。它支持動態(tài)的從各種數(shù)據(jù)源搜集數(shù)據(jù),并對數(shù)據(jù)進行過濾、分析、豐富、統(tǒng)一格式等操作,然后存儲到用戶指定的位置;支持普通log、自定義json格式的日志解析。

Kibana

數(shù)據(jù)分析和可視化平臺。通常與 Elasticsearch 配合使用,對其中數(shù)據(jù)進行搜索、分析和以統(tǒng)計圖表的方式展示。

filebeat

首先 filebeat 不屬于 ELK中的組件是為ELK服務(wù)的可選組件之一。因為Logstash 有一個致命的問題是它的性能以及資源消耗(默認的堆大小是 1GB)。盡管它的性能在近幾年已經(jīng)有很大提升,與它的替代者們相比還是要慢很多的。作為 Beats 家族的一員,F(xiàn)ilebeat 是一個輕量級的日志傳輸工具,它的存在正彌補了 Logstash 的缺點:Filebeat 作為一個輕量級的日志傳輸工具可以將日志推送到中心 Logstash。Filebeat 只是一個二進制文件沒有任何依賴。它占用資源極少,盡管它還十分年輕,正式因為它簡單,所以幾乎沒有什么可以出錯的地方,所以它的可靠性還是很高的。它也為我們提供了很多可以調(diào)節(jié)的點,例如:它以何種方式搜索新的文件,以及當文件有一段時間沒有發(fā)生變化時,何時選擇關(guān)閉文件句柄。

開始部署

系統(tǒng)環(huán)境檢查

1、因為elasticsearch服務(wù)運行需要java環(huán)境,所有首先要檢查服務(wù)器中的 java 環(huán)境。推薦jdk版本至少為1.8以上

[root@iZbp136dr1iwajle0r9j83Z soft]# java -version
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)

2、創(chuàng)建用戶,因為elasticsearch是不能再root賬戶下運行所以重新創(chuàng)建一個用戶

[root@iZbp136dr1iwajle0r9j83Z ~]# useradd elk

3、創(chuàng)建文件夾

[root@iZbp136dr1iwajle0r9j83Z ~]# su - elk  # 切換到 elk 用戶
[elk@iZbp136dr1iwajle0r9j83Z ~]$ mkdir soft  # 用來存儲原始壓縮包文件
[elk@iZbp136dr1iwajle0r9j83Z ~]$ mkdir applications  #  用來存放部署的文件
下載 ELK 文件

下載過程就自行到相關(guān)的官網(wǎng)上下載把,注意下載版本最好是能夠?qū)?yīng)起來的否則會出現(xiàn)一些莫名其妙的問題。

[root@iZbp136dr1iwajle0r9j83Z soft]# ll -l 
total 708484
-rw-rw-r-- 1 app app 290094012 Jan  2 14:31 elasticsearch-7.5.1-linux-x86_64.tar.gz
-rw-rw-r-- 1 app app  24086235 Jan  2 14:26 filebeat-7.5.1-x86_64.rpm
-rw-rw-r-- 1 app app 238481011 Jan  2 13:28 kibana-7.5.1-linux-x86_64.tar.gz
-rw-rw-r-- 1 app app 172809530 Jan  2 13:27 logstash-7.5.1.zip

這是我下載的文件,從文件屬性上還是屬于 app 用戶的。為了方便統(tǒng)一管理現(xiàn)在將這些文件全部轉(zhuǎn)給 elk 用戶

[root@iZbp136dr1iwajle0r9j83Z soft]# chown -R elk:elk *
[root@iZbp136dr1iwajle0r9j83Z soft]# ll -l 
total 708484
-rw-rw-r-- 1 elk elk 290094012 Jan  2 14:31 elasticsearch-7.5.1-linux-x86_64.tar.gz
-rw-rw-r-- 1 elk elk  24086235 Jan  2 14:26 filebeat-7.5.1-x86_64.rpm
-rw-rw-r-- 1 elk elk 238481011 Jan  2 13:28 kibana-7.5.1-linux-x86_64.tar.gz
-rw-rw-r-- 1 elk elk 172809530 Jan  2 13:27 logstash-7.5.1.zip

將文件解壓到 applications 文件夾中

# 處理 elasticsearch-7.5.1
[elk@iZbp136dr1iwajle0r9j83Z soft]$ tar -zxvf elasticsearch-7.5.1-linux-x86_64.tar.gz
[elk@iZbp136dr1iwajle0r9j83Z soft]$ mv elasticsearch-7.5.1 ../applications/
# 處理 logstatsh-7.5.1.zip
[elk@iZbp136dr1iwajle0r9j83Z soft]$ unzip logstatsh-7.5.1.zip
[elk@iZbp136dr1iwajle0r9j83Z soft]$ mv logstash-7.5.1 ../applications/
# 處理 kibana-7.5.1
[elk@iZbp136dr1iwajle0r9j83Z soft]$ tar -zxvf kibana-7.5.1-linux-x86_64.tar.gz
[elk@iZbp136dr1iwajle0r9j83Z soft]$ mv kibana-7.5.1-linux-x86_64 ../applications/
部署 elasticsearch

修改elasticsearch.yml文件

[elk@iZbp136dr1iwajle0r9j83Z config]$ vim elasticsearch.yml
#集群名稱
cluster.name: els
#節(jié)點名稱
node.name: els-1
#數(shù)據(jù)存放路徑
path.data: /data/els/data
#日志存放路徑
path.logs: /data/logs/els/log
#綁定IP地址
network.host: 172.16.240.19
#端口號
http.port: 7008
discovery.seed_hosts: ["172.16.240.19"]
cluster.initial_master_nodes: ["els-1"]
# 允許跨域訪問(kibana獲取數(shù)據(jù)時需要開放)
http.cors.enabled: true
http.cors.allow-origin: '*'

創(chuàng)建數(shù)據(jù)目錄

mkdir /data/els
mkdir /data/logs/els

啟動 elasticsearch

        [elk@iZbp136dr1iwajle0r9j83Z soft]$ cd /home/elk/applications/elasticsearch-7.5.1/bin/
        [elk@iZbp136dr1iwajle0r9j83Z soft]$ ./elasticsearch

訪問elasticsearch_ip:port 看是否啟動正常
ELKstack搭建微服務(wù)日志中心的教程

部署 kibana

編輯配置文件

cd /home/elk/applications/kibana-7.5.1-linux-x86_64/config
# 修改配置
server.port: 7011
server.host: "0.0.0.0"
server.name: "kibana-server"
elasticsearch.url: "http://172.16.240.19:7008"
kibana.index: ".kibana"
i18n.locale: "zh-CN"

啟動 kibana

cd /home/elk/applications/kibana-7.5.1-linux-x86_64/bin
./kibana

訪問kibana_ip:port
ELKstack搭建微服務(wù)日志中心的教程

部署 logstatsh

由于后期日志數(shù)據(jù)是通過 filebeat 收集上來的,因此針對 filebeat 單獨創(chuàng)建一個配置文件來進行處理

cd /home/elk/logstash-7.5.1/config
vim beat.conf
input {
  # 接收filebeat讀取的數(shù)據(jù)
  beats {
    port => 7110
    codec => "json"
  }
}

output {
  # 輸出到es
  elasticsearch {
    hosts => ["172.16.240.19:7008"]
    index => "cloud"
    document_type => "log"
    manage_template => false
  }
}

測試配置文件語法是否正確

cd /home/elk/applications/logstash-7.5.1/bin/
./logstash -f /home/elk/applications/logstash-7.5.1/config/beat.conf -t
Thread.exclusive is deprecated, use Thread::Mutex
Sending Logstash logs to /home/elk/applications/logstash-7.5.1/logs which is now configured via log4j2.properties
[2020-01-02T16:12:12,309][INFO ][logstash.setting.writabledirectory] Creating directory {:setting=>"path.queue", :path=>"/home/elk/applications/logstash-7.5.1/data/queue"}
[2020-01-02T16:12:12,461][INFO ][logstash.setting.writabledirectory] Creating directory {:setting=>"path.dead_letter_queue", :path=>"/home/elk/applications/logstash-7.5.1/data/dead_letter_queue"}
[2020-01-02T16:12:12,890][WARN ][logstash.config.source.multilocal] Ignoring the 'pipelines.yml' file because modules or command line options are specified
[2020-01-02T16:12:14,200][INFO ][org.reflections.Reflections] Reflections took 41 ms to scan 1 urls, producing 20 keys and 40 values 
[2020-01-02T16:12:14,673][WARN ][logstash.outputs.elasticsearch] You are using a deprecated config setting "document_type" set in elasticsearch. Deprecated settings will continue to work, but are scheduled for removal from logstash in the future. Document types are being deprecated in Elasticsearch 6.0, and removed entirely in 7.0. You should avoid this feature If you have any questions about this, please visit the #logstash channel on freenode irc. {:name=>"document_type", :plugin=><LogStash::Outputs::ElasticSearch index=>"cloud", manage_template=>false, id=>"c864643340e20cc0970e7438081bbe9f8d9e69638b91d662640155c5e847f531", hosts=>[//172.16.240.19:7008], document_type=>"log", enable_metric=>true, codec=><LogStash::Codecs::Plain id=>"plain_17e73b8f-bd1b-45ff-84ab-2a41f766a1a0", enable_metric=>true, charset=>"UTF-8">, workers=>1, template_name=>"logstash", template_overwrite=>false, doc_as_upsert=>false, script_type=>"inline", script_lang=>"painless", script_var_name=>"event", scripted_upsert=>false, retry_initial_interval=>2, retry_max_interval=>64, retry_on_conflict=>1, ilm_enabled=>"auto", ilm_rollover_alias=>"logstash", ilm_pattern=>"{now/d}-000001", ilm_policy=>"logstash-policy", action=>"index", ssl_certificate_verification=>true, sniffing=>false, sniffing_delay=>5, timeout=>60, pool_max=>1000, pool_max_per_route=>100, resurrect_delay=>5, validate_after_inactivity=>10000, http_compression=>false>}
Configuration OK
[2020-01-02T16:12:14,709][INFO ][logstash.runner          ] Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash

Configuration OK 則表示配置文件是成功的,接下來啟動 logstash 并加載 beat.conf

./logstash -f /home/elk/applications/logstash-7.5.1/config/beat.conf
部署 filebeat

在需要收集日志的主機上安裝 filebeat 組件

[root@iZbp14b5r2lytw5nc5z3w2Z filebeat]# pwd
/usr/share/filebeat
[root@iZbp14b5r2lytw5nc5z3w2Z filebeat]# ll -l
total 23524
-rw-r--r-- 1 root root 24086235 Jan  2 16:19 filebeat-7.5.1-x86_64.rpm
[root@iZbp14b5r2lytw5nc5z3w2Z filebeat]# sudo rpm -vi filebeat-7.5.1-x86_64.rpm 
warning: filebeat-7.5.1-x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID d88e42b4: NOKEY
Preparing packages...
filebeat-7.5.1-1.x86_64

編輯配置修改 /etc/filebeat/filebeat.yml 以設(shè)置連接信息:

filebeat.inputs:

# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.

- type: log
  enabled: true
  tags: ["clo***dmin"]
  paths:
    - /logs/S*******/clou***min/**/*.log  

  # 將非時間戳開頭的日志信息合并
  multiline.pattern: '^\[[0-9]{4}-[0-9]{2}-[0-9]{2}'
  multiline.negate: true
  multiline.match: after

    #----------------------------- Logstash output --------------------------------
output.logstash:
  # The Logstash hosts
  hosts: ["172.16.240.19:7112"]
  index: joy######loud

啟動 filebeat 服務(wù)

cd /usr/share/filebeat/bin
./filebeat -e -c /etc/filebeat/filebeat.yml 

配置 kibana

ELKstack搭建微服務(wù)日志中心的教程

配置完成后查看是否有日志收集上來
ELKstack搭建微服務(wù)日志中心的教程

可以看到已經(jīng)有數(shù)據(jù)上來了。然后再跟看tags值在檢索條件中過濾就能過濾出不同服務(wù)輸出的日志信息了。

上文描述的就是ELKstack搭建微服務(wù)日志中心的教程,具體使用情況還需要大家自己動手實驗使用過才能領(lǐng)會。如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI