当前位置:   article > 正文

应用日志集成到ElasticSearch

应用日志集成到ElasticSearch

1、阿里云sls平台集成日志

阿里sls集成日志步骤

2、filebeat 收集到指定es

安装docker容器

Docker安装

拉取镜像:

docker pull elastic/filebeat:7.5.1
  • 1

启动:

docker run -d --name=filebeat elastic/filebeat:7.5.1
  • 1

拷贝容器中的数据文件到宿主机:

mkdir -p /data/elk7
docker cp filebeat:/usr/share/filebeat /data/elk7/
  • 1
  • 2

设置权限

chmod 777 -R /data/elk7/filebeat
#go-w: 这个命令表示去掉文件的“组”和“其他用户”的写权限。其中,
#g 代表组权限,o 代表其他用户权限,-w 表示去掉写权限。
chmod go-w /data/elk7/filebeat/filebeat.yml
  • 1
  • 2
  • 3
  • 4

配置filebeat

vim /data/elk7/filebeat/filebeat.yml
  • 1

修改样例如下:

filebeat.inputs:
  - type: log
    enabled: true
    paths:
      - /var/log/*.log
    fields:
      AppId: "springbootadmin"
      ENV: "DEV"
    fields_under_root: true
    tags: ["服务ip地址自定义其他", "boot"]
    json.keys_under_root: true

  - type: log
    enabled: true
    paths:
      - /java/*.log
    fields:
      AppId: "live-admin"
      ENV: "DEV"
    fields_under_root: true
    tags: ["服务ip地址自定义其他", "live"]
    json.keys_under_root: true

processors:
  - timestamp:
      field: "time"
      timezone: "Asia/Shanghai"
      layouts:
        - "yyyy-MM-dd HH:mm:ss.SSS"

output.elasticsearch:
  hosts: 'es:9200'
  username: "elastic"
  password: "elastic"
  indices:
  
    - index: "spring-boot-admin-%{+yyyy.MM}"
      when.contains:
        tags: "boot"
    - index: "live-admin-%{+yyyy.MM}"
      when.contains:
        tags: "live"

setup.template.settings:
  index.number_of_shards: 3
  index.number_of_replicas: 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

日志输入源

  • 使用 filebeat.inputs
    部分定义了两个日志输入源。每个输入源监视不同路径下的日志文件,并根据路径指定的类型和字段进行处理。其中,第一个输入源监视
    /var/log/.log 路径下的日志文件,用于收集 Spring Boot Admin 应用的日志;第二个输入源监视
    /java/
    .log 路径下的日志文件,用于收集 Live Admin 应用的日志。

日志标签:

  • 在每个输入源的 tags
    配置中,使用了一组自定义的标签。这些标签通常用于标识日志的来源或类型。在这个配置中,每个输入源都定义了自己的标签,以便后续根据标签将日志发送到不同的索引中。

Elasticsearch 输出

  • 在 output.elasticsearch 部分指定了 Elasticsearch
    的地址和认证信息,并配置了索引的模板。根据不同的标签,将日志发送到不同的索引中。在这个配置中,根据标签 “boot” 将 Spring
    Boot Admin 的日志发送到名为 spring-boot-admin-%{+yyyy.MM} 的索引中,根据标签 “live” 将
    Live Admin 的日志发送到名为 live-admin-%{+yyyy.MM} 的索引中。

Elasticsearch 索引设置

  • 在 setup.template.settings 部分配置了 Elasticsearch
    索引的一些设置,包括分片数和副本数。这些设置可以优化 Elasticsearch 索引的性能和可用性。

删除之前的容器:

docker rm -f filebeat
  • 1

再重启启动:

docker run   --name=filebeat --restart=always \
 -v /data/elk7/filebeat/filebeat.yml:/usr/share/filebeat/filebeat.yml \
-v /var/log/springboot/:/var/log/springboot/ \
-v /data/log/live-admin/logs/:/var/log/live-admin/logs/ \
-d  elastic/filebeat:7.5.1
  • 1
  • 2
  • 3
  • 4
  • 5

排查问题命令

docker logs filebeat
  • 1

进入容器:

docker exec -it filebeat /bin/bash

docker exec -it filebeat /bin/sh
  • 1
  • 2
  • 3

nginx的日志采集

如果要采集nginx的日志的话需要设置nginx日志格式

#修改nginx.conf,在http 、https中设置日志格式
#添加 log_format  main 
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 10240;
events {
    worker_connections  10240;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    server_tokens off;
	
	proxy_hide_header X-Powered-By;
   	proxy_hide_header Server;

	log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    proxy_connect_timeout     30000;
    keepalive_timeout  30000;
    client_max_body_size 100m;
	client_header_buffer_size 512k;
	large_client_header_buffers 4 512k;


    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

在**filebeat.inputs:**新增一个log

- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  fields:
    AppId: "nginx"
    ENV: "DEV"
  fields_under_root: true
  tags: ["ip","nginx"]
  json.keys_under_root: true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在**indices:**下新增一个index

 - index: "nginx-%{+yyyy.MM}"
      when.contains:
        tags: "nginx"
  • 1
  • 2
  • 3

设置命令行中执行 Nginx 相关的命令时不输入完整路径

export PATH=/usr/sbin:/usr/local/nginx/sbin:$PATH
  • 1
  • /usr/sbin 目录通常包含系统管理和维护的命令,例如一些系统管理工具和服务程序的启动脚本等。
  • /usr/local/nginx/sbin 目录包含了 Nginx 服务器的可执行文件,包括启动 Nginx 的命令 nginx。

通过将这两个目录添加到 PATH 中,可以方便地在命令行中直接执行 nginx 命令来启动或管理 Nginx 服务器,而不必输入完整的路径。

再次删除容器重新启动,把nginx日志也挂在上去

docker run  --name=filebeat --restart=always \
-v /data/elk7/filebeat/filebeat.yml:/usr/share/filebeat/filebeat.yml \
-v /var/log/springboot/:/var/log/springboot/ \
-v /data/log/live-admin/logs/:/var/log/live-admin/logs/ \
-v /var/log/nginx/:/var/log/nginx/ \
-d  elastic/filebeat:7.5.1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/325742
推荐阅读
相关标签
  

闽ICP备14008679号