@@ -359,57 +359,66 @@ Note that this example requries json format output support in Kubernetes 1.8.
3593591 . install [ fluentd, fluent-plugin-forest and fluent-plugin-rewrite-tag-filter] [ fluentd_install_doc ] in the kube-apiserver node
3603601 . create a config file for fluentd
361361
362- $ cat <<EOF > /etc/fluentd/config
363- # fluentd conf runs in the same host with kube-apiserver
364- <source>
365- @type tail
366- # audit log path of kube-apiserver
367- path /var/log/audit
368- pos_file /var/log/audit.pos
369- format json
370- time_key time
371- time_format %Y-%m-%dT%H:%M:%S.%N%z
372- tag audit
373- </source>
362+ ``` shell
363+ $ cat << EOF > /etc/fluentd/config
364+ # fluentd conf runs in the same host with kube-apiserver
365+ <source>
366+ @type tail
367+ # audit log path of kube-apiserver
368+ path /var/log/audit
369+ pos_file /var/log/audit.pos
370+ format json
371+ time_key time
372+ time_format %Y-%m-%dT%H:%M:%S.%N%z
373+ tag audit
374+ </source>
374375
375- <filter audit>
376- #https://github.com/fluent/fluent-plugin-rewrite-tag-filter/issues/13
377- type record_transformer
378- enable_ruby
379- <record>
380- namespace ${record["objectRef"].nil? ? "none":(record["objectRef"]["namespace"].nil? ? "none":record["objectRef"]["namespace"])}
381- </record>
382- </filter>
376+ <filter audit>
377+ #https://github.com/fluent/fluent-plugin-rewrite-tag-filter/issues/13
378+ type record_transformer
379+ enable_ruby
380+ <record>
381+ namespace ${record["objectRef"].nil? ? " none" : (record["objectRef"]["namespace"].nil? ? " none" : record["objectRef"]["namespace"])}
382+ </record>
383+ </filter>
383384
384- <match audit>
385- # route audit according to namespace element in context
386- @type rewrite_tag_filter
387- rewriterule1 namespace ^(.+) ${tag}.$1
388- </match>
385+ <match audit>
386+ # route audit according to namespace element in context
387+ @type rewrite_tag_filter
388+ rewriterule1 namespace ^(.+) ${tag} .$1
389+ </match>
389390
390- <filter audit.**>
391- @type record_transformer
392- remove_keys namespace
393- </filter>
391+ <filter audit.**>
392+ @type record_transformer
393+ remove_keys namespace
394+ </filter>
394395
395- <match audit.**>
396- @type forest
397- subtype file
398- remove_prefix audit
399- <template>
400- time_slice_format %Y%m%d%H
401- compress gz
402- path /var/log/audit-${tag}.*.log
403- format json
404- include_time_key true
405- </template>
406- </match>
396+ <match audit.**>
397+ @type forest
398+ subtype file
399+ remove_prefix audit
400+ <template>
401+ time_slice_format %Y%m%d%H
402+ compress gz
403+ path /var/log/audit-${tag} .*.log
404+ format json
405+ include_time_key true
406+ </template>
407+ </match>
408+ ` ` `
409+
4074101. start fluentd
408411
409- $ fluentd -c /etc/fluentd/config -vv
412+ ` ` ` shell
413+ $ fluentd -c /etc/fluentd/config -vv
414+ ` ` `
415+
4104161. start kube-apiserver with the following options:
411417
412- --audit-policy-file=/etc/kubernetes/audit-policy.yaml --audit-log-path=/var/log/kube-audit --audit-log-format=json
418+ ` ` ` shell
419+ --audit-policy-file=/etc/kubernetes/audit-policy.yaml --audit-log-path=/var/log/kube-audit --audit-log-format=json
420+ ` ` `
421+
4134221. check audits for different namespaces in /var/log/audit-* .log
414423
415424# ### Use logstash to collect and distribute audit events from webhook backend
@@ -421,56 +430,68 @@ different users into different files.
4214301. install [logstash][logstash_install_doc]
4224311. create config file for logstash
423432
424- $ cat <<EOF > /etc/logstash/config
425- input{
426- http{
427- #TODO, figure out a way to use kubeconfig file to authenticate to logstash
428- #https://www.elastic.co/guide/en/logstash/current/plugins-inputs-http.html#plugins-inputs-http-ssl
429- port=>8888
430- }
431- }
432- filter{
433- split{
434- # Webhook audit backend sends several events together with EventList
435- # split each event here.
436- field=>[items]
437- # We only need event subelement, remove others.
438- remove_field=>[headers, metadata, apiVersion, "@timestamp", kind, "@version", host]
439- }
440- mutate{
441- rename => {items=>event}
442- }
443- }
444- output{
445- file{
446- # Audit events from different users will be saved into different files.
447- path=>"/var/log/kube-audit-%{[event][user][username]}/audit"
448- }
449- }
433+ ` ` ` shell
434+ $ cat << EOF > /etc/logstash/config
435+ input{
436+ http{
437+ #TODO, figure out a way to use kubeconfig file to authenticate to logstash
438+ #https://www.elastic.co/guide/en/logstash/current/plugins-inputs-http.html#plugins-inputs-http-ssl
439+ port=>8888
440+ }
441+ }
442+ filter{
443+ split{
444+ # Webhook audit backend sends several events together with EventList
445+ # split each event here.
446+ field=>[items]
447+ # We only need event subelement, remove others.
448+ remove_field=>[headers, metadata, apiVersion, "@timestamp", kind, "@version", host]
449+ }
450+ mutate{
451+ rename => {items=>event}
452+ }
453+ }
454+ output{
455+ file{
456+ # Audit events from different users will be saved into different files.
457+ path=>"/var/log/kube-audit-%{[event][user][username]}/audit"
458+ }
459+ }
460+ ` ` `
461+
4504621. start logstash
451463
452- $ bin/logstash -f /etc/logstash/config --path.settings /etc/logstash/
464+ ` ` ` shell
465+ $ bin/logstash -f /etc/logstash/config --path.settings /etc/logstash/
466+ ` ` `
467+
4534681. create a [kubeconfig file](/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/) for kube-apiserver webhook audit backend
454469
455- $ cat <<EOF > /etc/kubernetes/audit-webhook-kubeconfig
456- apiVersion: v1
457- clusters:
458- - cluster:
459- server: http://<ip_of_logstash>:8888
460- name: logstash
461- contexts:
462- - context:
463- cluster: logstash
464- user: ""
465- name: default-context
466- current-context: default-context
467- kind: Config
468- preferences: {}
469- users: []
470- EOF
470+ ` ` ` shell
471+ $ cat <<EOF > /etc/kubernetes/audit-webhook-kubeconfig
472+ apiVersion: v1
473+ clusters:
474+ - cluster:
475+ server: http://<ip_of_logstash>:8888
476+ name: logstash
477+ contexts:
478+ - context:
479+ cluster: logstash
480+ user: ""
481+ name: default-context
482+ current-context: default-context
483+ kind: Config
484+ preferences: {}
485+ users: []
486+ EOF
487+ ` ` `
488+
4714891. start kube-apiserver with the following options:
472490
473- --audit-policy-file=/etc/kubernetes/audit-policy.yaml --audit-webhook-config-file=/etc/kubernetes/audit-webhook-kubeconfig
491+ ` ` ` shell
492+ --audit-policy-file=/etc/kubernetes/audit-policy.yaml --audit-webhook-config-file=/etc/kubernetes/audit-webhook-kubeconfig
493+ ` ` `
494+
4744951. check audits in logstash node' s directories /var/log/kube-audit-*/audit
475496
476497Note that in addition to file output plugin, logstash has a variety of outputs that
0 commit comments