首页 > 解决方案 > 使用 http 插件的两个 fluentd 实例之间的通信因请求错误而失败

问题描述

我在使用 http_out 和 http_in 插件的 fluentds 之间进行通信时遇到问题。我有两个 kubernetes 集群,我希望第一个集群中的 fluentd-A 将其日志发送到第二个集群中的 fluentd-B。但是当 fluentd-A 尝试发送它收到的日志时:

#0 got unrecoverable error in primary and no secondary error_class=Fluent::UnrecoverableError error="400 Bad Request 400 Bad Request\n'json' or 'msgpack' parameter is required\n"
#0 bad chunk is moved to /tmp/fluent/backup/worker0/object_3ff8a73edae8/5a71a08ca19b1b343c8dce1b74c9a963.log

当我 cat 此文件时,其中的日志具有以下模式:

{log: ...., ... other json fields... }
{log: ...., ... other json fields... }
{log: ...., ... other json fields... }

我认为问题在于 json 对象的连接不是 json,这就是为什么 fluentd-B 作为响应 400 Bad Request 发送的原因。

我已经用 curl 测试了 fluentd-B 并提出了以下请求,它工作正常:

curl -X POST -d '{"foo":"bar"}' -H 'Content-Type: application/json' http://<fluentd-B-ip>:9880/app.log
curl -X POST -d 'json=[{"foo":"bar"},{"abc":"def"},{"xyz":"123"}]' http://<fluentd-B-ip>:9880/app.log

有人能告诉我如何将 fluentd-A 中的日志打包成有效的 json 吗?(不一定是json)

这是 fluentd-A 的配置:

<match **>
  @type http

  endpoint http://<Fluentd-B-IP>:9880/api.log
  open_timeout 2
  <format>
    @type json
  </format>
</match>

<match fluent.**>
  @type null
</match>

<source>
  @type tail
  @id in_tail_container_logs
  path /var/log/containers/*.log
  pos_file /var/log/fluentd-containers.log.pos
  tag "#{ENV['FLUENT_CONTAINER_TAIL_TAG'] || 'kubernetes.*'}"
  exclude_path "#{ENV['FLUENT_CONTAINER_TAIL_EXCLUDE_PATH'] || use_default}"
  read_from_head true
  <parse>
    @type "#{ENV['FLUENT_CONTAINER_TAIL_PARSER_TYPE'] || 'json'}"
    time_format %Y-%m-%dT%H:%M:%S.%NZ
  </parse>
</source>

<filter kubernetes.**>
  @type kubernetes_metadata
  @id filter_kube_metadata
  kubernetes_url "#{ENV['FLUENT_FILTER_KUBERNETES_URL'] || 'https://' + ENV.fetch('KUBERNETES_SERVICE_HOST') + ':' + ENV.fetch('KUBERNETES_SERVICE_PORT') + '/api'}"
  verify_ssl "#{ENV['KUBERNETES_VERIFY_SSL'] || true}"
  ca_file "#{ENV['KUBERNETES_CA_FILE']}"
</filter>

<filter kubernetes.**>
  @type grep
  <regexp>
    key $.kubernetes.namespace_name
    pattern /test/
  </regexp>
</filter>

这是 fluentd-B 的配置:

<source>
  @type http
  port 9880
  bind 0.0.0.0
  body_size_limit 32m
  keepalive_timeout 10s
  <parse>
    @type json
  </parse>

</source>

我希望有人可以帮助解决这个问题,我虽然这样的基本设置应该很容易,但也许我错过了一些重要的东西......

标签: kubernetesfluentd

解决方案


我在 fluentd github 页面中问了同样的问题,结果发现不支持与 http_in 和 http_out 的通信。它们用于与其他类型的组件进行通信,建议使用 forward_in 和 forward_out 插件在 fluentds 之间进行通信。

这是我的问题的链接。


推荐阅读