ELK实战

           架构说明:https://blog.csdn.net/beyond_qjm/article/details/81941820

 

一、基本架构

 ELK

二、Filebeat安装

服务器安装根目录
172.17.0.153/opt/filebeat-6.2.3-linux-x86_64
172.17.0.154/opt/filebeat-6.2.3-linux-x86_64
172.17.0.155/opt/filebeat-6.2.3-linux-x86_64
172.17.0.156/opt/filebeat-6.2.3-linux-x86_64

安装教程:https://blog.csdn.net/beyond_qjm/article/details/81944486

整合配置:

filebeat.prospectors:
- type: log
  enabled: true # 开关
  paths: # 日志文件路径,可以用用通配符
    - /logs/access/access*  #访问记录 
    #- c:\programdata\elasticsearch\logs\*  如果是windows服务器,用这个路径
  fields_under_root: true
  multiline: # 日志多行处理,列如java的堆栈信息
      pattern: ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # 匹配ip
      negate: true
      match: after
  fields: # 自定义属性,用于 Logstash 中
     log_type: access # 日志文件类型
     server_id: 172.17.0.153 # 服务器ip地址
  scan_frequency: 120s #扫描间隔,单位为秒;设置太小会引起filebeat频繁扫描文件,导致cpu占用百分比过高
  tail_files: false #是否是从文件尾部开始监听
  backoff: 30s #文件检查间隔时间
  max_backoff: 60
  tail_files: true
 

- type: log
  enabled: true
  paths:
    - /logs/console/console*  #log日志
  fields_under_root: true
  multiline:
      pattern: ^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}
      negate: true
      match: after
  fields:
     log_type: console
     server_id: 172.17.0.153
  scan_frequency: 120
  tail_files: false #是否是从文件尾部开始监听
  backoff: 30s #文件检查间隔时间
  max_backoff: 60
  tail_files: true


output.logstash: # 输出到logstash的安装位置,以及监听的端口
  hosts: ["172.17.0.184:9988"]

三、Logstash安装

服务器安装根目录
172.17.0.184/home/elk/logstash-6.2.3/

安装教程:https://blog.csdn.net/beyond_qjm/article/details/81945527

整合配置:

    1. 添加 /home/elk/logstash-6.2.3/bin/config/log4j.cfg

input{
        beats {
                port => "9988"
        }
}

filter{
        grok{
                match => [
                        "message" , "%{TIMESTAMP_ISO8601:time} %{DATA:thread} %{LOGLEVEL:level}  %{JAVACLASS:class} : %{GREEDYDATA:info}",

                        "message" , "%{TIMESTAMP_ISO8601:time} %{DATA:thread} %{LOGLEVEL:level} %{JAVACLASS:class} : %{GREEDYDATA:info}",
                        "message" , "%{URIHOST:visitHost} - - \[%{HTTPDATE:time}\] \"%{CISCO_REASON:method} %{URIPATHPARAM:request} %{SYSLOGPROG:protocol}\" %{NUMBER:responseCode:int} %{NUMBER:responseTime:int}"
                ]
        }

        date { 
                match => [
                        "time", "dd/MMM/yyyy:HH:mm:ss Z",
                        "yyyy-MM-dd HH:mm:ss.SSS",
                        "yyyy-MM-dd HH:mm:ss"
                ]
                target => "@timestamp"
                locale => "cn"
        }

        mutate{
                remove_field => ["@version","_score","_id","program","time","beat","offset","prospector","host","message","tags"]
        }

}


output{
        #file{path=>"/home/elk/out.log"}
        stdout{codec=>rubydebug}

        if [log_type] == "console" and [info] =~ /^\S/ {
                elasticsearch{
                        hosts => ["172.17.0.183:9200"]
                        index => "console-%{+YYYY.MM}"
                        manage_template => true
                        template_name => "console"
                        template_overwrite => true
                        template => "/home/elk/logstash-6.2.3/bin/config/console.json"
                }
        }

        if [log_type] == "access" {
                elasticsearch{
                        hosts => ["172.17.0.183:9200"]
                        index => "access-%{+YYYY.MM}"
                        manage_template => true
                        template_name => "access"
                        template_overwrite => true
                        template => "/home/elk/logstash-6.2.3/bin/config/access.json"
                }
        }
}

 
 

    2. 添加 /home/elk/logstash-6.2.3/bin/config/laccess.json

