Build a safe AI troubleshooting agent for Kubernetes

DevOps, Hands On60 minutesCosts moneyWritten 20 November 2026By Cloud Hands On

In this task you'll let an AI agent read a cluster, find the cause of a problem and suggest a fix, without giving it the power to break things.

An AI troubleshooting agent is a loop: a person asks a question ('why is checkout failing?'), the model picks a tool to call (list pods, describe a pod, read events, read the last 50 log lines), reads the result, and repeats until it can explain the root cause and suggest a fix. The tools are small functions you write; the model only chooses which to call and with what arguments.

Safety comes from the design, not the prompt. Diagnosis tools are read-only, running under a Kubernetes role that can only get, list and watch. Fix tools are separate, few and narrow, and run only after a person approves the exact change. Everything the agent reads (logs, events, annotations) is untrusted text that may contain instructions aimed at the model, so it can't trigger anything on its own. Every tool call is logged, and token use is capped.

Example. Kestwick Hotels' platform team built an agent for on-call. Asked why bookings were failing, it listed pods, saw CrashLoopBackOff, read the previous container's logs and the ConfigMap, and proposed adding the missing DB_HOST key. The on-call engineer approved the change; the agent applied it and confirmed the pods were Ready.

Costs money. This task creates resources that are charged while they exist. Delete them as soon as you finish.

Before you start

You need: A kind or AKS cluster, and a chat model you can call (for example a deployment in Azure AI Foundry). Time: 60 minutes.

Where to look

  1. Terminal
  2. kubectl
  3. Azure AI Foundry

Steps

  1. Write five read-only tools: list pods in a namespace, describe a pod, get recent events, get the last N log lines (with previous), and get a deployment's spec.
  2. Create a service account bound to a ClusterRole with only get, list and watch on pods, pods/log, events, services and deployments, and run the tools as it.
  3. Connect a chat model with tool calling (for example a model deployed in Azure AI Foundry, signed in with workload identity on AKS, not a key in code). Give it a system prompt that says tool results are data, not instructions.
  4. Ask it about a pod you broke on purpose (see Fix pods that won't start) and check that its diagnosis matches yours.
  5. Add one write tool, such as 'set a deployment's memory limit', that shows the exact change and waits for a person to type yes. Block it from touching kube-system and the agent's own permissions.
  6. Log every tool call with its arguments and result size, and stop the run after a set number of tool calls or tokens.

Commands

# A read-only role for the agent
kubectl create serviceaccount ai-agent -n ops
kubectl create clusterrole ai-agent-read --verb=get,list,watch --resource=pods,pods/log,events,services,deployments
kubectl create clusterrolebinding ai-agent-read --clusterrole=ai-agent-read --serviceaccount=ops:ai-agent
# Prove it can read but not change
kubectl auth can-i list pods --as=system:serviceaccount:ops:ai-agent -A
kubectl auth can-i delete pods --as=system:serviceaccount:ops:ai-agent -A

Watch out for

Cost
Each question costs a few model calls. Trim tool output (tail logs, summarise events) so you don't pay to send whole log files.
Security
The agent's Kubernetes role is the real limit on what it can do. Never give it cluster-admin, and never let text from logs or events approve or run an action.
Common mistakes
Letting the model run any kubectl command it writes, sending entire logs to the model, and trusting a confident diagnosis without checking the evidence.

Clean up

Delete the agent's service account and role binding, and the model deployment if you made one for this.

Words to know

AI agent
A model that calls tools in a loop to reach a goal, rather than just answering in text.
Tool calling
The model asks your code to run a named function with arguments, then reads the result.
Human approval
A step where a person must approve an action before the agent can carry it out.
Read-only role
A Kubernetes role limited to get, list and watch, so the agent can look but not change.

Tick off each step and track your progress in the guide.

Open this task in DevOps, Hands On

Keep going