如何将apache日志从一个容器发送到另一个容器中的logstash?

在过去的三天里,我一直在试图从我在Docker中的容器中收集所有日志,并将它们发送到Logstash。 我一直在使用ELK堆栈(Elasticsearch,Logstash和Kibana),我使用Logspout作为这个日志的路由器。

ELK Stack的所有三个实例都在不同的容器中运行。 我已经按照这个设置 。

我目前的Logstashconfiguration文件如下所示:

input { tcp { port => 5000 type => syslog } udp { port => 5000 type => syslog } } filter { if [type] == "syslog" { grok { match => { "message" => "%{SYSLOG5424PRI}%{NONNEGINT:ver} +(?:%{TIMESTAMP_ISO8601:ts}|-) +(?:%{HOSTNAME:containerid}|-) +(?:%{NOTSPACE:containername}|-) +(?:%{NOTSPACE:proc}|-) +(?:%{WORD:msgid}|-) +(?:%{SYSLOG5424SD:sd}|-|) +%{GREEDYDATA:msg}" } } syslog_pri { } date { match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ] } if !("_grokparsefailure" in [tags]) { mutate { replace => [ "@source_host", "%{syslog_hostname}" ] replace => [ "@message", "%{syslog_message}" ] } } mutate { remove_field => [ "syslog_hostname", "syslog_message", "syslog_timestamp" ] } } } output { elasticsearch { host => "elasticsearch" } stdout { codec => rubydebug } } 

我目前的问题是,我logging几乎所有必要的事件,除了从我的apache2容器称为laravel2的错误和访问日志。 它从容器中logging一些事件,但不是所有事情。 如果我通过更改index.php文件产生错误,它将不会在elasticsearch中正确logging。

我需要什么types的configuration才能获得这个Apache日志(访问和错误)? 我已经看到了一些解决scheme,但他们有一个input文件,我不能做,因为我在不同的容器中运行的东西。

编辑:

我的新logstash.sample.conf文件:

 input { tcp { port => 5000 type => syslog } udp { port => 5000 type => syslog } beats { # The port to listen on for filebeat connections. port => 5044 # The IP address to listen for filebeat connections. host => "0.0.0.0" type => apachelog } } filter { if [type] == "apachelog" { grok { match => { "message" => ["%{IPORHOST:[apache2][access][remote_ip]} - %{DATA:[apache2][access][user_name]} \[%{HTTPDATE:[apache2][access][time]}\] \"%{WORD:[apache2][access][method]} %{DATA:[apache2][access][url]} HTTP/%{NUMBER:[apache2][access][http_version]}\" %{NUMBER:[apache2][access][response_code]} %{NUMBER:[apache2][access][body_sent][bytes]}( \"%{DATA:[apache2][access][referrer]}\")?( \"%{DATA:[apache2][access][agent]}\")?", "%{IPORHOST:[apache2][access][remote_ip]} - %{DATA:[apache2][access][user_name]} \\[%{HTTPDATE:[apache2][access][time]}\\] \"-\" %{NUMBER:[apache2][access][response_code]} -" ] } remove_field => "message" } mutate { add_field => { "read_timestamp" => "%{@timestamp}" } } date { match => [ "[apache2][access][time]", "dd/MMM/YYYY:H:m:s Z" ] remove_field => "[apache2][access][time]" } useragent { source => "[apache2][access][agent]" target => "[apache2][access][user_agent]" remove_field => "[apache2][access][agent]" } geoip { source => "[apache2][access][remote_ip]" target => "[apache2][access][geoip]" } } if [type] == "syslog" { grok { match => { "message" => "%{SYSLOG5424PRI}%{NONNEGINT:ver} +(?:%{TIMESTAMP_ISO8601:ts}|-) +(?:%{HOSTNAME:containerid}|-) +(?:%{NOTSPACE:containername}|-) +(?:%{NOTSPACE:proc}|-) +(?:%{WORD:msgid}|-) +(?:%{SYSLOG5424SD:sd}|-|) +%{GREEDYDATA:msg}" } } syslog_pri { } date { match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ] } if !("_grokparsefailure" in [tags]) { mutate { replace => [ "@source_host", "%{syslog_hostname}" ] replace => [ "@message", "%{syslog_message}" ] } } mutate { remove_field => [ "syslog_hostname", "syslog_message", "syslog_timestamp" ] } } } output { elasticsearch { host => "elasticsearch" } stdout { codec => rubydebug } } 

和我的filebeat.full.yml文件:

 #----------------------------- Logstash output --------------------------------- #output.logstash: # Boolean flag to enable or disable the output module. enabled: true # The Logstash hosts hosts: ["localhost:5044"] 

为了完成这个,我的filebeat.yml文件:

 filebeat.prospectors: - input_type: log paths: - /var/log/apache2/access.log* - /var/log/apache2/other_vhosts_access.log* exclude_files: [".gz$"] output.logstash: hosts: ["localhost:5044"] processors: - add_cloud_metadata: output.elasticsearch: hosts: ['elasticsearch:9200'] username: elastic password: changeme 

可能,您的apache2容器只能将访问和错误logging到stdout。

一个选项是添加另一个运行filebeat容器,configuration为将数据推送到logstash(您还需要调整logstashconfiguration),在你的apache容器和这个新的容器之间build立一个共享卷,最后使apache写入日志在泄漏的音量。

看看这个链接如何在docker上运行filebeat

看看这个链接如何configurationfilebeat发送数据到logstash

最后, 在这里查看启用logstash从filebeat接收数据

首先,你需要创build一个共享卷:

 docker volume create --name apache-logs 

之后,你可以像这样运行你的docker容器:

 docker run -v apache-logs:/var/log/apache2 ... apache:version docker run -v apache-logs:/var/log/apache2 ... filebeat:version 

这样2容器将有一个共享目录。 您需要调整apache以便将日志写入/ var / log / apache2并设置filebeat,以便将数据从/ var / log / apache2转发到logstash。