This post is part of the Kubezilla sandboxing series: Part 1: The two layers · Part 2: The cluster layer · Part 3: The runtime layer and Docker Sandboxes.
In Part 1 we split sandboxing into two layers. This post covers the first one: the cluster layer. Its job is simple to say and easy to get wrong: limit what an agent workload can reach and change, even when it behaves badly.
None of these controls stop a kernel exploit. That is the runtime layer’s job. What they do is make sure that a confused or hijacked agent has very little to work with.
1. Give every agent its own namespace
A namespace is the unit that most Kubernetes policy attaches to. Put each agent, team or session into its own namespace so quotas, network rules and Pod Security labels apply cleanly.
apiVersion: v1
kind: Namespace
metadata:
name: agent-sandbox-01
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/enforce-version: latest
pod-security.kubernetes.io/warn: restricted
The labels turn on Pod Security Admission at the restricted level. Pods that run as root, ask for privilege escalation, use host namespaces or mount host paths are rejected.
2. Use a dedicated ServiceAccount and drop the token
By default, pods get a service account token mounted into the container. For most agents that run code, this is the first thing an attacker would look for. If the agent does not need the Kubernetes API, do not give it a token.
apiVersion: v1
kind: ServiceAccount
metadata:
name: agent-runner
namespace: agent-sandbox-01
automountServiceAccountToken: false
If the agent does need the API, bind the smallest possible Role in its own namespace. Never use a ClusterRole binding for agent workloads.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: agent-read-pods
namespace: agent-sandbox-01
rules:
- apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: agent-read-pods
namespace: agent-sandbox-01
subjects:
- kind: ServiceAccount
name: agent-runner
namespace: agent-sandbox-01
roleRef:
kind: Role
name: agent-read-pods
apiGroup: rbac.authorization.k8s.io
Check what the account can really do with kubectl auth can-i --list --as=system:serviceaccount:agent-sandbox-01:agent-runner -n agent-sandbox-01.
3. Deny all traffic, then allow what is needed
Egress is where agents leak data. A coding agent may only need a package registry and an LLM endpoint. Start from zero.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: agent-sandbox-01
spec:
podSelector: {}
policyTypes: ["Ingress", "Egress"]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
namespace: agent-sandbox-01
spec:
podSelector: {}
policyTypes: ["Egress"]
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
Plain NetworkPolicy works on IPs and ports, not domain names. If you need rules like “allow api.openai.com only”, use a CNI that supports DNS aware policy (for example Cilium with FQDN rules) or send all egress through an egress proxy that enforces an allow list. This is the same idea Docker Sandboxes uses on a laptop, where every outbound connection goes through a policy proxy.
Also block the cloud metadata endpoint (169.254.169.254). It is a common way to steal node credentials.
4. Set resource limits
Agents loop. Sometimes they loop forever. Quotas and default limits keep one agent from starving the rest of the node.
apiVersion: v1
kind: ResourceQuota
metadata:
name: agent-quota
namespace: agent-sandbox-01
spec:
hard:
pods: "5"
requests.cpu: "2"
requests.memory: 4Gi
limits.cpu: "4"
limits.memory: 8Gi
---
apiVersion: v1
kind: LimitRange
metadata:
name: agent-defaults
namespace: agent-sandbox-01
spec:
limits:
- type: Container
default:
cpu: "1"
memory: 1Gi
defaultRequest:
cpu: 250m
memory: 256Mi
5. Harden the pod spec
Pod Security Admission rejects bad pods, but it helps to write good ones from the start.
spec:
serviceAccountName: agent-runner
automountServiceAccountToken: false
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
containers:
- name: agent
image: your-registry/agent:1.0
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
volumeMounts:
- name: work
mountPath: /workspace
volumes:
- name: work
emptyDir:
sizeLimit: 2Gi
A read only root filesystem plus a small emptyDir workspace means the agent can write its files, but cannot change its own binaries.
6. Put sandboxes on their own nodes
Even with strong runtimes, it is good practice to keep untrusted workloads away from your core services. Create a node pool for sandboxes, taint it, and only let sandbox pods tolerate it.
kubectl taint nodes sandbox-node-1 workload=sandbox:NoSchedule
kubectl label nodes sandbox-node-1 workload=sandbox
spec:
nodeSelector:
workload: sandbox
tolerations:
- key: workload
operator: Equal
value: sandbox
effect: NoSchedule
This is also where you install gVisor or Kata, which is the topic of the next post.
7. Enforce it with admission policy
People forget labels. Use admission control so the rules hold even when someone copies an old manifest. ValidatingAdmissionPolicy is built into Kubernetes, and Kyverno or OPA Gatekeeper give you more options. Good rules to enforce for agent namespaces:
- Pods must set
runtimeClassNameto an approved sandbox runtime. - Pods must not mount a service account token unless they carry an approved label.
- Images must come from your trusted registry and, ideally, be signed.
- No
hostPath,hostNetworkor privileged containers.
8. Watch what happens
Turn on Kubernetes audit logs for agent namespaces and keep network flow logs if your CNI supports them. When an agent does something odd, you want to answer “what did it call, and where did it connect?” in minutes, not days.
Cluster layer summary
| Control | What it limits |
|---|---|
| Namespace + Pod Security labels | Privileged and root pods |
| ServiceAccount, no token, small Role | Access to the Kubernetes API |
| Default deny NetworkPolicy | Data leaks and lateral movement |
| ResourceQuota + LimitRange | Runaway CPU, memory and pod counts |
| Hardened securityContext | Changes to the container itself |
| Dedicated tainted node pool | Neighbours on the same node |
| Admission policy | Drift and copy paste mistakes |
| Audit and flow logs | Blind spots after an incident |
All of this still assumes the container cannot break out of the kernel. For code written by an LLM, that is a big assumption. Part 3 covers the runtime layer: gVisor, Kata Containers, microVMs and Docker Sandboxes (sbx).

One thought on “The Cluster Layer of Kubernetes Sandboxing: Namespaces, RBAC, NetworkPolicy and Pod Security for AI Agents”