API audit logging in RKE2 & Other Rancher K8s distribution
Unlike RKE1, RKE2 does not automatically enable kube-audit logs with a default policy. We need to add additional parameters or create it manually and update it in the UI.
Please check the documentation and the corresponding KB article for RKE2
https://www.suse.com/support/kb/doc/?id=000021171
How to collect the kube audit log along with Rancher logs:
https://www.suse.com/support/kb/doc/?id=000021022
Rancher doc:
https://ranchermanager.docs.rancher.com/how-to-guides/advanced-user-guides/enable-api-audit-log-in-downstream-clusters
PLEASE refer this GitHub issues for the history,like :
https://github.com/rancher/rke2/issues/1183#issuecomment-926155570
The below procedure is required when you want to enable it from cli. Earlier, there was no support form UI so we recommend to follow below procedure:
How to enable Kube-apiserver Audit logs?
The audit log of the Kubernetes API Server (kube-apiserver) is an essential component of Kubernetes security. It keeps track of all API server requests, including who initiated the request, what action was taken, and when it was completed. RKE2, Rancher's lightweight Kubernetes deployment, makes enabling the kube-API server audit log simple. This article guides the procedures to allow the kube-apiserver audit log in RKE2.
Step 1: Update the RKE2 Configuration File
The first step is to enable the audit log by updating the RKE2 config file. Accomplish this by executing the following command:
- echo "audit-policy-file: /etc/rancher/rke2/audit-policy.yaml" >> /etc/rancher/rke2/config.yaml
This command adds a new line to the RKE2 configuration file indicating the location of the audit policy file.
Step 2: Create an Audit Policy File
Create an audit policy file at the location specified in the RKE2 configuration file. Accomplish that by running the following command:
cat <<EOF > /etc/rancher/rke2/audit-policy.yaml
apiVersion: audit.k8s.io/v1
kind: Policy
rules:
- level: Metadata
EOF
This command creates a new file named audit-policy.yaml in the /etc/rancher/rke2/ directory with the provided audit policy. The above policy only records metadata-level audit events, which include request and response metadata.
Step 3: Restart RKE2 Server
Restart the RKE2 server after creating the audit policy file to apply the modifications.
- systemctl restart rke2-server
Step 4: Monitor Audit Log
Finally, monitor the audit log by running the following command:
- tail -f /var/lib/rancher/rke2/server/logs/audit.log
This command tails the audit log file in real-time, displaying the latest audit events as they occur.