K8sLogbotGoGPT is a command-line tool designed for Kubernetes administrators and DevOps teams. It assists in analyzing Kubernetes pod logs by leveraging advanced language models to identify and troubleshoot common issues, such as pod crashes, OOMKilled errors, and deployment failures. K8sLogbotGoGPT provides actionable insights to enhance cluster stability and performance.
Before running K8sLogbotGoGPT, set the necessary API keys:
export OPENAI_API_KEY=<your_openai_key>
export APIKEY=<your_K8s_key>
-log="partial_filename"
: Specify a partial log filename to match (e.g., "01-LOG").-stream
: Enable streaming output.-delay=milliseconds
: Set delay in milliseconds between streaming chunks (default is 50ms).-noninteractive
: Enable non-interactive mode for key point generation and full analysis.-output="filename.md"
: Specify the output Markdown file name (default is output.md).
Execute K8sLogbotGoGPT with or without additional options:
go run .
Analyze a specific log file with streaming output:
go run . -log="01-LOG" -stream
Perform non-interactive analysis and save output to a file:
go run . -log="01-LOG" -noninteractive -output="analysis.md"
Open a specific log file for review:
cat "LOGS/01-LOG-HIGH - Crashing pod K8s-controller-manager-controller-manager-758756d966-5q8pz in namespace kube-system.log"
Use K8sLogbotGoGPT’s step-by-step log analysis for detailed investigation:
go run . -log="01-LOG" -stream
Run analysis in a non-interactive mode and save the results in Markdown format:
go run . -log="01-LOG" -noninteractive -output="analysis.md"
Here’s a basic workflow using K8sLogbotGoGPT for Kubernetes log analysis:
- Initialize: Ensure your API keys are set.
- Start K8sLogbotGoGPT: Run
go run .
to start the analysis. - Provide Logs: Use options like
-log
,-stream
, and-noninteractive
to configure how logs are processed. - Review Output: Check the
analysis.md
file or the terminal output for insights.
K8sLogbotGoGPT's straightforward commands and powerful analysis make it a valuable tool for Kubernetes log analysis and troubleshooting.
- Ensure that you have Go installed and properly configured on your system to run K8sLogbotGoGPT.
- Replace
<your_openai_key>
and<your_K8s_key>
with your actual API keys. - For non-interactive analysis, the output will be saved in the specified Markdown file, which can be reviewed later.
This Go program is designed to interact with an API for generating key points and analyzing Kubernetes pod logs. It utilizes various packages to handle HTTP requests, JSON parsing, and Markdown rendering. Below is a breakdown of its main components and functionalities:
-
Message Struct: Represents each message in the conversation with a role (user or assistant) and content.
-
RequestBody Struct: Defines the structure of the API request body, including the model, messages, and streaming option.
-
ChatCompletionResponse Struct: Represents the structure of the API response, including details like ID, model used, choices made by the assistant, and usage statistics.
-
Functions:
handleNonStreamResponse
: Processes non-streaming responses from the API.handleStreamResponse
: Manages streaming responses with a delay for better readability.sendRequest
: Sends requests to the API and handles both streaming and non-streaming scenarios.generateLokiQueries
: Generates Loki query commands based on log content.
-
API Key Retrieval: The program retrieves necessary API keys from environment variables to authenticate requests.
-
Command-Line Flags: It accepts flags for log filename patterns, streaming options, delays between chunks, and output file specifications.
-
Log File Processing: The program searches for log files matching a specified pattern in a designated directory. It reads the contents of the first matching file.
-
Key Points Generation: It sends a request to generate key points from the log content using a structured prompt.
-
Non-Interactive vs Interactive Mode:
- In non-interactive mode, it performs full analysis and saves results to a Markdown file.
- In interactive mode, it allows users to engage in a chat session for further analysis based on generated key points.
-
Output Formatting: The program formats responses using Markdown for clarity and readability.
This Go program effectively integrates API interactions with Kubernetes log analysis, providing users with tools to extract insights and generate actionable queries while maintaining a user-friendly interface through command-line options.