ELK版本:7.4.0
elastic官網位址:https://www.elastic.co/cn/
elastic産品位址:https://www.elastic.co/cn/elastic-stack
yum源位址:https://mirrors.tuna.tsinghua.edu.cn/elasticstack/yum
一、ELK簡介
ELK主要由ElasticSearch、Logstash和Kibana三個開源工具組成,還有其他專門由于收集資料的輕量型資料采集器Beats。
Elasticsearch :分布式搜尋引擎。具有高可伸縮、高可靠、易管理等特點。可以用于全文檢索、結構化檢索和分析,并能将這三者結合起來。Elasticsearch 是用Java 基于 Lucene 開發,現在使用最廣的開源搜尋引擎之一,Wikipedia 、StackOverflow、Github 等都基于它來建構自己的搜尋引擎。在elasticsearch中,所有節點的資料是均等的。
Logstash :資料收集處理引擎。支援動态的從各種資料源搜集資料,并對資料進行過濾、分析、豐富、統一格式等操作,然後存儲以供後續使用。
Kibana :可視化化平台。它能夠搜尋、展示存儲在 Elasticsearch 中索引資料。使用它可以很友善的用圖表、表格、地圖展示和分析資料。
Filebeat:輕量級資料收集引擎。相對于Logstash所占用的系統資源來說,Filebeat 所占用的系統資源幾乎是微乎及微。它是基于原先 Logstash-fowarder 的源碼改造出來。換句話說:Filebeat就是新版的 Logstash-fowarder,也會是 ELK Stack 在 Agent 的第一選擇。
版本說明:
Elasticsearch、Logstash、Kibana、Filebeat安裝的版本号必須全部一緻,不然會出現kibana無法顯示web頁面。
ELK常見的幾種架構:
1 Elasticsearch + Logstash + Kibana
每台機器(用戶端)上部署Logstash,logstash收集了資料直接往es裡面寫,es分析日志,kibana查詢es的資料做展示。
這是一種最簡單的架構。這種架構雖然是官網介紹裡的方式,但是往往在生産中很少使用。因為這樣要在每台機器上都部署logstash,資源消耗比較大。
2 Elasticsearch + Logstash + filebeat + Kibana
每台機器(用戶端)上部署filebeat收集資料,filebeat往1台logstash裡面寫,logstash往es裡面寫,es分析日志,kibana查詢es的資料做展示。
與上一種架構相比,這種架構增加了一個filebeat子產品。filebeat是一個輕量的日志收集代理,優勢是消耗非常少的資源(較logstash), 是以生産中,往往會采取這種架構方式,但是這種架構有一個缺點,當logstash出現故障, 會造成日志的丢失。
3 Elasticsearch + Logstash + filebeat + redis(也可以是其他中間件,比如RabbitMQ、kafka) + Kibana
每台機器(用戶端)上部署filebeat收集資料,filebeat往消息中間件叢集寫,1台logstash消費消息中間件叢集然後往es裡面寫,es分析日志,kibana查詢es的資料做展示。
這種架構是上面那個架構的完善版,通過增加中間件,來避免資料的丢失。當Logstash出現故障,日志還是存在中間件中,當Logstash再次啟動,則會讀取中間件中積壓的日志。
二、部署
本文以es+logstash+filebeat+kafka+kibana為例

