The customized SDK bypasses the native E2B SDK's wildcard domain requirement, simplifying deployment.
Prerequisites
Upgrade the
acs-virtual-nodecomponent to v2.17.0 or later.Upgrade the
Kube Schedulercomponent.Cluster version
Kube Schedulercomponent versionv1.28
v1.28.12-aliyun-1.4.6 or later
v1.30
v1.30.3-aliyun-1.6.2 or later
v1.31
v1.31.0-aliyun-1.5.2 or later
v1.32
v1.32.0-apsara.6.11.11.3187ac8f or later
Upgrade the
ack-agent-sandbox-controllercomponent to v0.5.2 or later.Upgrade the
ack-sandbox-managercomponent to v0.3.2 or later.
Install components
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Add-ons.
Install an Ingress Controller component: Install any Ingress Controller component supported by ACK to access the
sandbox-managerservice from outside the cluster. This topic uses the ALB Ingress Controller as an example, which creates a public-facing ALB instance.Install the
ack-agent-sandbox-controllercomponent: Install the component with the default settings. The component version must be v0.5.2 or later.Install the
ack-sandbox-managercomponent: After you complete the environment configuration, setclassNametoalb(the IngressClass automatically created in Step 1), setdomainto your actual domain name, and setadminApiKeyto a custom API key. Leave the other settings at their default values. After installation, a route namedsandbox-manageris created in thesandbox-systemnamespace.Demo environment configuration:
Get the ALB DNS name: On the custom resources page, view the YAML of the AlbConfig resource object
alb, or run the commandkubectl get albconfig alb -o jsonpath='{.status.loadBalancer.dnsname}'to get the ALB DNS name, such asalb-*****62roo70i*****.cn-hangzhou.alb.aliyuncsslb.com. Set thedomainparameter to this DNS name.Update the
sandbox-managerroute: In thesandbox-systemnamespace, click Update next to thesandbox-managerroute. Temporarily disable the TLS configuration. After the update is complete, wait about one minute and refresh the route page. The endpoint information for the route appears.
Production environment configuration: For detailed instructions on preparing a domain name, configuring DNS resolution, and requesting a certificate, see Going Live. If you use the ALB Ingress Controller, you must also add an HTTPS:443 listener for the ALB instance and Ingress.
Create a sandbox environment
E2B SDK-compatible method
In the following steps, you first create and warm up an E2B template by using a SandboxSet, and then create a sandbox environment by using the customized ACK E2B Python SDK.
Step 1: Create and warm up an E2B template
In the navigation pane on the left, choose Workloads > custom resources. Select the CustomResourceDefinition tab, and then click Create from YAML.
Use the following YAML to create a SandboxSet resource. This registers an E2B template named
code-interpreterwithack-sandbox-manager. The template is based on the official E2B code-interpreter image and is used to execute Python code.apiVersion: agents.kruise.io/v1alpha1 kind: SandboxSet metadata: name: code-interpreter namespace: default spec: scaleStrategy: maxUnavailable: 500 replicas: 10 # The size of the warm pool. We recommend setting this slightly larger than the anticipated burst of requests. template: # metadata: # annotations: # Optional. If you leave this empty, the security group configuration in acs-profile is used. # Ensure that the corresponding security group has enough available IP addresses, at least more than the number of replicas. # Insufficient IPs can slow down or even fail sandbox creation. # network.alibabacloud.com/security-group-ids: sg-8****** # labels: # Optional. Used to schedule sandbox pods to ACS in an ACK cluster. # alibabacloud.com/acs: "true" spec: initContainers: # Declare agent-runtime as a native sidecar to automatically inject runtime components like envd into the sandbox container. - name: runtime image: registry-cn-hangzhou-vpc.ack.aliyuncs.com/acs/agent-runtime:v0.0.5 command: [ "sh", "/workspace/entrypoint_inner.sh" ] volumeMounts: # Shared directory with the main container. - name: envd-volume mountPath: /mnt/envd env: - name: ENVD_DIR value: /mnt/envd # This environment variable allows the sidecar to share the resources of the main container without incurring extra costs. - name: __IGNORE_RESOURCE__ value: "true" restartPolicy: Always containers: - name: sandbox # The official e2b code-interpreter image. It supports pulling from all regions and VPCs. image: registry-cn-hangzhou-vpc.ack.aliyuncs.com/acs/code-interpreter:v1.6 imagePullPolicy: IfNotPresent # We recommend that you set resource requests. Otherwise, in an ACK environment, the sandbox may be assigned very small specifications, which can affect performance. resources: limits: cpu: 1 memory: 1Gi requests: cpu: 1 memory: 1Gi startupProbe: failureThreshold: 10 httpGet: path: /health port: 49999 initialDelaySeconds: 1 periodSeconds: 2 timeoutSeconds: 1 env: # Specifies the location of the envd component injected by the runtime. - name: ENVD_DIR value: /mnt/envd volumeMounts: # Shared directory with the runtime. - name: envd-volume mountPath: /mnt/envd # Starts the envd service by using a postStart hook. lifecycle: postStart: exec: command: [ "/bin/bash", "-c", "/mnt/envd/envd-run.sh" ] # Ensures quick container termination to increase the probability of reuse. terminationGracePeriodSeconds: 1 volumes: - name: envd-volume emptyDir: { }In the navigation pane on the left, click Pods. Select the
defaultnamespace to view the created and warmed-upcode-interpretersandbox pods.
Step 2: Create a sandbox environment
Install the E2B Python SDK.
pip install e2b-code-interpreter==2.4.1Configure environment variables.
# Use the default domain from the ack-sandbox-manager component installation. Do not include "*". Modify as needed. export E2B_DOMAIN=your.domain.com # Use the default API Key from the ack-sandbox-manager component installation. Modify as needed. export E2B_API_KEY=admin-987654321Run the
git clone https://github.com/openkruise/agents.gitcommand to download the customized ACK SDK to your local machine.Navigate to the
/agents/sdk/customized_e2bdirectory of the customized SDK and save the following code as amain.pyfile.# Import the E2B SDK from e2b_code_interpreter import Sandbox from kruise_agents.patch_e2b import patch_e2b patch_e2b(https=False) # patch sdk sbx: Sandbox = Sandbox.create(template="code-interpreter") print(f"sandbox id: {sbx.sandbox_id}") result = sbx.run_code("print('hello, world')") print(f"run code result: {result}") text = input("enter some text to be saved to file 'text.txt' inside sandbox: ") sbx.files.write("text.txt", text) print(f"read file from sandbox via files api: [{sbx.files.read('text.txt')}]") print(f"read file from sandbox via commands api: [{sbx.commands.run('cat text.txt')}]") input("press ENTER to kill the sandbox") print(sbx.kill())Run the
main.pyfile to create and verify the sandbox environment.After the prompt appears for the first time, enter text such as
acs agent sandboxand press Enter. This action writesacs agent sandboxto the/home/user/text.txtfile in the Pod namedcode-interpreter-29***. If you press Enter again, the current sandbox environment is deleted.python main.pyExpected output:
sandbox id: default--code-interpreter-29*** run code result: Execution(Results: [], Logs: Logs(stdout: ['hello, world\n'], stderr: []), Error: None) enter some text to be saved to file 'text.txt' inside sandbox: ack agent sandbox read file from sandbox via files api: [ack agent sandbox] read file from sandbox via commands api: [CommandResult(stderr='', stdout='ack agent sandbox', exit_code=0, error='')] press ENTER to kill the sandbox True
Sandbox CR method
In the navigation pane on the left, choose Workloads > custom resources. Select the CustomResourceDefinition tab, and then click Create from YAML.
Use the following YAML to create a Sandbox custom resource object. This object is based on the official E2B code-interpreter image and executes Python code.
apiVersion: agents.kruise.io/v1alpha1 kind: Sandbox metadata: name: code-interpreter spec: template: metadata: labels: agent: code-interpreter spec: initContainers: # Declare agent-runtime as a native sidecar to automatically inject runtime components like envd into the sandbox container. - name: runtime image: registry-cn-hangzhou-vpc.ack.aliyuncs.com/acs/agent-runtime:v0.0.5 command: [ "sh", "/workspace/entrypoint_inner.sh" ] volumeMounts: # Shared directory with the main container. - name: envd-volume mountPath: /mnt/envd env: - name: ENVD_DIR value: /mnt/envd # This environment variable allows the sidecar to share the resources of the main container without incurring extra costs. - name: __IGNORE_RESOURCE__ value: "true" restartPolicy: Always containers: - name: sandbox image: registry-cn-hangzhou-vpc.ack.aliyuncs.com/acs/code-interpreter:v1.6 imagePullPolicy: IfNotPresent resources: requests: cpu: 1 memory: 1Gi ephemeral-storage: 30Gi startupProbe: failureThreshold: 10 httpGet: path: /health port: 49999 initialDelaySeconds: 1 periodSeconds: 2 timeoutSeconds: 1 env: # Specifies the location of the envd component injected by the runtime. - name: ENVD_DIR value: /mnt/envd volumeMounts: # Shared directory with the runtime. - name: envd-volume mountPath: /mnt/envd # Starts the envd service by using a postStart hook. lifecycle: postStart: exec: command: [ "/bin/bash", "-c", "/mnt/envd/envd-run.sh" ] # Ensures quick container termination to increase the probability of reuse. terminationGracePeriodSeconds: 1 volumes: - name: envd-volume emptyDir: { }In the navigation pane on the left, click Pods. Select the
defaultnamespace to view the createdcode-interpretersandbox environment.
Going live
Prepare a domain name
The customized ACK SDK supports non-wildcard domains. Refer to Add or delete a private zone to configure the domains your.domain.com and *.your.domain.com and resolve them to the Ingress address. If access is entirely within the ACK cluster, you can also directly use the in-cluster Headless Service address: sandbox-manager.sandbox-system.svc.cluster.local.
Configure DNS resolution
Run the following command to view the access point information:
kubectl get ingress sandbox-manager -o jsonpath='{range .status.loadBalancer.ingress[*]}{.hostname}{.ip}{"\n"}{end}' -n sandbox-systemBased on the output, configure the DNS resolution for your.domain.com or *.your.domain.com. For more information about DNS resolution, see Quick start.
If the output is an IP address (for example,
47.114.***.***), create an A record for the host*.your.domain.comthat points to this IP address.If the output is a domain name (for example,
alb-*****62roo70i*****.cn-hangzhou.alb.aliyuncsslb.com), create a CNAME record for the host*.your.domain.comthat points to this domain name.If multiple access points are returned, you can resolve the record to any one of them or configure round-robin DNS for all of them.
Request a certificate
The E2B client can send requests to the backend over HTTPS. For production scenarios, we recommend using one of the following methods for certificate provisioning.