Skip to content

Commit 91343db

Browse files
committed
Add two examples about how to analysis audits of kube-apiserver
1 parent e8e2777 commit 91343db

File tree

1 file changed

+163
-3
lines changed
  • docs/tasks/debug-application-cluster

1 file changed

+163
-3
lines changed

docs/tasks/debug-application-cluster/audit.md

Lines changed: 163 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,30 @@ API can be found [here][audit-api] with more details about the exact fields capt
188188
The behavior of the `--audit-log-path` flag changes when enabling the `AdvancedAuditing`
189189
feature flag. This includes the cleanups discussed above, such as changes to the `method`
190190
values and the introduction of a "stage" for each event. As before, the `id` field of
191-
the log line indicates which events were generated from the same request. Events are
192-
formatted as follows:
191+
the log indicates which events were generated from the same request. With default legacy
192+
format, events are formatted as follows:
193193

194194
```
195195
2017-06-15T21:50:50.259470834Z AUDIT: id="591e9fde-6a98-46f6-b7bc-ec8ef575696d" stage="RequestReceived" ip="10.2.1.3" method="update" user="system:serviceaccount:kube-system:default" groups="\"system:serviceaccounts\",\"system:serviceaccounts:kube-system\",\"system:authenticated\"" as="<self>" asgroups="<lookup>" namespace="kube-system" uri="/api/v1/namespaces/kube-system/endpoints/kube-controller-manager" response="<deferred>"
196196
2017-06-15T21:50:50.259470834Z AUDIT: id="591e9fde-6a98-46f6-b7bc-ec8ef575696d" stage="ResponseComplete" ip="10.2.1.3" method="update" user="system:serviceaccount:kube-system:default" groups="\"system:serviceaccounts\",\"system:serviceaccounts:kube-system\",\"system:authenticated\"" as="<self>" asgroups="<lookup>" namespace="kube-system" uri="/api/v1/namespaces/kube-system/endpoints/kube-controller-manager" response="200"
197197
```
198198

199199
Logged events omit the request and response bodies. The `Request` and
200-
`RequestResponse` levels are equivalent to `Metadata` for this backend.
200+
`RequestResponse` levels are equivalent to `Metadata` for legacy format.
201+
202+
Since Kubernetes 1.8, structed json fromat is supported for log backend.
203+
Use the following option to switch log to json format:
204+
205+
```
206+
--audit-log-format=json
207+
```
208+
209+
With json format, events are formatted as follows:
210+
211+
```
212+
{"kind":"Event","apiVersion":"audit.k8s.io/v1alpha1","metadata":{"creationTimestamp":null},"level":"Metadata","timestamp":"2017-07-12T11:02:43Z","auditID":"2e79f0c9-a941-45ae-a9ce-663a1b19ff14","stage":"RequestReceived","requestURI":"/api/v1/namespaces/default/pods","verb":"list","user":{"username":"kubecfg","groups":["system:masters","system:authenticated"]},"sourceIPs":["172.16.116.128"],"objectRef":{"resource":"pods","namespace":"default","apiVersion":"/v1"}}
213+
{"kind":"Event","apiVersion":"audit.k8s.io/v1alpha1","metadata":{"creationTimestamp":null},"level":"Metadata","timestamp":"2017-07-12T11:02:43Z","auditID":"2e79f0c9-a941-45ae-a9ce-663a1b19ff14","stage":"ResponseComplete","requestURI":"/api/v1/namespaces/default/pods","verb":"list","user":{"username":"kubecfg","groups":["system:masters","system:authenticated"]},"sourceIPs":["172.16.116.128"],"objectRef":{"resource":"pods","namespace":"default","apiVersion":"/v1"},"responseStatus":{"metadata":{},"code":200}}
214+
```
201215

202216
#### Webhook backend
203217

