Skip to main content
Audit logging in Kubernetes captures detailed records of all API server requests, helping you detect suspicious or unauthorized activities within your cluster. By defining audit policies, you can control which events to log, reducing noise and focusing on critical operations.
Audit logging is disabled by default in Kubernetes. Enabling it requires configuring the API server to use an audit policy and log backend.

Why Audit Logging Matters

  • Security: Track changes and identify unauthorized access.
  • Compliance: Maintain an immutable record of user actions.
  • Troubleshooting: Correlate events with incidents for diagnostics.

Viewing Falco Alerts

Before diving into Kubernetes-native auditing, you might already be using Falco to detect suspicious container activities. For example:
These alerts complement Kubernetes audit logs by surfacing container-level anomalies.

Kubernetes API Server Request Stages

Every API request flows through the server in four logical stages. You can choose to log specific stages via your audit policy:

Defining an Audit Policy

An audit policy is a YAML file that specifies which events to include or omit. Start with a minimal policy:
  • omitStages: Skip logging for specified stages (optional).
  • rules: A list of match conditions and the level of logging.

Audit Levels

Example: Log Pod Deletions in Production

This policy logs only DELETE operations on the Pod webapp-pod in prod-namespace at the full request/response level:
You can add another rule to capture all secret-related operations at the Metadata level:

Enabling Audit Logging

To activate auditing, point your API server to the audit policy and log file:

kubeadm-based Clusters

Edit the static pod manifest /etc/kubernetes/manifests/kube-apiserver.yaml:

systemd-based API Server

Add the same flags to the service unit file under the ExecStart section.
  • --audit-policy-file: Path to your YAML policy
  • --audit-log-path: Destination for audit logs
  • --audit-log-maxage: Retention days for old logs
  • --audit-log-maxbackup: Number of rotated files to keep
  • --audit-log-maxsize: Max size (MB) before rotation
After updating, restart the API server to apply changes.

Verifying Audit Logs

Delete the target Pod to generate an audit entry:
Inspect the logs:
You should see a delete event for webapp-pod in prod-namespace.
Use kubectl apply -f audit-policy.yaml to update your policy dynamically and trigger events for testing.

References

Watch Video

Practice Lab