如何安装和配置 Filebeat? 用于开发/生产环境的轻量级日志转发器
已发表: 2020-05-03 在过去的几年里,我一直在使用Filebeat
它是生产应用程序中最好的lightweight
日志/数据转发器之一。
考虑一个场景,您必须将日志从一个客户端位置传输到中心位置以进行分析。 Splunk
是转发日志的替代方案之一,但成本太高。 在我看来,这太昂贵了。
这就是 Filebeat 发挥作用的地方。 它重量超轻,简单,易于设置,使用更少的内存和太高效。 Filebeat 是Elastic.co
的产品。
它很强大,不会错过任何一个节拍。 它guarantees delivery of logs
。
它已准备好所有类型的容器:
- Kubernetes
- 码头工人
使用简单的单行命令,Filebeat 可以处理来自以下任何环境的日志的收集、解析和可视化:
- 阿帕奇
- NGINX
- 系统
- MySQL
- 阿帕奇2
- 已审核
- 弹性搜索
- 代理服务器
- 伊辛加
- IIS
- iptables
- 卡夫卡
- 木花
- 日志存储
- MongoDB
- 奥斯查询
- PostgreSQL
- 雷迪斯
- 苏里卡塔
- 特拉菲克
- 和更多…
Filebeat 带有内部模块(auditd、Apache、NGINX、System、MySQL 等),可将常见日志格式的收集、解析和可视化简化为单个命令。
如何在 Linux 环境下安装 Filebeat?
如果您有以下任何问题,那么您来对地方了:
- 开始使用 Filebeat
- Filebeat 教程:入门
- 安装、配置和使用 FileBeat – Elasticsearch
- Filebeat 设置和配置示例
- 如何安装 Elasticsearch、Logstash?
- 如何在 Ubuntu 上安装 Elastic Stack?
Step-1) 安装
使用以下命令下载并提取 Filebeat 二进制文件。
Linux环境:
1 2 3 4 5 6 |
root @ localhost : ~ # curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.7.0-linux-x86_64.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 11.1M 100 11.1M 0 0 13.2M 0 -- : -- : -- -- : -- : -- -- : -- : -- 13.2M root @ localhost : ~ # tar xzvf filebeat-6.7.0-linux-x86_64.tar.gz |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
root @ localhost : ~ # cd filebeat-6.7.0-linux-x86_64/ root @ localhost : ~ / filebeat - 6.7.0 - linux - x86_64 # pwd / root / filebeat - 6.7.0 - linux - x86_64 root @ localhost : ~ / filebeat - 6.7.0 - linux - x86_64 # ls -ltra total 36720 - rw - r -- r -- 1 root root 13675 Mar 21 14 : 30 LICENSE . txt - rw - r -- r -- 1 root root 163444 Mar 21 14 : 30 NOTICE . txt drwxr - xr - x 4 root root 4096 Mar 21 14 : 31 kibana drwxr - xr - x 2 root root 4096 Mar 21 14 : 33 modules . d drwxr - xr - x 21 root root 4096 Mar 21 14 : 33 module - rw - r -- r -- 1 root root 146747 Mar 21 14 : 33 fields . yml - rw ------- 1 root root 7714 Mar 21 14 : 33 filebeat . yml - rw - r -- r -- 1 root root 69996 Mar 21 14 : 33 filebeat . reference . yml - rwxr - xr - x 1 root root 37161549 Mar 21 14 : 34 filebeat - rw - r -- r -- 1 root root 802 Mar 21 14 : 35 README . md - rw - r -- r -- 1 root root 41 Mar 21 14 : 35 . build_hash . txt drwx ------ 9 root root 4096 Mar 30 13 : 46 . . drwxr - xr - x 5 root root 4096 Mar 30 13 : 46 . |
Mac 下载:
1 2 |
curl - L - O https : //artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.7.0-darwin-x86_64.tar.gz tar xzvf filebeat - 6.7.0 - darwin - x86_64 . tar . gz |
RPM 下载:
1 2 |
curl - L - O https : //artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.7.0-x86_64.rpm sudo rpm - vi filebeat - 6.7.0 - x86_64 . rpm |
Step-2) 配置 filebeat.yml 配置文件
签filebeat.yml
文件。 它是filebeat配置文件。
这是一个简单的文件内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
root @ localhost : ~ / filebeat - 6.7.0 - linux - x86_64 # cat filebeat.yml ###################### Filebeat Configuration Example ######################### # This file is an example configuration file highlighting only the most common # options. The filebeat.reference.yml file from the same directory contains all the # supported options with more comments. You can use it as a reference. # # You can find the full configuration reference here: # https://www.elastic.co/guide/en/beats/filebeat/index.html # For more available modules and options, please see the filebeat.reference.yml sample # configuration file. #=========================== 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. - type : log # Change to true to enable this input configuration. enabled : false # 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. #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 ### Multiline options # Multiline can be used for log messages spanning multiple lines. This is common # for Java Stack Traces or C-Line Continuation # The regexp Pattern that has to be matched. The example pattern matches all lines starting with [ #multiline.pattern: ^\[ # Defines if the pattern set under pattern should be negated or not. Default is false. #multiline.negate: false # Match can be set to "after" or "before". It is used to define if lines should be append to a pattern # that was (not) matched before or after or as long as a pattern is not matched based on negate. # Note: After is the equivalent to previous and before is the equivalent to to next in Logstash #multiline.match: after #============================= Filebeat modules =============================== filebeat . config . modules : # Glob pattern for configuration loading path : $ { path . config } / modules . d /* . yml # Set to true to enable config reloading reload . enabled : false # Period on which files under path should be checked for changes #reload.period: 10s #==================== Elasticsearch template setting ========================== setup . template . settings : index . number_of_shards : 3 #index.codec: best_compression #_source.enabled: false #================================ General ===================================== # The name of the shipper that publishes the network data. It can be used to group # all the transactions sent by a single shipper in the web interface. #name: # The tags of the shipper are included in their own field with each # transaction published. #tags: ["service-X", "web-tier"] # Optional fields that you can specify to add additional information to the # output. #fields: # env: staging #============================== Dashboards ===================================== # These settings control loading the sample dashboards to the Kibana index. Loading # the dashboards is disabled by default and can be enabled either by setting the # options here, or by using the `-setup` CLI flag or the `setup` command. #setup.dashboards.enabled: false # The URL from where to download the dashboards archive. By default this URL # has a value which is computed based on the Beat name and version. For released # versions, this URL points to the dashboard archive on the artifacts.elastic.co # website. #setup.dashboards.url: #============================== Kibana ===================================== # Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API. # This requires a Kibana endpoint configuration. setup . kibana : # Kibana Host # Scheme and port can be left out and will be set to the default (http and 5601) # In case you specify and additional path, the scheme is required: http://localhost:5601/path # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601 #host: "localhost:5601" # Kibana Space ID # ID of the Kibana Space into which the dashboards should be loaded. By default, # the Default Space will be used. #space.id: #============================= Elastic Cloud ================================== # These settings simplify using filebeat with the Elastic Cloud (https://cloud.elastic.co/). # The cloud.id setting overwrites the `output.elasticsearch.hosts` and # `setup.kibana.host` options. # You can find the `cloud.id` in the Elastic Cloud web UI. #cloud.id: # The cloud.auth setting overwrites the `output.elasticsearch.username` and # `output.elasticsearch.password` settings. The format is `<user>:<pass>`. #cloud.auth: #================================ 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 : [ "localhost:9200" ] # Enabled ilm (beta) to use index lifecycle management instead daily indices. #ilm.enabled: false # Optional protocol and basic auth credentials. #protocol: "https" #username: "elastic" #password: "changeme" #----------------------------- Logstash output -------------------------------- #output.logstash: # The Logstash hosts #hosts: ["localhost:5044"] # Optional SSL. By default is off. # List of root certificates for HTTPS server verifications #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"] # Certificate for SSL client authentication #ssl.certificate: "/etc/pki/client/cert.pem" # Client Certificate Key #ssl.key: "/etc/pki/client/cert.key" #================================ Processors ===================================== # Configure processors to enhance or manipulate events generated by the beat. processors : - add_host_metadata : ~ - add_cloud_metadata : ~ #================================ Logging ===================================== # Sets log level. The default log level is info. # Available log levels are: error, warning, info, debug #logging.level: debug # At debug level, you can selectively enable logging only for some components. # To enable all selectors use ["*"]. Examples of other selectors are "beat", # "publish", "service". #logging.selectors: ["*"] #============================== Xpack Monitoring =============================== # filebeat can export internal metrics to a central Elasticsearch monitoring # cluster. This requires xpack monitoring to be enabled in Elasticsearch. The # reporting is disabled by default. # Set to true to enable the monitoring reporter. #xpack.monitoring.enabled: false # Uncomment to send the metrics to Elasticsearch. Most settings from the # Elasticsearch output are accepted here as well. Any setting that is not set is # automatically inherited from the Elasticsearch output configuration, so if you # have the Elasticsearch output configured, you can simply uncomment the # following line. #xpack.monitoring.elasticsearch: |

