ACS Agent Sandbox is compatible with the E2B SDK, so you can use the SDK's standard API to create, manage, and connect to sandbox instances.
Background information
ACS Agent Sandbox is compatible with the E2B SDK. Use the E2B SDK to create, manage, and connect to sandbox instances. Agent Sandbox implements E2B protocol compatibility based on OpenKruise Agents and supports two E2B protocols:
-
Native Protocol: Each sandbox uses a dedicated subdomain per port. The access format is
<PORT>-<SANDBOX_ID>.DOMAIN, and the API endpoint isapi.DOMAIN. Wildcard DNS resolution and TLS certificates are required. Suitable for production environments. -
Private Protocol: All sandboxes share a single domain and are distinguished by URL path. The access format is
DOMAIN/kruise/<SANDBOX_ID>/<PORT>, and the API endpoint isDOMAIN/kruise/api. Only a single domain is required, which simplifies network configuration and reduces certificate cost.
|
Item |
Native Protocol |
Private Protocol |
|
Domain requirement |
Wildcard domain ( |
Single domain ( |
|
TLS certificate |
Wildcard certificate |
Single-domain certificate |
|
E2B SDK requirement |
Natively supported |
Requires the |
|
Use case |
Production environments |
Testing and validation, rapid integration |
Install components
If the components are installed, upgrade the ack-agent-sandbox-controller component to v0.5.14-release.1 or later and the ack-sandbox-manager component to v0.6.0 or later.
-
Log on to the ACS console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Add-ons.
-
Install the Ingress controller and sandbox-related components.
-
Install an Ingress controller.
Install an Ingress controller component supported by ACS to access the
sandbox-managerservice from outside the cluster. The following steps use Install the ALB Ingress Controller as an example to create a new public-facing ALB instance. -
Install the ack-agent-sandbox-controller component.
When you install this component for the first time, you must grant the AliyunCSManagedAgentSandboxRole role permissions to access your cloud resources. You can use the default parameter settings. If you require higher concurrency, you can increase the resource specifications.
For more information about the features of this component, see ack-agent-sandbox-controller.
-
Install the ack-sandbox-manager component.
-
Prepare an E2B domain name.
For more information about preparing a domain name, configuring DNS resolution, and applying for a certificate, see Use in a production environment.
-
Configure the component parameters.
Change
classNametoalb(the IngressClass that is automatically created when you install the Ingress controller component), changedomainto your actual domain name, and changeadminApiKeyto a custom API Key. Keep the other configurations at their default values. After the component is installed, an Ingress namedsandbox-manageris created in thesandbox-systemnamespace. -
If you use the ALB Ingress controller, you must also add an HTTPS:443 listener for both the ALB instance and the Ingress.
-
-
Step 1: Create a warm-up pool
-
In the navigation pane on the left, choose Custom Resources. On the CRDs tab, click Create from YAML.
-
Create a SandboxSet resource by using the following YAML. The
ack-sandbox-managerautomatically detects this resource and initializes a sandbox template configuration named code-interpreter.To enable dynamic storage mounting, you must allow privileged containers and access to the hostPath (
/var/run/csi). You can submit a ticket to have the security restrictions lifted. However, you must assume responsibility for the associated security risks. For more information about this mechanism, see the shared responsibility model.apiVersion: agents.kruise.io/v1alpha1 kind: SandboxSet metadata: name: code-interpreter namespace: default spec: runtimes: - name: csi # Supports CSI mounting. The corresponding sidecar is injected into new sandboxes. - name: agent-runtime # Injects environment management tools such as envd. replicas: 4 template: metadata: labels: alibabacloud.com/acs: "true" alibabacloud.com/compute-class: agent-sandbox # Agent Sandbox instance type. alibabacloud.com/compute-qos: default # Compute QoS: default or best-effort. spec: automountServiceAccountToken: false containers: - image: registry-cn-zhangjiakou-vpc.ack.aliyuncs.com/acs/code-interpreter:v1.6 # Replace the region with the one where your cluster is deployed. imagePullPolicy: IfNotPresent name: sandbox resources: limits: cpu: "1" memory: 1Gi requests: cpu: "1" memory: 1Gi ephemeral-storage: 30Gi terminationGracePeriodSeconds: 30 -
Click Pods in the left navigation bar, select the corresponding namespace, and view the created
code-interpreterSandbox.You can view SandboxSet resources by running the
kubectl get sbscommand, whereAVAILABLEindicates the number of ready Sandboxes.
Step 2: Install the E2B SDK
The E2B SDK supports multiple programming languages, including Python, Java, and Go. This topic uses Python as an example. For detailed integration guides in other languages, see e2b-client.
-
Install the Python runtime.
-
Install the E2B Python SDK. Agent Sandbox supports only E2B SDK versions earlier than v2.25.0.
pip install "e2b-code-interpreter==2.7.0" "e2b==2.24.0"
Step 3: Configure E2B_DOMAIN
E2B_DOMAIN is the domain configuration that the E2B SDK uses to connect to sandbox-manager. The client and add-on configurations must be identical.
For information about registering and resolving a domain, see Alibaba Cloud DNS.
Configure the add-on
The following example uses ALB Ingress Controller. For detailed operations, see Install add-ons and configure parameters.
-
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Add-ons.
-
In the console, go to Add-ons, search for the
ack-sandbox-manageradd-on, click Configuration, and modify thedomainparameter of the add-on. Replace it with your actual domain.The
domainparameter does not include the*prefix. For example, if your domain is*.sandbox.example.com, setdomaintosandbox.example.com. -
(Optional) To access sandbox-manager over HTTP only (without configuring a TLS certificate), clear the Whether to enable TLS for ingress option on the same configuration page.
Configure the client
Set the following environment variables in your local environment.
The value ofE2B_DOMAINmust exactly match thedomainparameter of the ack-sandbox-manager add-on. Otherwise, the SDK cannot connect to the sandbox-manager service.
# Set E2B_DOMAIN to match the domain parameter of the add-on
export E2B_DOMAIN=<YOUR_E2B_DOMAIN>
# Set E2B_API_KEY to match the adminApiKey parameter of the add-on
export E2B_API_KEY=<YOUR_API_KEY>
(Optional) Step 4: Install a certificate
To access sandbox-manager over HTTPS, install a TLS certificate. A certificate from a trusted CA is recommended. For details about applying for and configuring certificates, see the following topics:
Verify that the Secret is created and that its type is correct (should be kubernetes.io/tls):
kubectl get secret sandbox-manager-tls -n sandbox-system
Step 5: Select an integration method to connect to the sandbox
Based on your network environment and requirements, select one of the following integration methods to connect to the sandbox.
In the following examples, code-interpreter is a placeholder. Replace it with the actual SandboxSet name that you created.
-
Method 1: Access from outside the cluster using the Native Protocol: Recommended for production environments. Requires a wildcard domain and TLS certificate.
-
Method 2: Access from outside the cluster using the Private Protocol (HTTPS): Requires only a single domain, which lowers the deployment barrier.
-
Method 3: Use port forwarding for local debugging: Suitable for local development and debugging.
Method 1: Access from outside the cluster using the Native Protocol
Requests are routed to the sandbox-manager service through a wildcard domain, and each sandbox uses a dedicated subdomain. Suitable for production environments.
-
Configure wildcard DNS resolution to point
*.your.domain.comto the external IP of the cluster Ingress. Also install a certificate for the wildcard domain. -
Sample code.
from e2b_code_interpreter import Sandbox sbx = Sandbox.create(template="code-interpreter") print(f"sandbox id: {sbx.sandbox_id}") execution = sbx.run_code("print('hello, world')") print(f"run code result: {execution}") sbx.kill()
Method 2: Access from outside the cluster using the Private Protocol (HTTPS)
Different sandboxes are distinguished by a single domain and URL path. Only a single-domain certificate is required. Suitable for testing, validation, and rapid integration.
For information about registering and resolving a domain, see Alibaba Cloud DNS.
-
Install the
kruise-agentsextension package.pip install "git+https://github.com/openkruise/agents.git#subdirectory=sdk/customized_e2b" -
Configure single-domain DNS resolution to point
your.domain.comto the external IP of the cluster Ingress. Also install a certificate for the single domain. -
Sample code.
When using the Private Protocol, you must call
patch_e2b()before importing the E2B SDK to switch the E2B SDK request routing to the Private Protocol path. When accessing over HTTPS from outside the cluster, passhttps=True.from kruise_agents.patch_e2b import patch_e2b patch_e2b(https=True) from e2b_code_interpreter import Sandbox sbx = Sandbox.create(template="code-interpreter") print(f"sandbox id: {sbx.sandbox_id}") execution = sbx.run_code("print('hello, world')") print(f"run code result: {execution}") sbx.kill()
Method 3: Use port forwarding for local debugging
Use kubectl port forwarding to map the sandbox-manager service to your local machine. Suitable for local development and debugging.
-
Set environment variables.
E2B_DOMAINmust match thedomainparameter of sandbox-manager. Set both tolocalhost.export E2B_DOMAIN=localhost export E2B_API_KEY=<YOUR_API_KEY> -
Install the
kruise-agentsextension package.pip install "git+https://github.com/openkruise/agents.git#subdirectory=sdk/customized_e2b" -
Forward the sandbox traffic port of sandbox-manager to local port 80. Port
7788is the sandbox traffic port; adjust it based on the actual Service port.sudo kubectl port-forward services/sandbox-manager 80:7788 -n sandbox-system -
When using the Private Protocol, call
patch_e2b(https=False)before importing the E2B SDK. Example:from kruise_agents.patch_e2b import patch_e2b patch_e2b(https=False) from e2b_code_interpreter import Sandbox sbx = Sandbox.create(template="code-interpreter") print(f"sandbox id: {sbx.sandbox_id}") execution = sbx.run_code("print('hello, world')") print(f"run code result: {execution}") sbx.kill()