ELK是Elasticsearch、Logstash、 Kibana的三款软件的整合,是一套开源的、完整的日志收集、存储以及展示监控系统。
dockerhub ELK最新镜像是8.6.2
Elasticsearch 7.17.9
Kibana 7.17.9
Logstash 7.17.9
docker pull elasticsearch:7.17.9
docker pull logstash:7.17.9
docker pull kibana:7.17.9
vim docker-compose.yml
version: '3' #compose版本
services:
elasticsearch: #服务名
image: elasticsearch:7.17.9 #镜像版本
container_name: es-elasticsearch-1 #容器名称
networks: #网络
- host
environment: #环境变量(在dockerhub上面查看)
cluster.name: 'elasticsearch' #设置集群名称为elasticsearch
discovery.type: 'single-node' #以单一节点模式启动
ES_JAVA_OPTS: '-Xms512m -Xmx512m' #设置使用jvm内存大小
xpack.security.enabled: 'true'
xpack.security.http.ssl.enabled: 'false'
ELASTIC_PASSWORD: 'es123456'
volumes:
- /opt/module/docker-compose/elk/elasticsearch/plugins:/usr/share/elasticsearch/plugins #插件文件挂载
- /opt/module/docker-compose/elk/elasticsearch/data:/usr/share/elasticsearch/data #数据文件挂载
ports:
- 9200:9200
kibana:
image: kibana:7.17.9
container_name: es-kibana-1
networks:
- host
links:
- elasticsearch:es #容器名:给容器取的别名(kibana中可以使用es访问elasticsearch)
depends_on:
- elasticsearch #kibana在elasticsearch启动之后再启动
environment:
elasticsearch.hosts: 'http://es:9200' #设置访问elasticsearch的地址
volumes:
- /opt/module/docker-compose/elk/kibana/config:/usr/share/kibana/config #配置文件挂载
ports:
- 5601:5601
logstash:
image: logstash:7.17.9
container_name: es-logstash-1
networks:
- host
volumes:
- /opt/module/docker-compose/elk/logstash/pipeline/logstash.conf:/usr/share/logstash/pipeline/logstash.conf #挂载logstash的配置文件
- /opt/module/docker-compose/elk/logstash/config/logstash.yml:/usr/share/logstash/config/logstash.yml
depends_on:
- elasticsearch #logstash在elasticsearch启动之后再启动
links:
- elasticsearch:es #容器名:给容器取的别名(logstash中可以使用es访问elasticsearch)
ports:
- 4560:4560
networks:
host:
chmod 777 /opt/module/docker-compose/elk/elasticsearch/data```
# 改变设置
sysctl -w vm.max_map_count=262144
# 使之立即生效
sysctl -p
注:这一步是为了防止启动容器时,报出如下错误:
bootstrap checks failed max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
Elasticsearch关于vm.max_map_count的解释
input{
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
}
}
output{
stdout{
codec => rubydebug
}
elasticsearch {
hosts => ["elasticsearch:9200"]
}
}
xpack.monitoring.enabled: "true"
xpack.monitoring.elasticsearch.hosts: "http://es:9200"
xpack.monitoring.elasticsearch.username: "elastic"
xpack.monitoring.elasticsearch.password: "es123456"
docker-compose -f docker-compose-dev.yml up -d
# 进入logstash容器
docker exec -it es-logstash-1 /bin/bash
# 进入bin目录
cd /bin/
# 安装插件
logstash-plugin install logstash-codec-json_lines
# 退出容器
exit
# 重启logstash服务
docker restart eslogstash-1
pom文件引入Logstash依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-loggingartifactId>
<version>2.7.5version>
dependency>
<dependency>
<groupId>net.logstash.logbackgroupId>
<artifactId>logstash-logback-encoderartifactId>
<version>7.2version>
dependency>
在application.yml中引用logback-spring配置
如果配置文件名称为:logback-spring.xml 或者 logback.xml则无需引用
logging:
config: classpath:logback.xml
logback-spring.xml的配置如下
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="TRACE"/>
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>192.168.10.110:4560destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
appender>
<springProfile name="dev,test">
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="INFO"/>
<logger name="com.jiayuan" level="DEBUG"/>
springProfile>
<springProfile name="prod">
<logger name="org.springframework.web" level="ERROR"/>
<logger name="org.springboot.sample" level="ERROR"/>
<logger name="com.jiayuan" level="ERROR"/>
springProfile>
<root level="DEBUG">
<appender-ref ref="LOGSTASH"/>
root>
configuration>
还在用命令行看日志?快用Kibana吧,可视化日志分析YYDS!
使用Docker快速搭建ELK日志分析平台