Collect Kubernetes audit logs to track everything that happens inside your Kubernetes clusters, including every call made to the Kubernetes API by any service. This includes the control plane (built-in controllers, the scheduler), node daemons (the kubelet, kube-proxy, and others), cluster services (such as the cluster autoscaler), users making kubectl requests, and even the Kubernetes API itself.
With the Kubernetes audit logs integration, you can diagnose permission issues, identify RBAC policies that need to be updated, and track slow API requests that are impacting your whole cluster. Deep dive into these topics with the Datadog talk at KubeCon 2019.
Create the policy file at /etc/kubernetes/audit-policies/policy.yaml to specify the types of API requests you want to capture in your audit logs. Audit policy rules are evaluated in order. The API server follows the first matching rule it finds for each type of operation or resource. Example of an audit policy:
# /etc/kubernetes/audit-policies/policy.yamlapiVersion:audit.k8s.io/v1kind:Policyrules:# do not log requests to the following- level:NonenonResourceURLs:- '/healthz*'- '/logs'- '/metrics'- '/swagger*'- '/version'# limit level to Metadata so token is not included in the spec/status- level:MetadataomitStages:- RequestReceivedresources:- group:authentication.k8s.ioresources:- tokenreviews# extended audit of auth delegation- level:RequestResponseomitStages:- RequestReceivedresources:- group:authorization.k8s.ioresources:- subjectaccessreviews# log changes to pods at RequestResponse level- level:RequestResponseomitStages:- RequestReceivedresources:# core API group; add third-party API services and your API services if needed- group:''resources:['pods']verbs:['create','patch','update','delete']# log everything else at Metadata level- level:MetadataomitStages:- RequestReceived
This example policy file configures the API server to log at the highest level of detail for certain types of cluster-changing operations (update, patch, create, delete). It also tracks requests to the subjectaccessreviews resource at the highest level to help troubleshoot authentication delegation issues.
You may want to reduce the level of verbosity to Metadata for endpoints that contain sensitive data, such as the tokenreviews resource. Datadog also omits the RequestReceived stage from logs.
In the last section, for everything that was not explicitly configured by the previous rules, the policy is configured to log at Metadata level. As audit logs might be verbose, you can choose to exclude less critical actions/verbs, such as operations that don’t change the cluster state like list, watch, and get.
Log collection is disabled by default. Enable it in the env section of your DaemonSet:
env:# (...)- name:DD_LOGS_ENABLEDvalue:'true'
Mount the audit log directory as well as a directory that the Agent uses to store a pointer to know which log was last sent from that file. To do this, add the following in the volumeMounts section of the daemonset: