ELK官網(wǎng):https://www.elastic.co/cn/
中文指南:https://www.gitbook.com/book/chenryn/elk-stack-guide-cn/details
ELK-Docker:
https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html
1. 什么是ELK
ELK 是 Elasticsearch滔悉、Logstrash 和 Kibana 的縮寫,是一套成熟的日志管理系統(tǒng)单绑,最近新增Beats回官,它是一個(gè)輕量級(jí)的日志收集處理工具(Agent),Beats 占用資源少询张,適合于在各個(gè)服務(wù)器上搜集日志后傳輸給 Logstash孙乖,官方也推薦此工具。ELK Stack 已經(jīng)成為目前最流行的集中式日志解決管理方案。
- Elasticsearch
分布式搜索和分析引擎唯袄,它的特點(diǎn)有:分布式弯屈,零配置,自動(dòng)發(fā)現(xiàn)恋拷,索引自動(dòng)分片资厉,索引副本機(jī)制,restful風(fēng)格接口蔬顾,多數(shù)據(jù)源宴偿,自動(dòng)搜索負(fù)載【骰恚基于 Apache Lucene 構(gòu)建窄刘,能對(duì)大容量的數(shù)據(jù)進(jìn)行接近實(shí)時(shí)的存儲(chǔ)、搜索和分析操作舷胜。通常被用作某些應(yīng)用的基礎(chǔ)搜索引擎娩践,使其具有復(fù)雜的搜索功能; - Logstrash
數(shù)據(jù)收集引擎烹骨。它支持動(dòng)態(tài)的從各種數(shù)據(jù)源搜集數(shù)據(jù)翻伺,并對(duì)數(shù)據(jù)進(jìn)行過(guò)濾、分析沮焕、豐富吨岭、統(tǒng)一格式等操作,然后存儲(chǔ)到用戶指定的位置峦树; - Kibana
數(shù)據(jù)分析和可視化平臺(tái)辣辫。通常與 Elasticsearch 配合使用,對(duì)其中數(shù)據(jù)進(jìn)行搜索魁巩、分析和以統(tǒng)計(jì)圖表的方式展示 - Beats
Beats 家族有 6 個(gè)成員络它,早期的 ELK 架構(gòu)中使用 Logstash 收集、解析日志歪赢,但是 Logstash 對(duì)內(nèi)存化戳、cpu、io 等資源消耗比較高埋凯。相比 Logstash点楼,Beats 所占系統(tǒng)的 CPU 和內(nèi)存幾乎可以忽略不計(jì)。
ELK Stack (5.0版本之后)--> Elastic Stack == (ELK Stack + Beats)白对。
目前 Beats 包含六種工具:
Packetbeat: 網(wǎng)絡(luò)數(shù)據(jù)(收集網(wǎng)絡(luò)流量數(shù)據(jù))
Metricbeat: 指標(biāo)(收集系統(tǒng)掠廓、進(jìn)程和文件系統(tǒng)級(jí)別的 CPU 和內(nèi)存使用情況等數(shù)據(jù))
Filebeat: 日志文件(收集文件數(shù)據(jù))。一個(gè)輕量級(jí)開(kāi)源日志文件數(shù)據(jù)搜集器甩恼,基于 Logstash-Forwarder 源代碼開(kāi)發(fā)是對(duì)它的替代蟀瞧。在需要采集日志數(shù)據(jù)的服務(wù)上安裝 Filebeat沉颂,并指定日志目錄或日志文件后,F(xiàn)ilebeat就能讀取日志文件數(shù)據(jù)悦污,迅速發(fā)送到 Logstash進(jìn)行解析铸屉,或直接發(fā)送到 Elasticsearch進(jìn)行集中式存儲(chǔ)和分析。
Winlogbeat: windows 事件日志(收集 Windows 事件日志數(shù)據(jù))
Auditbeat:審計(jì)數(shù)據(jù)(收集審計(jì)日志)
Heartbeat:運(yùn)行時(shí)間監(jiān)控(收集系統(tǒng)運(yùn)行時(shí)的數(shù)據(jù))
2. 基于Filebeat分布式集群架構(gòu)部署方案
具體基于Filebeat的ELK分布式集中日志解決方案架構(gòu)如圖所示:
ELK 分布式集群部署
日志采集流程如圖:
日志采集流程
logstash是java應(yīng)用切端,解析日志是非的消耗cpu和內(nèi)存彻坛,logstash安裝在應(yīng)用部署的機(jī)器上顯得非常的影響應(yīng)用的性能。最常見(jiàn)的做法是用filebeat部署在應(yīng)用的機(jī)器上踏枣,logstash單獨(dú)部署昌屉,然后由filebeat將日志輸出給logstash解析,解析完由logstash再傳給elasticsearch
3. 部署配置
準(zhǔn)備條件:兩臺(tái)云主機(jī)
主機(jī)1:模擬我們的生產(chǎn)環(huán)境茵瀑,只部署filebeat
主機(jī)2:部署logstash间驮,es集群(這里只部署一臺(tái)),kibana
- 主機(jī)1配置
1)目錄結(jié)構(gòu)
[root@localhost elk-filebeat]# ls -l
total 16
-rw-r--r-- 1 root root 522 Feb 6 10:38 docker-compose.yml
-rw-r--r-- 1 root root 667 Feb 6 10:40 filebeat.yml
-rwxr-xr-x 1 root root 777 Feb 6 10:39 init.sh
- docker-compose.yml
version: "3"
services:
filebeat:
# 容器名稱
container_name: filebeat
# 主機(jī)名稱
hostname: filebeat
# 鏡像
image: docker.elastic.co/beats/filebeat:7.1.1
# 重啟機(jī)制
restart: always
# 持久化掛載
volumes:
- ./filebeat/conf/filebeat.yml:/usr/share/filebeat/filebeat.yml
# 映射到容器中[作為數(shù)據(jù)源]
- /data/shigongbao/logs/django:/data/logs
- ./filebeat/logs:/usr/share/filebeat/logs
- ./filebeat/data:/usr/share/filebeat/data
- filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
# 當(dāng)前目錄下的所有.log文件
- /data/logs/*.log
multiline.pattern: ^\[
multiline.negate: true
multiline.match: after
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 1
setup.dashboards.enabled: false
setup.kibana:
host: "http://主機(jī)2的ip:5601"
# 不直接傳輸至ES
#output.elasticsearch:
# hosts: ["http://es-master:9200"]
# index: "filebeat-%{[beat.version]}-%{+yyyy.MM.dd}"
output.logstash:
hosts: ["主機(jī)2的ip:5044"]
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
4)部署腳本init.sh
#! /bin/bash
if [ ! -d "./filebeat/" ]; then
mkdir -p ./filebeat/conf ./filebeat/logs ./filebeat/data
fi
if [ -d "./filebeat/" ]; then
chmod 777 ./filebeat/data/ ./filebeat/logs/
fi
if [ -f "./filebeat.yml" ]; then
cp ./filebeat.yml ./filebeat/conf
fi
docker-compose up --build -d
- 主機(jī)2配置
1)目錄結(jié)構(gòu)
/data/elk-master
[root@xiabing elk-master]# ls -l
total 44
-rw-r--r-- 1 root root 2358 Feb 6 10:53 docker-compose.yml
-rw-r--r-- 1 root root 748 Feb 6 10:49 es-master.yml
-rw-r--r-- 1 root root 667 Feb 6 10:35 filebeat.yml
-rwxr-xr-x 1 root root 1943 Feb 5 15:17 init.sh
-rw-r--r-- 1 root root 152 Feb 6 10:42 kibana.yml
-rw-r--r-- 1 root root 444 Feb 6 10:36 logstash-filebeat.conf
-rw-r--r-- 1 root root 171 Feb 6 10:49 logstash.yml
- docker-compose.yml
version: "3"
services:
es-master:
container_name: es-master
hostname: es-master
image: elasticsearch:7.1.1
restart: always
ports:
- 9200:9200
- 9300:9300
volumes:
- /data/elk-master/elasticsearch/master/conf/es-master.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- /data/elk-master/elasticsearch/master/data:/usr/share/elasticsearch/data
- /data/elk-master/elasticsearch/master/logs:/usr/share/elasticsearch/logs
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
es-head:
container_name: es-head
image: mobz/elasticsearch-head:5
restart: always
ports:
- 9100:9100
depends_on:
- es-master
kibana:
container_name: kibana
hostname: kibana
image: kibana:7.1.1
restart: always
ports:
- 5601:5601
volumes:
- /data/elk-master/kibana/conf/kibana.yml:/usr/share/kibana/config/kibana.yml
environment:
- elasticsearch.hosts=http://本機(jī)外網(wǎng)ip:9200
depends_on:
- es-master
filebeat:
# 容器名稱
container_name: filebeat
# 主機(jī)名稱
hostname: filebeat
# 鏡像
image: docker.elastic.co/beats/filebeat:7.1.1
# 重啟機(jī)制
restart: always
# 持久化掛載
volumes:
- /data/elk-master/filebeat/conf/filebeat.yml:/usr/share/filebeat/filebeat.yml
# 映射到容器中[作為數(shù)據(jù)源]
- /data/shigongbao/logs/django:/data/logs
- /data/elk-master/filebeat/logs:/usr/share/filebeat/logs
- /data/elk-master/filebeat/data:/usr/share/filebeat/data
# 將指定容器連接到當(dāng)前連接马昨,可以設(shè)置別名蜻牢,避免ip方式導(dǎo)致的容器重啟動(dòng)態(tài)改變的無(wú)法連接情況
links:
- logstash
# 依賴服務(wù)[可無(wú)]
depends_on:
- es-master
logstash:
container_name: logstash
hostname: logstash
image: logstash:7.1.1
command: logstash -f ./conf/logstash-filebeat.conf
restart: always
volumes:
# 映射到容器中
- /data/elk-master/logstash/conf/logstash-filebeat.conf:/usr/share/logstash/conf/logstash-filebeat.conf
environment:
- elasticsearch.hosts=http://本機(jī)外網(wǎng)ip:9200
# 解決logstash監(jiān)控連接報(bào)錯(cuò)
- xpack.monitoring.elasticsearch.hosts=http://本機(jī)外網(wǎng)ip:9200
ports:
- 5044:5044
depends_on:
- es-master
3)es-master.yml
# 集群名稱
cluster.name: es-cluster
# 節(jié)點(diǎn)名稱
node.name: es-node1
# 是否可以成為master節(jié)點(diǎn)
node.master: true
# 是否允許該節(jié)點(diǎn)存儲(chǔ)數(shù)據(jù),默認(rèn)開(kāi)啟
node.data: true
# 網(wǎng)絡(luò)綁定
network.host: 0.0.0.0
network.publish_host: 本機(jī)外網(wǎng)ip
# 設(shè)置對(duì)外服務(wù)的http端口
http.port: 9200
# 設(shè)置節(jié)點(diǎn)間交互的tcp端口
transport.port: 9300
# 集群發(fā)現(xiàn)
discovery.seed_hosts:
- 本機(jī)外網(wǎng)ip
# 手動(dòng)指定可以成為 mater 的所有節(jié)點(diǎn)的 name 或者 ip,這些配置將會(huì)在第一次選舉中進(jìn)行計(jì)算
cluster.initial_master_nodes:
- es-node1
# 支持跨域訪問(wèn)
http.cors.enabled: true
http.cors.allow-origin: "*"
# 安全認(rèn)證
xpack.security.enabled: false
#http.cors.allow-headers: "Authorization"
4)filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
# 當(dāng)前目錄下的所有.log文件
- /data/logs/*.log
multiline.pattern: ^\[
multiline.negate: true
multiline.match: after
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 1
setup.dashboards.enabled: false
setup.kibana:
host: "http://本機(jī)外網(wǎng)ip:5601"
# 不直接傳輸至ES
#output.elasticsearch:
# hosts: ["http://es-master:9200"]
# index: "filebeat-%{[beat.version]}-%{+yyyy.MM.dd}"
output.logstash:
hosts: ["本機(jī)外網(wǎng)ip:5044"]
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
5)kibana.yml
# 服務(wù)端口
server.port: 5601
# 服務(wù)IP
server.host: "0.0.0.0"
# ES
elasticsearch.hosts: ["http://本機(jī)外網(wǎng)ip:9200"]
# 漢化
i18n.locale: "zh-CN"
6)logstash.yml
# X-Pack Monitoring
xpack.monitoring.enabled: true
xpack.monitoring.elasticsearch.hosts: ["http://本機(jī)外網(wǎng)ip:9200"]
# X-Pack Management
xpack.management.enabled: false
7)logstash-filebeat.conf
input {
# 來(lái)源beats
beats {
# 端口
port => "5044"
}
}
# 分析偏陪、過(guò)濾插件,可以多個(gè)
filter {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}"}
}
geoip {
source => "clientip"
}
}
output {
# 選擇elasticsearch
elasticsearch {
hosts => ["http://本機(jī)外網(wǎng)ip:9200"]
index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
}
}
8)部署腳本init.sh
#! /bin/bash
# 創(chuàng)建目錄
if [ ! -d "./elasticsearch/" ]; then
mkdir -p ./elasticsearch/master/conf ./elasticsearch/master/data ./elasticsearch/master/logs
fi
if [ ! -d "./kibana/" ]; then
mkdir -p ./kibana/conf ./kibana/logs
fi
if [ ! -d "./logstash/" ]; then
mkdir -p ./logstash/conf ./logstash/logs
fi
if [ ! -d "./filebeat/" ]; then
mkdir -p ./filebeat/conf ./filebeat/logs ./filebeat/data
fi
# 目錄授權(quán)(data/logs 都要授讀/寫權(quán)限)
if [ -d "./elasticsearch/" ]; then
chmod 777 ./elasticsearch/master/data/ ./elasticsearch/master/logs/
fi
if [ -d "./filebeat/" ]; then
chmod 777 ./filebeat/data/ ./filebeat/logs/
fi
# 移動(dòng)配置文件
if [ -f "./es-master.yml" ]; then
cp ./es-master.yml ./elasticsearch/master/conf
fi
if [ -f "./kibana.yml" ]; then
cp ./kibana.yml ./kibana/conf
fi
if [ -f "./logstash-filebeat.conf" ]; then
cp ./logstash-filebeat.conf ./logstash/conf
fi
if [ -f "./logstash.yml" ]; then
cp ./logstash.yml ./logstash/conf
fi
if [ -f "./filebeat.yml" ]; then
cp ./filebeat.yml ./filebeat/conf
fi
# 部署項(xiàng)目
docker-compose up --build -d
4. 常見(jiàn)問(wèn)題
常見(jiàn)排查命令:
- docker logs -f container_id/name
常見(jiàn)錯(cuò)誤:
- max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144](elasticsearch用戶擁有的內(nèi)存權(quán)限太小煮嫌,至少需要262144)
解決方法:
sysctl -w vm.max_map_count=262144
sysctl -p
- es-master和es-slave1 es-slave2容器反復(fù)重啟
原因可能是配置錯(cuò)誤笛谦,我遇到的日志路徑不存在會(huì)導(dǎo)致es容器反復(fù)重啟,filebeat映射的路徑一定要存在 - 無(wú)法訪問(wèn)es-master 9200端口
原因可能是filebeat中的日志路徑內(nèi)無(wú)日志
5. 結(jié)果
- 查看es-head 集群狀態(tài)
http://x.x.x.x:9100
es-head
- 登錄kibana查看日志
http://x.x.x.x:5601
kibana