9.1 安装 filebeat

  1. 下载 filebeat

https://www.elastic.co/cn/downloads/beats/filebeat,下载 zip 版本
image.png

  1. 解压 zip 包,以管理员身份启动 PowerShell,进入目录,输入以下命令将 Filebeat 注册为 windows 服务

    1. .\install-service-filebeat.ps1

    image.png
    如果在系统上禁用了脚本执行,则需要为当前会话设置执行策略以允许脚本运行。例如:

    1. PowerShell.exe -ExecutionPolicy UnRestricted -File .\install-service-filebeat.ps1.

    9.2 连接 Elastic Stack

  2. 打开 filebeat.yml,设置连接 elasticsearch 相关配置,当您首次启动 Elasticsearch 时,默认情况下会启用 Elasticsearch 的网络加密(TLS)等安全功能。如果您使用的是 Elasticsearch 首次启动时生成的自签名证书,则需要在此处添加其指纹。指纹打印在 Elasticsearch 启动日志上。 ```yaml

    ================================== Outputs ===================================

Configure what output to use when sending the data collected by the beat.

—————————————— Elasticsearch Output ——————————————

output.elasticsearch:

Array of hosts to connect to.

hosts: [“https://172.18.1.31:1001","https://172.18.1.31:1002","https://172.18.1.31:1003“]

Protocol - either http (default) or https.

protocol: “https”

Authentication credentials - either API key or username/password.

api_key: “id:api_key”

username: “elastic” password: “5_QD9o4D-VZn0dUJDO=c” ssl: enabled: true ca_trusted_fingerprint: “22e8f9824c14c392fe403fe81f4a21790ce1026eebca02bb0a348c4038884483”

  1. 2. 设置连接 kibana 相关配置
  2. ```yaml
  3. # =================================== Kibana ===================================
  4. # Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
  5. # This requires a Kibana endpoint configuration.
  6. setup.kibana:
  7. # Kibana Host
  8. # Scheme and port can be left out and will be set to the default (http and 5601)
  9. # In case you specify and additional path, the scheme is required: http://localhost:5601/path
  10. # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
  11. host: "172.18.1.31:5601"
  12. username: "elastic"
  13. password: "5_QD9o4D-VZn0dUJDO=c"
  14. # Kibana Space ID
  15. # ID of the Kibana Space into which the dashboards should be loaded. By default,
  16. # the Default Space will be used.
  17. #space.id:
  1. 设置输入相关配置 ```yaml

    ============================== Filebeat inputs ===============================

filebeat.inputs:

Each - is an input. Most options can be set at the input level, so

you can use different inputs for various configurations.

Below are the input specific configurations.

filestream is an input for collecting log messages from files.

  • type: filestream

    Unique ID among all inputs, an ID is required.

    id: my-filestream-id

    Change to true to enable this input configuration.

    enabled: true

    Paths that should be crawled and fetched. Glob based paths.

    paths:

    - /var/log/*.log

    • c:\programdata\elasticsearch\logs*

    Exclude lines. A list of regular expressions to match. It drops the lines that are

    matching any regular expression from the list.

    exclude_lines: [‘^DBG’]

    Include lines. A list of regular expressions to match. It exports the lines that are

    matching any regular expression from the list.

    include_lines: [‘^ERR’, ‘^WARN’]

    Exclude files. A list of regular expressions to match. Filebeat drops the files that

    are matching any regular expression from the list. By default, no files are dropped.

    prospector.scanner.exclude_files: [‘.gz$’]

    Optional additional fields. These fields can be freely picked

    to add additional information to the crawled log files for filtering

    fields:

    level: debug

    review: 1

    ```

    9.3 启用和配置指标收集模块

  1. 确定需要启用的模块。要查看可用模块的列表,请运行:

    1. .\filebeat.exe modules list
  2. 启用 elasticsearch 模块,启动后,目录 modules.d 中的 elasticsearch.yml 后缀 disable会自行取消,变得可以配置

    1. .\filebeat.exe modules enable elasticsearch
  3. 修改 elasticsearch.yml ```yaml

    Module: elasticsearch

    Docs: https://www.elastic.co/guide/en/beats/filebeat/8.2/filebeat-module-elasticsearch.html

  • module: elasticsearch

    Server log

    server: enabled: true

    Set custom paths for the log files. If left empty,

    Filebeat will choose the paths depending on your OS.

    var.paths:

    1. - D:\elasticsearch-cluster\node-1001\logs\*_server.json
    2. - D:\elasticsearch-cluster\node-1002\logs\*_server.json
    3. - D:\elasticsearch-cluster\node-1003\logs\*_server.json

    gc: enabled: true

    Set custom paths for the log files. If left empty,

    Filebeat will choose the paths depending on your OS.

    var.paths:

    • D:\elasticsearch-cluster\node-1001\logs\gc.log.[0-9]*
    • D:\elasticsearch-cluster\node-1002\logs\gc.log.[0-9]*
    • D:\elasticsearch-cluster\node-1003\logs\gc.log.[0-9]*

    audit: enabled: true

    Set custom paths for the log files. If left empty,

    Filebeat will choose the paths depending on your OS.

    var.paths:

    • D:\elasticsearch-cluster\node-1001\logs*_audit.json
    • D:\elasticsearch-cluster\node-1002\logs*_audit.json
    • D:\elasticsearch-cluster\node-1003\logs*_audit.json

    slowlog: enabled: true

    Set custom paths for the log files. If left empty,

    Filebeat will choose the paths depending on your OS.

    var.paths:

    • D:\elasticsearch-cluster\node-1001\logs*_index_search_slowlog.json
    • D:\elasticsearch-cluster\node-1001\logs*_index_indexing_slowlog.json
    • D:\elasticsearch-cluster\node-1002\logs*_index_search_slowlog.json
    • D:\elasticsearch-cluster\node-1002\logs*_index_indexing_slowlog.json
    • D:\elasticsearch-cluster\node-1003\logs*_index_search_slowlog.json
    • D:\elasticsearch-cluster\node-1003\logs*_index_indexing_slowlog.json

    deprecation: enabled: true

    Set custom paths for the log files. If left empty,

    Filebeat will choose the paths depending on your OS.

    var.paths:

    • D:\elasticsearch-cluster\node-1001\logs*_deprecation.json
    • D:\elasticsearch-cluster\node-1002\logs*_deprecation.json
    • D:\elasticsearch-cluster\node-1003\logs*_deprecation.json
      1. <a name="oY1aZ"></a>
      2. # 9.4 加载索引模板供 Kibana 可视化
      3. 在 kibana 运行的情况下,从安装目录中,运行:
      4. ```bash
      5. .\filebeat.exe setup -e
      -e 的含义:日志直接命令行输出,而非日志。

      9.5 启动 filebeat

      等上一步执行成功后,再启动 filebeat
      1. .\filebeat.exe -e
      至此,整个 filebeat 就全部配置完毕,登录 kibana,选择“堆栈监测”,看到所有节点的日志
      image.png
      image.png
      image.png
      【注意】
  1. 以上演示为在集群状态下,一台物理机部署三个节点的情况,如果在多台物理机部署,则需要在每个节点部署一个 filebeat,分别连接各自对应的节点即可
  2. 启动成功后,下次可以从 windows 服务启动和关闭 filebeat
  3. 默认情况下,Windows 日志文件存储在 C:\ProgramData\filebeat\Logs 中