kube-apiserver audit logs can be a useful resource to troubleshoot cluster issues as they record all the interactions of clients and services with the Kubernetes API.
In TKGi clusters, audit logs are stored by default inside the master nodes' "/var/vcap/sys/log/kube-apiserver/audit/log/" directory.
The default audit configuration, together with other kube-apiserver flags, can be viewed in "/var/vcap/data/jobs/kube-apiserver/<id>/config/bpm.yml" file.
At the time of writing this KB, the default audit logs retention policy looks as follows:
- "--audit-log-maxage=30"
- "--audit-log-maxbackup=10"
- "--audit-log-maxsize=100"
- "--audit-log-path=/var/vcap/sys/log/kube-apiserver/audit/log/audit.log"
- "--audit-policy-file=/var/vcap/jobs/kube-apiserver/config/audit_policy.yml"
Details on audit policy flags can be checked in the Kubernetes official Docs, Log backend.
There may be situations in which you'd want to customize the audit logs retention policy, for example, allowing more files to be stored before rotation takes place, or increasing the size of each of the audit log files.
This KB walks you through how to achieve this configuration through TKGi Kubernetes Profiles.
To configure the kube-apiserver audit logs retention policy you need to create a TKGi Kubernetes Profile and update your cluster with it.
Example of audit log retention Kubernetes Profile:
{ "name": "audit-logs", "description": "Audit Logs Profile", "experimental_customizations": [ { "component": "kube-apiserver", "arguments": { "audit-log-maxbackup": "20", "audit-log-maxsize": "200" } } ] }
In the above example we're indicating we want to keep up to 20 audit log files (audit-log-maxbackup), each of them with a max size of 200 MB (audit-log-maxsize). After an audit log file reaches 200 MB, it'll be rotated and a new one will be created in "/var/vcap/sys/log/kube-apiserver/audit/log/" directory.
The steps to configure your clusters with the above Kubernetes Profile are as follows:
# vim audit-logs-k8s-profile.json# tkgi create-k8s-profile audit-logs-k8s-profile.json# tkgi k8s-profiles# tkgi create-cluster CLUSTER-NAME --external-hostname HOSTNAME --plan PLAN-NAME --kubernetes-profile audit-logs# vim audit-logs-k8s-profile.json# tkgi create-k8s-profile audit-logs-k8s-profile.json# tkgi k8s-profiles# tkgi update-cluster CLUSTER-NAME --kubernetes-profile audit-logsIf you're updating a cluster that already has a k8s-profile applied to it and you want to retain that configuration, you need to create a new k8s-profile with both configurations combined, i.e. the existing k8s-profile configuration plus the new audit-logs configuration. Then you update the cluster with the new k8s-profile.
# tkgi cluster CLUSTER-NAME# tkgi k8s-profile <applied-k8s-profile-name> --json > new-combined-k8s-profile.json{ "name": "my-profile3", "uuid": "<UUID>", "owner": "admin", "description": "My profile description", "created_at": 1749031948, "customizations": [ { "component": "kube-apiserver", "arguments": { "service-node-port-range": "30000-40000" } } ], "experimental_customizations": [ { "component": "kubelet", "arguments": { "maximum-dead-containers": "1000", "feature-gates": "APIListChunking=true" } } ] }# vim new-combined-k8s-profile.json{ "name": "combined-k8s-profiles-audit-logs", "description": "My profile description", "customizations": [ { "component": "kube-apiserver", "arguments": { "service-node-port-range": "30000-40000" } } ], "experimental_customizations": [ { "component": "kubelet", "arguments": { "maximum-dead-containers": "1000", "feature-gates": "APIListChunking=true" } }, { "component": "kube-apiserver", "arguments": { "audit-log-maxbackup": "20", "audit-log-maxsize": "200" } } ] } # tkgi create-k8s-profile new-combined-k8s-profile.json# tkgi k8s-profiles# tkgi update-cluster CLUSTER-NAME --kubernetes-profile combined-k8s-profiles-audit-logs