@@ -284,6 +298,152 @@ Events are POSTed as a JSON serialized `EventList`. An example payload:
284298
}
285299
```
286300

301+
### Log Collector Examples
302+
303+
#### Use fluentd to collect and distribute audit events from log file
304+
305+
[Fluentd][fluentd] is an open source data collector for unified logging layer.
306+
In this example, we will use fluentd to split audit events by different namespaces.
307+
Note that this example requries json format output support in Kubernetes 1.8.
308+
309+
- install [fluentd, fluent-plugin-forest and fluent-plugin-rewrite-tag-filter][fluentd_install_doc] in the kube-apiserver node
310+
- create a config file for fluentd
311+
312+
```shell
313+
$ cat <<EOF > /etc/fluentd/config
314+
# fluentd conf runs in the same host with kube-apiserver
315+
<source>
316+
@type tail
317+
# audit log path of kube-apiserver
318+
path /var/log/audit
319+
pos_file /var/log/audit.pos
320+
format json
321+
time_key time
322+
time_format %Y-%m-%dT%H:%M:%S.%N%z
323+
tag audit
324+
</source>
325+
326+
<filter audit>
327+
#https://github.com/fluent/fluent-plugin-rewrite-tag-filter/issues/13
328+
type record_transformer
329+
enable_ruby
330+
<record>
331+
namespace ${record["objectRef"].nil? ? "<none>":(record["objectRef"]["namespace"].nil? ? "<none>":record["objectRef"]["namespace"])}
332+
</record>
333+
</filter>
334+
335+
<match audit>
336+
# route audit according to user section in context
337+
@type rewrite_tag_filter
338+
rewriterule1 namespace ^(.+) ${tag}.$1
339+
</match>
340+
341+
<filter audit.**>
342+
@type record_transformer
343+
remove_keys namespace
344+
</filter>
345+
346+
<match audit.**>
347+
@type forest
348+
subtype file
349+
remove_prefix audit
350+
<template>
351+
time_slice_format %Y%m%d%H
352+
compress gz
353+
path /var/log/audit-${tag}.*.log
354+
format json
355+
include_time_key true
356+
</template>
357+
</match>
358+
EOF
359+
```
360+
- start fluentd
361+
362+
```shell
363+
$ fluentd -c /etc/fluentd/config -vv
364+
```
365+
- start kube-apiserver with the following options:
366+
367+
```
368+
--feature-gates=AdvancedAuditing=true --audit-policy-file=/etc/kubernetes/audit-policy.yaml --audit-log-path=/var/log/kube-audit --audit-log-format=json
369+
```
370+
- check audits for different namespaces in /var/log/audit-<namespace>*.log
371+
372+
#### Use logstash to collect and distribute audit events from webhook backend
373+
374+
[Logstash][logstash] is an open source, server-side data processing tool. In this example,
375+
we will use logstash to collect audit events from webhook backend, and save events of
376+
different users into different files.
377+
378+
- install [logstash][logstash_install_doc]
379+
- create config file for logstash
380+
381+
```shell
382+
$ cat <<EOF > /etc/logstash/config
383+
input{
384+
http{
385+
#TODO, does logstash support https input?
386+
port=>8888
387+
}
388+
}
389+
filter{
390+
split{
391+
# Webhook audit backend sends several events together with EventList
392+
# split each event here.
393+
field=>[items]
394+
# We only need event subelement, remove others.
395+
remove_field=>[headers, metadata, apiVersion, "@timestamp", kind, "@version", host]
396+
}
397+
mutate{
398+
rename => {items=>event}
399+
}
400+
}
401+
output{
402+
file{
403+
# Audit events from different users will be saved into different files.
404+
path=>"/var/log/kube-audit-%{[event][user][username]}/audit"
405+
}
406+
}
407+
```
408+
- start logstash
409+
410+
```
411+
$ bin/logstash -f /etc/logstash/config --path.settings /etc/logstash/
412+
```
413+
- create a [kubeconfig file](/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/) for kube-apiserver webhook audit backend
414+
415+
```shell
416+
$ cat <<EOF > /etc/kubernetes/audit-webhook-kubeconfig
417+
apiVersion: v1
418+
clusters:
419+
- cluster:
420+
server: http://<ip_of_logstash>:8888
421+
name: logstash
422+
contexts:
423+
- context:
424+
cluster: logstash
425+
user: ""
426+
name: default-context
427+
current-context: default-context
428+
kind: Config
429+
preferences: {}
430+
users: []
431+
```
432+
- start kube-apiserver with the following options:
433+
434+
```
435+
--feature-gates=AdvancedAuditing=true --audit-policy-file=/etc/kubernetes/audit-policy.yaml --audit-webhook-config-file=/etc/kubernetes/audit-webhook-kubeconfig
436+
```
437+
- check audits in logstash node's directories /var/log/kube-audit-<username>/audit
438+
439+
Note that in addition to file output plugin, logstash has a variety of outputs that
440+
let users route data where they want. For example, users can emit audit events to elasticsearch
441+
plugin which supports full-text search and analytics.
442+
287443
[audit-api]: https://github.com/kubernetes/kubernetes/blob/v1.7.0-rc.1/staging/src/k8s.io/apiserver/pkg/apis/audit/v1alpha1/types.go
288444
[kube-apiserver]: /docs/admin/kube-apiserver
289445
[gce-audit-profile]: https://github.com/kubernetes/kubernetes/blob/v1.7.0/cluster/gce/gci/configure-helper.sh#L490
446+
[fluentd]: http://www.fluentd.org/
447+
[fluentd_install_doc]: http://docs.fluentd.org/v0.12/articles/quickstart#step1-installing-fluentd
448+
[logstash]: https://www.elastic.co/products/logstash
449+
[logstash_install_doc]: https://www.elastic.co/guide/en/logstash/current/installing-logstash.html

0 commit comments

Comments
 (0)