如何安裝和配置 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?
