Skip to content

Commit a57a596

Browse files
add guides
1 parent b5ab85e commit a57a596

File tree

14 files changed

+1580
-0
lines changed

14 files changed

+1580
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: "Guides"
3+
linkTitle: "Guides"
4+
weight: 20
5+
description: >
6+
This page provides a collection of official guides and code examples for integrating the agent sandbox into your projects.
7+
menu:
8+
main:
9+
weight: 20
10+
---
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
title: "Arbitrary Code Execution"
3+
linkTitle: "Arbitrary Code Execution"
4+
weight: 2
5+
description: >
6+
This guide shows how to deploy a simple Python server that executes arbitrary commands in a sandbox container.
7+
---
8+
9+
The server application includes a FastAPI server that can execute commands that are sent to it through HTTP requests.
10+
11+
## Python Classes of the server app
12+
13+
The `examples/python-runtime-sandbox/main.py` file defines the following Pydantic models to ensure type-safe data for the API endpoints:
14+
15+
### `ExecuteRequest`
16+
This class models the request body for the `/execute` endpoint.
17+
- **`command: str`**: The shell command to be executed in the sandbox.
18+
19+
### `ExecuteResponse`
20+
This class models the response body for the `/execute` endpoint.
21+
- **`stdout: str`**: The standard output from the executed command.
22+
- **`stderr: str`**: The standard error from the executed command.
23+
- **`exit_code: int`**: The exit code of the executed command.
24+
## Install Agent Sandbox on a local Kind cluster
25+
26+
In this example we will create a [Kind (Kubernetes In Docker)](https://kind.sigs.k8s.io/) cluster to install the Agent Sandbox.
27+
28+
1. Clone the `agent-sandbox` repository if needed:
29+
30+
```sh
31+
git clone https://github.com/kubernetes-sigs/agent-sandbox.git
32+
```
33+
34+
2. Move to the repository folder:
35+
36+
```sh
37+
cd agent-sandbox
38+
```
39+
40+
3. Create a Kind cluster and deploy the agent controller by following this [installation tutorial](../../installation/_index.md).
41+
42+
## Deploy Python Runtime Sandbox
43+
44+
1. Go to the Python Runtime example folder:
45+
46+
```sh
47+
cd examples/python-runtime-sandbox
48+
```
49+
50+
2. Build image with Python Runtime
51+
52+
```sh
53+
docker build -t sandbox-runtime .
54+
```
55+
56+
3. Load the resulting image into the Kind cluster:
57+
58+
```sh
59+
kind load docker-image sandbox-runtime:latest --name agent-sandbox
60+
```
61+
62+
4. Apply Python runtime sandbox CRD and deployment:
63+
64+
```sh
65+
kubectl apply -f sandbox-python-kind.yaml
66+
```
67+
68+
5. Wait for the sandbox pod to be ready:
69+
70+
```sh
71+
kubectl wait --for=condition=ready pod --selector=sandbox=my-python-sandbox --timeout=60s
72+
```
73+
74+
## Test runtime sandbox
75+
76+
1. Create another terminal session and port-forward sandbox’s pod in order to access it:
77+
78+
```sh
79+
kubectl port-forward "pod/sandbox-python-example" 8888:8888
80+
```
81+
82+
2. Verify that runtime sandbox’s server is up:
83+
84+
```sh
85+
curl 127.0.0.1:8888/
86+
```
87+
88+
The output should be similar to:
89+
90+
```log
91+
{"status":"ok","message":"Sandbox Runtime is active."}
92+
```
93+
94+
3. Create an environment variable with the command that has to be executed:
95+
96+
```sh
97+
PAYLOAD="{\"command\": \"echo 'hello world'\"}"
98+
```
99+
100+
4. Execute the command:
101+
102+
```sh
103+
curl -X POST -H "Content-Type: application/json" -d "${PAYLOAD}" 127.0.0.1:8888/execute
104+
```
105+
106+
The output should be similar to:
107+
108+
```log
109+
{"stdout":"hello world\n","stderr":"","exit_code":0}
110+
```
111+
112+
## Cleanup
113+
114+
1. Delete the Kind cluster:
115+
116+
```sh
117+
kind delete cluster --name agent-sandbox
118+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: "Chrome in a Sandbox"
3+
linkTitle: "Chrome in a Sandbox"
4+
weight: 2
5+
description: >
6+
This guide documents the complete setup and deployment of Chrome in a Sandbox that runs locally on Kubernetes using Kind (Kubernetes in Docker) with the agent-sandbox controller.
7+
---
8+
# chrome in a Sandbox
9+
10+
Navigate to the folder with resources by running:
11+
```bash
12+
cd examples/chrome-sandbox
13+
```
14+
15+
This example runs Chrome in a container; we are starting by running it in a Docker container,
16+
but the plan is to run it in a Sandbox as we stand up the infrastructure there.
17+
18+
Currently you can test it out by running `run-test`; it will build a (local) container image,
19+
then run it. The image will capture screenshots roughly every 100ms so you can observe
20+
the progress as Chrome launches and opens (currently) https://google.com
21+
22+
The screenshots are in an unusual xwg format, so the script depends on the `convert`
23+
utility to convert those to an animated gif.
24+
25+
Plans:
26+
27+
* Move to Sandbox
28+
* Implement a better test for readiness
29+
* Maybe support selenium / playwright to make this a more useful example
30+
* Incorporate into our e2e tests
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
title: "Composing Sandbox with Network Policies"
3+
linkTitle: "Composing Sandbox with Network Policies"
4+
weight: 2
5+
description: >
6+
This guide documents the complete setup Sandbox with Network Policies that runs locally on Kubernetes using Kind (Kubernetes in Docker) with the agent-sandbox controller.
7+
---
8+
9+
## Composing Sandbox with Network Policies
10+
11+
The `Sandbox` API is a low-level primitive for creating secure sandboxes. In real-world scenarios, you often want to compose a `Sandbox` with other Kubernetes resources, such as `NetworkPolicy`, `Ingress` and `Service`, to create a more complete and secure "agentic sandbox" environment.
12+
13+
* `Ingress` can be used to expose the Sandbox to Users and Agents outside the cluster.
14+
* `NetworkPolicy` can be used to control who can connect to the Sandbox and limit the Sandbox outgoing connections to other pods or the internet.
15+
* `Service` can be used to expose the Sandbox ports for `Ingress`
16+
17+
```bash
18+
graph TD
19+
User --> Ingress
20+
Agent --> Ingress
21+
Ingress --> Service
22+
Service --> NetworkPolicy
23+
NetworkPolicy --> Sandbox
24+
Sandbox --> NetworkPolicy
25+
NetworkPolicy <--> Internet
26+
NetworkPolicy <--> Pods
27+
```
28+
29+
This composition can be achieved in multiple ways:
30+
31+
* **Writing a custom controller:** You can write a dedicated Kubernetes controller that watches for a higher-level CRD and creates the `Sandbox`, `NetworkPolicy`, and `Service` resources.
32+
* **Using a tool like KRO:** [KRO (Kubernetes Resource Orchestrator)](https://kro.run/docs/overview) allows you to define composite resources declaratively using a `ResourceGraphDefinition` (RGD). This creates a server side CRD that can be consumed by users.
33+
* **Using Helm:** Similar to KRO, we can use Helm to create a client side composition of resources and use the helm-cli to manage it.
34+
35+
This example demonstrates the second approach, using KRO to define a new `AgenticSandbox` CRD that encapsulates a `Sandbox`, a `NetworkPolicy`, and a `Service`.
36+
37+
The `Service` created here is distinct from the one created by the `agent-sandbox` controller. This layering allows for more complex networking configurations and showcases the value of composing resources.
38+
39+
### Brief introduction to KRO
40+
41+
KRO is a powerful tool for defining and managing composite resources in Kubernetes. At its core, KRO introduces the `ResourceGraphDefinition` (RGD) custom resource. An RGD allows you to declaratively specify a graph of Kubernetes resources that should be created, updated, and deleted together as a single unit. When an RGD is applied to a cluster, KRO automatically generates a new Custom Resource Definition (CRD) based on the RGD's schema. Users can then interact with this new, higher-level CRD, and KRO will handle the orchestration of the underlying resources defined in the RGD. This enables administrators to define complex, opinionated abstractions that users can consume easily, without needing to manage the individual components.
42+
43+
### Example: Using KRO to create an `AgenticSandbox`
44+
45+
#### Install KRO
46+
47+
First, install KRO on your cluster:
48+
```bash
49+
dev/tools/install-kro
50+
```
51+
52+
#### Create RGD
53+
54+
Install the `ResourceGraphDefinition` (RGD) which defines our new `AgenticSandbox` CRD
55+
56+
```bash
57+
kubectl apply -f examples/composing-sandbox-nw-policies/rgd.yaml
58+
```
59+
60+
Validate that the RGD is installed correctly:
61+
62+
```bash
63+
$ kubectl get rgd
64+
NAME APIVERSION KIND STATE AGE
65+
agentic-sandbox v1alpha1 AgenticSandbox Active 6m38s
66+
```
67+
68+
Validate that the new `AgenticSandbox` CRD is installed correctly:
69+
70+
```bash
71+
$ kubectl get crd
72+
NAME CREATED AT
73+
agenticsandboxes.custom.agents.x-k8s.io 2025-09-20T05:03:49Z # << THIS
74+
resourcegraphdefinitions.kro.run 2025-09-20T04:35:37Z
75+
sandboxes.agents.x-k8s.io 2025-09-19T22:40:05Z
76+
```
77+
78+
#### Create an `AgenticSandbox`
79+
80+
You can now create an example `AgenticSandbox` resource:
81+
82+
```yaml
83+
apiVersion: custom.agents.x-k8s.io/v1alpha1
84+
kind: AgenticSandbox
85+
metadata:
86+
name: demo
87+
spec:
88+
image: nginx
89+
service:
90+
port: 80
91+
```
92+
93+
Apply it to the cluster:
94+
95+
```bash
96+
kubectl apply -f instance.yaml
97+
```
98+
99+
Check the status of the created resources:
100+
101+
```bash
102+
kubectl get agenticsandboxes
103+
kubectl get agenticsandboxes demo -o yaml
104+
```
105+
106+
The KRO reconciler reconciles `AgenticSandbox` instance and creates the following resources:
107+
* Sandbox
108+
* Service
109+
* NetworkPolicy (if enabled)
110+
* Ingress (if enbled)
111+
112+
The user/agent can access the Sanbox via the Ingress or the Service as appropriate.
113+
114+
If needed the user can update `AgenticSandbox` instance and reapply.
115+
116+
Once done using the Sandbox, the user can delete the `AgenticSandbox` instance, and KRO will clean up all the resources it created:
117+
118+
```bash
119+
kubectl delete agenticsandbox demo
120+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: "gVisor on GKE"
3+
linkTitle: "gVisor on GKE"
4+
weight: 2
5+
description: >
6+
This guide shows how to run [Agent Sangbox](https://github.com/kubernetes-sigs/agent-sandbox) with the [gVisor](https://gvisor.dev) runtime using GKE as a cluster.
7+
---
8+
9+
## Prerequisites
10+
11+
* [gcloud CLI](https://docs.cloud.google.com/sdk/docs/install)
12+
* [kubectl](https://kubernetes.io/docs/tasks/tools/)
13+
14+
## Create a GKE cluster
15+
16+
Specify your project:
17+
18+
```sh
19+
export PROJECT_ID=$(gcloud config get project)
20+
```
21+
22+
Run the following command to create a GKE cluster:
23+
24+
```sh
25+
gcloud container clusters create demo-gvisor-cluster \
26+
--location=us-central1-c \
27+
--project=$PROJECT_ID
28+
```
29+
30+
Then add a node that supports `gVisor` by running:
31+
32+
```sh
33+
gcloud container node-pools create gvisor-node-pool \
34+
--region us-central1-c \
35+
--cluster=demo-gvisor-cluster \
36+
--num-nodes=1 \
37+
--sandbox type=gvisor
38+
```
39+
40+
To access your cluster run this command:
41+
42+
```sh
43+
gcloud container clusters get-credentials demo-gvisor-cluster \
44+
--region us-central1-c \
45+
--project ${PROJECT_ID}
46+
```
47+
48+
## Install Sandbox CRD
49+
50+
Run these commands to install `Sandbox CRD`:
51+
52+
```sh
53+
export VERSION=v0.1.0-rc.0
54+
kubectl apply -f https://github.com/kubernetes-sigs/agent-sandbox/releases/download/${VERSION}/manifest.yaml
55+
kubectl apply -f https://github.com/kubernetes-sigs/agent-sandbox/releases/download/${VERSION}/extensions.yaml
56+
```
57+
58+
## Testing
59+
60+
Run this command to create an ampty Sandbox that uses `gVisor` as a RuntimeClass:
61+
62+
```sh
63+
kubectl apply -f gvisor-empty-sandbox.yaml
64+
```
65+
66+
## Cleanup
67+
68+
```sh
69+
gcloud container clusters delete demo-gvisor-cluster \
70+
--location=us-central1-c \
71+
--project=$PROJECT_ID
72+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: sandbox-ns
5+
---
6+
apiVersion: v1
7+
kind: ServiceAccount
8+
metadata:
9+
name: your-sandbox-sa
10+
namespace: sandbox-ns
11+
---
12+
apiVersion: agents.x-k8s.io/v1alpha1
13+
kind: Sandbox
14+
metadata:
15+
name: sandbox-example
16+
namespace: sandbox-ns
17+
spec:
18+
podTemplate:
19+
metadata:
20+
labels:
21+
sandbox: my-sandbox
22+
annotations:
23+
test: "yes"
24+
spec:
25+
# Each sandboxed pod can use a distinct KSA, then they will have distinct identities
26+
runtimeClassName: gvisor
27+
serviceAccountName: your-sandbox-sa
28+
containers:
29+
- name: my-container
30+
image: busybox
31+
command: ["/bin/sh", "-c", "sleep 3600"]
32+
volumeMounts:
33+
- name: my-pvc
34+
mountPath: /my-data
35+
volumeClaimTemplates:
36+
- metadata:
37+
name: my-pvc
38+
spec:
39+
accessModes: [ "ReadWriteOnce" ]
40+
resources:
41+
requests:
42+
storage: 1Gi

0 commit comments

Comments
 (0)