EFK之filebeat线上使用方法个人线上应用及参考地址--我带你入门到成F神

Filebeat是一个轻量级的托运人,用于转发和集中日志数据。Filebeat作为代理安装在服务器上,监视您指定的日志文件或位置,收集日志事件,并将它们转发到Elasticsearch或 Logstash进行索引。

以下是Filebeat的工作原理:启动Filebeat时,它会启动一个或多个输入,这些输入将查找您为日志数据指定的位置。对于Filebeat找到的每个日志,Filebeat启动一个收集器。每个收集器为新内容读取单个日志,并将新日志数据发送到libbeat,libbeat聚合事件并将聚合数据发送到您为Filebeat配置的输出。
EFK之filebeat线上使用方法个人线上应用及参考地址--我带你入门到成F神

Filebeat是一个Beat,它基于libbeat框架。

Step 1: Install Filebeat

要下载并安装Filebeat,请使用适用于您的系统的命令:
DEB:

curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.4-amd64.deb
sudo dpkg -i filebeat-6.5.4-amd64.deb~~

转:

curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.4-x86_64.rpm
sudo rpm -vi filebeat-6.5.4-x86_64.rpm

苹果电脑:

curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.4-darwin-x86_64.tar.gz
tar xzvf filebeat-6.5.4-darwin-x86_64.tar.gz

Linux的:

curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.4-linux-x86_64.tar.gz
tar xzvf filebeat-6.5.4-linux-x86_64.tar.gz

第二步、配置Filebeat
要配置Filebeat,请编辑配置文件。调用默认配置文件 filebeat.yml。文件的位置因平台而异。
还有一个名为的完整示例配置文件filebeat.reference.yml ,显示了所有未弃用的选项
(注意主配置文件filebeat.yml可以不唯一,启动时可以制定自己写的yml)
以下是filebeat该filebeat.yml文件部分的示例。Filebeat使用大多数配置选项的预定义默认值。

filebeat.inputs:
-  type:log 
  enabled:true 
  paths:
    - 
    / var / log / * 。log # -  c:\ programdata \ elasticsearch \ logs \ *

配置Filebeat:

定义日志文件的路径(或路径)。

对于最基本的Filebeat配置,您可以使用单个路径定义单个输入。例如:

