ELK 7.2日志分析平台搭建



安装环境安装目录安装版本安装用户
CentOS 7.7/opt/elk7.2.0非root用户

安装ElasticSearch 7.2.0

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-linux-x86_64.tar.gz.sha512
shasum -a 512 -c elasticsearch-7.2.0-linux-x86_64.tar.gz.sha512 
tar -xzf elasticsearch-7.2.0-linux-x86_64.tar.gz
cd elasticsearch-7.2.0/config

修改配置文件elasticsearch.yml

#数据及日志目录
path.data: /opt/elk/elasticsearch-7.2.0/data
path.logs: /opt/elk/elasticsearch-7.2.0/logs

#绑定IP,默认仅本机可访问,此处设置为允许任何IP
network.host: 0.0.0.0

#客户端http访问端口
http.port: 9200

#启用xpack安全验证功能
xpack.security.enabled: true

后台启动

cd /opt/elk/elasticsearch-7.2.0/bin && ./elasticsearch -d

如果在启动时出现以下错误:

max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]

在elasticsearch.yml配置中添加如下配置后重新启动:

transport.host: localhost
transport.tcp.port: 9300

也可以通过root身份修改/etc/security/limits.conf,追加如下配置:

soft nofile 65536
hard nofile 65536

此文件修改后需要重新登录才会生效。

为内置账号设置密码

./bin/elasticsearch-setup-passwords interactive

内置用户为:

  • elastic
  • apm_system
  • kibana
  • logstash_system
  • beats_system
  • remote_monitoring_user

安装Kibana 7.2.0

wget https://artifacts.elastic.co/downloads/kibana/kibana-7.2.0-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.2.0-linux-x86_64.tar.gz.sha512
shasum -a 512 -c kibana-7.2.0-linux-x86_64.tar.gz.sha512
tar -xzf kibana-7.2.0-linux-x86_64.tar.gz
cd kibana-7.2.0/config

修改配置文件kibana.yml

#后台服务端口
server.port: 5601

#指定服务绑定地址,默认为localhost,仅本机可访问
server.host: "10.75.41.133"

#Elasticsearch实例地址
elasticsearch.hosts: ["http://es-ip-address:9200"]

#添加为kibana用户设置的密码
elasticsearch.username: "kibana"
elasticsearch.password: "your_kibana_password"

后台启动

cd /opt/elk/kibana-7.2.0-linux-x86_64/
nohup ./bin/kibana &

访问地址为:

http://kibana-ip-address:5601

注意kibana用户无法登录,需要使用elastic/elastic_password来登录。

安装Logstash 7.2.0

wget https://artifacts.elastic.co/downloads/logstash/logstash-7.2.0.tar.gz
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.2.0.tar.gz.sha512
shasum -a 512 -c logstash-7.2.0.tar.gz.sha512
tar -xzf logstash-7.2.0.tar.gz

添加自定义配置

首先创建配置加载目录conf.d,创建配置文件app.conf如下:

input {
  tcp {
    mode => "server"
    host => "0.0.0.0"
    type => "app-dev"
    port => 14416
    codec => json_lines
  }

}

filter {
  ruby {
     code => "
       event.set('timestamp', event.get('@timestamp').time.localtime + 8*60*60)
       event.set('index_date', event.get('@timestamp').time.localtime.strftime('%Y.%m.%d'))
       "
  }
  
  if ![caller_method_name] {
    mutate { 
      add_field => { 
      "caller_method_name" => "" 
      } 
    }
  }
  if ![caller_line_number] {
    mutate { 
      add_field => { 
      "caller_line_number" => "" 
      } 
    }
  }
  mutate {
    update => {
      "message" => "[%{timestamp}] [%{level}] --- [%{thread_name}]:[%{logger_name},%{caller_method_name},%{caller_line_number}]:[%{message}]"
    }
  }
}

output {
  elasticsearch {
      action       => "index"
      hosts        => ["10.75.41.133:9200"]
      user         => "elastic"
      password     => "elastic_password"
      index        => "%{type}-%{index_date}"
  }
}

后台启动

cd /opt/elk/logstash-7.2.0
./bin/logstash -f /opt/elk/logstash-7.2.0/conf.d &

SpringBoot微服务配置

添加依赖

<dependency>
    <groupId>net.logstash.logback</groupId>
    <artifactId>logstash-logback-encoder</artifactId>
    <version>7.2</version>
</dependency>

修改logback-spring.xml

<!--此处logstash.destination需要在application.properties中配置,如果使用了配置中心,需要将此属性提前加载,否则无法读取-->
<!--也可以考虑在logback-spring.xml中直接配置logstash服务地址-->
<springProperty name="LOGSTASH_DESTINATION" source="logstash.destination"/>
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
    <destination>${LOGSTASH_DESTINATION:- }</destination>
    <includeCallerData>true</includeCallerData>
    <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">
        <includeCallerData>true</includeCallerData>
    </encoder>
</appender>
<root level="info">
    <appender-ref ref="LOGSTASH" />
    <appender-ref ref="console" />
    ...
</root>

需要说明的是,编码器中的includeCallerData标签用于开启请求者信息功能,主要包含以下信息:
caller_class_namecaller_method_namecaller_file_namecaller_line_number
输出日志事件的完整类名输出日志事件的方法名称输出日志事件的文件名输出日志事件的行号

另外如果编码器包含了异步追加器(如AsyncAppender、LoggingEventAsyncDisruptorAppender、LogstashTcpSocketAppender等),那么在appender和encoder上需要同时设置includeCallerData为true。

也可以在编码器中添加自定义字段,示例如下:

<customFields>
{
    "appname": "app-service",
    "buildinfo": {
        "version": "0.0.1-SNAPSHOT",
        "lastcommit": "eceb10e6c80e91009169752296d02e3b4ac3ff5b"
    }
}
</customFields>

kibana配置优化及权限管理

使用elastic用户登录kibana

权限管理

创建space

space可以理解为资源组,每个space中可以设置各项菜单功能是否可见,默认是全部可见。当然这里仅仅是在UI上设置是否可见,但具体功能仍未被禁用,需要在Roles中进行配置。

创建Role

在创建role时,可以指定能够访问到哪些索引(可使用通配符),以及对应索引下包含的权限。对于一般的开发人员而言,权限设置为read、view_index_metadata两项即可。

在space privilege中可以指定role所在的space(可见权限),设置各项功能的权限等级(使用权限)

创建User

在创建user时设置用户名密码以及上面创建好的role即可正常登录。

配置优化

设置项修改

  • Date format:YYYY-MM-DD HH:mm:ss.SSS
  • Day of week:Monday
  • Formatting locale:chs
  • Default columns:message
  • Maximum table cell height:0