|
| 1 | +# Filebeat 运维 |
| 2 | + |
| 3 | +> Beats 平台集合了多种单一用途数据采集器。它们从成百上千或成千上万台机器和系统向 Logstash 或 Elasticsearch 发送数据。 |
| 4 | +> |
| 5 | +> 因为我只接触过 Filebeat,所有本文仅介绍 Filebeat 的日常运维。 |
| 6 | +
|
| 7 | +## 1. Filebeat 安装 |
| 8 | + |
| 9 | +### 1.1. 环境要求 |
| 10 | + |
| 11 | +> 版本:Elastic Stack 7.4 |
| 12 | +
|
| 13 | +### 1.2. 安装步骤 |
| 14 | + |
| 15 | +Unix / Linux 系统建议使用下面方式安装,因为比较通用。 |
| 16 | + |
| 17 | +``` |
| 18 | +wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.1.1-linux-x86_64.tar.gz |
| 19 | +tar -zxf filebeat-6.1.1-linux-x86_64.tar.gz |
| 20 | +``` |
| 21 | + |
| 22 | +> 更多内容可以参考:[filebeat-installation](https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-installation.html) |
| 23 | +
|
| 24 | +## 2. Filebeat 配置 |
| 25 | + |
| 26 | +> 首先,必须要知道的是:`filebeat.yml` 是 filebeat 的配置文件。其路径会因为你安装方式而有所不同。 |
| 27 | +> |
| 28 | +> Beat 所有系列产品的配置文件都基于 [YAML](http://www.yaml.org/) 格式,FileBeat 当然也不例外。 |
| 29 | +> |
| 30 | +> 更多 filebeat 配置内容可以参考:[配置 filebeat](https://www.elastic.co/guide/en/beats/filebeat/current/configuring-howto-filebeat.html) |
| 31 | +> |
| 32 | +> 更多 filebeat.yml 文件格式内容可以参考:[filebeat.yml 文件格式](https://www.elastic.co/guide/en/beats/libbeat/6.1/config-file-format.html) |
| 33 | +
|
| 34 | +filebeat.yml 部分配置示例: |
| 35 | + |
| 36 | +```yml |
| 37 | +filebeat: |
| 38 | + prospectors: |
| 39 | + - type: log |
| 40 | + paths: |
| 41 | + - /var/log/*.log |
| 42 | + multiline: |
| 43 | + pattern: '^[' |
| 44 | + match: after |
| 45 | +``` |
| 46 | +
|
| 47 | +### 2.1. 重要配置项 |
| 48 | +
|
| 49 | +> 下面我将列举 Filebeat 的较为重要的配置项。 |
| 50 | +> |
| 51 | +> 如果想了解更多配置信息,可以参考: |
| 52 | +> |
| 53 | +> 更多 filebeat 配置内容可以参考:[配置 filebeat](https://www.elastic.co/guide/en/beats/filebeat/current/configuring-howto-filebeat.html) |
| 54 | +> |
| 55 | +> 更多 filebeat.yml 文件格式内容可以参考:[filebeat.yml 文件格式](https://www.elastic.co/guide/en/beats/libbeat/6.1/config-file-format.html) |
| 56 | +
|
| 57 | +#### 2.1.1. filebeat.prospectors |
| 58 | +
|
| 59 | +(文件监视器)用于指定需要关注的文件。 |
| 60 | +
|
| 61 | +**示例** |
| 62 | +
|
| 63 | +```yaml |
| 64 | +filebeat.prospectors: |
| 65 | + - type: log |
| 66 | + enabled: true |
| 67 | + paths: |
| 68 | + - /var/log/*.log |
| 69 | +``` |
| 70 | +
|
| 71 | +#### 2.1.2. output.elasticsearch |
| 72 | +
|
| 73 | +如果你希望使用 filebeat 直接向 elasticsearch 输出数据,需要配置 output.elasticsearch 。 |
| 74 | +
|
| 75 | +**示例** |
| 76 | +
|
| 77 | +```yaml |
| 78 | +output.elasticsearch: |
| 79 | + hosts: ['192.168.1.42:9200'] |
| 80 | +``` |
| 81 | +
|
| 82 | +#### 2.1.3. output.logstash |
| 83 | +
|
| 84 | +如果你希望使用 filebeat 向 logstash 输出数据,然后由 logstash 再向 elasticsearch 输出数据,需要配置 output.logstash。 |
| 85 | +
|
| 86 | +> **注意** |
| 87 | +> |
| 88 | +> 相比于向 elasticsearch 输出数据,个人更推荐向 logstash 输出数据。 |
| 89 | +> |
| 90 | +> 因为 logstash 和 filebeat 一起工作时,如果 logstash 忙于处理数据,会通知 FileBeat 放慢读取速度。一旦拥塞得到解决,FileBeat 将恢复到原来的速度并继续传播。这样,可以减少管道超负荷的情况。 |
| 91 | +
|
| 92 | +**示例** |
| 93 | +
|
| 94 | +```yaml |
| 95 | +output.logstash: |
| 96 | + hosts: ['127.0.0.1:5044'] |
| 97 | +``` |
| 98 | +
|
| 99 | +此外,还需要在 logstash 的配置文件(如 logstash.conf)中指定 beats input 插件: |
| 100 | +
|
| 101 | +```yaml |
| 102 | +input { |
| 103 | + beats { |
| 104 | + port => 5044 # 此端口需要与 filebeat.yml 中的端口相同 |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +# The filter part of this file is commented out to indicate that it is |
| 109 | +# optional. |
| 110 | +# filter { |
| 111 | +# |
| 112 | +# } |
| 113 | + |
| 114 | +output { |
| 115 | + elasticsearch { |
| 116 | + hosts => "localhost:9200" |
| 117 | + manage_template => false |
| 118 | + index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}" |
| 119 | + document_type => "%{[@metadata][type]}" |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +#### 2.1.4. setup.kibana |
| 125 | + |
| 126 | +如果打算使用 Filebeat 提供的 Kibana 仪表板,需要配置 setup.kibana 。 |
| 127 | + |
| 128 | +**示例** |
| 129 | + |
| 130 | +```yaml |
| 131 | +setup.kibana: |
| 132 | + host: 'localhost:5601' |
| 133 | +``` |
| 134 | +
|
| 135 | +#### 2.1.5. setup.template.settings |
| 136 | +
|
| 137 | +在 Elasticsearch 中,[索引模板](https://www.elastic.co/guide/en/elasticsearch/reference/6.1/indices-templates.html)用于定义设置和映射,以确定如何分析字段。 |
| 138 | +
|
| 139 | +在 Filebeat 中,setup.template.settings 用于配置索引模板。 |
| 140 | +
|
| 141 | +Filebeat 推荐的索引模板文件由 Filebeat 软件包安装。如果您接受 filebeat.yml 配置文件中的默认配置,Filebeat 在成功连接到 Elasticsearch 后自动加载模板。 |
| 142 | +
|
| 143 | +您可以通过在 Filebeat 配置文件中配置模板加载选项来禁用自动模板加载,或加载自己的模板。您还可以设置选项来更改索引和索引模板的名称。 |
| 144 | +
|
| 145 | +> **参考** |
| 146 | +> |
| 147 | +> 更多内容可以参考:[filebeat-template](https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-template.html) |
| 148 | +> |
| 149 | +> **说明** |
| 150 | +> |
| 151 | +> 如无必要,使用 Filebeat 配置文件中的默认索引模板即可。 |
| 152 | +
|
| 153 | +#### 2.1.6. setup.dashboards |
| 154 | +
|
| 155 | +Filebeat 附带了示例 Kibana 仪表板。在使用仪表板之前,您需要创建索引模式 `filebeat- *`,并将仪表板加载到 Kibana 中。为此,您可以运行 `setup` 命令或在 `filebeat.yml` 配置文件中配置仪表板加载。 |
| 156 | + |
| 157 | +为了在 Kibana 中加载 Filebeat 的仪表盘,需要在 `filebeat.yml` 配置中启动开关: |
| 158 | + |
| 159 | +``` |
| 160 | +setup.dashboards.enabled: true |
| 161 | +``` |
| 162 | +
|
| 163 | +> **参考** |
| 164 | +> |
| 165 | +> 更多内容可以参考:[configuration-dashboards](https://www.elastic.co/guide/en/beats/filebeat/current/configuration-dashboards.html) |
| 166 | +
|
| 167 | +## 3. Filebeat 命令 |
| 168 | +
|
| 169 | +filebeat 提供了一系列命令来完成各种功能。 |
| 170 | +
|
| 171 | +执行命令方式: |
| 172 | +
|
| 173 | +```bash |
| 174 | +./filebeat COMMAND |
| 175 | +``` |
| 176 | + |
| 177 | +> **参考** |
| 178 | +> |
| 179 | +> 更多内容可以参考:[command-line-options](https://www.elastic.co/guide/en/beats/filebeat/current/command-line-options.html) |
| 180 | +> |
| 181 | +> **说明** |
| 182 | +> |
| 183 | +> 个人认为命令行没有必要一一掌握,因为绝大部分功能都可以通过配置来完成。且通过命令行指定功能这种方式要求每次输入同样参数,不利于固化启动方式。 |
| 184 | +> |
| 185 | +> 最重要的当然是启动命令 run 了。 |
| 186 | +> |
| 187 | +> **示例** 指定配置文件启动 |
| 188 | +> |
| 189 | +> ```bash |
| 190 | +> ./filebeat run -e -c filebeat.yml -d "publish" |
| 191 | +> ./filebeat -e -c filebeat.yml -d "publish" # run 可以省略 |
| 192 | +> ``` |
| 193 | +
|
| 194 | +## 4. Filebeat 模块 |
| 195 | +
|
| 196 | +> [Filebeat](https://www.elastic.co/cn/products/beats/filebeat) 和 [Metricbeat](https://www.elastic.co/cn/products/beats/metricbeat) 内部集成了一系列模块,用以简化常见日志格式(例如 NGINX、Apache 或诸如 Redis 或 Docker 等系统指标)的收集、解析和可视化过程。 |
| 197 | +
|
| 198 | +- 配置 elasticsearch 和 kibana |
| 199 | +
|
| 200 | +``` |
| 201 | +output.elasticsearch: |
| 202 | + hosts: ["myEShost:9200"] |
| 203 | + username: "elastic" |
| 204 | + password: "elastic" |
| 205 | +setup.kibana: |
| 206 | + host: "mykibanahost:5601" |
| 207 | + username: "elastic" |
| 208 | + password: "elastic |
| 209 | +``` |
| 210 | +
|
| 211 | +> username 和 password 是可选的,如果不需要认证则不填。 |
| 212 | +
|
| 213 | +- 初始化环境 |
| 214 | +
|
| 215 | +执行下面命令,filebeat 会加载推荐索引模板。 |
| 216 | +
|
| 217 | +``` |
| 218 | +./filebeat setup -e |
| 219 | +``` |
| 220 | +
|
| 221 | +- 指定模块 |
| 222 | +
|
| 223 | +执行下面命令,指定希望加载的模块。 |
| 224 | +
|
| 225 | +``` |
| 226 | +./filebeat -e --modules system,nginx,mysql |
| 227 | +``` |
| 228 | +
|
| 229 | +> 更多内容可以参考: |
| 230 | +> |
| 231 | +> - [配置 filebeat 模块](https://www.elastic.co/guide/en/beats/filebeat/current/configuration-filebeat-modules.html) |
| 232 | +> - [filebeat 支持模块](https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-modules.html) |
| 233 | +
|
| 234 | +## 5. 参考资料 |
| 235 | +
|
| 236 | +- [Beats 官网](https://www.elastic.co/cn/products/beats) |
| 237 | +- [Beats Github](https://github.com/elastic/beats) |
| 238 | +- [Beats 官方文档](https://www.elastic.co/guide/en/beats/libbeat/current/index.html) |
0 commit comments