打开 filebeat.yml 文件并设置您的日志文件位置:
Step-3) 发送日志到 ElasticSearch
确保在运行 Filebeat 之前已在本地启动ElasticSearch
。 我将在今天晚些时候发布一篇文章,介绍how to install and run ElasticSearch
。
这是 ElasticSearch 的 filebeat.yml 文件配置。
ElasticSearch 在端口 9200 上运行。
1 2 3 |
output . elasticsearch : # Array of hosts to connect to. hosts : [ "localhost:9200" ] |
你都准备好了。
步骤 4) 运行 Filebeat
1 2 |
bash - 3.2 $ sudo chown root filebeat . yml bash - 3.2 $ sudo . / filebeat - e |
从 filebeat 根目录执行以上两个命令,您应该会看到如下 filebeat 启动日志。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
root @ localhost : / user / crunchify / filebeat - 6.6.2 - linux - x86_64 # sudo chown root filebeat.yml root @ localhost : / user / crunchify / filebeat - 6.6.2 - linux - x86_64 # sudo ./filebeat -e 2019 - 03 - 30T14 : 52 : 02.608Z INFO instance / beat . go : 616 Home path : [ / user / crunchify / filebeat - 6.6.2 - linux - x86_64 ] Config path : [ / user / crunchify / filebeat - 6.6.2 - linux - x86_64 ] Data path : [ / user / crunchify / filebeat - 6.6.2 - linux - x86_64 / data ] Logs path : [ / user / crunchify / filebeat - 6.6.2 - linux - x86_64 / logs ] 2019 - 03 - 30T14 : 52 : 02.608Z INFO instance / beat . go : 623 Beat UUID : da7e202d - d480 - 42df - 907a - 1073b19c8e2d 2019 - 03 - 30T14 : 52 : 02.609Z INFO [ seccomp ] seccomp / seccomp . go : 116 Syscall filter successfully installed 2019 - 03 - 30T14 : 52 : 02.609Z INFO [ beat ] instance / beat . go : 936 Beat info { "system_info" : { "beat" : { "path" : { "config" : "/user/crunchify/filebeat-6.6.2-linux-x86_64" , "data" : "/user/crunchify/filebeat-6.6.2-linux-x86_64/data" , "home" : "/user/crunchify/filebeat-6.6.2-linux-x86_64" , "logs" : "/user/crunchify/filebeat-6.6.2-linux-x86_64/logs" } , "type" : "filebeat" , "uuid" : "da7e202d-d480-42df-907a-1073b19c8e2d" } } } 2019 - 03 - 30T14 : 52 : 02.609Z INFO [ beat ] instance / beat . go : 945 Build info { "system_info" : { "build" : { "commit" : "1eea934ce81be553337f2828bd12131896fea8e4" , "libbeat" : "6.6.2" , "time" : "2019-03-06T14:17:59.000Z" , "version" : "6.6.2" } } } 2019 - 03 - 30T14 : 52 : 02.609Z INFO [ beat ] instance / beat . go : 948 Go runtime info { "system_info" : { "go" : { "os" : "linux" , "arch" : "amd64" , "max_procs" : 2 , "version" : "go1.10.8" } } } 2019 - 03 - 30T14 : 52 : 02.611Z INFO [ beat ] instance / beat . go : 952 Host info { "system_info" : { "host" : { "architecture" : "x86_64" , "boot_time" : "2019-01-15T18:44:58Z" , "containerized" : false , "name" : "localhost" , "ip" : [ "127.0.0.1/8" , "::1/128" , "50.116.13.161/24" , "192.168.177.126/17" , "2600:3c01::f03c:91ff:fe17:4534/64" , "fe80::f03c:91ff:fe17:4534/64" ] , "kernel_version" : "4.18.0-13-generic" , "mac" : [ "f2:3c:91:17:45:34" ] , "os" : { "family" : "debian" , "platform" : "ubuntu" , "name" : "Ubuntu" , "version" : "18.10 (Cosmic Cuttlefish)" , "major" : 18 , "minor" : 10 , "patch" : 0 , "codename" : "cosmic" } , "timezone" : "UTC" , "timezone_offset_sec" : 0 , "id" : "1182104d1089460dbcc0c94ff1954c8c" } } } 2019 - 03 - 30T14 : 52 : 02.611Z INFO [ beat ] instance / beat . go : 981 Process info { "system_info" : { "process" : { "capabilities" : { "inheritable" : null , "permitted" : [ "chown" , "dac_override" , "dac_read_search" , "fowner" , "fsetid" , "kill" , "setgid" , "setuid" , "setpcap" , "linux_immutable" , "net_bind_service" , "net_broadcast" , "net_admin" , "net_raw" , "ipc_lock" , "ipc_owner" , "sys_module" , "sys_rawio" , "sys_chroot" , "sys_ptrace" , "sys_pacct" , "sys_admin" , "sys_boot" , "sys_nice" , "sys_resource" , "sys_time" , "sys_tty_config" , "mknod" , "lease" , "audit_write" , "audit_control" , "setfcap" , "mac_override" , "mac_admin" , "syslog" , "wake_alarm" , "block_suspend" , "audit_read" ] , "effective" : [ "chown" , "dac_override" , "dac_read_search" , "fowner" , "fsetid" , "kill" , "setgid" , "setuid" , "setpcap" , "linux_immutable" , "net_bind_service" , "net_broadcast" , "net_admin" , "net_raw" , "ipc_lock" , "ipc_owner" , "sys_module" , "sys_rawio" , "sys_chroot" , "sys_ptrace" , "sys_pacct" , "sys_admin" , "sys_boot" , "sys_nice" , "sys_resource" , "sys_time" , "sys_tty_config" , "mknod" , "lease" , "audit_write" , "audit_control" , "setfcap" , "mac_override" , "mac_admin" , "syslog" , "wake_alarm" , "block_suspend" , "audit_read" ] , "bounding" : [ "chown" , "dac_override" , "dac_read_search" , "fowner" , "fsetid" , "kill" , "setgid" , "setuid" , "setpcap" , "linux_immutable" , "net_bind_service" , "net_broadcast" , "net_admin" , "net_raw" , "ipc_lock" , "ipc_owner" , "sys_module" , "sys_rawio" , "sys_chroot" , "sys_ptrace" , "sys_pacct" , "sys_admin" , "sys_boot" , "sys_nice" , "sys_resource" , "sys_time" , "sys_tty_config" , "mknod" , "lease" , "audit_write" , "audit_control" , "setfcap" , "mac_override" , "mac_admin" , "syslog" , "wake_alarm" , "block_suspend" , "audit_read" ] , "ambient" : null } , "cwd" : "/user/crunchify/filebeat-6.6.2-linux-x86_64" , "exe" : "/user/crunchify/filebeat-6.6.2-linux-x86_64/filebeat" , "name" : "filebeat" , "pid" : 20394 , "ppid" : 20393 , "seccomp" : { "mode" : "filter" , "no_new_privs" : true } , "start_time" : "2019-03-30T14:52:01.740Z" } } } 2019 - 03 - 30T14 : 52 : 02.611Z INFO instance / beat . go : 281 Setup Beat : filebeat ; Version : 6.6.2 2019 - 03 - 30T14 : 52 : 05.613Z INFO add_cloud_metadata / add_cloud_metadata . go : 319 add_cloud_metadata : hosting provider type not detected . 2019 - 03 - 30T14 : 52 : 05.614Z INFO elasticsearch / client . go : 165 Elasticsearch url : http : //localhost:9200 2019 - 03 - 30T14 : 52 : 05.615Z INFO [ publisher ] pipeline / module . go : 110 Beat name : localhost 2019 - 03 - 30T14 : 52 : 05.615Z INFO instance / beat . go : 403 filebeat start running . 2019 - 03 - 30T14 : 52 : 05.615Z INFO registrar / registrar . go : 134 Loading registrar data from / user / crunchify / filebeat - 6.6.2 - linux - x86_64 / data / registry 2019 - 03 - 30T14 : 52 : 05.615Z INFO [ monitoring ] log / log . go : 117 Starting metrics logging every 30s 2019 - 03 - 30T14 : 52 : 05.616Z INFO registrar / registrar . go : 141 States Loaded from registrar : 0 2019 - 03 - 30T14 : 52 : 05.616Z INFO crawler / crawler . go : 72 Loading Inputs : 1 2019 - 03 - 30T14 : 52 : 05.616Z INFO log / input . go : 138 Configured paths : [ / crunchify / tutorials / log / crunchify - filebeat - test . log ] 2019 - 03 - 30T14 : 52 : 05.616Z INFO input / input . go : 114 Starting input of type : log ; ID : 7740765267175828127 2019 - 03 - 30T14 : 52 : 05.617Z INFO crawler / crawler . go : 106 Loading and starting Inputs completed . Enabled inputs : 1 2019 - 03 - 30T14 : 52 : 05.617Z INFO cfgfile / reload . go : 150 Config reloader started 2019 - 03 - 30T14 : 52 : 05.617Z INFO cfgfile / reload . go : 205 Loading of config files completed . |
步骤 5) 结果
下一步是让您检查哪些日志将进入 Elastic Search 以及您的可视化方式。 我们将很快完成详细的教程。 敬请关注。
下一步是什么? 设置弹性搜索
如何在您的开发/生产环境中安装和配置 Elasticsearch?