一)简介
elk系统是一套目前较为流行的日志收集分析系统,主要由elasticserch,logstash,kibana三部分组成,其中elasticsearch负责数据的存储,logstash负责日志的收集过滤,kibana负责日志的可视化部分。整个工作流程为logstash收集日志,过滤后输出并保存到elasticsearch中,最后用户通过kibana从elasticsearch中读取数据并处理。本文中日志收集引入filebeat收集日志,logstash监听在5000端口并接受filebeat传入的日志。
各组件分配如下:
192.168.1.17filebeat
192.168.1.18elk
二)搭建过程
1.在官方网站下载安装包
https://www.elastic.co/cn/downloads
2.因为elk依赖java开发环境,所以首先安装jdk
yum install java-1.8.0-openjdk
3.安装elk(192.168.1.18主机上)
yum install elasticsearch-5.5.2.rpm kibana-5.5.2-x86_64.rpm logstash-5.5.2.rpm
4.创建elasticsearch数据存储目录并赋予权限
mkdir -p /data/elasticsearch
chown elasticsearch.elasticsearch /data/elasticsearch
5.修改elasticsearch配置文件,更改其中的数据存储路径
vim /etc/elasticsearch/elasticsearch.yml
path.data: /data/elasticsearch
6.启动elasticsearch服务,并查看9200端口是否处于监听状态
systemctl start elasticsearch
ss -tnl
7.修改kibana配置文件,改变其监听的ip地址
vim /etc/kibana/kibana.yml
server.host: "0.0.0.0" (也可更改为192.168.1.17,此处主要是为了方便访问)
8.启动kibana服务并查看端口
systemctl start kibana
ss -tnl
9.在/etc/logstash/conf.d/目录中添加logstash配置文件nginx.conf
input {
beats {
port => 5000
type => "logs"
}
}
filter {
if [type] == "nginx-all" {
grok {
match => [ "message","(?:%{IPORHOST:clientip}|-) - %{NOTSPACE:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})\" %{NUMBER:response} (?:%{NUMBER:bytes}|-) %{QS:referrer} %{QS:agent} \"(?:%{IPV4:http_x_forwarded_for}|-)\"" ]
}
geoip {
source => "clientip"
add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]
add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}" ]
}
mutate {
convert => [ "[geoip][coordinates]", "float" ]
convert => [ "response","integer" ]
convert => [ "bytes","integer" ]
}
syslog_pri {}
date {
match => [ "timestamp" , "dd/MMM/YYYY:HH:mm:ss Z" ]
}
}
}
output {
elasticsearch {
hosts => ["127.0.0.1:9200"]
index => "logstash-nginx-access-%{+YYYY.MM.dd}"
}
stdout { codec => rubydebug }
}
input段:表示监听的端口以及类型
filter段:
grok:日志的匹配,数据结构转换,需要根据日志的具体格式进行匹配,匹配格式可以参考https://grokdebug.herokuapp.com/patterns,匹配结果可以在https://grokdebug.herokuapp.com/中进行验证
geoip:获取ip,以便最后显示地理位置,部分系统可能需要安装geoip
mutate:数据类型转换
date:用日志中的时间对timestamp进行转换,若不做转换,在导入老数据的时候系统会根据当前时间排序
output段:输出到elasticsearch中
10.启动logstash服务并查看端口
systemctl start logstash
ss -tnl
11.安装filebeat(192.168.1.17)
yum install filebeat-5.5.2-x86_64.rpm (包需要提前下载)
12.修改配置文件/etc/filebeat/filebeat.yml
filebeat:
spool_size: 1024
idle_timeout: 5s
registry_file: .filebeat
config_dir: /etc/filebeat/conf.d
output:
logstash:
hosts:
- 192.168.1.18:5000
enabled: true
shipper: {}
logging: {}
runoptions: {}
其中output中host指向logstash
13.创建/etc/filebeat/conf.d/目录。并添加配置文件nginx.yml
filebeat:
prospectors:
- paths:
- /var/log/nginx/access.log
encoding: plain
fields_under_root: false
input_type: log
ignore_older: 24h
document_type: nginx-all
scan_frequency: 10s
harvester_buffer_size: 16384
tail_files: false
force_close_files: false
backoff: 1s
max_backoff: 1s
backoff_factor: 2
partial_line_waiting: 5s
max_bytes: 10485760
其中path指向需要收集的日志文件,type需要与logstash配置文件中对应
14.启动filebeat并查看
systemctl start filebeat
netstat -altp|grep filebeat(与logstash建立了连接)