仓库源文站点原文


layout: post title: "使用Docker搭建ELK日志系统" categories: Docker tags: ElasticSearch Logstash Kibana Filebeat

author: 张乘辉

ElasticSearch

下载镜像:

$ sudo docker pull elasticsearch:5.5.0

运行ElasticSearch容器:

$ sudo docker run -it -d -p 9200:9200 -p 9300:9300 \
-v ~/elasticsearch/data:/usr/share/elasticsearch/data \
--name myes elasticsearch:5.5.0

特别注意的是如果使用v6以上版本会出现jdk的错误,我们查看日志

$ docker logs -f myes

查看日志:

OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.

网上找到大概的意思是:

jdk9对elasticSearch不太友好(版本太新),必须使用JDK8,本人使用的是JDK8u152(jdk-8u152-windows-x64.exe)。如果使用JDK9,使用elasticSearch-rtf(v5.1.1),会出现下面的错误,请特别注意,elasticSearch6.0的版本则必须使用JDK9,否则官网下载的msi不能安装成功,原因还没有去仔细检查。

所以也是一个很坑爹的问题,所以我干脆直接就安装v5.5.0稳定版本吧。

Logstash

下载镜像:

$ sudo docker pull logstash:5.5.0

新建配置文件:

$ mkdir ~/logstash && cd ~/logstash
$ mkdir conf.d && cd conf.d
$ vim logstash.conf

logstash.conf:

input {

  beats {
    port => 5044 # 此端口需要与 filebeat.yml 中的端口相同
  }

  file {
    path => "/data/logs"
    # start_position => "beginning"
  }
}

filter {
  #grok {
  #  match => { "message" => "%{COMBINEDAPACHELOG}" }
  #}
  #date {
  #  match => ["timestamp", "dd/MMM/yyyy:HH:mm:ss Z"]
  #}

 grok {

    patterns_dir => "/etc/logstash/conf.d/patterns"
    match => {"message" => "%{TIMESTAMP_ISO8601:time}\S%{MYSELF:msgid}%{MYSELF:method}%{MYDATA:data}%{MYSELF:UserInfo}\S%{LOGLEVEL:level}\S%{MYSELF:thread}%{MYSELF:application}%{MYSELF:ip}"}
}
date {
     #match => [ "time", "YYYY-MM-dd HH:mm:ss,SSS" ]
     match => [ "time", "ISO8601" ]
     target => "@timestamp"
     timezone => "Asia/Phnom_Penh"
 }

}

output {

  stdout {
    codec => rubydebug
  }

  elasticsearch {
    action => "index"
    hosts => ["172.17.10.114:9200"]
    index => "%{[fields][indexname]}-%{+YYYY.MM.dd}"
  }
}

添加 patterns:

$ cd ~/logstash/conf.d
$ mkdir patterns && cd patterns
$ vim custom

custom:

MYSELF \[\S*?\]
MYDATA [\s\S]*?(?=\[User)

运行Logstash容器:

$ sudo docker run -it -d -p 5044:5044 \
-v ~/logstash/conf.d:/etc/logstash/conf.d \
-v ~/logstash/data/logs:/data/logs \
--name logstash logstash:5.5.0 \
-f /etc/logstash/conf.d/logstash.conf

Kibana

下载镜像:

$ sudo docker pull kibana:5.5.0

新建配置文件:

$ mkdir ~/kibana && cd ~/kibana
$ vim kibana.yml

kibana.yml:

server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://172.17.10.114:9200"

运行Kibana容器:

$ sudo docker run -it -d -p 5601:5601 \
-v ~/kibana:/etc/kibana \
--name kibana kibana:5.5.0

Filebeat

Filebeat需要部署在需要收集日志的服务器上。

下载镜像:

$ sudo docker pull docker.elastic.co/beats/filebeat:5.5.0

新建配置文件:

filebeat.prospectors:
- type: log
   paths:
      - ~/filebeat/logs  # 指定需要收集的日志文件的路径
fields:
   indexname: xxx # 这里填写项目名称,对应index => "%{[fields][indexname]}-%{+YYYY.MM.dd}"
output.logstash:
  hosts: ["172.17.10.114:5044"]

运行Filebeat容器:

$ sudo docker run -it -d \
-v ~/filebeat/filebeat.yml:/usr/share/filebeat/filebeat.yml \
--name filebeat docker.elastic.co/beats/filebeat:5.5.0

附上一张ELK结构流程图:

ELK