filebeat多实例运行:
1)需求
一台服务器上,需要运行多个filebeat实例
2)启动filebeat实例1
cat > config/11-log_tcp-to-console.yaml <<EOF
filebeat.inputs:
- type: log
paths:
- /tmp/oldboyedu-linux82/*.log
# 监听TCP的参数
- type: tcp
# 监听的IP:port
host: 0.0.0.0:8888
output.console:
pretty: true
EOF
./filebeat -e -c config/11-log_tcp-to-console.yaml
3)启动filebeat实例2
cat > config/10-es_error-to-es.yaml <<EOF
filebeat.inputs:
- type: log
paths:
- /oldboyedu/logs/es7/oldboyedu-linux82-es7.log
multiline.type: pattern
multiline.pattern: '^\['
multiline.negate: true
multiline.match: after
output.elasticsearch:
hosts:
- "http://10.0.0.101:9200"
- "http://10.0.0.102:9200"
- "http://10.0.0.103:9200"
index: "oldboyedu-linux82-es-error-%{+yyyy.MM.dd}"
setup.ilm.enabled: false
setup.template.name: "oldboyedu-linux82-es"
setup.template.pattern: "oldboyedu-linux82-es-*"
setup.template.settings:
index.number_of_shards: 3
index.number_of_replicas: 0
EOF
./filebeat -c config/10-es_error-to-es.yaml --path.data /tmp/oldboyedu-linux82-filebeat-data --path.logs /tmp/oldboyedu-linux82-filebeat-logs
Q1: 写多个输入端使用一个filebeat和启动多个filebeat实例哪种方案更好?请说明原因?
方案一: 写多个输入端使用一个filebeat
优点:
1)启动一个filebeat实例即可;
2)配置需要配置一个即可;
3)当业务不是特别复杂时,维护起来相对来说比较方便;
缺点:
1)有特定要求,输出端只有一个;
2)维护起来耦合性较强,当多个input类型,有任何一个需要修改时,需要停止整个服务,尽管其他的input类型不需要修改,影响较大;
3)当配置文件参数较多时,可读性较差,拍错成功也增长了;
方案二: 启动多个filebeat实例
优点:
1)方便指定多个输出类型,可以定制化;
2)没有太强的耦合性,如果某个业务需要修改,仅需要修改对应的filebeat实例的配置文件即可,其他filebeat实例并不会受到影响;
3)当配置较多时,后期维护也非常方便;
缺点:
1)需要运维人员维护多份配置文件;
2)需要启动多个filebeat实例;
cat > config/12-tcp_http-to-console.yaml <<EOF
filebeat.inputs:
- type: tcp
host: 0.0.0.0:8888
# 对应的是一个http协议的输入端。
- type: http_endpoint
# 监听地址
listen_address: 0.0.0.0
# 监听的端口
listen_port: 8080
# 响应状态码
response_code: 222
# 响应消息
response_body: '{"message": "oldboyedu linux82 success"}'
# 客户端请求路径
url: "/linux82"
# 顶级字段接受消息的前缀名称
prefix: "oldboyedu-linux82-json"
output.console:
pretty: true
EOF
filestream和log类型的区别:
1)配置JSON解析格式字段有区别
cat > config/13-filestream-to-console.yaml <<EOF
filebeat.inputs:
# 指定类型是一个文件流
- type: filestream
# 指定文件的路径
paths:
- /var/log/nginx/access.log
# 配置解析器
parsers:
# 配置JSON格式解析的
- ndjson:
# 指定解析JSON格式的数据存储路径的KEY,若不指定,则将解析后的结果存放在顶级字段。
target: ""
output.console:
pretty: true
EOF
2)多行匹配问题
cat > config/14-filestream-to-console.yaml <<EOF
filebeat.inputs:
- type: filestream
paths:
- /oldboyedu/logs/es7/oldboyedu-linux82-es7.log
parsers:
# 配置多行匹配
- multiline:
type: pattern
pattern: '^\['
negate: true
match: after
output.elasticsearch:
hosts:
- "http://10.0.0.101:9200"
- "http://10.0.0.102:9200"
- "http://10.0.0.103:9200"
index: "oldboyedu-linux82-es-error-%{+yyyy.MM.dd}"
setup.ilm.enabled: false
setup.template.name: "oldboyedu-linux82-es"
setup.template.pattern: "oldboyedu-linux82-es-*"
setup.template.settings:
index.number_of_shards: 3
index.number_of_replicas: 0
EOF
filebeat的模块使用:
1)模块的基本管理
./filebeat modules enable nginx tomcat mysql
启用模块
./filebeat modules list
查看模块列表
./filebeat modules disable tomcat mysql
禁用模块
2)模块的管理的底层原理
mv modules.d/{tomcat.yml.disabled,tomcat.yml}
mv modules.d/{mysql.yml.disabled,mysql.yml}
启用tomcat和mysql模块。
mv modules.d/{mysql.yml,mysql.yml.disabled}
mv modules.d/{tomcat.yml,tomcat.yml.disabled}
禁用tomcat和mysql模块。
3)修改nginx模块的配置文件
cat > modules.d/nginx.yml <<EOF
- module: nginx
access:
enabled: true
var.paths: ["/var/log/nginx/access.log*"]
error:
enabled: true
var.paths: ["/var/log/nginx/error.log*"]
ingress_controller:
enabled: false
EOF
4)使用nginx模块测试
cat > config/15-modules-to-console.yaml <<EOF
# 启动模块,若启用了模块,则可以不指定input类型
filebeat.config.modules:
# 指定模块的配置文件路径,"${path.config}"是filebeat的内置变量,表示filebeat安装目录。
path: ${path.config}/modules.d/*.yml
# 是否支持热加载
reload.enabled: true
output.elasticsearch:
hosts:
- "http://10.0.0.101:9200"
- "http://10.0.0.102:9200"
- "http://10.0.0.103:9200"
index: "oldboyedu-linux82-nginx-access-%{+yyyy.MM.dd}"
setup.ilm.enabled: false
setup.template.name: "oldboyedu-linux82-nginx"
setup.template.pattern: "oldboyedu-linux82-nginx-access-*"
setup.template.settings:
index.number_of_shards: 3
index.number_of_replicas: 0
EOF
5)请说一下Modules工作原理
1.启动filebeat实例;
2.读取配置文件,找Input|modules,output
若没有指定input,则必须配置modules,找到启用的modules文件后读取相关的配置文件。从而找到需要watch的文件。
3.模块的作用不仅仅是指定input的文件位置,而且还会源源数据进行解析为JSON格式哟,只不过这个解析方式是官方指定的,我们去修改这种格式定义就不太方便啦;
温馨提示:
(1)实际工作中,很多运维小伙伴都会直接使用input,因为input可以定制化源日志格式;
(2)官方的modules支持的中间件服务类型是有限的,在实际工作中,可能有的中间件或者你们公司开发的闭源产品日志可能需要你手动配置;
filebeat输入端通用字段:
enabled:
表示是否启用当前的input类型,若不设置,则默认值为true。
tags:
filebeat给每条消息打标记。
fields:
为消息添加自定义字段
fields_under_root:
若指定为true,则将自定义字段放在消息的顶级字段中
若不指定,则默认值为false,会将自定义字段放在一个"fields"字段下哟~
processors:
自定义处理器,可以根据用户需求,来对input数据源进行处理分析。
推荐阅读:
https://www.elastic.co/guide/en/beats/filebeat/7.17/defining-processors.html
https://www.elastic.co/guide/en/beats/filebeat/7.17/defining-processors.html#processors
参考案例:
cat > config/16-input_common_options-to-console.yaml <<EOF
filebeat.inputs:
- type: tcp
host: 0.0.0.0:8888
# 是否启用该类型,默认值为true。
enabled: false
# 给当前类型打tag
tags: ["oldboyedu-linux82-tcp","oldboyedu-elk","oldboyedu-linux"]
# 为消息添加自定义字段
fields:
school: oldboyedu
class: linux82
# 若指定为true,则将自定义字段放在消息的顶级字段中
# 若不指定,则默认值为false,会将自定义字段放在一个"fields"字段下哟~
fields_under_root: true
# 自定义处理器,可以对数据进行处理
# processors:
# # 删除事件(event)
# - drop_event:
# # when语句表示当满足某个条件执行相应的动作
# when:
# # 匹配正则表达式
# regexp:
# # 要求message字段包含以"INFO:"开头的时间,直接删除时间!
# message: "^INFO:"
#
- type: log
enabled: true
paths:
- /tmp/oldboyedu-linux82/*
# 自定义处理器,删除包含"info:"所有事件。
processors:
- drop_event:
when:
contains:
message: "info:"
output.console:
pretty: true
EOF
filebeat收集docker日志:
1)安装docker环境
tar xf oldboyedu-docker-ce-20_10_17.tar.gz
cd docker-ce-20_10_17/ && yum -y localinstall *.rpm
2)添加自动补全功能
source /usr/share/bash-completion/bash_completion
3)配置镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://tuv7rqqq.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
4)加载nginx镜像
docker load -i oldboyedu-nginx_1-20-1.tar.gz
5)启动nginx容器
docker run -dp 88:80 --name oldboyedu-linux82-nginx nginx:1.20.1 # 启动容器
docker logs -f oldboyedu-linux82-nginx # 查看日志
6)使用filebeat收集docker日志案例一,7.2版本已废弃,不推荐使用。
cat > config/17-docker-to-console.yaml <<EOF
filebeat.inputs:
# 数据源的类型为docker
- type: docker
# 指定docker容器的ID,可以匹配所有的容器ID,也可以指定容器ID进行匹配
containers.ids:
- '*'
output.console:
pretty: true
EOF
7)使用filebeat收集docker日志案例二,推荐使用。
cat > config/18-container-to-console.yaml <<EOF
filebeat.inputs:
# 数据源的类型为container
- type: container
# 指定容器日志的路径
paths:
- '/var/lib/docker/containers/*/*.log'
# 指定收集的日志类型,合法的参数为: all,stdout,stderr
stream: stdout
output.console:
pretty: true
EOF
kibana组件的基本使用:
(1)安装kibana
yum -y localinstall kibana-7.17.5-x86_64.rpm
(2)修改kibana的配置文件
vim /etc/kibana/kibana.yml
...
# kibana的监听端口
server.port: 5601
# kibana的服务器IP
server.host: "0.0.0.0"
# kibana服务器的名称,可以自定义
server.name: "oldboyedu-linux82-kibana"
# 指定ES集群的地址
elasticsearch.hosts: ["http://10.0.0.101:9200","http://10.0.0.102:9200","http://10.0.0.103:9200"]
# kibana支持的语言
i18n.locale: "zh-CN"
(3)启动kibana服务
systemctl start kibana
(4)查看kibana的端口号
ss -ntl | grep 5601
(5)访问kibana的WebUI
http://10.0.0.101:5601/
(6)添加官方样例图
见视频。