{
    "template": "access*",
    "settings": {
        "index.number_of_shards": 5,
        "number_of_replicas": 1
    },
    "mappings": {
        "access": {
            "_all": {
                "enabled": false
            },
            "properties": {
                "@timestamp": {
                    "type": "date",
                },
                "server_id": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "source": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "visitHost": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "method": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "request": {
                    "type": "string",
                    "index": "analyzed"
                },
                "protocol": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "responseCode": {
                    "type": "integer",
                    "index": "not_analyzed"
                },
                "responseTime": {
                    "type": "integer",
                    "index": "not_analyzed"
                },
                "log_type": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}

    3. 添加 /home/elk/logstash-6.2.3/bin/config/console.json

{
    "template": "console*",
    "settings": {
        "index.number_of_shards": 5,
        "number_of_replicas": 1
    },
    "mappings": {
        "console": {
            "_all": {
                "enabled": false
            },
            "properties": {
                "@timestamp": {
                    "type": "date",
                },
                "server_id": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "source": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "thread": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "level": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "class": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "info": {
                    "type": "string",
                    "index": "analyzed"
                },
                "log_type": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}

四、Elasticsearch安装

服务器安装根目录节点类型
172.17.0.181/home/elk/elasticsearch-6.2.3data
172.17.0.182/home/elk/elasticsearch-6.2.3data
172.17.0.183/home/elk/elasticsearch-6.2.3master

安装教程:https://blog.csdn.net/beyond_qjm/article/details/81943552

elasticsearch-head安装配置:https://blog.csdn.net/beyond_qjm/article/details/81947181

五、Kibana安装

服务器安装根目录
172.17.0.183/home/elk/kibana-6.2.3-linux-x86_64

安装教程:https://blog.csdn.net/beyond_qjm/article/details/81946384

整合配置:

    修改 /home/elk/kibana-6.2.3-linux-x86_64/config/kibana.yml 中的  server.host: "172.17.0.183"

六、log日志配置

    1. Spring boot - logback(Spring boot项目)

           (1)配置系统环境

 

           dev、test为测试环境,prod为生产环境

           (2)在resource目录下添加logback-spring.xml 配置文件

                   需要修改属性 APP_NAME 改为当前项目名,用对区别不同系统的日志

 

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <contextName>logback-demo</contextName>

    <!-- 定义日志文件名称,与工程名一致 -->
    <property name="APP_NAME" value="duban-home" />
    <!-- 定义日志的根目录 -->
    <property name="LOG_HOME" value="/logs/console" />

    <!-- 输出到控制台 ConsoleAppender -->
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <!-- 展示格式 layout  控制台输出使用 layout ,文件输出使用 encoder -->
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                <!--
                    输出格式
                        %d{HH: mm:ss.SSS}——日志输出时间
                        %thread——输出日志的进程名字,这在Web应用以及异步任务处理中很有用
                        %-5level——日志级别,并且使用5个字符靠左对齐
                        %logger{36}——日志输出者的名字
                        %msg——日志消息
                        %n——平台的换行符
                -->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %thread %-5level %c : %msg%n</pattern>
            </pattern>
        </layout>
    </appender>

    <!-- 输出到文件 RollingFileAppender -->
    <appender name="fileLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--如果只是想要 Error 级别的日志,那么需要过滤一下,默认是 info 级别的,ThresholdFilter-->
        <!--<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>Error</level>
        </filter>-->

        <!--日志名称,如果没有File 属性,那么只会使用FileNamePattern的文件路径规则
        如果同时有<File>和<FileNamePattern>,那么当天日志是<File>,明天会自动把今天
        的日志改名为今天的日期。即,<File> 的日志都是当天的。
        -->
        <!--<File>${LOG_HOME}/console_${APP_NAME}.log</File>-->

        <!--滚动策略,按照时间滚动 TimeBasedRollingPolicy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--文件路径,定义了日志的切分方式——把每一天的日志归档到一个文件中,以防止日志填满整个磁盘空间-->
            <FileNamePattern>${LOG_HOME}/console_${APP_NAME}_%d{yyyy-MM-dd}.log</FileNamePattern>
            <!--只保留最近90天的日志-->
            <maxHistory>90</maxHistory>
            <!--用来指定日志文件的上限大小,那么到了这个值,就会删除旧的日志-->
            <!--<totalSizeCap>1GB</totalSizeCap>-->
        </rollingPolicy>

        <!--日志输出编码格式化-->
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %thread %-5level %c : %msg%n</pattern>
        </encoder>
    </appender>

    <!-- 指定最基础的日志输出级别 appender将会添加到这个loger -->
    <!-- 测试环境+开发环境. 多个使用逗号隔开. -->
    <springProfile name="dev,test">
        <root level="INFO">
            <appender-ref ref="consoleLog"/>
            <appender-ref ref="fileLog"/>
        </root>
    </springProfile>

    <!-- 生产环境 -->
    <springProfile name="prod">
        <root level="INFO">
            <appender-ref ref="fileLog"/>
        </root>
    </springProfile>

</configuration>

         (3)加载logback配置文件

 

2. 整合log4j 

           (1)修改pom.xml文件

                    Springboot项目

<!--删除Spring boot默认使用的日志依赖-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
   <exclusions>
      <exclusion>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-logging</artifactId>
      </exclusion>
   </exclusions>
</dependency>
<!-- 加入log4j日志依赖 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-log4j</artifactId>
   <version>1.3.8.RELEASE</version>
</dependency>

                  非 Springboot项目

<!-- 加入log4j日志依赖 -->
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>1.7.21</version>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>1.7.21</version>
</dependency>
<dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>1.2</version>
</dependency>

           (2)加入log4j.properties

                      将duban-home修改为当前项目名,用对区别不同系统的日志

log4j.rootLogger=info,ServerDailyRollingFile,stdout

 

log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender

log4j.appender.ServerDailyRollingFile.DatePattern='-'yyyy-MM-dd'.log'

log4j.appender.ServerDailyRollingFile.File=/logs/console/console_duban-home

log4j.appender.DAILY_ROLLING_FILE.Append=true

log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout

log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %t %-5p %c : %m%n

log4j.appender.ServerDailyRollingFile.Append=true

 

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %t %-5p %c : %m%n

3. Tomcat access访问日志

           Tomcat中默认的情况下,access log是没有设置的。在server.xml文件中配置如下。

<!-- Access log processes all example.  

            Documentation at: /docs/config/valve.html -->  

       <!--  

       <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"    

              prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>  

       -->  

          改为

                    1. prefix中的duban-home为服务器名,用于识别不同的服务器

                    2. pattern为输出格式

<!-- Access log processes all example.  

            Documentation at: /docs/config/valve.html -->  

        

       <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/logs/access/"    

              prefix="access_duban-home_log." suffix=".txt" pattern="%h - - %t &quot;%r&quot; %s %D" resolveHosts="false"/>  

       

                    pattern说明

 

pattern属性值由字符串常量和pattern标识符加上前缀"%"组合而成。pattern标识符加上前缀"%",用来代替当前请求/响应中的对应的变量值。目前支持如下的pattern:

· %a - 远端IP地址

· %A - 本地IP地址

· %b - 发送的字节数,不包括HTTP头,如果为0,使用"-"

· %B - 发送的字节数,不包括HTTP头

· %h - 远端主机名(如果resolveHost=false,远端的IP地址)

· %H - 请求协议

· %l - 从identd返回的远端逻辑用户名(总是返回 '-')

· %m - 请求的方法(GET,POST,等)

· %p - 收到请求的本地端口号

· %q - 查询字符串(如果存在,以 '?'开始)

· %r - 请求的第一行,包含了请求的方法和URI

· %s - 响应的状态码

· %S - 用户的session ID

· %t - 日志和时间,使用通常的Log格式

· %u - 认证以后的远端用户(如果存在的话,否则为'-')

· %U - 请求的URI路径

· %v - 本地服务器的名称

· %D - 处理请求的时间,以毫秒为单位

· %T - 处理请求的时间,以秒为单位

 

七、添加为服务

     将以上应用添加为服务方便管理

     教程:https://blog.csdn.net/beyond_qjm/article/details/81947493

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值