How to Configure Mastra Kubernetes Sandbox

Published
Length
3 min read

Luis Felipe Dias

All posts

You configure a mastra kubernetes sandbox by creating a dedicated namespace and binding a scoped Role to the agent service account this setup isolates container workloads and restricts which cluster apis the agent framework can call. Teams often start by granting broad cluster grants so agents can spin up pods and run tasks fast. Overly broad roles let an agent read secrets or modify workloads outside its designated sandbox. How a mastra kubernetes sandbox works with native RBAC A mastra kube

Kubernetes RBAC + AI Sandbox Environments: the complete guide

Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.

Free white paper. No spam. Unsubscribe anytime.

You configure a mastra kubernetes sandbox by creating a dedicated namespace and binding a scoped Role to the agent service account this setup isolates container workloads and restricts which cluster apis the agent framework can call. Teams often start by granting broad cluster grants so agents can spin up pods and run tasks fast. Overly broad roles let an agent read secrets or modify workloads outside its designated sandbox.

How a mastra kubernetes sandbox works with native RBAC

A mastra kubernetes sandbox relies on native Kubernetes role-based access control to isolate agent execution in Kubernetes, grants are purely additive, which means there are no deny rules. You must start with zero rights and grant only the exact verbs and resources the agent needs.

First, create a dedicated namespace for the agent workloads. A Role sets grants within one exact namespace. A ClusterRole covers cluster-scoped resources or all namespaces for an isolated sandbox. Define a Role inside your target namespace rather than a ClusterRole. Next, create a RoleBinding in that same namespace to attach the role to the service account used by Mastra.

When the agent needs to run code or inspect output inside a pod. It interacts with Kubernetes subresources. RBAC refers to resources by the exact name in the API URL. Writing subresources with a slash. You must grant grants for the pods/log subresource to read output and the pods/exec subresource to run commands without these explicit subresource rules. The API server blocks the agent from running code inside the container.

What native RBAC solves and what it leaves open

Native Kubernetes RBAC controls which identity can call. Exact API endpoints and subresources. It ensures that a Mastra agent in one namespace cannot list. Pods or read secrets in another namespace by scoping the RoleBinding to a single namespace. You contain the blast radius at the cluster orchestration layer.

Still, Kubernetes RBAC only checks grants the API request begins. Once the API server authorizes a pods/exec request, RBAC steps out of the way. It does not inspect the actual shell commands. Scripts, or payloads that the agent executes inside the container an agent with exec grants can run destructive commands. Install blocked packages, or pull sensitive data inside that pod.

Kubernetes RBAC + AI Sandbox Environments: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Also, Kubernetes does not allow you to restrict. Create requests on subresources to exact resource names if an agent has grants to use pods/exec in a namespace that grants applies across the allowed scope. Native controls decide who can enter the sandbox. Not what single commands do in flight.

Securing agent execution inside the cluster

To run autonomous agents safely. Teams need controls that operate below the Kubernetes API access layer. Start by separating the keys that manage pod lifecycles from the keys that execute commands inside containers do not mount cluster admin tokens or broad service account tokens inside the sandbox pods themselves.

Pair namespace isolation with strict network policies so sandbox pods cannot reach internal metadata services or production databases. When humans or automated workflows run commands inside the cluster route those sessions through an inspection layer. This approach lets security teams approve sensitive actions and redact secrets from output. The caller sees them.

Add request guardrails and response masking beside Kubernetes

Cluster roles and service accounts govern who reaches the Kubernetes API. Still, a broad role or agent can delete workloads or print sensitive values from ConfigMaps and logs. In practice, hoop.dev runs beside the API server as a sidecar for your Kubernetes API and checks every request and response alongside RBAC.

With hoop.dev Kubernetes controls, guardrails can allow listing Secret names while refusing reads of Secret data before execution. Data masking redacts matched keys in returned JSON responses and log streams in memory. Furthermore, hoop.dev holds SOC 2 Type II and is MIT licensed on GitHub.

Frequently asked questions

Should I use a Role or a ClusterRole for a Mastra Kubernetes sandbox?

Use a Role paired with a RoleBinding. Inside a dedicated namespace. A Role restricts grants to a single namespace. Whereas a ClusterRoleBinding grants access across the entire cluster.

Why can my agent create pods but fail to run commands inside them?

Kubernetes treats command execution and log reading as separate subresources in the API. You must explicitly add the pods/exec and pods/log subresources to your RBAC Role rules.

Can Kubernetes RBAC block specific shell commands inside a sandbox pod?

No, Kubernetes RBAC only authorizes the initial API. Request to open an exec session. It does not inspect or filter the shell commands that run inside the container the session starts.

Save the open-source gateway.

hoop is MIT-licensed infrastructure for controlling how AI agents reach production data. Star hoophq/hoop so you can inspect it, deploy it, or share it when your team starts governing agent access.