首页 > 解决方案 > 将 filebeat 日志发送到 logstash 以使用 docker 元数据进行索引

问题描述

我试图在 filebeat 和 logstash 的帮助下在 elastichsearch 中建立索引。这是 filebeat.yml :

filebeat.inputs:
- type: docker
  combine_partial: true
  containers:
    path: "/usr/share/dockerlogs/data"
    stream: "stdout"
    ids:
      - "*"
  exclude_files: ['\.gz$']
  ignore_older: 10m

processors:
  # decode the log field (sub JSON document) if JSON encoded, then maps it's fields to elasticsearch fields
- decode_json_fields:
    fields: ["log", "message"]
    target: ""
    # overwrite existing target elasticsearch fields while decoding json fields
    overwrite_keys: true
- add_docker_metadata:
    host: "unix:///var/run/docker.sock"

filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false

# setup filebeat to send output to logstash
output.logstash:
  hosts: ["xxx.xx.xx.xx:5044"]

# Write Filebeat own logs only to file to avoid catching them with itself in docker log files
logging.level: info
logging.to_files: false
logging.to_syslog: false
loggins.metrice.enabled: false
logging.files:
  path: /var/log/filebeat
  name: filebeat
  keepfiles: 7
  permissions: 0644
ssl.verification_mode: none

这是logstash.conf:

input
  {
    beats {
      port => 5044
      host => "0.0.0.0"
    }
  }

output
  {
    stdout {
      codec => dots
    }
    elasticsearch {
      hosts => "http://xxx.xx.xx.x:9200"
      index => "%{[docker][container][labels][com][docker][swarm][service][name]}-%{+xxxx.ww}"
    }
  }

我正在尝试使用 docker 名称进行索引,因此它比我们一直看到的通常模式(如“filebeat-xxxxxx.some-date”)更具可读性和更清晰。我尝试了几件事:

- index => "%{[docker][container][labels][com][docker][swarm][service][name]}-%{+xxxx.ww}"
- index => "%{[docker][container][labels][com][docker][swarm][service][name]}-%{+YYYY.MM}"
- index => "%{[docker][swarm][service][name]}-%{+xxxx.ww}"

但没有任何效果。我究竟做错了什么 ?也许我做错了什么或在 filebeat.yml 文件中丢失了东西。也可能是这样。感谢您的任何帮助或任何领导。

标签: elasticsearchlogstashfilebeat

解决方案


看起来您不确定要添加哪些 docker 元数据字段。最好先使用默认索引名称(例如“filebeat-xxxxxx.some-date”或其他)成功建立索引,然后查看日志事件以查看 docker 元数据字段的格式。

我没有和你一样的设置,但作为参考,我在 AWS ECS 上,所以我的 docker 字段的格式是:

"docker": {
  "container": {
    "name": "",
    "labels": {
      "com": {
        "amazonaws": {
          "ecs": {
            "cluster": "",
            "container-name": "",
            "task-definition-family": "",
            "task-arn": "",
            "task-definition-version": ""
          }
        }
      }
    },
    "image": "",
    "id": ""
  }
}

在看到可用的格式和字段后,我可以使用上面的方法添加一个自定义的“application_name”字段。这个字段是在我的输入插件中生成的,在我的例子中是 redis,但所有输入插件都应该有 add_field 选项(https://www.elastic.co/guide/en/logstash/current/plugins-inputs-beats. html#plugins-inputs-beats-add_field):

input {
  redis {
    host => "***"
    data_type => "list"
    key       => "***"
    codec     => json
    add_field => {
      "application_name" => "%{[docker][container][labels][com][amazonaws][ecs][task-definition-family]}"
    }
  }
}

获得这个新的自定义字段后,我能够为不同的“application_name”字段运行特定的过滤器(grok、json、kv 等),因为它们具有不同的日志格式,但对您来说重要的部分是您可以使用它您对 Elasticsearch 的输出以获取索引名称:

output {
  elasticsearch {
      user => ***
      password => ***
      hosts => [ "***" ]
      index => "logstash-%{application_name}-%{+YYY.MM.dd}"
  }
}

推荐阅读