65.9K
CodeProject 正在变化。 阅读更多。
Home

配置 Logstash 中的多个 Pipeline

2020 年 6 月 19 日

CPOL

2分钟阅读

viewsIcon

22158

如何在 Logstash 中使用多个源(仅 Filebeats)创建多个 Pipeline

引言

假设您在不同的两台服务器上安装了多个 Filebeat,并且希望为每个 Filebeat 创建单独的 Pipeline,并将输出发送到 Elasticsearch 或任何文件。

背景

让我们以一个例子来说明,我们有多个服务器,例如:

  1. 数据库服务器
  2. API 服务器
  3. Web服务器
  4. Elasticsearch 服务器(此服务器将包含 Elasticsearch,我将将其视为 sink,Kibana 用于日志和数据的可视化,以及 Logstash)

现在我们希望将所有日志都存储在 Elasticsearch 中,但需要进行一些过滤和数据操作。 让我们通过下面的图表来理解这个过程。

Fig 1

现在看上面的图片,有三台服务器安装了 Filebeat,它们收集日志数据,配置完成后,logdata 将被发送到 elasticsearch。 现在问题是什么?

让我们通过一个示例 Logstash 输入来理解问题

input {
    beats {
        p => 5044
    }
}

上面的代码显示我们可以有多个 source,但对于 beats,我们只有一个。 那么我们如何划分我们的 pipeline 呢? 现在让我们来玩转 Beats。

配置 filebeat.yml 文件(用于 DB、API 和 WEB 服务器)

在记事本中打开 filebeats.yml 文件,并为所有发送到 Logstash 的日志配置您的服务器名称

filebeat.inputs:
- type: log
  fields:
    source: 'DB Server Name'
  fields_under_root: true
filebeat.inputs:
- type: log
  fields:
    source: 'API Server Name'
  fields_under_root: true
filebeat.inputs:
- type: log
  fields:
    source: 'WEB Server Name'
  fields_under_root: true</code>

既然我们完成了 Filebeat 的更改,让我们继续创建 Logstash pipeline conf 文件。

创建 Pipeline Conf 文件

我们可以配置 Logstash 中多个 pipeline 的方法有很多种,一种方法是在 pipeline.yml 文件中设置所有内容,并运行 Logstash,所有输入和输出配置都将在同一个文件上,如下面的代码所示,但这并不是理想的

pipeline.id: dblog-process
config.string: input { pipeline { address => dblog } }

第二种方法是为 beats 创建单独的 conf 文件。 在创建文件之前,让我们在 config 文件夹中创建一个名为 pipeline 的文件夹。

  1. dblogpipeline.conf
  2. apilogpipeline.conf
  3. weblogpipeline.conf

现在让我们将我们的代码放入我们的 conf 文件中。

dblogpipeline.conf

input { 
    pipeline { 
        address => dblog 
    } 
}
output {
    file {
        path => ["C:/Logs/dblog_%{+yyyy_MM_dd}.log"]
    }
}

apilogpipeline.conf

input { 
    pipeline { 
        address => apilog 
    } 
}
output {
    file {
        path => ["C:/Logs/apilog_%{+yyyy_MM_dd}.log"]
    }
}

weblogpipeline.conf

input { 
    pipeline { 
        address => weblog 
    } 
}
output {
    file {
        path => ["C:/Logs/weblog_%{+yyyy_MM_dd}.log"]
    }
}

现在我们的 conf 文件已经准备好,我们只剩下 pipeline.yml 文件的配置了。

pipeline.yml

- pipeline.id: beats-server
  config.string: |
    input { beats { port => 5044 } }
    output {
        if [source] == 'dbservername' {
          pipeline { send_to => dblog }
        } else if [source] == 'apiservername' {
          pipeline { send_to => apilog }
        } else if [source] == 'webservername' { 
          pipeline { send_to => weblog } 
        }
    }

- pipeline.id: dblog-processing
  path.config: "/Logstash/config/pipelines/dblogpipeline.conf"

- pipeline.id: apilog-processing 
  path.config: "/Logstash/config/pipelines/apilogpipeline.conf"

- pipeline.id: weblog-processing
  path.config: "/Logstash/config/pipelines/weblogpipeline.conf"

然后去运行 Logstash。 :)

历史

  • 2020 年 6 月 19 日:初始版本
© . All rights reserved.