k8s的fluentd插件升级到1.3

前言

在较老的k8s版本(1.6 1.7),官方的efk日志收集例子中 fluentd手机的日志,丢失了毫秒级的时间精度,当时为了项目需要,就自己重新做了镜像,现在官方的例子中,已经没有这个问题,于是趁着升级elastic search,就一并升级。

在老版本的模式中,k8s是通过dockerfile往镜像中复制特定的配置文件,然后用环境变量来控制部分参数,而新版中,用configmap来挂载配置文件,更加灵活了,改动细节参考这个。这就方便我们采集除了k8s容器以外的其他日志。

systemd监控

fluentd本身就有system的插件,而且k8s打包的镜像里也包含了该插件,Gemfile里又这行

1
gem 'fluent-plugin-systemd', '~>1.0.1'

在system.input.conf项下,加入以下配置,即可按照systemd的名称来采集日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<source>
@id journald-flanneld
@type systemd
matches [{ "_SYSTEMD_UNIT": "flanneld.service" }]
<storage>
@type local
persistent true
path /var/log/journald-flanneld.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.flanneld
</source>

其中,

id,不能重复,

match,是匹配的参数,也就是 journalctl 的查询条件,可以在输入journalctl后按tab或者man来了解,关于各种条件的组合,查看这里

path pod文件的位置,唯一,而且要选择挂载宿主机的路径。

fields_strip_underscores fields_lowercase 这2个是字段转换的,去掉下划线和变成小写,不然在elastic search里,下划线开头的字段不能过滤。

read_from_head 建议加上

tag 标签,建议同一类的收集,用统一的前缀,后面方便通配符来做filter过滤

普通的文件监控

1
2
3
4
5
6
7
8
9
10
11
12
<source>
@id kubelet.log
@type tail
format multiline
multiline_flush_interval 5s
format_firstline /^\w\d{4}/
format1 /^(?<severity>\w)(?<time>\d{4} [^\s]*)\s+(?<pid>\d+)\s+(?<source>[^ \]]+)\] (?<message>.*)/
time_format %m%d %H:%M:%S.%N
path /var/log/kubelet.INFO
pos_file /var/log/es-kubelet.INFO.pos
tag kubelet
</source>

format_firstline
format1 这2个是日志的匹配正则,正则里的有名匹配项,会作为json的字段。

删除多余的字段

默认的采集配置中,又很多字段,可能在后期查询的时候并不会怎么用到。比如kubernetes的pod_id,一串乱码,明显没有pod名称好用,我们可以在推送到es之前就把他们删了。out.conf可以加入以下配置

1
2
3
4
5
6
7
8
9
10
11

<filter kubernetes.**>
@type record_transformer
enable_ruby true
remove_keys $["kubernetes"]["pod_id"],$.docker.container_id,$["kubernetes"]["namespace_id"],$.kubernetes.labels.pod-template-hash
</filter>

<filter systemd.*>
@type record_transformer
remove_keys boot_id,cap_effective,cmdline,exe,gid,machine_id,pid,priority,syslog_facility,systemd_cgroup,systemd_slice,uid
</filter>

bug注意点

在fluentd中,对于匹配嵌套字段和包含空格的字段,可以用以下的json path格式。

1
2
$.key1[0].key2 ---> record["key1"][0]["key2"]
$['dot.key'][0]['space key'] --> record["dot.key"][0]["space key"]

然而,在1.3.1以前的版本中,如果在根路径中,包含点的key匹配有bug,会无法匹配,升级为1.3.1或者用普通模式来删除吧

1
2
3
4
5
6
<filter kubernetes.**>
@type record_transformer
enable_ruby true
#remove_keys $['foo.bar'] #not working
remove_keys foo.bar #working
</filter>

多行插件在某些版本可能又问题,如果发现fluentd报错,或者es一只没收到。可以尝试先关闭了

1
2
3
4
5
6
7
8
# Concatenate multi-line logs
#<filter **>
# @type concat
# key message
# multiline_end_regexp /\n$/
# separator ""
# flush_interval 30
#</filter>

完整的yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
kind: ConfigMap
apiVersion: v1
metadata:
name: fluentd-es-config-v0.1.6
namespace: kube-system
labels:
addonmanager.kubernetes.io/mode: Reconcile
data:
system.conf: |-
<system>
root_dir /tmp/fluentd-buffers/
</system>

containers.input.conf: |-

