I Built an AI Agent That Can Query My Kubernetes Cluster, But Never Break It
The article describes a safe, read-only AI agent for querying Kubernetes clusters using local LLMs (Qwen3 via Ollama) and fixed Python functions wrapping kubectl commands. Safety is enforced through two layers: the model only expresses intent by selecting from predefined tools (never generating raw commands), and Kubernetes RBAC restricts the ServiceAccount to get/list/watch permissions only. A key technical challenge was networking within Kubernetes pods, requiring direct IP addressing of the D
Analysis
TL;DR
- The article describes a safe, read-only AI agent for querying Kubernetes clusters using local LLMs (Qwen3 via Ollama) and fixed Python functions wrapping kubectl commands.
- Safety is enforced through two layers: the model only expresses intent by selecting from predefined tools (never generating raw commands), and Kubernetes RBAC restricts the ServiceAccount to get/list/watch permissions only.
- A key technical challenge was networking within Kubernetes pods, requiring direct IP addressing of the Docker bridge gateway and container names instead of localhost or published ports.
- Self-hosted smaller LLMs showed unreliability in tool-calling compared to hosted models, with issues including plain-text JSON output and incomplete calls, necessitating fallback parsers and careful schema management.
Why It Matters
This work provides a practical blueprint for deploying AI agents in sensitive infrastructure environments where safety is paramount. It demonstrates that robust security can be achieved not just by trusting the model's reasoning, but by architecting the system so that the model's capabilities are strictly bounded by both application-level function selection and infrastructure-level permission controls. This approach is highly relevant for DevOps teams looking to integrate AI assistants without introducing risk of accidental or malicious cluster modification.
Technical Details
- Architecture: Browser dashboard → FastAPI/ask endpoint → Local Ollama instance running Qwen3 → Fixed Python functions (get_pods, get_logs, describe_pod, get_deployments, get_services, get_nodes) → Kubernetes API → Model generates natural language response.
- Safety Mechanism 1 (Application Layer): The model never executes commands directly; it selects one of six predefined functions with parameters (e.g., pod name). Each function wraps a single hardcoded kubectl command template, preventing command injection or chaining.
- Safety Mechanism 2 (Infrastructure Layer): The agent runs under a Kubernetes ServiceAccount bound to a ClusterRole granting only get, list, and watch permissions on specific resources (pods, logs, deployments, services, nodes). Write operations and secret access are explicitly denied and verified via attempted write rejection.
- Networking Solution: Resolved pod-to-host communication by discovering the Docker bridge network gateway IP (via
ip aon br-xxxxxx interface) and setting OLLAMA_URL to this IP. For Kubernetes API, connected directly to the control-plane container by name on port 6443 after attaching to the same Docker network. - Model Limitations: Self-hosted LLMs exhibited inconsistent tool-calling behavior—sometimes outputting unstructured JSON instead of invoking the tool mechanism, or failing to complete valid tool requests. Schema mismatches (e.g., get_nodes registered backend but missing in sent schema) caused reasoning without execution capability. Model switching (llama3.2 → llama3.1 → qwen3) improved reliability.
Industry Insight
AI agents managing critical infrastructure must adopt a "defense-in-depth" strategy combining constrained model interfaces (intent-only selection over command generation) with strict least-privilege IAM policies at the platform level. Developers should anticipate significant non-model challenges when deploying agents inside containers—particularly networking complexities in orchestrated environments like Kubernetes—and validate permissions empirically rather than assuming configuration correctness. Additionally, self-hosted smaller models require rigorous testing and fallback logic for tool invocation due to inherent instability in structured output generation compared to large hosted alternatives.
Disclaimer: The above content is generated by AI and is for reference only.