docker-compose 部署ELK

  • Post author:
  • Post category:其他

目录结构在这里插入图片描述

docker-compose 文件

version: '3'
services:
  elasticsearch:
    image: elasticsearch:7.17.2
    container_name: elasticsearch
    restart: always
    ports:
      - 9200:9200
      - 9300:9300
    environment:
      # 设置集群名称
      cluster.name: elasticsearch
      # 以单一节点模式启动
      discovery.type: single-node
      ES_JAVA_OPTS: "-Xms512m -Xmx512m"
    volumes:
      - ./elk/elasticsearch/plugins:/usr/share/elasticsearch/plugins
      - ./elk/elasticsearch/data:/usr/share/elasticsearch/data
      - ./elk/elasticsearch/logs:/usr/share/elasticsearch/logs
  kibana:
    image: kibana:7.17.2
    container_name: kibana
    restart: always
    ports:
      - 5601:5601
    depends_on:
      # kibana在elasticsearch启动之后再启动
      - elasticsearch
    environment:
      #设置系统语言文中文
      I18N_LOCALE: zh-CN
      # 访问域名
      # SERVER_PUBLICBASEURL: https://kibana.cloud.com
    volumes:
      - ./elk/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml

  logstash:
    image: logstash:7.17.2
    container_name: logstash
    restart: always
    ports:
      - 4560:4560
    volumes:
      - ./elk/logstash/pipeline/logstash.conf:/usr/share/logstash/pipeline/logstash.conf
      - ./elk/logstash/config/logstash.yml:/usr/share/logstash/config/logstash.yml
    depends_on:
      - elasticsearch

编写kibana.yml文件

server.host: "0.0.0.0"
server.shutdownTimeout: "5s"
elasticsearch.hosts: [ "host" ]
monitoring.ui.container.elasticsearch.enabled: true

编写 logstash-conf 文件

input {
  tcp {
    mode => "server"
    host => "0.0.0.0"
    port => 4560
    codec => json_lines
  }
}
output {
  elasticsearch {
    hosts => "host"
    index => "%{[spring.application.name]}-%{+YYYY.MM.dd}"
  }
}

部署

	#授权
  	chmod 777 /elk/elasticsearch/data
  	chmod 777 /elk/elasticsearch/logs
	# 部署
	docker-compose -f docker-compose.yaml up -d

springboot 集成

pom中加入依赖

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

logback.xml 日志文件中引入

<springProperty scope="context" name="appName" source="spring.application.name"/>

    <!--输出到logstash的appender-->
<appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
    <!--可以访问的logstash日志收集端口-->
    <destination>logstash_host_port</destination>
    <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">
        <customFields>{"spring.application.name":"${appName}"}</customFields>
    </encoder>
</appender>
<root level="info">
     <appender-ref ref="logstash" />
</root>

作者声明

如有问题,欢迎指正!