filebeat、elasticsearch、logstash、kibana此为日志可视化的基础设施。不过根据业务系统的体量不同,这些组件不是全都必须的。这里介绍EKF的部署方式,即elasticksearch、kibana、filebeat。
安装filebeat(centos)
- 执行
sudo rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
- 在
/etc/yum.repos.d/
目录下创建elastic.repo
文件,内容如下:[elastic-7.x] name=Elastic repository for 7.x packages baseurl=https://mirror.tuna.tsinghua.edu.cn/elasticstack/7.x/yum/ gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md
- 执行如下命令:
sudo yum install filebeat sudo systemctl enable filebeat
filebeat配置
这里不再具体介绍,filebeat可以把应用产生的日志文件发送到logstash或者elasticsearch中去。这里主要介绍一些关键配置。
- 在filebeat.yml文件中,首先修改
filebeat.inputs
:
#=========================== 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: true
# Paths that should be crawled and fetched. Glob based paths.
paths:
- /home/xxx/xx/logs/xxx1.log
tags: ['xxx1']
fields:
filetype: xxx1
fields_under_root: true # 新增一个字段,方便对不同的服务的log区分
multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}' #将异常栈信息合并
multiline.negate: true
multiline.match: after
- type: log
# Change to true to enable this input configuration.
enabled: true
# Paths that should be crawled and fetched. Glob based paths.
paths:
- /home/xxx/xx/logs/xxx2.log
tags: ['xxx2']
fields:
filetype: xxx1
fields_under_root: true
multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}' #将异常栈信息合并
multiline.negate: true
multiline.match: after
这样可以给不同的服务的log文件加上不同的tag。这个tag在后面建立索引的时候很有用。
- 其次要修改
output.elasticsearch
:
#-------------------------- Elasticsearch output ------------------------------
setup.template.name: "fb-"
setup.template.pattern: "fb-"
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["host:9200"]
# Protocol - either `http` (default) or `https`.
protocol: "http"
indices:
- index: "fb-xxx1-%{+yyyy.MM.dd}"
when.contains:
tags: "xxx1"
- index: "fb-xxx2-%{+yyyy.MM.dd}"
when.contains:
tags: "xxx2"
# Authentication credentials - either API key or username/password.
#api_key: "id:api_key"
username: "elastic"
password: "xxxxx"
#index: "filebeat-%{+yyyy.MM.dd}"
注意indices
这里就是根据上面的tag对log进行归类,创建不同的ES索引。这里的%{+yyyy.MM.dd}
是根据日期来创建索引,为之后按天来删除历史索引数据提供方便。
需要注意的是前两行
setup.template
的配置一定要跟你修改的index
的前缀一致。