新款的集中式的便捷的日志管理平台elk,通过web页面的形式,对大量日志做集中管理,划重点。
ELK架构
怎么部署的?
通过一系列工具进行信息搜集:filebeat和logstash
收集信息后,信息交到ElasticSearch(ES)集群上,
通过kibana平台来展示从ES集群中得到的信息。
Filebeat
用于收集日志的工具,根据要求发送到指定目标上。可以是机器、终端、屏幕。
需要配置,配置什么?告诉他要收集啥日志。
- 第一次会从每个文件的开头一直读到当前文件的最后一行。
- 每一行称为一个事件,格式是一个包含很多字段的大字典,也是JSON格式的数据,在Filebeat中负责完成这个动作的官方称为Harvester(收割机、矿工)
- 这个事件将来会被保存到ES集群中。
- 收割机读到文件最后,会停止工作,知道文件有新的内容才继续工作。
实验准备:三台服务器,一台4G,另外两台最少3G
02 7‘38
elk1
##下载filebeat包
filebeat-7.13.2-linux-x86_64.tar.gz
## 移动解压后的包,并改名
mv filebeat-7.13.2-linux-x86_64 /usr/local/filebeat
##配置systemd方式的filebeat启动文件
vim /usr/lib/systemd/system/filebeat.service
[Unit]
Description=Filebeat sends log files to Logstash or directly to Elasticsearch.
Wants=network‐online.target
After=network‐online.target
[Service]
ExecStart=/usr/local/filebeat/filebeat ‐c /usr/local/filebeat/filebeat.yml
Restart=always
[Install]
WantedBy=multi‐user.target
systemctl daemon-reload
建立系统进程,重新装载一下
filebeat简单使用
创建一个新文件,在/tmp/access.log用来测试数据
# vim /tmp/access.log
123.127.39.50 ‐ ‐ [04/Mar/2021:10:50:28 +0800] "GET /logo.jpg HTTP/1.1" 200 14137 "http://81.68.233.173/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36" "‐"
vim /usr/local/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true #true加载
paths:
- /tmp/*.log #paths,日志去哪里拿,日志地址
filebeat.config.modules:
#
path: ${path.config}/modules.d/*.yml
#安装路径,是filebeat的内置变量
reload.enabled: false
#这里如果开的话会影响收集日志的过程,配好之后手动重启就完了。
setup.template.settings:
#设定临时的template,index共享的索引数量,默认值
index.number_of_shards: 1
output.console: #console控制台的意思,输出到屏幕上。这两句是要手动添加上去的。
pretty: true #不用了的话改成false
processors:
- add_host_metadata:
when.not.contains.tags: forwarded
# processors处理,添加此主机的源数据信息到输出数据中,如ip mac os等信息。
# ~表示为空
filebeat的测试行为
##运行测试
/usr/local/filebeat/filebeat -c /usr/local/filebeat/filebeat.yml
##专用日志搜集模块
/usr/local/filebeat/filebeat -c /usr/local/filebeat/filebeat.yml modules list
##模块配置文件存储位置
/usr/local/filebeat/modules.d
##禁用模块
/usr/local/filebeat/filebeat modules disable +模块名
##启用模块
/usr/local/filebeat/filebeat modules enable +模块名
示例模块——nginx模块的使用
- 准备nginx示例日志
- 访问日志示例文件
# vim /var/log/access.log
123.127.39.50 ‐ ‐ [04/Mar/2021:10:50:28 +0800] "GET /logo.jpg HTTP/1.1" 200 14137 "http://81.68.233.173/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36" "‐"
- 错误日志示例文件
# vim /var/log/error.log
2021/03/04 10:50:28 [error] 11396#0: *5 open() "/farm/bg.jpg" failed (2: No such file or directory), client: 123.127.39.50, server: localhost, request: "GET /bg.jpg HTTP/1.1", host: "81.68.233.173", referrer: "http://81.68.233.173/"
-
启用nginx模块
/usr/local/filebeat/filebeat -c /usr/local/filebeat/filebeat.yml modules enable nginx
-
修改模块的默认日志路径为非默认路径,用于测试
默认的nginx路径在/var/log/nginx/access.log*
/var/log/nginx/error.log*
vim /usr/local/filebeat/modules.d/nginx.yml
- module: nginx
access:
enabled: true
var.paths: ["/var/log/access.log"]
##var.paths属性是用于搜集日志真实路径和手机模块默认不一致时使用,如果不设置此选项,Filebeat将根据您的操作系统选择路径选择使用默认值。
error:
enabled: true
var.paths: ["/var/log/error.log"]
ingress_controller:
enabled: false
##可以写成一个属组
##var.paths: [ "/opt/nginx/log/nginx/access.log*","/opt/nginx/log/error.log*" ]
测试
/usr/local/filebeat/filebeat -c /usr/local/filebeat/filebeat.yml -e
再开一台机器,修改日志文件,可以看见日志输出正常。
output的配置(输出属性)
Filebeat是用于搜集日志,之后把日志推送到某个接收的系统中,这些系统或者装置再filebeat中称为output。
- console 终端屏幕
- elasticsearch 存放日志,并提供查询
- logstash进一步对日志数据进行处理
- kafka 消息队列
详细output列表参考filebeat官方文档
##两种输出到屏幕的配置:
- 完整
output.console:
pretty: true
- 某些字段,定义输出的格式
output.console:
codec.format:
##codec代码 format格式---codec.format代码格式,也就是输出格式
string: '%{[@timestamp]} %{[message]}'
##输出到elasticsearch
output.elasticsearch:
hosts: ['http://es01:9200', 'http://es02:9200']
##输出到logstash
output.logstash:
hosts: ["127.0.0.1:5044"]
重读日志文件
反复重读日志文件可能会出现一个问题,什么问题呢?会出现文件锁死的一个情况,如下:
rm -rf /usr/local/filebeat/data
锁文件在这里
这个时候,查看进程,
ps -ef | grep 'filebea[t]'
如果有的话,杀死。
precessors模块 处理过滤和增强数据
用来追加主机元数据信息,再推送出来,除了加东西,还可以减东西,日志里某些东西不需要,就扔掉
vim /usr/local/filebeat/filebeat.yml
# ---------------------------- Elasticsearch Output ----------------------------
#output.elasticsearch:
# Array of hosts to connect to.
# hosts: ["localhost:9200"]
processors:
- drop_event: #丢弃事件
when: #当
regexp: #表示下面有正则表达式
message: "^DBG" #消息里带DBG的,都扔掉。
- add_host_metadata:
when.not.contains.tags: forwarded
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~
##向输出的数据中添加某些自定义字段
processors:
‐ add_fields:
target: project # 要添加的自定义字段key名称
fields:
name: myproject
id: '574734885120952459'
##删某些字段
processors:
‐ drop_fields:
fields: ["field1", "field2", ...]
ignore_missing: false
例如:
‐ drop_fields:
fields: ['input', "ecs.version"]