filebeat.inputs:
-  type:log 
  enabled:true 
  paths:
    -  /var/log/*.log

此示例中的输入收集路径中的所有文件/var/log/*.log,这意味着Filebeat将收集目录/var/log/结尾的所有文件.log。

要从预定义级别的子目录中获取所有文件,可以使用以下模式: /var/log//.log。这.log将从子文件夹中获取所有文件/var/log。它不从/var/log文件夹本身获取日志文件。目前,无法以递归方式获取目录的所有子目录中的所有文件。
配置输出。Filebeat支持各种 输出,但通常您可以将事件直接发送到Elasticsearch,也可以发送到Logstash以进行其他处理。

要将输出直接发送到Elasticsearch(不使用Logstash),请设置Elasticsearch安装的位置:

  • 如果您 在Elastic Cloud上运行我们 托管的Elasticsearch Service,请指定您的Cloud ID。例如:

cloud.id:“staging:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw ==”

  • 如果您在自己的硬件上运行Elasticsearch,请设置Filebeat可以找到Elasticsearch安装的主机和端口。例如:
output.elasticsearch:
  hosts:[“myEShost:9200”]

如果您计划使用随Filebeat提供的示例Kibana仪表板,请配置Kibana端点。如果Kibana与Elasticsearch在同一主机上运行,​​则可以跳过此步骤。

setup.kibana:
  host:“mykibanahost:5601”

例如,运行Kibana的计算机的主机名和端口mykibanahost:5601。如果在端口号后面指定路径,请包括方案和端口:http://mykibanahost:5601/path

如果要使用Logstash对Filebeat收集的数据执行其他处理,则需要配置Filebeat以使用Logstash。
第三步
要执行此操作,请编辑Filebeat配置文件以通过注释掉它来禁用Elasticsearch输出,并通过取消注释logstash部分来启用Logstash输出:

#----------------------------- Logstash输出------------------ --------------

output.logstash:
  hosts: ["127.0.0.1:5044"]

该hosts选项指定Logstash服务器和port(5044),其中Logstash配置为侦听传入的Beats连接。

对于此配置,您必须手动将索引模板加载到Elasticsearch中, 因为自动加载模板的选项仅适用于Elasticsearch输出。

第四步 在elasticsearch中加载索引模板

在Elasticsearch中,索引模板用于定义确定如何分析字段的设置和映射。

Filebeat的推荐索引模板文件由Filebeat包安装。如果接受filebeat.yml配置文件中的默认配置,则 Filebeat会在成功连接到Elasticsearch后自动加载模板。如果模板已存在,则除非您配置Filebeat,否则不会覆盖该模板。

配置模板加载编辑
默认情况下,fields.yml如果启用了Elasticsearch输出,Filebeat会自动加载推荐的模板文件 。如果要使用默认索引模板,则不需要其他配置。否则,您可以将filebeat.yml配置文件中的默认值更改为:

  • 加载不同的模板
setup.template.name:“your_template_name”
setup.template.fields:“path / to / fields.yml”

如果模板已存在,则除非您配置Filebeat,否则不会覆盖该模板。

  • 覆盖现有模板

setup.template.overwrite:true

* 禁用自动模板加载

setup.template.enabled:false


*  更改索引名称

默认情况下,Filebeat将事件写入命名的索引 filebeat-6.5.4-yyyy.MM.dd,其中yyyy.MM.dd是事件索引的日期。要使用其他名称,请index在Elasticsearch输出中设置该 选项。您指定的值应包括索引的根名称以及版本和日期信息。您还需要配置setup.template.name和 setup.template.pattern选项以匹配新名称。例如:

output.elasticsearch.index:“customname - %{[beat.version]} - %{+ yyyy.MM.dd}”
setup.template.name:“customname”
setup.template.pattern:“customname- *”

如果您使用的是预先构建的Kibana仪表板,请同时设置该 setup.dashboards.index选项。例如:

`setup.dashboards.index:“customname- *”``



**手动加载模板编辑**
要手动加载模板,请运行该setup命令。需要连接到Elasticsearch。如果启用了另一个输出,则需要暂时​​禁用该输出并使用该-E选项启用Elasticsearch 。此处的示例假定已启用Logstash输出。-E如果已启用Elasticsearch输出,则可以省略标志。

如果要连接到安全的Elasticsearch集群,请确保已按照步骤2:配置Filebeat中所述配置凭据。

如果运行Filebeat的主机没有与Elasticsearch的直接连接,请参阅手动加载模板(备用方法)。

要加载模板,请使用适用于您系统的命令。

deb和rpm:

> filebeat setup --template -E output.logstash.enabled = false -E'output.elasticsearch.hosts = [“localhost:9200”]'

苹果电脑:

> ./filebeat setup --template -E output.logstash.enabled = false -E'output.elasticsearch.hosts = [“localhost:9200”]'

Linux的:

./filebeat setup --template -E output.logstash.enabled = false -E'output.elasticsearch.hosts = [“localhost:9200”]'
docker:

> docker run docker.elastic.co/beats/filebeat:6.5.4 setup --template -E output.logstash.enabled = false -E'output.elasticsearch.hosts = [“localhost:9200”]'

win:

以管理员身份打开PowerShell提示符(右键单击PowerShell图标,然后选择“以管理员身份运行”)。

在PowerShell提示符下,切换到Filebeat的安装目录,然后运行:

> PS>。\ filebeat.exe setup --template -E output.logstash.enabled = false -E'output.elasticsearch.hosts = [“localhost:9200”]'

强制Kibana查看最新的文档编辑
如果您已经使用Filebeat将数据索引到Elasticsearch,则索引可能包含旧文档。加载索引模板后,您可以删除旧文档filebeat-*以强制Kibana查看最新文档。

使用此命令:

deb和rpm:

> curl -XDELETE'http:// localhost:9200 / filebeat- *'

苹果电脑:

> curl -XDELETE'http:// localhost:9200 / filebeat- *'

Linux的:

> curl -XDELETE'http:// localhost:9200 / filebeat- *'

win:

> PS> Invoke-RestMethod -Method删除“http:// localhost:9200 / filebeat- *”

此命令删除与模式匹配的所有索引filebeat-*。在运行此命令之前,请确保要删除与该模式匹配的所有索引。

手动加载模板(备用方法)编辑
If the host running Filebeat does not have direct connectivity to Elasticsearch, you can export the index template to a file, move it to a machine that does have connectivity, and then install the template manually.

To export the index template, run:

deb and rpm:

> filebeat export template > filebeat.template.json

mac:

> ./filebeat export template > filebeat.template.json

linux:

> ./filebeat export template > filebeat.template.json

win:

> PS > .\filebeat.exe export template --es.version 6.5.4 | Out-File -Encoding UTF8 filebeat.template.json
> To install the template, run:

deb and rpm:

> curl -XPUT -H 'Content-Type: application/json' http://localhost:9200/_template/filebeat-6.5.4 [email protected]

mac:

> curl -XPUT -H 'Content-Type: application/json' http://localhost:9200/_template/filebeat-6.5.4 [email protected]

linux:

> curl -XPUT -H 'Content-Type: application/json' http://localhost:9200/_template/filebeat-6.5.4 [email protected]

win:

> PS > Invoke-RestMethod -Method Put -ContentType "application/json" -InFile filebeat.template.json -Uri http://localhost:9200/_template/filebeat-6.5.4

好了今天入门就到这里了,改天整理文档的时候继续。

猜你喜欢

转载自blog.51cto.com/13120271/2333399