機器部署計劃
node1: es、filebeat、kafka、zk
node2: es、filebeat、kafka、zk、logstash
node3: es、filebeat、kafka、zk、kibana
1.首先安裝JDK
1.8
注意es的版本要和JDK的版本比對
2.安裝kafka+zk叢集
zk叢集安裝:https://blog.csdn.net/jy02268879/article/details/80615189
kafka叢集安裝:https://blog.csdn.net/jy02268879/article/details/80616889
注意這裡在啟動kafka前還需要修改一下配置
vi /app/kafka/kafka_2.12-2.0.0/config/server.properties
listeners=PLAINTEXT://172.20.10.8:9092
advertised.listeners=PLAINTEXT://172.20.10.8:9092
這裡的IP是本機的IP,預設為localhost,不補齊會導緻filebeat啟動時連接配接kafka會有問題:
3.安裝es
https://www.elastic.co/cn/downloads/past-releases#elasticsearch
每台機器下載下傳7.4.0并且解壓
每台機器建立資料目錄 mkdir -p /app/es/data
每台機器修改配置
cd /app/es/elasticsearch-7.4.0/config
vi elasticsearch.yml
cluster.name: my-log-es (叢集名稱)
node.name: node-1 (節點名稱)
path.data: /app/es/data (存儲資料的目錄,需要根據具體伺服器目錄配置)
network.host: 172.20.10.8 (ip位址,需要根據具體伺服器位址配置)
http.port: 9200 (端口号,預設為9200)
discovery.seed_hosts: ["172.20.10.11", "172.20.10.10"]
cluster.initial_master_nodes: ["node-1", "node-2","node-3"]
啟動
./bin/elasticsearch -d (輸入-d表示背景運作)
Linux下elasticsearch不允許root使用者運作,需要切換到其他使用者執行該指令
在浏覽器上輸入http://172.20.10.10:9200 如果顯示以下頁面則說明安裝成功
還要在浏覽器輸入http://172.20.10.8:9200/_cat/nodes?pretty
啟動報錯
ERROR: [4] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max number of threads [3795] for user [sid] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[4]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
解決方法
修改配置檔案,即可解決[4]的報錯
vi /app/es/elasticsearch-7.4.0/config/elasticsearch.yml
cluster.initial_master_nodes: ["node-1", "node-2","node-3"]
切換到root使用者,編輯limits.conf 添加類似如下内容,即可解決[1][2]的報錯
vi /etc/security/limits.conf
添加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
切換到root使用者,即可解決[3]的報錯
執行指令:
sysctl -w vm.max_map_count=262144
檢視結果:
sysctl -a|grep vm.max_map_count
顯示:
vm.max_map_count = 262144
上述方法修改之後,如果重新開機虛拟機将失效
在 /etc/sysctl.conf檔案最後添加一行,即可解決[3]的報錯
vm.max_map_count=262144
4.Logstash安裝
下載下傳https://www.elastic.co/cn/downloads/logstash 7.4.0版本
在node2安裝
在config檔案中定義一個kafka.conf檔案,定義input以及output
cd /app/logstash/logstash-7.4.0/config
vi kafka.conf
input {
kafka { #指定kafka服務
type => "app_log"
codec => "json" #通用選項,用于輸入資料的編解碼器
topics => "topic.log.es" #這裡定義的topic
decorate_events => true #此屬性會将目前topic、group、partition等資訊也帶到message中
bootstrap_servers => "172.20.10.8:9092,172.20.10.11:9092,172.20.10.10:9092" #kafka叢集IP和端口号9092
}
}
output{ #輸出插件,将事件發送到特定目标
elasticsearch { #輸出到es
hosts => ["172.20.10.8:9200","172.20.10.11:9200","172.20.10.10:9200"] #指定es服務的ip加端口
index => ["%{type}-%{+YYYY.MM.dd}"] #引用input中的type名稱,定義輸出的格式
}
}
注意這裡的type的值為app_log,在kibana才啟動後喊建立索引,能看到:
其實這個type,比如說哈,我有5套環境,每套環境有7個元件。那這裡可以分開,寫35個input。
每個的type叫環境名-元件名。且每個走不同的topic
啟動服務 ./bin/logstash -f config/kafka.conf
背景啟動 nohup ./bin/logstash -f config/kafka.conf &
報錯
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /app/logstash/logstash-7.4.0/hs_err_pid7568.log
解決方法
我是在虛拟機上運作的,虛拟機的記憶體隻有1G,改成了2G
報錯
Got response code '503' contacting Elasticsearch at URL 'http://172.20.10.8:9200/_template/logstash'
5.Kibana安裝
在node3安裝
下載下傳:https://www.elastic.co/cn/downloads/past-releases#kibana
下載下傳7.4.0版本并且解壓到/app/kibana
修改config/kibana.yml檔案
config/kibana.yml檔案加入配置使之變成中文界面
i18n.locale: "zh-CN"
啟動
nohup ./bin/kibana &
同樣也是不能以root啟動
在浏覽器輸入http://172.20.10.10:5601
啟動報錯
{"type":"log","@timestamp":"2021-08-23T12:50:39Z","tags":["reporting","warning"],"pid":23049,"message":"Reporting plugin self-check failed. Please check the Kibana Reporting settings. Error: Could not close browser client handle!"}
Could not create APM Agent configuration: Request Timeout after 30000ms
解決方法
參考:https://blog.csdn.net/qq_40907977/article/details/104499178
https://www.oschina.net/question/2312022_2314950
6.filebeat安裝
下載下傳:https://www.elastic.co/cn/downloads/past-releases#filebeat
下載下傳7.4.0.版本并且解壓
cd /app/filebeat/filebeat-7.4.0-linux-x86_64
vi filebeat.yml
加入
output.kafka:
# initial brokers for reading cluster metadata
hosts: ["172.20.10.8:9092", "172.20.10.11:9092", "172.20.10.10:9092"]
# message topic selection + partitioning
topic: 'topic.log.es'
partition.round_robin:
reachable_only: false
required_acks: 1
compression: gzip
max_message_bytes: 1000000
其中paths表示從采集什麼日志檔案
type:log表示輸入内容Log方式
啟動 nohup ./filebeat -e -c filebeat.yml &
三、kibana簡單使用、及驗證
kibana啟動起來後建立索引