Isolating LLM Code Execution in Kubernetes
If you build an AI agent that writes and runs Python scripts, where does that code actually run? Most teams start by spinning up a regular container worker in Kubernetes and piping generated scripts straight into a subsh
If you build an AI agent that writes and runs Python scripts, where does that code actually run? Most teams start by spinning up a regular container worker in Kubernetes and piping generated scripts straight into a subshell.
That setup feels isolated, but it relies on a dangerous assumption. Standard containers share the host operating system kernel NIST container security guide. When an agent executes untrusted, model-generated code, a single prompt injection can turn a worker pod into a complete node takeover.
Here is why standard containers fall short, and how to build a reliable sandbox in Kubernetes across three practical layers.
The default container trap
Traditional microservices run deterministic code that engineers wrote, reviewed, and packaged into an image. In that setup, standard Linux namespaces and cgroups do a good job keeping workloads separated.
LLM agents invert that pattern. They run dynamic, unreviewed instructions generated on the fly. If an attacker injects commands through a prompt injection or a tainted document, that script executes directly against the host Linux kernel.
Mounting the host Docker socket (/var/run/docker.sock) into the worker is a dangerous shortcut teams take to let agents run subsidiary containers. Any subshell command can then talk to the Docker daemon directly to spawn sibling containers with the host filesystem mounted:
curl -s -X POST --unix-socket /var/run/docker.sock \
-H "Content-Type: application/json" \
-d '{"Image": "alpine", "HostConfig": {"Binds": ["/:/host:rw"]}}' \
http://localhost/containers/create
Even without mounting the socket, kernel privilege escalation bugs or runtime escape flaws such as CVE-2024-21626 advisory allow untrusted code to break out of standard runc containers.
When reviewing these architectures, tracking how untrusted data moves from the model to underlying infrastructure sinks is vital. You can see how to trace these handoffs in CodeReviewLab's guide to application architecture for code review.
To run untrusted AI scripts safely, you need three distinct barriers: kernel isolation, pod privilege restriction, and network egress control.
Barrier 1: Kernel isolation with gVisor or microVMs
The first step is decoupling agent commands from the host Linux kernel. Kubernetes lets you replace the default runtime using the runtimeClassName field.
You have two main cloud-native options:
-
User-space application kernels (Google gVisor /
runsc): gVisor replaces direct host kernel calls with an application kernel named Sentry gVisor architecture guide. Sentry intercepts and emulates Linux system calls in Go, while a helper process named Gofer handles file access. This shrinks the attack surface exposed to the host. The trade-off is higher CPU overhead on heavy file input and output. - Hardware-assisted microVMs (AWS Firecracker / Kata Containers): MicroVMs boot an isolated, minimalist Linux guest kernel inside a hardware virtualisation boundary via KVM MicroVM sandboxing benchmarks. Kata Containers manages this directly inside Kubernetes. MicroVMs offer hypervisor-grade isolation with boot times around 100 milliseconds, but the underlying worker nodes must support hardware virtualisation.
For most agent workloads running Python or Bash scripts, gVisor offers a practical compromise between quick boot times and robust kernel containment.
Barrier 2: Pod privilege lockdown
Kernel isolation alone is not enough if your Pod runs with elevated privileges or mounts cluster credentials. You need to harden the Pod specification itself.
Start by disabling automatic mounting of the Kubernetes service account token. By default, every pod receives a token that can query the cluster API. An attacker who gains a shell inside the container can use that token to inspect the cluster.
Next, drop all Linux capabilities, force the container to run as a non-root UID, and set the root filesystem to read-only. When using a read-only root filesystem with Python, set PYTHONDONTWRITEBYTECODE so the interpreter does not fail when attempting to write bytecode caches to disk. Provide explicit labels so egress policies can find the pod, and mount an in-memory tmpfs volume with a strict size limit for scratch files:
apiVersion: v1
kind: Pod
metadata:
name: secure-code-runner
labels: { role: agent-sandbox }
spec:
runtimeClassName: gvisor
automountServiceAccountToken: false
securityContext:
runAsNonRoot: true
runAsUser: 10001
containers:
- name: python-interpreter
image: sandbox-python:1.0.0
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities: { drop: ["ALL"] }
env:
- { name: PYTHONDONTWRITEBYTECODE, value: "1" }
volumeMounts:
- { mountPath: /tmp, name: scratch }
volumes:
- name: scratch
emptyDir: { medium: Memory, sizeLimit: 128Mi }
Never inject static database keys or cloud provider secrets into this pod via environment variables. If an untrusted script runs os.environ, all injected credentials are immediately visible.
Barrier 3: Network egress filtering
A sandboxed pod can still do serious damage if outbound networking is open. If an agent can make unrestricted HTTP requests, an attacker can exploit it in two common ways:
-
Cloud credential theft: The agent queries
http://169.254.169.254/latest/meta-data/to pull cloud IAM role credentials assigned to the worker node LLM sandbox escape patterns. While IMDSv2 and hop limits mitigate unauthenticated access, deterministic Layer 4 blackholing guarantees credentials cannot leak even if instance metadata settings are misconfigured. - Data exfiltration: The script posts sensitive prompts, local files, or environment data directly to an external server.
A Kubernetes NetworkPolicy blocks this by enforcing default-deny egress. You can allow internal DNS resolution while restricting outbound TCP traffic exclusively to an authenticated internal broker:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: sandbox-egress-lockdown
spec:
podSelector:
matchLabels: { role: agent-sandbox }
policyTypes: ["Ingress", "Egress"]
ingress: []
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 }
- to:
- podSelector:
matchLabels: { app: agent-tool-broker }
ports:
- { protocol: TCP, port: 8443 }
Notice that the DNS rule explicitly selects the kube-system namespace. In Kubernetes, omitting namespaceSelector restricts the pod selector to the local namespace, which breaks cluster DNS lookups. Allowing both UDP and TCP on port 53 prevents intermittent failures when DNS responses exceed 512 bytes.
Remember that standard Kubernetes NetworkPolicies operate at Layer 3 and Layer 4 Tetrate egress security guide. They filter on IP addresses and ports, not domain names or URLs. If your agent must fetch packages from PyPI or call external APIs, route traffic through an outbound Layer 7 proxy like Envoy to enforce hostname allowlists.
Handling unvetted network calls and untrusted output requires careful guardrails across the entire system. You can explore how raw model responses trigger downstream risks in CodeReviewLab's guide to LLM output security.
Review checklist for agent sandboxes
When reviewing pull requests that introduce code execution workers, check these five controls before shipping:
-
Runtime class: Check that the pod defines an isolated runtime, such as
runtimeClassName: gvisororkata, rather than the defaultrunc. -
Volume mounts: Ensure
/var/run/docker.sockand host directories are never mounted. -
Service tokens: Verify that
automountServiceAccountToken: falseis set on the Pod specification. -
Read-only root: Confirm
readOnlyRootFilesystem: trueis present,PYTHONDONTWRITEBYTECODE: "1"is configured for Python workloads, and scratch files are limited to an in-memorytmpfs. -
Metadata blackholing: Confirm that curling
169.254.169.254from inside the sandbox times out.
Spotting misconfigurations before they reach production takes regular practice. You can build your instincts across real code review scenarios on CodeReviewLab
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.