bed*_*bza 2 logging fluentd kibana kubernetes elastic-stack
我有一种ELK堆栈,使用Fluentd而不是logstash,在Kubernetes集群上作为DaemonSet运行,并将所有容器中的所有日志以Logstash格式发送到Elasticsearch服务器。
在Kubernetes集群上运行的许多容器中,有些是nginx容器,它们输出以下格式的日志:
121.29.251.188 - [16/Feb/2017:09:31:35 +0000] host="subdomain.site.com" req="GET /data/schedule/update?date=2017-03-01&type=monthly&blocked=0 HTTP/1.1" status=200 body_bytes=4433 referer="https://subdomain.site.com/schedule/2589959/edit?location=23092&return=monthly" user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0" time=0.130 hostname=webapp-3188232752-ly36o
Run Code Online (Sandbox Code Playgroud)
在Kibana中可见的字段如下图所示:
在对这种类型的日志建立索引之后,是否可以从中提取字段?
流利的收集器配置有以下可处理所有容器的源,因此由于来自不同容器的输出差异很大,因此在此阶段无法强制执行格式:
<source>
type tail
path /var/log/containers/*.log
pos_file /var/log/es-containers.log.pos
time_format %Y-%m-%dT%H:%M:%S.%NZ
tag kubernetes.*
format json
read_from_head true
</source>
Run Code Online (Sandbox Code Playgroud)
在理想情况下,我想用“ log”字段中的元字段来丰富上面屏幕快照中的可见字段,例如“ host”,“ req”,“ status”等。
经过几天的研究并习惯了EFK堆栈,我来到了EFK特定解决方案,这与Darth_Vader的回答相反,后者只能在ELK堆栈上实现。
综上所述,我使用的是Fluentd而不是Logstash,因此,如果您还安装Fluentd Grok插件(我决定不这样做),那么任何grok解决方案都可以使用,因为:
事实证明,Fluentd通过使用解析器过滤器具有自己的字段提取功能。为了解决我的问题中的问题,就在该<match **>
行之前,因此在日志行对象已经充满了kubernetes元数据字段和标签之后,我添加了以下内容:
<filter kubernetes.var.log.containers.webapp-**.log>
type parser
key_name log
reserve_data yes
format /^(?<ip>[^-]*) - \[(?<datetime>[^\]]*)\] host="(?<hostname>[^"]*)" req="(?<method>[^ ]*) (?<uri>[^ ]*) (?<http_version>[^"]*)" status=(?<status_code>[^ ]*) body_bytes=(?<body_bytes>[^ ]*) referer="(?<referer>[^"]*)" user_agent="(?<user_agent>[^"]*)" time=(?<req_time>[^ ]*)/
</filter>
Run Code Online (Sandbox Code Playgroud)
解释:
<filter kubernetes.var.log.containers.webapp-**.log>
-在与该标签匹配的所有行上应用块;在我的情况下,Web服务器组件的容器称为webapp- {something}
type parser
-告诉流利的应用解析器过滤器
key_name log
-仅将模式应用于log
日志行的属性,而不应用于整行,即json字符串
reserve_data yes
-非常重要,如果未指定,则整个日志行对象仅由从中提取的属性替换format
,因此,如果您已经具有其他属性(如kubernetes_metadata
过滤器添加的属性),则在不添加reserve_data
选项时将其删除
format
-应用于log
键值以提取命名属性的正则表达式
请注意,我使用的是Fluentd 1.12,因此此语法与较新的1.14语法不完全兼容,但是该原理将对解析器声明进行一些细微调整。
归档时间: |
|
查看次数: |
1600 次 |
最近记录: |