【运维知识大神篇】超详细的ELFK日志分析教程7(filebeat常用模块+filebeat采集固定格式日志+自定义日志格式写入ES+EFK架构转ELFK架构+两个业务实战练习)

本篇文章继续给大家介绍ELFK日志分析,详细请见下面目录。

目录

filebeat采集nginx日志

filebeat模块使用

一、Nginx模块

二、tomcat模块

三、filebeat写数据到ES集群自定义索引

四、filebeat自定义字段之nginx写入ES

五、filebeat自定义字段之tomcat写入ES

六、indices模块实现多个input写入不同的ES索引

企业实战filebeat写入ES

一、自定义Nginx日志格式并写入ES

二、自定义tomcat日志格式并写入ES

EFK架构转ELFK架构

文章总结

实战练习(filebeat+logstash+pipeline+ES+kibana)

一、画图,展示项目流程

二、项目流程及维护事宜

三、filebeat采集30w记录的nginx日志和不同格式的商品信息,用两个配置文件实现,一个业务一个配置文件

四、logstash写两个配置文件,监听两个端口

五、编写pipeline配置文件,运行logstash和filebeat

六、在kibana中分别制作两个的业务的可视化


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,收集一些日志数据

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查看数据

[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解析出来的有区别,这里解析的是日志中的时间,不是日志写入时间

可以发现字段都被拆分解析出来了 

5、使用kibana创建地图

可以根据字段添加地图,但是由于我是本机IP访问的nginx,是私网,所以没有在地图上显示

二、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获取日志数据

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/    #删除数据记录
[root@ELK101 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/01-modules-to-es.yaml 

7、查看kibana,成功写入了索引 

三、filebeat写数据到ES集群自定义索引

虽然写入了索引,但是我们需要自定义索引的名称及其他内容,可以在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,我们自定义的将全部失效,还会写入之前随机的索引的模板,将其关闭我们的索引才可以自定义,若关闭了它没有自定义索引,也会创建成功索引,但是创建的索引就没有后面的计数了,而且会提示生命周期错误。
# 设置索引模板名称
setup.template.name: "koten-filebeat-tomcat"
# 设置索引的匹配模式
setup.template.pattern: "koten-filebeat-tomcat*"
# 是否覆盖现有的索引模板,若为true,不管索引模板是否一致都会覆盖,若有多个filebeat实例时,建议关闭,会让ES压力过大
setup.template.overwrite: false
# 配置索引的设置
setup.template.settings:
  # 设置分片数量
  index.number_of_shards: 5
  # 设置分片副本数量。
  index.number_of_replicas: 0

2、执行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},分片数为3,副本分片数为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
# 设置索引模板名称
setup.template.name: "koten-filebeat-nginx"
# 设置索引的匹配模式
setup.template.pattern: "koten-filebeat-nginx*"
# 是否覆盖现有的索引模板,若有多个filebeat实例时,建议关闭,会让ES压力过大
setup.template.overwrite: true
# 配置索引的设置
setup.template.settings:
  # 设置分片数量
  index.number_of_shards: 3
  # 设置分片副本数量。
  index.number_of_replicas: 0

2、设置只启用nginx模块,并修改nginx模块配置信息(也可以不设置只启用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},分片数为5,副本分片数为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
# 设置索引模板名称
setup.template.name: "koten-filebeat-tomcat"
# 设置索引的匹配模式
setup.template.pattern: "koten-filebeat-tomcat*"
# 是否覆盖现有的索引模板,若有多个filebeat实例时,建议关闭,会让ES压力过大
setup.template.overwrite: true
# 配置索引的设置
setup.template.settings:
  # 设置分片数量
  index.number_of_shards: 5
  # 设置分片副本数量。
  index.number_of_replicas: 1

2、运行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/04-tomcat-to-es_index.yaml 

 

六、indices模块实现多个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、写入日志信息,观察创建的索引信息

[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

企业实战filebeat写入ES

模块的缺陷其实也有,比如tomcat模块并不会解析出来字段,在企业中我们不用模块写入,我们都是直接定义Nginx和tomcat,包括一些公司自研产品的日志格式,定义成自己想要的json格式,然后由filebeat解析json格式直接写入ES就行,如果想要解析出公网ip的经纬度,再写入logstash再写入ES中,我们接下来演示下自定义nginx和tomcat日志写入ES,自建产品可以直接将日志写成json格式也可以写成别的格式通过logstash转。

一、自定义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
# 设置索引模板名称
setup.template.name: "koten-filebeat-nginx"
# 设置索引的匹配模式
setup.template.pattern: "koten-filebeat-nginx*"
# 是否覆盖现有的索引模板,若有多个filebeat实例时,建议关闭,会让ES压力过大
setup.template.overwrite: true
# 配置索引的设置
setup.template.settings:
  # 设置分片数量
  index.number_of_shards: 3
  # 设置分片副本数量。
  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="{&quot;clientip&quot;:&quot;%h&quot;,&quot;ClientUser&quot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;request&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?string&quot;:&quot;%q&quot;,&quot;partner&quot;:&quot;%{Referer}i&quot;,&quot;http_user_agent&quot;:&quot;%{User-Agent}i&quot;}"/>

          </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
# 设置索引模板名称
setup.template.name: "koten-filebeat-tomcat"
# 设置索引的匹配模式
setup.template.pattern: "koten-filebeat-tomcat*"
# 是否覆盖现有的索引模板,若有多个filebeat实例时,建议关闭,会让ES压力过大
setup.template.overwrite: true
# 配置索引的设置
setup.template.settings:
  # 设置分片数量
  index.number_of_shards: 5
  # 设置分片副本数量。
  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架构转ELFK架构

当我们的efk不能满足需求时,例如有一些字段信息,我们想要移除,用到logstash,我们就选择将logstash加入到我们的架构中

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解析,且不保留原有数据
  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实例需要指定新的数据目录

4、在kibana中验证数据是否存在,看输出是否有删除我们指定的字段

[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"
}
......

文章总结

文章讲解了filebeat采集日志时,nginx,tomcat,自建产品日志的一些场景和措施,将efk架构转成elfk架构,与logstash中的grok、date、geoip、muate、user_agent、drop、json等插件结合使用,讲解了filebeat可以对logstash和elasticsearsh进行输出,接下来还有一个实战练习,可以对这些内容进行练习巩固。

实战练习(filebeat+logstash+pipeline+ES+kibana)

使用filebeat采集自己生成的近30万条记录的nginx日志与采集不同格式的商品日志,写入logstash分析经纬度,日期处理,客户端设备分析,过滤无用的字段,要求使用logstash的pipline技术实现,使用kibana出图展示,这些操作之前画图,说明项目流程,项目架构,操作中实时写步骤,以及你维护的具体事宜。

一、画图,展示项目流程

二、项目流程及维护事宜

业务1:30万条数据都是nginx原本日志格式,通过filbeat将数据转到logstash,logstash排除多余字段,转换的时候加上geoip,解析公网的IP地址,用于kibana进行数据分析

业务2:商品数据是不同的格式,bulk是有多余字段,为节省logstash压力,我们选择通过filebeat过滤create字段,转到logstash之后再进行字段过滤再进行进一步的解析,有txt格式用字段分割的可以打上tags或指定其他字段用if语句进行判断,logstash排除多余字段,转换的时候加上geoip,解析公网的IP地址,用于kibana进行数据分析

注意:业务1数据量大,添加索引的时候可能需要加大es堆内存,之前设置的是256m,视情况调整;可以先测试下输出,看下两个业务的ip解析出来是什么字段,再进行geoip解析经纬度。

总结:由于需求用到pipeline,所以我们在logstash写两个配置文件,一个业务一个;logstash也是两个配置文件,一个业务一个;kibana可视化我们制作geoip的地图,其他的可以点点点自行添加

三、filebeat采集30w记录的nginx日志和不同格式的商品信息,用两个配置文件实现,一个业务一个配置文件

业务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"]

业务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写两个配置文件,监听两个端口

业务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"
    } 
}

业务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配置文件,运行logstash和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"

运行前可以先创建模板,加个地理坐标点的映射 

  

运行logstash

[root@ELK102 ~]# logstash

等待logstash运行起来运行filebeat,注意要指定数据目录

[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

由于我们业务1,30万条数据加索引,会占用大量堆内存报错,我们可以增加堆内存 

ELK所有集群节点都增加下JVM

[root@ELK102 ~]# egrep -v '^$|^*#' /koten/softwares/elasticsearch-7.17.5/config/jvm.options
-Xms512m
-Xmx512m
......
[root@ELK102 ~]# systemctl restart es7

六、在kibana中分别制作两个的业务的可视化

1、查看索引

2、创建索引模式 

3、制作地图可视化

业务1

图片违规了。。。

业务2 

图片违规了。。。


我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!

  • 18
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是koten

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值