本篇文章繼續(xù)給大家介紹ELFK日志分析,詳細請見下面目錄。
目錄
filebeat采集nginx日志
filebeat模塊使用
一、Nginx模塊
二、tomcat模塊
三、filebeat寫數(shù)據(jù)到ES集群自定義索引
四、filebeat自定義字段之nginx寫入ES
五、filebeat自定義字段之tomcat寫入ES
六、indices模塊實現(xiàn)多個input寫入不同的ES索引
企業(yè)實戰(zhàn)filebeat寫入ES
一、自定義Nginx日志格式并寫入ES
二、自定義tomcat日志格式并寫入ES
EFK架構(gòu)轉(zhuǎn)ELFK架構(gòu)
文章總結(jié)
實戰(zhàn)練習(xí)(filebeat+logstash+pipeline+ES+kibana)
一、畫圖,展示項目流程
二、項目流程及維護事宜
三、filebeat采集30w記錄的nginx日志和不同格式的商品信息,用兩個配置文件實現(xiàn),一個業(yè)務(wù)一個配置文件
四、logstash寫兩個配置文件,監(jiān)聽兩個端口
五、編寫pipeline配置文件,運行l(wèi)ogstash和filebeat
六、在kibana中分別制作兩個的業(yè)務(wù)的可視化
filebeat采集nginx日志
1、安裝nginx
[root@ELK101 ~]# curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@ELK101 ~]# yum -y install nginx
[root@ELK101 ~]# systemctl enable --now nginx
2、訪問nginx,收集一些日志數(shù)據(jù)
http://10.0.0.101/
3、配置filebeat采集nginx日志并寫入ES集群
[root@ELK101 ~]# cat config/02-nginx-to-es.yaml
filebeat.inputs:
- type: log
paths: ["/var/log/nginx/access.log*"]
output.elasticsearch:
# hosts: ["http://10.0.0.101:19200","http://10.0.0.102:19200","http://10.0.0.103:19200"]
hosts:
- "http://10.0.0.101:19200"
- "http://10.0.0.102:19200"
- "http://10.0.0.103:19200"
# output.console:
# pretty: true
4、運行filebeat,通過kibana查看數(shù)據(jù)
[root@ELK101 ~]# filebeat -e -c config/02-nginx-to-es.yaml
[root@ELK101 ~]# cat /var/log/nginx/access.log |wc -l
9
filebeat模塊使用
一、Nginx模塊
1、啟用filebeat的nginx模塊
[root@ELK101 ~]# filebeat modules enable nginx
Enabled nginx
2、查看啟用的模塊
[root@ELK101 ~]# filebeat modules list|head -8
Enabled:
nginx
Disabled:
activemq
apache
auditd
aws
3、編輯filebeat配置文件
[root@ELK101 ~]# cat config/03-nginx-module-es.yaml
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
output.elasticsearch:
# hosts: ["http://10.0.0.101:19200","http://10.0.0.102:19200","http://10.0.0.103:19200"]
hosts:
- "http://10.0.0.101:19200"
- "http://10.0.0.102:19200"
- "http://10.0.0.103:19200"
# output.console:
# pretty: true
4、啟用實例,查看kibana中的索引是否解析出了字段
[root@ELK101 ~]# rm -rf /var/lib/filebeat/*
[root@ELK101 ~]# filebeat -e -c config/03-nginx-module-es.yaml
注意,這里模塊解析出來的@timestamp與logstash解析出來的有區(qū)別,這里解析的是日志中的時間,不是日志寫入時間
可以發(fā)現(xiàn)字段都被拆分解析出來了?
5、使用kibana創(chuàng)建地圖
可以根據(jù)字段添加地圖,但是由于我是本機IP訪問的nginx,是私網(wǎng),所以沒有在地圖上顯示
二、tomcat模塊
使用filebeat模塊采集tomcat日志,這次我們使用二進制部署的filebeat,二進制部署的軟件包,可以將它安裝在任意的位置
1、下載filebeat二進制軟件包,解壓軟件包
[root@ELK101 ~]# wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.5-linux-x86_64.tar.gz
[root@ELK101 ~]# tar xf filebeat-7.17.5-linux-x86_64.tar.gz -C /koten/softwares/
2、安裝tomcat、解壓軟件包并啟動
[root@ELK101 ~]# wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.75/bin/apache-tomcat-9.0.75.tar.gz --no-check-certificate
[root@ELK101 ~]# tar xf apache-tomcat-9.0.75.tar.gz -C /koten/softwares/
[root@ELK101 ~]# /koten/softwares/apache-tomcat-9.0.75/bin/startup.sh
3、訪問tomcat獲取日志數(shù)據(jù)
http://10.0.0.101:8080/
4、啟用filebeat的tomcat模塊
[root@ELK101 apache-tomcat-9.0.75]# cd /koten/softwares/filebeat-7.17.5-linux-x86_64/
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat modules enable tomcat
Enabled tomcat
5、配置tomcat模塊的配置文件
[root@ELK101 filebeat-7.17.5-linux-x86_64]# egrep -v '^*#|^$' modules.d/tomcat.yml
- module: tomcat
log:
enabled: true
var.input: file
var.paths:
- /koten/softwares/apache-tomcat-9.0.75/logs/*.txt #指定apache的日志
6、編輯filebeat配置文件并啟動filebeat
[root@ELK101 filebeat-7.17.5-linux-x86_64]# mkdir config
[root@ELK101 filebeat-7.17.5-linux-x86_64]# cat config/01-modules-to-es.yaml
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: true
output.elasticsearch:
hosts:
- "http://10.0.0.101:19200"
- "http://10.0.0.102:19200"
- "http://10.0.0.103:19200"
[root@ELK101 filebeat-7.17.5-linux-x86_64]# rm -rf data/ #刪除數(shù)據(jù)記錄
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/01-modules-to-es.yaml
7、查看kibana,成功寫入了索引?
三、filebeat寫數(shù)據(jù)到ES集群自定義索引
雖然寫入了索引,但是我們需要自定義索引的名稱及其他內(nèi)容,可以在filebeat中自定義索引模板,還是用我們二進制軟件包解壓的filebeat。
注意,自定義模板的時候不要有比這個模板匹配范圍更廣的模板,否則會出問題。
1、編輯filebeat配置文件
[root@ELK101 filebeat-7.17.5-linux-x86_64]# cat config/02-modules-to-es_index.yaml
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: true
output.elasticsearch:
hosts:
- "http://10.0.0.101:19200"
- "http://10.0.0.102:19200"
- "http://10.0.0.103:19200"
# 配置索引的名稱
index: "koten-filebeat-tomcat-access-%{+yyyy.MM.dd}"
# 禁用索引的生命周期管理
setup.ilm.enabled: false #為true,我們自定義的將全部失效,還會寫入之前隨機的索引的模板,將其關(guān)閉我們的索引才可以自定義,若關(guān)閉了它沒有自定義索引,也會創(chuàng)建成功索引,但是創(chuàng)建的索引就沒有后面的計數(shù)了,而且會提示生命周期錯誤。
# 設(shè)置索引模板名稱
setup.template.name: "koten-filebeat-tomcat"
# 設(shè)置索引的匹配模式
setup.template.pattern: "koten-filebeat-tomcat*"
# 是否覆蓋現(xiàn)有的索引模板,若為true,不管索引模板是否一致都會覆蓋,若有多個filebeat實例時,建議關(guān)閉,會讓ES壓力過大
setup.template.overwrite: false
# 配置索引的設(shè)置
setup.template.settings:
# 設(shè)置分片數(shù)量
index.number_of_shards: 5
# 設(shè)置分片副本數(shù)量。
index.number_of_replicas: 0
2、執(zhí)行filebeat,在kibana中查看模板和索引信息
[root@ELK101 filebeat-7.17.5-linux-x86_64]# rm -rf data/
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/02-modules-to-es_index.yaml
四、filebeat自定義字段之nginx寫入ES
將nginx日志使用filebeat寫入ES集群,要求指定索引名稱為koten-filebeat-nginx-access-%{+yyyy.MM.dd},分片數(shù)為3,副本分片數(shù)為0
1、編寫filebeat配置文件
[root@ELK101 filebeat-7.17.5-linux-x86_64]# cat config/03-nginx-to-es_index.yaml
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: true
output.elasticsearch:
hosts:
- "http://10.0.0.101:19200"
- "http://10.0.0.102:19200"
- "http://10.0.0.103:19200"
# 配置索引的名稱
index: "koten-filebeat-nginx-access-%{+yyyy.MM.dd}"
# 禁用索引的生命周期管理
setup.ilm.enabled: false
# 設(shè)置索引模板名稱
setup.template.name: "koten-filebeat-nginx"
# 設(shè)置索引的匹配模式
setup.template.pattern: "koten-filebeat-nginx*"
# 是否覆蓋現(xiàn)有的索引模板,若有多個filebeat實例時,建議關(guān)閉,會讓ES壓力過大
setup.template.overwrite: true
# 配置索引的設(shè)置
setup.template.settings:
# 設(shè)置分片數(shù)量
index.number_of_shards: 3
# 設(shè)置分片副本數(shù)量。
index.number_of_replicas: 0
2、設(shè)置只啟用nginx模塊,并修改nginx模塊配置信息(也可以不設(shè)置只啟用nginx模塊,可以在filebeat配置文件中固定使用哪個模塊)
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat modules enable nginx
Enabled nginx
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat modules list |head -8
Enabled:
nginx
Disabled:
activemq
apache
auditd
aws
[root@ELK101 filebeat-7.17.5-linux-x86_64]# egrep -v '^*#|^$' modules.d/nginx.yml
- module: nginx
access:
enabled: true
var.paths: ["/var/log/nginx/access.log*"]
error:
enabled: true
ingress_controller:
enabled: false
3、運行filebeat并在kibana中查看索引信息
[root@ELK101 filebeat-7.17.5-linux-x86_64]# rm -rf data/
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/03-nginx-to-es_index.yaml
五、filebeat自定義字段之tomcat寫入ES
將tomcat日志使用filebeat寫入ES集群,要求指定索引名稱為koten-filebeat-tomcat-access-%{+yyyy.MM.dd},分片數(shù)為5,副本分片數(shù)為1
1、編寫filebeat配置文件
[root@ELK101 filebeat-7.17.5-linux-x86_64]# cat config/04-tomcat-to-es_index.yaml
filebeat.config.modules:
path: ${path.config}/modules.d/tomcat.yml #固定模塊,啟用的時候就不用只留tomcat了
reload.enabled: true
output.elasticsearch:
hosts:
- "http://10.0.0.101:19200"
- "http://10.0.0.102:19200"
- "http://10.0.0.103:19200"
# 配置索引的名稱
index: "koten-filebeat-tomcat-access-%{+yyyy.MM.dd}"
# 禁用索引的生命周期管理
setup.ilm.enabled: false
# 設(shè)置索引模板名稱
setup.template.name: "koten-filebeat-tomcat"
# 設(shè)置索引的匹配模式
setup.template.pattern: "koten-filebeat-tomcat*"
# 是否覆蓋現(xiàn)有的索引模板,若有多個filebeat實例時,建議關(guān)閉,會讓ES壓力過大
setup.template.overwrite: true
# 配置索引的設(shè)置
setup.template.settings:
# 設(shè)置分片數(shù)量
index.number_of_shards: 5
# 設(shè)置分片副本數(shù)量。
index.number_of_replicas: 1
2、運行filebeat并在kibana中查看索引信息
結(jié)論:如果已經(jīng)存在了索引模板,且與要更新的索引模塊在分片數(shù)量上有沖突,那么索引模板的信息會更新,但是如果索引名字是之前的話還是會寫入之前的索引,原因是修改索引模板后只對修改后生成的索引生效,解決辦法就是更改索引名字,新建個新的索引,分片數(shù)就與索引模板的一致了,新建索引不再演示
[root@ELK101 filebeat-7.17.5-linux-x86_64]# rm -rf data/
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/04-tomcat-to-es_index.yaml
?
六、indices模塊實現(xiàn)多個input寫入不同的ES索引
1、編寫配置文件
[root@ELK101 filebeat-7.17.5-linux-x86_64]# cat config/05-log-to-es.yaml
filebeat.inputs:
- type: log
paths: ["/tmp/apps.log"]
tags: "apps"
- type: log
paths: ["/tmp/hobby.json"]
tags: "json"
output.elasticsearch:
hosts:
- "http://10.0.0.101:19200"
- "http://10.0.0.102:19200"
- "http://10.0.0.103:19200"
indices:
- index: "koten-filebeat-indices-json-%{+yyyy.MM.dd}"
when.contains:
tags: "json"
- index: "koten-filebeat-indices-apps-%{+yyyy.MM.dd}"
when.contains:
tags: "apps"
setup.ilm.enabled: false
setup.template.name: "koten-filebeat-indices"
setup.template.pattern: "koten-filebeat-indices*"
setup.template.overwrite: true
setup.template.settings:
index.number_of_shards: 10
index.number_of_replicas: 0
2、啟動filebeat
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/05-log-to-es.yaml
3、寫入日志信息,觀察創(chuàng)建的索引信息
[root@ELK101 apache-tomcat-9.0.75]# echo apps > /tmp/apps.log
[root@ELK101 apache-tomcat-9.0.75]# echo {"name":"koten","hobby":["linux","nginx","tomcat"]} > /tmp/hobby.json
企業(yè)實戰(zhàn)filebeat寫入ES
模塊的缺陷其實也有,比如tomcat模塊并不會解析出來字段,在企業(yè)中我們不用模塊寫入,我們都是直接定義Nginx和tomcat,包括一些公司自研產(chǎn)品的日志格式,定義成自己想要的json格式,然后由filebeat解析json格式直接寫入ES就行,如果想要解析出公網(wǎng)ip的經(jīng)緯度,再寫入logstash再寫入ES中,我們接下來演示下自定義nginx和tomcat日志寫入ES,自建產(chǎn)品可以直接將日志寫成json格式也可以寫成別的格式通過logstash轉(zhuǎn)。
一、自定義Nginx日志格式并寫入ES
1、先清空下之前的nginx日志
[root@ELK101 apache-tomcat-9.0.75]# > /var/log/nginx/access.log
[root@ELK101 apache-tomcat-9.0.75]# > /var/log/nginx/error.log
2、 修改nginx配置文件、檢查語法、重啟nginx,訪問http://10.0.0.101/獲取日志
[root@ELK101 apache-tomcat-9.0.75]# cat /etc/nginx/nginx.conf
log_format koten_nginx_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"SendBytes":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
access_log /var/log/nginx/access.log koten_nginx_json;
[root@ELK101 apache-tomcat-9.0.75]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ELK101 apache-tomcat-9.0.75]# systemctl restart nginx
3、編寫filebeat配置文件
[root@ELK101 filebeat-7.17.5-linux-x86_64]# cat config/06-nginx-to-es.yaml
filebeat.inputs:
- type: log
paths: ["/var/log/nginx/access.log"]
processors:
- decode_json_fields:
fields: ["message"]
target: ""
output.elasticsearch:
hosts:
- "http://10.0.0.101:19200"
- "http://10.0.0.102:19200"
- "http://10.0.0.103:19200"
# 配置索引的名稱
index: "koten-filebeat-nginx-access-%{+yyyy.MM.dd}-1"
# 禁用索引的生命周期管理
setup.ilm.enabled: false
# 設(shè)置索引模板名稱
setup.template.name: "koten-filebeat-nginx"
# 設(shè)置索引的匹配模式
setup.template.pattern: "koten-filebeat-nginx*"
# 是否覆蓋現(xiàn)有的索引模板,若有多個filebeat實例時,建議關(guān)閉,會讓ES壓力過大
setup.template.overwrite: true
# 配置索引的設(shè)置
setup.template.settings:
# 設(shè)置分片數(shù)量
index.number_of_shards: 3
# 設(shè)置分片副本數(shù)量。
index.number_of_replicas: 0
4、運行filebeat,觀察索引信息與索引下文檔?
[root@ELK101 filebeat-7.17.5-linux-x86_64]# rm -rf /data
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/06-nginx-to-es.yaml
二、自定義tomcat日志格式并寫入ES
1、先清空下之前的tomcat日志
[root@ELK101 apache-tomcat-9.0.75]# > logs/*.txt
2、 修改tomcat配置文件、重啟tomcat,訪問http://10.0.0.101:8080獲取日志
[root@ELK101 filebeat-7.17.5-linux-x86_64]# cat /koten/softwares/apache-tomcat-9.0.75/conf/server.xml
......
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="tomcat.oldboyedu.com_access_log" suffix=".txt"
pattern="{"clientip":"%h","ClientUser":"%l","authenticated":"%u","AccessTime":"%t","request":"%r","status":"%s","SendBytes":"%b","Query?string":"%q","partner":"%{Referer}i","http_user_agent":"%{User-Agent}i"}"/>
</Host>
</Engine>
</Service>
</Server>
[root@ELK101 filebeat-7.17.5-linux-x86_64]# /koten/softwares/apache-tomcat-9.0.75/bin/shutdown.sh
[root@ELK101 filebeat-7.17.5-linux-x86_64]# /koten/softwares/apache-tomcat-9.0.75/bin/startup.sh
3、編寫filebeat配置文件
[root@ELK101 filebeat-7.17.5-linux-x86_64]# cat config/07-tomcat-to-es.yaml
filebeat.inputs:
- type: log
paths: ["/koten/softwares/apache-tomcat-9.0.75/logs/*.txt"]
processors:
- decode_json_fields:
fields: ["message"]
target: ""
output.elasticsearch:
hosts:
- "http://10.0.0.101:19200"
- "http://10.0.0.102:19200"
- "http://10.0.0.103:19200"
# 配置索引的名稱
index: "koten-filebeat-tomcat-access-%{+yyyy.MM.dd}-1"
# 禁用索引的生命周期管理
setup.ilm.enabled: false
# 設(shè)置索引模板名稱
setup.template.name: "koten-filebeat-tomcat"
# 設(shè)置索引的匹配模式
setup.template.pattern: "koten-filebeat-tomcat*"
# 是否覆蓋現(xiàn)有的索引模板,若有多個filebeat實例時,建議關(guān)閉,會讓ES壓力過大
setup.template.overwrite: true
# 配置索引的設(shè)置
setup.template.settings:
# 設(shè)置分片數(shù)量
index.number_of_shards: 5
# 設(shè)置分片副本數(shù)量。
index.number_of_replicas: 1
4、運行filebeat,觀察索引信息與索引下文檔?
[root@ELK101 filebeat-7.17.5-linux-x86_64]# rm -rf data/
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/07-tomcat-to-es.yaml
?
EFK架構(gòu)轉(zhuǎn)ELFK架構(gòu)
當我們的efk不能滿足需求時,例如有一些字段信息,我們想要移除,用到logstash,我們就選擇將logstash加入到我們的架構(gòu)中
1、修改logstash配置文件,啟動logstash
[root@ELK102 ~]# cat config_logstash/16-filebeat-to-es.conf
input {
beats {
port => 8888
}
}
filter {
mutate {
remove_field => [ "@version","agent","host","input","ecs","log","tags" ]
}
}
output {
if [apps] == "nginx" {
elasticsearch {
hosts => ["10.0.0.101:19200","10.0.0.102:19200","10.0.0.103:19200"]
index => "koten-if-nginx-%{+yyyy.MM.dd}"
}
} else if [apps] == "tomcat" {
elasticsearch {
hosts => ["10.0.0.101:19200","10.0.0.102:19200","10.0.0.103:19200"]
index => "koten-if-tomcat-%{+yyyy.MM.dd}"
}
}
stdout {
codec => rubydebug
}
}
[root@ELK102 ~]# logstash -rf config_logstash/16-filebeat-to-es.conf
2、采集nginx日志
[root@ELK101 filebeat-7.17.5-linux-x86_64]# cat config/08-nginx-to-logstash.yaml
filebeat.inputs:
- type: log
paths: ["/var/log/nginx/access.log"]
processors:
- decode_json_fields:
fields: ["message"]
target: ""
- add_fields:
target: ""
fields:
apps: nginx
output.logstash:
hosts: ["10.0.0.102:8888"]
[root@ELK101 filebeat-7.17.5-linux-x86_64]# rm -rf data/
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/08-nginx-to-logstash.yaml
3、采集tomcat日志
[root@ELK101 filebeat-7.17.5-linux-x86_64]# cat config/09-tomcat-to-logstash.yaml
filebeat.inputs:
- type: log
paths: ["/koten/softwares/apache-tomcat-9.0.75/logs/*.txt"]
fields:
apps: tomcat
fields_under_root: true #放在頂部字段
json.keys_under_root: true #將message進行json解析,且不保留原有數(shù)據(jù)
json.add_error_key: true #如果解析格式出錯,會打印錯誤
# 用上面和下面的辦法進行json解析都可以
# processors:
# - decode_json_fields:
# fields: ["message"]
# target: ""
output.logstash:
hosts: ["10.0.0.102:8888"]
[root@ELK101 filebeat-7.17.5-linux-x86_64]# rm -rf tomcat/
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/09-tomcat-to-logstash.yaml --path.data tomcat #再次運行filebeat實例需要指定新的數(shù)據(jù)目錄
4、在kibana中驗證數(shù)據(jù)是否存在,看輸出是否有刪除我們指定的字段
[root@ELK102 ~]# logstash -rf config_logstash/16-filebeat-to-es.conf
......
{
"message" => "{\"@timestamp\":\"2023-05-31T21:17:40+08:00\",\"host\":\"10.0.0.101\",\"clientip\":\"10.0.0.1\",\"SendBytes\":4833,\"responsetime\":0.000,\"upstreamtime\":\"-\",\"upstreamhost\":\"-\",\"http_host\":\"10.0.0.101\",\"uri\":\"/index.html\",\"domain\":\"10.0.0.101\",\"xff\":\"-\",\"referer\":\"-\",\"tcp_xff\":\"-\",\"http_user_agent\":\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57\",\"status\":\"200\"}",
"clientip" => "10.0.0.1",
"tcp_xff" => "-",
"upstreamhost" => "-",
"referer" => "-",
"uri" => "/index.html",
"status" => "200",
"SendBytes" => 4833,
"http_user_agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57",
"http_host" => "10.0.0.101",
"@timestamp" => 2023-05-31T16:11:34.711Z,
"domain" => "10.0.0.101",
"apps" => "nginx",
"xff" => "-",
"responsetime" => 0,
"upstreamtime" => "-"
}
......
{
"ClientUser" => "-",
"Query?string" => "",
"AccessTime" => "[01/Jun/2023:00:07:12 +0800]",
"status" => "200",
"SendBytes" => "11230",
"partner" => "-",
"http_user_agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57",
"@timestamp" => 2023-05-31T16:11:36.664Z,
"clientip" => "10.0.0.1",
"apps" => "tomcat",
"authenticated" => "-",
"request" => "GET / HTTP/1.1"
}
......
文章總結(jié)
文章講解了filebeat采集日志時,nginx,tomcat,自建產(chǎn)品日志的一些場景和措施,將efk架構(gòu)轉(zhuǎn)成elfk架構(gòu),與logstash中的grok、date、geoip、muate、user_agent、drop、json等插件結(jié)合使用,講解了filebeat可以對logstash和elasticsearsh進行輸出,接下來還有一個實戰(zhàn)練習(xí),可以對這些內(nèi)容進行練習(xí)鞏固。
實戰(zhàn)練習(xí)(filebeat+logstash+pipeline+ES+kibana)
使用filebeat采集自己生成的近30萬條記錄的nginx日志與采集不同格式的商品日志,寫入logstash分析經(jīng)緯度,日期處理,客戶端設(shè)備分析,過濾無用的字段,要求使用logstash的pipline技術(shù)實現(xiàn),使用kibana出圖展示,這些操作之前畫圖,說明項目流程,項目架構(gòu),操作中實時寫步驟,以及你維護的具體事宜。
一、畫圖,展示項目流程
二、項目流程及維護事宜
業(yè)務(wù)1:30萬條數(shù)據(jù)都是nginx原本日志格式,通過filbeat將數(shù)據(jù)轉(zhuǎn)到logstash,logstash排除多余字段,轉(zhuǎn)換的時候加上geoip,解析公網(wǎng)的IP地址,用于kibana進行數(shù)據(jù)分析
業(yè)務(wù)2:商品數(shù)據(jù)是不同的格式,bulk是有多余字段,為節(jié)省logstash壓力,我們選擇通過filebeat過濾create字段,轉(zhuǎn)到logstash之后再進行字段過濾再進行進一步的解析,有txt格式用字段分割的可以打上tags或指定其他字段用if語句進行判斷,logstash排除多余字段,轉(zhuǎn)換的時候加上geoip,解析公網(wǎng)的IP地址,用于kibana進行數(shù)據(jù)分析
注意:業(yè)務(wù)1數(shù)據(jù)量大,添加索引的時候可能需要加大es堆內(nèi)存,之前設(shè)置的是256m,視情況調(diào)整;可以先測試下輸出,看下兩個業(yè)務(wù)的ip解析出來是什么字段,再進行g(shù)eoip解析經(jīng)緯度。
總結(jié):由于需求用到pipeline,所以我們在logstash寫兩個配置文件,一個業(yè)務(wù)一個;logstash也是兩個配置文件,一個業(yè)務(wù)一個;kibana可視化我們制作geoip的地圖,其他的可以點點點自行添加
三、filebeat采集30w記錄的nginx日志和不同格式的商品信息,用兩個配置文件實現(xiàn),一個業(yè)務(wù)一個配置文件
業(yè)務(wù)1配置文件
[root@ELK101 ~]# cat config/09-nginx-to-logstash.yaml
filebeat.inputs:
- type: log
paths:
- /tmp/all_nginx.txt #提前準備好30w條nginx原生記錄
output.logstash:
hosts: ["10.0.0.102:8888"]
業(yè)務(wù)2配置文件
[root@ELK101 ~]# cat config/10-txt-bulk-json-to-logstash.yaml
filebeat.inputs:
- type: log
paths: ["/tmp/shop/*.txt"]
processors:
- add_fields:
target: ""
fields:
apps: txt
- type: log
paths: ["/tmp/shop/*.bulk"]
tags: "bulk"
processors:
- add_fields:
target: ""
fields:
apps: bulk
- drop_event:
when:
contains:
message: "create"
- type: log
paths: ["/tmp/shop/*.json"]
tags: "json"
processors:
- add_fields:
target: ""
fields:
apps: json
output.logstash:
hosts: ["10.0.0.102:8889"]
四、logstash寫兩個配置文件,監(jiān)聽兩個端口
業(yè)務(wù)1配置文件
[root@ELK102 ~]# cat config_logstash/17-filebeat-to-es.conf
input {
beats {
port => 8888
}
}
filter {
grok{
match => {
"message" => "%{COMMONAPACHELOG}"
}
}
mutate {
remove_field => [ "@version","agent","host","input","ecs","log" ]
}
geoip {
source => "clientip"
}
}
output {
elasticsearch {
hosts => ["10.0.0.101:19200","10.0.0.102:19200","10.0.0.103:19200"]
index => "koten-if-nginx-%{+yyyy.MM.dd}-01"
}
}
業(yè)務(wù)2配置文件
[root@ELK102 ~]# cat config_logstash/18-filebeat-to-es.conf
input {
beats {
port => 8889
}
}
filter {
if [apps] == "txt" {
mutate {
split => { "message" => "," }
}
mutate {
add_field => {
ip_addr => "%{[message][1]}"
title => "%{[message][3]}"
price => "%{[message][5]}"
brand => "%{[message][7]}"
item => "%{[message][9]}"
group => "%{[message][11]}"
author => "%{[message][13]}"
}
}
} else if [apps] == "bulk" {
json {
source => "message"
}
} else if [apps] == "json" {
json {
source => "message"
}
}
mutate {
remove_field => [ "@version","agent","host","input","ecs","log","tags" ]
}
geoip {
source => "ip_addr"
}
}
output {
elasticsearch {
hosts => ["10.0.0.101:19200","10.0.0.102:19200","10.0.0.103:19200"]
index => "koten-shop-%{+yyyy.MM.dd}-01"
}
}
五、編寫pipeline配置文件,運行l(wèi)ogstash和filebeat
編寫pipeline配置文件
[root@ELK102 ~]# egrep -v '^$|^*#' /etc/logstash/pipelines.yml
- pipeline.id: nginx
path.config: "/root/config_logstash/17-filebeat-to-es.conf"
- pipeline.id: shop
path.config: "/root/config_logstash/18-filebeat-to-es.conf"
運行前可以先創(chuàng)建模板,加個地理坐標點的映射?
?
?
運行l(wèi)ogstash
[root@ELK102 ~]# logstash
等待logstash運行起來運行filebeat,注意要指定數(shù)據(jù)目錄
[root@ELK101 ~]# filebeat -e -c config/09-nginx-to-logstash.yaml --path.data nginx
[root@ELK101 ~]# filebeat -e -c config/10-txt-bulk-json-to-logstash.yaml --path.data txt-bulk-json
由于我們業(yè)務(wù)1,30萬條數(shù)據(jù)加索引,會占用大量堆內(nèi)存報錯,我們可以增加堆內(nèi)存?
ELK所有集群節(jié)點都增加下JVM
[root@ELK102 ~]# egrep -v '^$|^*#' /koten/softwares/elasticsearch-7.17.5/config/jvm.options
-Xms512m
-Xmx512m
......
[root@ELK102 ~]# systemctl restart es7
六、在kibana中分別制作兩個的業(yè)務(wù)的可視化
1、查看索引
2、創(chuàng)建索引模式?
3、制作地圖可視化
業(yè)務(wù)1
圖片違規(guī)了。。。文章來源:http://www.zghlxwxcb.cn/news/detail-752331.html
業(yè)務(wù)2?
圖片違規(guī)了。。。
我是koten,10年運維經(jīng)驗,持續(xù)分享運維干貨,感謝大家的閱讀和關(guān)注!文章來源地址http://www.zghlxwxcb.cn/news/detail-752331.html
到了這里,關(guān)于【運維知識大神篇】超詳細的ELFK日志分析教程7(filebeat常用模塊+filebeat采集固定格式日志+自定義日志格式寫入ES+EFK架構(gòu)轉(zhuǎn)ELFK架構(gòu)+兩個業(yè)務(wù)實戰(zhàn)練習(xí))的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!