kubectl for AI agents
kubectl for coding agents: inspect pods, read logs, diff manifests, and server-side dry-run applies, with context and namespace rules that prevent accidents.
What kubectl does for an agent
kubectl is the official Kubernetes command-line tool. It talks to a cluster's API server to list and describe resources, read logs, apply manifests, and roll out or roll back deployments.
Which cluster it talks to depends on the current kubeconfig context, so an agent should name the context and namespace on every command rather than trust whatever was last selected.
Official site: kubernetes.io
Install kubectl
The install commands from the official docs. Check it worked with kubectl version --client.
brew install kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && sudo install -m 0755 kubectl /usr/local/bin/kubectl
Signing in kubectl for an agent
kubectl reads cluster credentials from a kubeconfig file, ~/.kube/config by default or the files in KUBECONFIG. Cloud CLIs such as aws, gcloud, and az write those entries. Give agents a context bound to a read-only role.
kubectl config current-context
kubectl config get-contexts
kubectl auth can-i delete pods -n my-namespace
Environment variables: KUBECONFIG. Set them in the agent's environment; never paste values into prompts or commit them.
What agents use kubectl for
Example commands with placeholder names. Swap in your own project, app, or file names.
List pods in a namespace
kubectl --context my-cluster get pods -n my-namespace
Describe a failing pod
kubectl --context my-cluster describe pod my-pod -n my-namespace
Read recent logs
kubectl --context my-cluster logs deployment/my-app -n my-namespace --since=1h --tail=200
Diff local manifests against the cluster
kubectl --context my-cluster diff -f k8s/
Validate an apply on the server without changing anything
kubectl --context my-cluster apply -f k8s/ --dry-run=server
Safety rules for agents
Safe to run without asking
kubectl getkubectl describekubectl logskubectl diffkubectl apply --dry-run=serverkubectl auth can-i
Ask a human first
kubectl applykubectl deletekubectl scalekubectl rollout restart / undokubectl execkubectl edit
- Pass --context and -n on every command; never rely on the current context.
- Show kubectl diff or a --dry-run=server result before any apply.
- Never print Secret contents (kubectl get secret -o yaml) into chat or logs.
- Bind the agent's credentials to a read-only ClusterRole such as view.
AGENTS.md or CLAUDE.md snippet
Paste this into the instruction file your agent reads at the start of every session, then adjust the rules to your setup.
## kubectl Use `kubectl` for inspecting Kubernetes workloads, reading logs, and previewing manifest changes. - Check it is installed with `kubectl version --client`. If it is missing, ask before installing it. - Auth: a human signs kubectl in. Check with `kubectl config current-context`; if it fails, stop and ask. Never run the login flow yourself, and never print tokens (KUBECONFIG). - Run without asking (read-only): `kubectl get`, `kubectl describe`, `kubectl logs`, `kubectl diff`, `kubectl apply --dry-run=server`, `kubectl auth can-i`. - Ask a human first (changes or deletes things): `kubectl apply`, `kubectl delete`, `kubectl scale`, `kubectl rollout restart / undo`, `kubectl exec`, `kubectl edit`. - Pass --context and -n on every command; never rely on the current context. - Show kubectl diff or a --dry-run=server result before any apply. - Never print Secret contents (kubectl get secret -o yaml) into chat or logs. - Bind the agent's credentials to a read-only ClusterRole such as view. - Prefer JSON output where the command supports it, and keep output short.
kubectl or an MCP server?
No official MCP server. The Kubernetes project does not publish an official MCP server; several community servers wrap the API. Search the directory and compare trust scores, or let the agent use kubectl's read-only verbs.
Rule of thumb: in a terminal agent that a human has signed in, the CLI is usually faster and cheaper in context. In a chat client without a shell, or when you want a fixed set of typed tools, use an MCP server.
Other Cloud & deploy tools
kubectl questions
How do I install kubectl?
Homebrew: brew install kubectl. Other official options: Linux x86_64 (official binary). Check the install with kubectl version --client, and see the Kubernetes project (CNCF) docs for other platforms.
How does an AI agent authenticate kubectl?
kubectl reads cluster credentials from a kubeconfig file, ~/.kube/config by default or the files in KUBECONFIG. Cloud CLIs such as aws, gcloud, and az write those entries. Give agents a context bound to a read-only role. The agent can check the sign-in with kubectl config current-context and should stop and ask if it fails.
Which kubectl commands are safe for an agent to run?
Commands that only read state are safe to run without asking: kubectl get; kubectl describe; kubectl logs; kubectl diff; kubectl apply --dry-run=server; kubectl auth can-i. Ask a human before anything that changes or deletes something: kubectl apply; kubectl delete; kubectl scale; kubectl rollout restart / undo; kubectl exec; kubectl edit.
Is there an MCP server for kubectl?
The Kubernetes project does not publish an official MCP server; several community servers wrap the API. Search the directory and compare trust scores, or let the agent use kubectl's read-only verbs.