This repository holds the code for K8NTEXT, a project that aims to enhance Kubernetes audit logs by correlating them. The goal is to provide a more comprehensive understanding of the events occurring in a Kubernetes cluster by linking related audit log entries together.
The following files are available:
README.md: this file;parseLog: the source code for K8NTEXT, which includes the logic for parsing and correlating audit logs;analysis: contains a script for comparing the results of the clustering process, including an HTML visualizer;data-collection: scripts used to collect the dataset from a Kubernetes cluster;plots: scripts for generating plots and visualizations from the results;tests: some shell scripts for evaluating K8NTEXT. The data is then fed to theplotsscripts.scripts: miscellaneous scripts used for various tasks. Not fundamental to the project.
Kubernetes audit logs provide a detailed record of the activities occurring within a Kubernetes cluster. However, these logs can be overwhelming due to their verbosity and lack of context. K8NTEXT addresses this challenge by correlating related audit log entries, thereby enhancing the interpretability of the logs.
From a high-level perspective, K8NTEXT works as follows:
- Audit logs are collected from a Kubernetes cluster with auditing enabled. Logs are exposed in JSON format with a specific structure which is defined by the Kubernetes API.
- Data is automatically preprocessed to reorder and clean the logs. Then, a ML model automatically divides the logs in batches and applies labels to them. A majority voting system is used to assign a label to each log entry from the multiple predictions made on each batch.
- Once the logs have been labeled, each label group is further divided into clusters using a custom clustering algorithm, which groups together related log entries based on a series of heuristics such as time proximity, resource similarity, user identity, and more.
- After processing, the logs with their labels and UUIDs can be used in
downstream tasks, such as visualization and analysis. The
analysisdirectory contains a script that generates an HTML file with a timeline view of the clustered events, allowing for easy exploration and analysis of the relationships between different log entries.
After processing, the logs are enriched with additional fields that indicate their labels and cluster assignments. This enriched data can then be used for further analysis and visualization.
To get started with K8NTEXT, follow these steps:
-
Clone the repository:
git clone https://github.com/daisyfbk/k8ntext.git
-
Navigate to the project directory:
cd k8ntext -
Install the required dependencies:
pip install -r requirements.txt # If on macOS, use requirements-macos.txt instead
The dataset is in the Releases section of this repository, due to its size. Download it and extract it to a folder of your choice (e.g., audit-log). The dataset can be also be created using the data-collection scripts (see below).
In order to train a model:
-
cdintoparseLog:cd parseLog -
Use
model.pyto train a model:python3 model.py -f $DATASET_FILEwhere
$DATASET_FILEis a JSON file containing a labeled dataset. By default,model.pyrunsSTATISTICS_ATTEMPTStraining attempts with different train/test splits, saves each trained model inout/attempt_*, and writes aggregate statistics inout.If you want a single training run instead, disable statistics:
python3 model.py -f $DATASET_FILE --no-statisticsUse
--kfoldsto switch to k-fold cross-validation, or--no-saveto skip saving trained models during training and labeled datasets during inference.
The model can be deeply customized by editing the parameters.py file. The features used for training are in model_features.py. For example, in parameters.py, the key of the label can be changed by modifying the LABEL_KEY variable, which is set to label by default.
For convenience, our dataset is available in the audit-log directory. The train-test-validation split is automatically done by model.py.
Once a model has been trained, it can be used to make predictions on new data.
-
cdintoparseLog:cd parseLog -
Use
model.pyto make predictions:python3 model.py -m $MODEL_FILE -f $DATASET_FILE
where
$MODEL_FILEis the path to the trained model (for example,out/model.kerasafter--no-statistics, orout/attempt_0/model_0.keraswith the default training flow) and$DATASET_FILEis a JSON file containing the dataset to be used for inference. The predictions will be saved in theoutdirectory, and inference also writeslabeled.jsonby default unless--no-saveis set. The same dataset format used for training is used for inference.
Once predictions have been made, the results can be clustered using the cluster.py script.
-
cdintoparseLog:cd parseLog -
Use
cluster.pyto cluster the results:python3 cluster.py -f $DATASET_FILE [-k $LABEL_KEY]
where
$DATASET_FILEis the JSON file containing the dataset with predictions,-k $LABEL_KEYis an optional argument to specify the key used for labels (default islabel, but if you are running it on a labeled dataset, you might want to set it topredicted_label). The results are output to theoutdirectory.
The results of the clustering process can be visualized using the visualize_timeline.py script under analysis. This script generates an HTML file that provides a timeline view of the clustered events, allowing for easy exploration and analysis of the relationships between different log entries.
To visualize the results:
-
cdintoanalysis:cd analysis -
Use
visualize_timeline.pyto generate the visualization:python3 visualize_timeline.py -f $LOG_FILE -o $OUTPUT_FILE --scale $SCALE --palette $PALETTE
where
-fspecifies the input JSON file containing the clustered results,-ospecifies the output HTML file,--scaleadjusts the time scale of the timeline, and--paletteallows you to choose a color palette for the visualization (you can choose fromcluster,verb,user, orresource).
In the docs/additional-features.md file, you can find documentation on additional features and scripts. This includes scripts for labeling logs, testing the labeling process, and generating plots from the results.
This software is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International license. More information is available in the LICENSE file.
When citing this project, please use the following citation:
[1] M. Franzil, V. Armani, L. A. Dias Knob, and D. Siracusa, ‘Sharpening Kubernetes Audit Logs with Context Awareness’, Computer Networks, p. 111890, Nov. 2025, doi: 10.1016/j.comnet.2025.111890. Available: https://www.sciencedirect.com/science/article/pii/S1389128625008564.
The authors of this project are:
- Matteo Franzil, University of Trento and Fondazione Bruno Kessler -
matteo.franzil@unitn.it - Valentino Armani, Fondazione Bruno Kessler -
varmani@fbk.eu - Luis Augusto Dias Knob, Fondazione Bruno Kessler -
l.diasknob@fbk.eu - Domenico Siracusa, University of Trento and Fondazione Bruno Kessler -
domenico.siracusa@unitn.it
