ELK部署
本文使用 docker-compose 部署 docker环境以及docker-compose 环境 自行百度
配置编写
创建 docker-compose.yaml 文件 将以下内容写入
version: '3'
services:
elasticsearch:
image: elasticsearch:7.7.0
container_name: elk_elasticsearch
restart: always
environment:
- "cluster.name=elasticsearch-spring"
- "discovery.type=single-node"
- "ES_JAVA_OPTS=-Xms512m -Xmx1024m"
volumes:
- /home/es/elasticsearch/plugins:/usr/share/elasticsearch/plugins
- /home/es/elasticsearch/data:/usr/share/elasticsearch/data
- /home/es/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
ports:
- 9200:9200
kibana:
image: kibana:7.7.0
container_name: elk_kibana
restart: always
depends_on:
- elasticsearch
environment:
- ELASTICSEARCH_URL=http://elasticsearch:9200
ports:
- 5601:5601
logstash:
image: logstash:7.7.0
container_name: elk_logstash
restart: always
volumes:
- /home/es/logstash-springboot.conf:/usr/share/logstash/pipeline/logstash.conf
depends_on:
- elasticsearch
links:
- elasticsearch:es
ports:
- 4560:4560
创建 elasticsearch 目录 创建 data,config,plugins 目录 授予 777 权限 命令:· chmod 777
config文件夹下创建 elasticsearch.yml
network.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
xpack.security.enabled: true
创建 logstash-springboot.conf 文件 将以下内容写入
input {
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
}
}
output {
elasticsearch {
hosts => "es:9200"
index => "boutique-logstash-%{+YYYY.MM.dd}"
#user => elastic #用户名
#password => "neihan241.." #es 配置了密码 把这个打开
}
}
docker-compose up -d 进行启动
设置Elasticsearch密码登录(测试环境下可忽略)
docker exec -it elk_elasticsearch /bin/bash #进入ES容器内部
cd bin
elasticsearch-setup-passwords interactive #执行 设置六个账号密码
依次设置用户:elastic、apm_system、kibana_system、logstash_system、beats_system、remote_monitoring_user共6个用户。
Kibana汉化,以及设置安全登录
docker exec -it elk_kibana /bin/bash # 进入容器内部
cd config #进入配置文件夹 因为在启动的时候没有进行文件挂载需要进入到容器内部进行汉化
vi kibana.yml
i18n.locale: "zh-CN"
elasticsearch.username: elastic
elasticsearch.password: elastic
重启 Kibana 、 Elasticsearch
docker restart elk_kibann
docker restart elk_elasticsearch
如果设置了密码,当然logstash 也要做一些改变
上面的配置文件注释的打开即可(填写自己的密码)
SpringBoot日志集成
当然版本号啥的自行百度
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.2</version>
</dependency>
在resources下创建logback-spring.xml文件(注意更改自己的IP)
可自行百度 寻找合适的logback-spring.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<springProperty scope="context" name="springAppName"
source="spring.application.name"/>
<property name="LOG_FILE" value="${BUILD_FOLDER:-build}/${springAppName}"/>
<property name="CONSOLE_LOG_PATTERN"
value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
<appender name="logstash"
class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>你自己的IP:4560</destination>
<encoder
class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<timeZone>UTC</timeZone>
</timestamp>
<pattern>
<pattern>
{
"severity": "%level",
"service": "${springAppName:-}",
"trace": "%X{X-B3-TraceId:-}",
"span": "%X{X-B3-SpanId:-}",
"exportable": "%X{X-Span-Export:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger{40}",
"rest": "%message"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="console"/>
<appender-ref ref="logstash"/>
</root>
</configuration>
启动服务
localohost:5601 进入Kibann可视化界面
然后创建索引模式
索引名称可在配置文件中更改 下一步就完事了
这样就可以查看到日志了!!!
默认的用户名elastic
|