關(guān)鍵詞: filebeat
logstash
docker
docker-compose
PS : 本篇文章的改進版本垮耳,請點擊filebeat 5.3.1 結(jié)合 rancher 和 data-volume 實現(xiàn)橫向擴展
Background
上一篇文章《結(jié)合docker快速搭建啟動ELK日志收集平臺》講到搭建ELK service,這一章主要為了通過filebeat發(fā)送日志文件到前面我們所搭建的ELK平臺里邊
Filebeat
在beats中,filebeat作為輕量級的logs shipper查蓉,幫助用戶將無數(shù)client端上的log文件以一種輕量級的方式轉(zhuǎn)發(fā)并集中日志和文件到ELK stack中榜贴,主要通過elasticsearch或者logstash指定的port進行日志轉(zhuǎn)發(fā),本文主要講filebeat和logstash的integration
Introduction
filebeat:輕量級logs shipper鹃共,在beats中發(fā)送應用日志文件的表現(xiàn)尤為突出驶拱,擁有backpressure-sensitive協(xié)議會讓filebeat知道當logstash忙于處理數(shù)據(jù)的時候蓝纲,自動減緩讀取日志文件內(nèi)容的速度晌纫,給logstash足夠的resource永丝,等到logstash恢復正常之后再回到正常的讀取與發(fā)送速度。
Prerequisite
OS : Centos 7.x
# 如果選擇快速啟動的方式必須有一下兩個要求
Docker engine > 1.12.x
Docker-compose > 1.11.x
安裝并啟動filebeat service
通過直接安裝在宿主機上的方式
-
使用RPM安裝官方rpm package
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.1.1-x86_64.rpm sha1sum filebeat-5.1.1-x86_64.rpm rpm -vi filebeat-5.1.1-x86_64.rpm
-
配置
filebeat
與logstash
host哥牍,相關(guān)配置文件:/etc/filbeat/filebeat.yml
filebeat配置默認連接elasticsearch嗅辣,這里我們主要使用logstash蛇耀,把無關(guān)的配置注釋掉:#-------------------------- Elasticsearch output ------------------------------ # output.elasticsearch: # hosts: ["localhost:9200"]
在logstash下面配置logstash host配置并去掉注釋,這里我們以filebeat和logstash在同一臺機器為例译暂,相應的logstash host為localhost:
#----------------------------- Logstash output --------------------------------
#output.logstash:
hosts: ["localhost:5044"]
index: "logstash-%{+yyyy.MM.dd}"
配置日志目錄撩炊,注意縮進拧咳,這里我們以/var/log/*.log
為例,配置也支持*號通配:
- input_type: log
# Paths that should be crawled and fetched. Glob based paths.
paths:
- /var/log/*.log
- 啟動filebeat service
systemctl start filebeat systemctl status filebeat
LC_ALL=C systemctl status filebeat \* filebeat.service - filebeat Loaded: loaded (/usr/lib/systemd/system/filebeat.service; disabled; vendor preset: disabled) Active: active (running) since Sat 2017-03-25 03:59:03 EDT; 43s ago Docs: https://www.elastic.co/guide/en/beats/filebeat/current/index.html Main PID: 16277 (filebeat) Memory: 2.5M CGroup: /system.slice/filebeat.service `-16277 /usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml -path.home /usr/share/filebea... Mar 25 03:59:03 elk systemd[1]: Started filebeat. Mar 25 03:59:03 elk systemd[1]: Starting filebeat...
啟動成功祭衩。
通過docker-compose
-
Clone GIT folder 到本地阅签,建議放在user HOME下,eg:
/home/user1/
cd ~ git clone https://github.com/easonlau02/filebeat.git
-
修改配置文件:
~/filebeat/.env
# ~/filebeat/.env # environment environment=PROD # Logstash host LOGSTASH_HOST=localhost # for docker compose volumes: replacement, use absolute path, eg: /var/log/nginx # if multiple path, use space to separate them, eg: VOLUME_FOLDER_LIST="path1 path2 ... ..." VOLUME_FOLDER_LIST="/var/log/nginx/" # for filebeat.hml paths: replacement, support regex, eg: /var/log/nginx/*.log # if mutiple file pattern, use space to separate them, eg: PROSPECT_FILE_PATTERN_LIST="path1/*.log path2/*.log.* path3/access.log path4/access.*" PROSPECT_FILE_PATTERN_LIST="/var/log/nginx/*.log"
上述基本配置屬性是啟動filebeat的基本屬性:
-
environment:
和ELK一樣用來區(qū)分日志環(huán)境 -
LOGSTASH_HOST:
: 指定logstash host,這里以localhost為(與logstash在同一臺機器养交,默認經(jīng)過5044 port)碎连,如果不在同一臺機器,需指定logstash的hostname,并確port:5044可以訪問 -
VOLUME_FOLDER_LIST:
配置需要發(fā)送的日志文件所在的文件夾絕對路徑疲吸,eg:如果需要發(fā)送/var/log/nginx
下的所有.log
結(jié)尾的日志文件前鹅,需修改為VOLUME_FOLDER_LIST="/var/log/nginx/"
舰绘,如果有多個文件夾,則使用空格分開捂寿,例如VOLUME_FOLDER_LIST="path1 path2 ... ..."
-
PROSPECT_FILE_PATTERN_LIST:
filebeat真正讀取的文件路徑秦陋,具體到文件,可以使用*號通配赤嚼,eg:/var/log/nginx/*.log
顺又,記住要具體到文件,而不是文件夾蹂空,如果有多中不同文件夾的日志需要發(fā)送果录,同樣使用空格分開,eg:PROSPECT_FILE_PATTERN_LIST="path1/*.log path2/*.log.* path3/access.log path4/access.*"
- 使用gen-config.sh生成
filebeat.yml
和docker-compose.yml
文件$ cd ~/filebeat/ $ chmod +x gen-config.sh $ ./gen-config.sh VOLUME_FOLDER_LIST=/var/log/nginx/ PROSPECT_FILE_PATTERN_LIST=/var/log/nginx/*.log -rw-rw-r--. 1 user1 user1 504 Mar 25 04:24 docker-compose.yml -rw-rw-r--. 1 user1 user1 788 Mar 25 04:24 filebeat.yml
查看~/filebeat/filebeat.yml
filebeat.prospectors:
- input_type: log
paths:
- /var/log/nginx/*.log
.....
output.logstash:
# The Logstash hosts
hosts: ["${logstash}:5044"]
index: "logstash-%{+yyyy.MM.dd}"
......
~/filebeat/docker-compose.yml
version: '2'
services:
filebeat:
image: eason02/filebeat:5.1.1
container_name: filebeat-image
restart: always
environment:
- env=${environment}
- logstash=${LOGSTASH_HOST}
network_mode: host
....
volumes:
- ./5.1.1/logs:/etc/filebeat/logs
- ./5.1.1/data:/etc/filebeat/data
- ./filebeat.yml:/etc/filebeat/filebeat.yml
- /var/log/nginx/:/var/log/nginx/
....
- 使用docker-compose啟動filebeat service
$ cd ~/filebeat/
$ docker-compose up -d
# ouput
Creating filebeat-image
$ docker container ls -f name=filebeat-image
# output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bb77764beb8a eason02/filebeat:5.1.1.1 "/docker-entrypoin..." 2 minutes ago Up 2 minutes filebeat-image
通過Kibana查看日志localhost:5601
如果ELK沒有收集過日志,當訪問kibana的時候要求先建立index:
回到discovery查看我們通過filebeat發(fā)送的日志:
至此蘸泻,filebeat service安裝并啟動成功,你可以根據(jù)你的需要選擇不同的安裝方式悦施,第二種是經(jīng)過抽離以及優(yōu)化的,有問題大家下面評論穷蛹。未完待續(xù)....
** 文章所有步驟都是經(jīng)過實踐檢驗并可行,若有問題鬼雀,下方請評論
——END——
作者 : Eason
蛙吏,專注各種技術(shù)、平臺励烦、集成泼诱,不滿現(xiàn)狀,喜歡改改改
文章屉栓、技術(shù)合作
Email : eason.lau02@hotmail.com</small>