<source>
@id fluentd-containers.log
@type tail
path /var/log/containers/*.log
pos_file /var/log/es-containers.log.pos
tag raw.kubernetes.*
read_from_head true
<parse>
@type multi_format
<pattern>
format json
time_key time
time_format %Y-%m-%dT%H:%M:%S.%NZ
</pattern>
<pattern>
format /^(?<time>.+) (?<stream>stdout|stderr) [^ ]* (?<log>.*)$/
time_format %Y-%m-%dT%H:%M:%S.%N%:z
</pattern>
</parse>
</source>

<match raw.kubernetes.**>
@id raw.kubernetes
@type detect_exceptions
remove_tag_prefix raw
message log
stream stream
multiline_flush_interval 5
max_bytes 500000
max_lines 1000
</match>

system.input.conf: |-
<source>
@id minion
@type tail
format /^(?<time>[^ ]* [^ ,]*)[^\[]*\[[^\]]*\]\[(?<severity>[^ \]]*) *\] (?<message>.*)$/
time_format %Y-%m-%d %H:%M:%S
path /var/log/salt/minion
pos_file /var/log/salt.pos
tag salt
</source>




<source>
@id journald-etcd
@type systemd
matches [{ "_SYSTEMD_UNIT": "etcd.service" }]
<storage>
@type local
persistent true
path /var/log/journald-etcd.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.etcd
</source>



<source>
@id journald-kube-proxy
@type systemd
matches [{ "_SYSTEMD_UNIT": "kube-proxy.service" }]
<storage>
@type local
persistent true
path /var/log/journald-kube-proxy.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.kube-proxy
</source>

<source>
@id journald-kube-apiserver
@type systemd
matches [{ "_SYSTEMD_UNIT": "kube-apiserver.service" }]
<storage>
@type local
persistent true
path /var/log/journald-kube-apiserver.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.kube-apiserver
</source>

<source>
@id journald-kube-controller-manager
@type systemd
matches [{ "_SYSTEMD_UNIT": "kube-controller-manager.service" }]
<storage>
@type local
persistent true
path /var/log/journald-kube-controller-manager.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.kube-controller-manager
</source>

<source>
@id journald-kube-scheduler
@type systemd
matches [{ "_SYSTEMD_UNIT": "kube-scheduler.service" }]
<storage>
@type local
persistent true
path /var/log/journald-kube-scheduler.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.kube-scheduler
</source>



<source>
@id journald-docker
@type systemd
matches [{ "_SYSTEMD_UNIT": "docker.service" }]
<storage>
@type local
persistent true
path /var/log/journald-docker.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.docker
</source>

<source>
@id journald-container-runtime
@type systemd
matches [{ "_SYSTEMD_UNIT": "{{ fluentd_container_runtime_service }}.service" }]
<storage>
@type local
persistent true
path /var/log/journald-container-runtime.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.container-runtime
</source>

<source>
@id journald-kubelet
@type systemd
matches [{ "_SYSTEMD_UNIT": "kubelet.service" }]
<storage>
@type local
persistent true
path /var/log/journald-kubelet.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.kubelet
</source>

<source>
@id journald-node-problem-detector
@type systemd
matches [{ "_SYSTEMD_UNIT": "node-problem-detector.service" }]
<storage>
@type local
persistent true
path /var/log/journald-node-problem-detector.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.node-problem-detector
</source>

<source>
@id journald-flanneld
@type systemd
matches [{ "_SYSTEMD_UNIT": "flanneld.service" }]
<storage>
@type local
persistent true
path /var/log/journald-flanneld.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.flanneld
</source>

<source>
@id journald-cephosd
@type systemd
matches [{ "_EXE": "/usr/bin/ceph-osd" }]
<storage>
@type local
persistent true
path /var/log/journald-cephosd.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.cephosd
</source>

<source>
@id journald-cephmon
@type systemd
matches [{ "_EXE": "/usr/bin/ceph-mon" }]
<storage>
@type local
persistent true
path /var/log/journald-cephmon.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.cephmon
</source>

<source>
@id journald-radosgw
@type systemd
matches [{ "_EXE": "/usr/bin/radosgw" }]
<storage>
@type local
persistent true
path /var/log/journald-radosgw.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.radosgw
</source>

<source>
@id kernel
@type systemd
matches [{ "_TRANSPORT": "kernel" }]
<storage>
@type local
persistent true
path /var/log/kernel.pos
</storage>
<entry>
fields_strip_underscores true
fields_lowercase true
</entry>
read_from_head true
tag systemd.kernel
</source>

forward.input.conf: |-
<source>
@type forward
</source>

monitoring.conf: |-
<source>
@type prometheus
</source>

<source>
@type monitor_agent
</source>

<source>
@type prometheus_monitor
<labels>
host ${hostname}
</labels>
</source>

<source>
@type prometheus_output_monitor
<labels>
host ${hostname}
</labels>
</source>

<source>
@type prometheus_tail_monitor
<labels>
host ${hostname}
</labels>
</source>

output.conf: |-
<filter kubernetes.**>
@type kubernetes_metadata
</filter>
<filter kubernetes.**>
@type record_transformer
enable_ruby true
remove_keys $["kubernetes"]["pod_id"],$.docker.container_id,$["kubernetes"]["namespace_id"],$.kubernetes.labels.pod-template-hash
</filter>

<filter systemd.*>
@type record_transformer
remove_keys boot_id,cap_effective,cmdline,exe,gid,machine_id,pid,priority,syslog_facility,systemd_cgroup,systemd_slice,uid
</filter>

<match **>
@id elasticsearch
@type elasticsearch
@log_level info
type_name fluentd
include_tag_key true
host es-master
port 9200
logstash_format true
<buffer>
@type file
path /var/log/fluentd-buffers/kubernetes.system.buffer
flush_mode interval
retry_type exponential_backoff
flush_thread_count 2
flush_interval 5s
retry_forever
retry_max_interval 30
chunk_limit_size 2M
queue_limit_length 8
overflow_action block
</buffer>
</match>