This example demonstrates how to deploy an E2B code-interpreter sandbox through OpenKruise Agents and invoke it via the E2B SDK.
Sandbox is the core CRD of OpenKruise Agents. It manages the lifecycle of a sandbox instance (such as a Pod) and
provides advanced features including Pause, Resume, Checkpoint, Fork, and in-place upgrades.
SandboxSet is the workload that manages Sandbox. Its function is similar to a ReplicaSet that manages Pods.
It enables sub-second sandbox startup by pre-warming a pool of sandbox instances. Optimized specifically for scaling
performance, SandboxSet can rapidly replenish sandboxes as they are consumed.
sandbox-manager is a stateless backend management component that provides a set of E2B protocol-compatible APIs for
managing and operating sandbox instances.
agent-runtime is a Sidecar injected into the Sandbox that provides a series of advanced features for the sandbox,
including E2B envd-compatible remote operation interfaces, dynamic CSI mounting, etc.
OpenKruise Agents provides the E2B protocol-compatible backend management component sandbox-manager, allowing users to
directly manage and operate sandboxes through the native E2B SDK. In this example, we will deploy an official E2B
code-interpreter template in the K8s cluster.
SandboxSet, as the workload managing Sandbox, will be automatically recognized as a template by sandbox-manager.
You can refer to sandboxset.yaml to create a SandboxSet in K8s to create a template named code-interpreter.
agent-runtime provides E2B-compatible interfaces that support its command execution, file operations, code running,
and other functions. If the official images do not meet your requirements, you can replace them with custom images.
To reduce cluster load during large-scale pre-warming, you can deploy templates across namespaces by creating identically
named SandboxSet resources in each target namespace.
You can connect the native E2B Python SDK and JavaScript SDK to sandbox-manager through the following environment
variables. In this section, we will use the Python SDK as an example for introduction.
Domain name and initial API Token should be configured via helm values during installation
export E2B_DOMAIN=your.domain
export E2B_API_TOKEN=your-tokenYou can install the E2B code interpreter Python SDK with the following command:
pip install e2b-code-interpretersandbox-manager is compatible with E2B's standard capabilities, including management and sandbox instance operations.
The following example demonstrates creating sandboxes, executing code, operating files, executing commands, and other
functions through the E2B SDK.
The following code can quickly allocate a sandbox instance from the pre-warming pool. Upon completion of allocation,
SandboxSet will immediately create a new sandbox instance for replenishment.
import os
# Import the E2B SDK
from e2b_code_interpreter import Sandbox
# Create a sandbox using the E2B Python SDK
# The template name here must match the SandboxSet name
sbx = Sandbox.create(template="code-interpreter", timeout=300)
print(f"sandbox id: {sbx.sandbox_id}")
sbx.kill()
print(f"sandbox {sbx.sandbox_id} killed")with Sandbox.create(template="code-interpreter", timeout=300) as sbx:
sbx.run_code("print('hello world')")with Sandbox.create(template="code-interpreter", timeout=300) as sbx:
with open(os.path.abspath(__file__), "rb") as file:
sbx.files.write("/home/user/my-file", file)
file_content = sbx.files.read("/home/user/my-file")
print(file_content)with Sandbox.create(template="code-interpreter", timeout=300) as sbx:
result = sbx.commands.run('echo hello; sleep 1; echo world', on_stdout=lambda data: print(data), on_stderr=lambda data: print(data))
print(result)Note: Currently, memory state preservation during pausing and resuming is only supported on Alibaba Cloud ACS
with Sandbox.create(template="code-interpreter", timeout=300) as sbx:
# Pause the sandbox
sbx.run_code("a = 1")
sbx.beta_pause()
# Resume the sandbox
sbx.connect()
sbx.run_code("print(a)")In addition to E2B standard capabilities, OpenKruise Agents also provides a series of extended functions.
OpenKruise Agents supports specifying an image for in-place upgrade when calling the Sandbox.create interface,
replacing the pre-warmed container image with the specified image. This is very useful in some reinforcement learning
scenarios. Using in-place upgrades will affect the delivery speed of the create interface and may not complete delivery
at the second level.
from e2b_code_interpreter import Sandbox
sbx = Sandbox.create(template="some-template", timeout=300, metadata={
"e2b.agents.kruise.io/image": "e2bdev/code-interpreter:latest"
})OpenKruise Agents supports dynamic mounting of CSI Volumes when creating sandboxes through Sandbox.create, specifying
separate mount volumes (such as Alibaba Cloud NAS, OSS, etc.) for each sandbox. This capability depends on agent-runtime
and will also affect delivery efficiency.
from e2b_code_interpreter import Sandbox
from kruise_agents.csi import AlibabaCloudNAS
sbx = Sandbox.create(template="some-template", timeout=300, metadata={
"e2b.agents.kruise.io/csi-volume-name": "oss-pv-test",
"e2b.agents.kruise.io/csi-mount-point": "/data"
})The aforementioned example describes a scenario in which a CSI-type OSS-backed persistent volume (PV), named "oss-pv-test",
is mounted to the container's /data directory upon sandbox instantiation.